blob: 59a767a0c3a0f6f3d7c61497836a95f46e65a9d3 [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 Moolenaar45d2eea2016-06-06 21:07:52 +0200823static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200824#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100825static void f_tan(typval_T *argvars, typval_T *rettv);
826static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200827#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100828#ifdef FEAT_TIMERS
829static void f_timer_start(typval_T *argvars, typval_T *rettv);
830static void f_timer_stop(typval_T *argvars, typval_T *rettv);
831#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static void f_tolower(typval_T *argvars, typval_T *rettv);
833static void f_toupper(typval_T *argvars, typval_T *rettv);
834static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000835#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100836static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000837#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100838static void f_type(typval_T *argvars, typval_T *rettv);
839static void f_undofile(typval_T *argvars, typval_T *rettv);
840static void f_undotree(typval_T *argvars, typval_T *rettv);
841static void f_uniq(typval_T *argvars, typval_T *rettv);
842static void f_values(typval_T *argvars, typval_T *rettv);
843static void f_virtcol(typval_T *argvars, typval_T *rettv);
844static void f_visualmode(typval_T *argvars, typval_T *rettv);
845static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100846static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100847static void f_win_getid(typval_T *argvars, typval_T *rettv);
848static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
849static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
850static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100851static void f_winbufnr(typval_T *argvars, typval_T *rettv);
852static void f_wincol(typval_T *argvars, typval_T *rettv);
853static void f_winheight(typval_T *argvars, typval_T *rettv);
854static void f_winline(typval_T *argvars, typval_T *rettv);
855static void f_winnr(typval_T *argvars, typval_T *rettv);
856static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
857static void f_winrestview(typval_T *argvars, typval_T *rettv);
858static void f_winsaveview(typval_T *argvars, typval_T *rettv);
859static void f_winwidth(typval_T *argvars, typval_T *rettv);
860static void f_writefile(typval_T *argvars, typval_T *rettv);
861static void f_wordcount(typval_T *argvars, typval_T *rettv);
862static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
865static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
866static int get_env_len(char_u **arg);
867static int get_id_len(char_u **arg);
868static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
869static 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 +0000870#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
871#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
872 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
874static int eval_isnamec(int c);
875static int eval_isnamec1(int c);
876static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
877static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static typval_T *alloc_string_tv(char_u *string);
879static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100880#ifdef FEAT_FLOAT
881static float_T get_tv_float(typval_T *varp);
882#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883static linenr_T get_tv_lnum(typval_T *argvars);
884static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
886static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
887static hashtab_T *find_var_ht(char_u *name, char_u **varname);
888static funccall_T *get_funccal(void);
889static void vars_clear_ext(hashtab_T *ht, int free_val);
890static void delete_var(hashtab_T *ht, hashitem_T *hi);
891static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
892static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
893static void set_var(char_u *name, typval_T *varp, int copy);
894static int var_check_ro(int flags, char_u *name, int use_gettext);
895static int var_check_fixed(int flags, char_u *name, int use_gettext);
896static int var_check_func_name(char_u *name, int new_var);
897static int valid_varname(char_u *varname);
898static int tv_check_lock(int lock, char_u *name, int use_gettext);
899static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
900static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100901static 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 +0100902static int eval_fname_script(char_u *p);
903static int eval_fname_sid(char_u *p);
904static void list_func_head(ufunc_T *fp, int indent);
905static ufunc_T *find_func(char_u *name);
906static int function_exists(char_u *name);
907static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000908#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100909static void func_do_profile(ufunc_T *fp);
910static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
911static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000912static int
913# ifdef __BORLANDC__
914 _RTLENTRYF
915# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100916 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000917static int
918# ifdef __BORLANDC__
919 _RTLENTRYF
920# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100921 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000922#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100923static int script_autoload(char_u *name, int reload);
924static char_u *autoload_name(char_u *name);
925static void cat_func_name(char_u *buf, ufunc_T *fp);
926static void func_free(ufunc_T *fp);
927static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
928static int can_free_funccal(funccall_T *fc, int copyID) ;
929static void free_funccal(funccall_T *fc, int free_val);
930static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
931static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
932static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
933static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
934static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
935static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
936static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
937static int write_list(FILE *fd, list_T *list, int binary);
938static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000939
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200940
941#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100942static int compare_func_name(const void *s1, const void *s2);
943static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200944#endif
945
Bram Moolenaar33570922005-01-25 22:26:29 +0000946/*
947 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000948 */
949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100950eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 int i;
953 struct vimvar *p;
954
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200955 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
956 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200957 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000958 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000959 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000960
961 for (i = 0; i < VV_LEN; ++i)
962 {
963 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200964 if (STRLEN(p->vv_name) > 16)
965 {
966 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
967 getout(1);
968 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 STRCPY(p->vv_di.di_key, p->vv_name);
970 if (p->vv_flags & VV_RO)
971 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
972 else if (p->vv_flags & VV_RO_SBX)
973 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
974 else
975 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000976
977 /* add to v: scope dict, unless the value is not always available */
978 if (p->vv_type != VAR_UNKNOWN)
979 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000980 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000981 /* add to compat scope dict */
982 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000983 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100984 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
985
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000986 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100987 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200988 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100989 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100990
991 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
992 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
993 set_vim_var_nr(VV_NONE, VVAL_NONE);
994 set_vim_var_nr(VV_NULL, VVAL_NULL);
995
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200996 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200997
998#ifdef EBCDIC
999 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +01001000 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001001 */
1002 sortFunctions();
1003#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001004}
1005
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001006#if defined(EXITFREE) || defined(PROTO)
1007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001008eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001009{
1010 int i;
1011 struct vimvar *p;
1012
1013 for (i = 0; i < VV_LEN; ++i)
1014 {
1015 p = &vimvars[i];
1016 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001017 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001018 vim_free(p->vv_str);
1019 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001020 }
1021 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1022 {
1023 list_unref(p->vv_list);
1024 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001025 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001026 }
1027 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001028 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001029 hash_clear(&compat_hashtab);
1030
Bram Moolenaard9fba312005-06-26 22:34:35 +00001031 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001032# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001033 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001034# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001035
1036 /* global variables */
1037 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001038
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001039 /* autoloaded script names */
1040 ga_clear_strings(&ga_loaded);
1041
Bram Moolenaarcca74132013-09-25 21:00:28 +02001042 /* Script-local variables. First clear all the variables and in a second
1043 * loop free the scriptvar_T, because a variable in one script might hold
1044 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001045 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001046 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001047 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001048 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001049 ga_clear(&ga_scripts);
1050
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001051 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001052 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001053
1054 /* functions */
1055 free_all_functions();
1056 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001057}
1058#endif
1059
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 * Return the name of the executed function.
1062 */
1063 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001064func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067}
1068
1069/*
1070 * Return the address holding the next breakpoint line for a funccall cookie.
1071 */
1072 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
Bram Moolenaar33570922005-01-25 22:26:29 +00001075 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076}
1077
1078/*
1079 * Return the address holding the debug tick for a funccall cookie.
1080 */
1081 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001082func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
Bram Moolenaar33570922005-01-25 22:26:29 +00001084 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085}
1086
1087/*
1088 * Return the nesting level for a funccall cookie.
1089 */
1090 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001091func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
Bram Moolenaar33570922005-01-25 22:26:29 +00001093 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094}
1095
1096/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001097funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001099/* pointer to list of previously used funccal, still around because some
1100 * item in it is still being used. */
1101funccall_T *previous_funccal = NULL;
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103/*
1104 * Return TRUE when a function was ended by a ":return" command.
1105 */
1106 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001107current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108{
1109 return current_funccal->returned;
1110}
1111
1112
1113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 * Set an internal variable to a string value. Creates the variable if it does
1115 * not already exist.
1116 */
1117 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001118set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001120 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001121 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122
1123 val = vim_strsave(value);
1124 if (val != NULL)
1125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001127 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001129 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 }
1133}
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137static char_u *redir_endp = NULL;
1138static char_u *redir_varname = NULL;
1139
1140/*
1141 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001142 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 * Returns OK if successfully completed the setup. FAIL otherwise.
1144 */
1145 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001146var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147{
1148 int save_emsg;
1149 int err;
1150 typval_T tv;
1151
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001153 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 {
1155 EMSG(_(e_invarg));
1156 return FAIL;
1157 }
1158
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 redir_varname = vim_strsave(name);
1161 if (redir_varname == NULL)
1162 return FAIL;
1163
1164 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1165 if (redir_lval == NULL)
1166 {
1167 var_redir_stop();
1168 return FAIL;
1169 }
1170
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171 /* The output is stored in growarray "redir_ga" until redirection ends. */
1172 ga_init2(&redir_ga, (int)sizeof(char), 500);
1173
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001175 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001176 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1178 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001179 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 if (redir_endp != NULL && *redir_endp != NUL)
1181 /* Trailing characters are present after the variable name */
1182 EMSG(_(e_trailing));
1183 else
1184 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001185 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 var_redir_stop();
1187 return FAIL;
1188 }
1189
1190 /* check if we can write to the variable: set it to or append an empty
1191 * string */
1192 save_emsg = did_emsg;
1193 did_emsg = FALSE;
1194 tv.v_type = VAR_STRING;
1195 tv.vval.v_string = (char_u *)"";
1196 if (append)
1197 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1198 else
1199 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001200 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001202 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203 if (err)
1204 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001205 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 var_redir_stop();
1207 return FAIL;
1208 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209
1210 return OK;
1211}
1212
1213/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001214 * Append "value[value_len]" to the variable set by var_redir_start().
1215 * The actual appending is postponed until redirection ends, because the value
1216 * appended may in fact be the string we write to, changing it may cause freed
1217 * memory to be used:
1218 * :redir => foo
1219 * :let foo
1220 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221 */
1222 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001223var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001224{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001225 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001226
1227 if (redir_lval == NULL)
1228 return;
1229
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001230 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001231 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001232 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001235 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001236 {
1237 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001238 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001239 }
1240 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001241 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001242}
1243
1244/*
1245 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001246 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001247 */
1248 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001249var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001250{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001251 typval_T tv;
1252
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001253 if (redir_lval != NULL)
1254 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001255 /* If there was no error: assign the text to the variable. */
1256 if (redir_endp != NULL)
1257 {
1258 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1259 tv.v_type = VAR_STRING;
1260 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001261 /* Call get_lval() again, if it's inside a Dict or List it may
1262 * have changed. */
1263 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001264 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001265 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1266 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1267 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001268 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001269
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001270 /* free the collected output */
1271 vim_free(redir_ga.ga_data);
1272 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001273
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001274 vim_free(redir_lval);
1275 redir_lval = NULL;
1276 }
1277 vim_free(redir_varname);
1278 redir_varname = NULL;
1279}
1280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281# if defined(FEAT_MBYTE) || defined(PROTO)
1282 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001283eval_charconvert(
1284 char_u *enc_from,
1285 char_u *enc_to,
1286 char_u *fname_from,
1287 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288{
1289 int err = FALSE;
1290
1291 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1292 set_vim_var_string(VV_CC_TO, enc_to, -1);
1293 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1294 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1295 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1296 err = TRUE;
1297 set_vim_var_string(VV_CC_FROM, NULL, -1);
1298 set_vim_var_string(VV_CC_TO, NULL, -1);
1299 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1300 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1301
1302 if (err)
1303 return FAIL;
1304 return OK;
1305}
1306# endif
1307
1308# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001310eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 int err = FALSE;
1313
1314 set_vim_var_string(VV_FNAME_IN, fname, -1);
1315 set_vim_var_string(VV_CMDARG, args, -1);
1316 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1317 err = TRUE;
1318 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1319 set_vim_var_string(VV_CMDARG, NULL, -1);
1320
1321 if (err)
1322 {
1323 mch_remove(fname);
1324 return FAIL;
1325 }
1326 return OK;
1327}
1328# endif
1329
1330# if defined(FEAT_DIFF) || defined(PROTO)
1331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001332eval_diff(
1333 char_u *origfile,
1334 char_u *newfile,
1335 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 int err = FALSE;
1338
1339 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1340 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1341 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1342 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1343 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1344 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1345 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1346}
1347
1348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001349eval_patch(
1350 char_u *origfile,
1351 char_u *difffile,
1352 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
1354 int err;
1355
1356 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1357 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1358 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1359 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1360 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1361 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1362 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1363}
1364# endif
1365
1366/*
1367 * Top level evaluation function, returning a boolean.
1368 * Sets "error" to TRUE if there was an error.
1369 * Return TRUE or FALSE.
1370 */
1371 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001372eval_to_bool(
1373 char_u *arg,
1374 int *error,
1375 char_u **nextcmd,
1376 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
Bram Moolenaar33570922005-01-25 22:26:29 +00001378 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 int retval = FALSE;
1380
1381 if (skip)
1382 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 else
1386 {
1387 *error = FALSE;
1388 if (!skip)
1389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001390 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393 }
1394 if (skip)
1395 --emsg_skip;
1396
1397 return retval;
1398}
1399
1400/*
1401 * Top level evaluation function, returning a string. If "skip" is TRUE,
1402 * only parsing to "nextcmd" is done, without reporting errors. Return
1403 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1404 */
1405 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001406eval_to_string_skip(
1407 char_u *arg,
1408 char_u **nextcmd,
1409 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 char_u *retval;
1413
1414 if (skip)
1415 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 retval = NULL;
1418 else
1419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 retval = vim_strsave(get_tv_string(&tv));
1421 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 }
1423 if (skip)
1424 --emsg_skip;
1425
1426 return retval;
1427}
1428
1429/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001430 * Skip over an expression at "*pp".
1431 * Return FAIL for an error, OK otherwise.
1432 */
1433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001434skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001435{
Bram Moolenaar33570922005-01-25 22:26:29 +00001436 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001437
1438 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001440}
1441
1442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001444 * When "convert" is TRUE convert a List into a sequence of lines and convert
1445 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 * Return pointer to allocated memory, or NULL for failure.
1447 */
1448 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001449eval_to_string(
1450 char_u *arg,
1451 char_u **nextcmd,
1452 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
Bram Moolenaar33570922005-01-25 22:26:29 +00001454 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001456 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001457#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001458 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001461 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 retval = NULL;
1463 else
1464 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001465 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001466 {
1467 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001468 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001469 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001470 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001471 if (tv.vval.v_list->lv_len > 0)
1472 ga_append(&ga, NL);
1473 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001474 ga_append(&ga, NUL);
1475 retval = (char_u *)ga.ga_data;
1476 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001477#ifdef FEAT_FLOAT
1478 else if (convert && tv.v_type == VAR_FLOAT)
1479 {
1480 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1481 retval = vim_strsave(numbuf);
1482 }
1483#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001484 else
1485 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
1488
1489 return retval;
1490}
1491
1492/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001493 * Call eval_to_string() without using current local variables and using
1494 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001497eval_to_string_safe(
1498 char_u *arg,
1499 char_u **nextcmd,
1500 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 char_u *retval;
1503 void *save_funccalp;
1504
1505 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001506 if (use_sandbox)
1507 ++sandbox;
1508 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001509 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001510 if (use_sandbox)
1511 --sandbox;
1512 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 restore_funccal(save_funccalp);
1514 return retval;
1515}
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517/*
1518 * Top level evaluation function, returning a number.
1519 * Evaluates "expr" silently.
1520 * Returns -1 for an error.
1521 */
1522 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001523eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
Bram Moolenaar33570922005-01-25 22:26:29 +00001525 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001527 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
1529 ++emsg_off;
1530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 retval = -1;
1533 else
1534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001535 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 --emsg_off;
1539
1540 return retval;
1541}
1542
Bram Moolenaara40058a2005-07-11 22:42:07 +00001543/*
1544 * Prepare v: variable "idx" to be used.
1545 * Save the current typeval in "save_tv".
1546 * When not used yet add the variable to the v: hashtable.
1547 */
1548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001549prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001550{
1551 *save_tv = vimvars[idx].vv_tv;
1552 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1553 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1554}
1555
1556/*
1557 * Restore v: variable "idx" to typeval "save_tv".
1558 * When no longer defined, remove the variable from the v: hashtable.
1559 */
1560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001562{
1563 hashitem_T *hi;
1564
Bram Moolenaara40058a2005-07-11 22:42:07 +00001565 vimvars[idx].vv_tv = *save_tv;
1566 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1567 {
1568 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1569 if (HASHITEM_EMPTY(hi))
1570 EMSG2(_(e_intern2), "restore_vimvar()");
1571 else
1572 hash_remove(&vimvarht, hi);
1573 }
1574}
1575
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001576#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001577/*
1578 * Evaluate an expression to a list with suggestions.
1579 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001580 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001581 */
1582 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001583eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001584{
1585 typval_T save_val;
1586 typval_T rettv;
1587 list_T *list = NULL;
1588 char_u *p = skipwhite(expr);
1589
1590 /* Set "v:val" to the bad word. */
1591 prepare_vimvar(VV_VAL, &save_val);
1592 vimvars[VV_VAL].vv_type = VAR_STRING;
1593 vimvars[VV_VAL].vv_str = badword;
1594 if (p_verbose == 0)
1595 ++emsg_off;
1596
1597 if (eval1(&p, &rettv, TRUE) == OK)
1598 {
1599 if (rettv.v_type != VAR_LIST)
1600 clear_tv(&rettv);
1601 else
1602 list = rettv.vval.v_list;
1603 }
1604
1605 if (p_verbose == 0)
1606 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001607 restore_vimvar(VV_VAL, &save_val);
1608
1609 return list;
1610}
1611
1612/*
1613 * "list" is supposed to contain two items: a word and a number. Return the
1614 * word in "pp" and the number as the return value.
1615 * Return -1 if anything isn't right.
1616 * Used to get the good word and score from the eval_spell_expr() result.
1617 */
1618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001619get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001620{
1621 listitem_T *li;
1622
1623 li = list->lv_first;
1624 if (li == NULL)
1625 return -1;
1626 *pp = get_tv_string(&li->li_tv);
1627
1628 li = li->li_next;
1629 if (li == NULL)
1630 return -1;
1631 return get_tv_number(&li->li_tv);
1632}
1633#endif
1634
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001635/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001636 * Top level evaluation function.
1637 * Returns an allocated typval_T with the result.
1638 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001639 */
1640 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001641eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001642{
1643 typval_T *tv;
1644
1645 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001646 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001647 {
1648 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001649 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001650 }
1651
1652 return tv;
1653}
1654
1655
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001658 * Uses argv[argc] for the function arguments. Only Number and String
1659 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001662 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001663call_vim_function(
1664 char_u *func,
1665 int argc,
1666 char_u **argv,
1667 int safe, /* use the sandbox */
1668 int str_arg_only, /* all arguments are strings */
1669 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
Bram Moolenaar33570922005-01-25 22:26:29 +00001671 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 long n;
1673 int len;
1674 int i;
1675 int doesrange;
1676 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001679 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 for (i = 0; i < argc; i++)
1684 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001685 /* Pass a NULL or empty argument as an empty string */
1686 if (argv[i] == NULL || *argv[i] == NUL)
1687 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001688 argvars[i].v_type = VAR_STRING;
1689 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001690 continue;
1691 }
1692
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001693 if (str_arg_only)
1694 len = 0;
1695 else
1696 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001697 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 if (len != 0 && len == (int)STRLEN(argv[i]))
1699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001700 argvars[i].v_type = VAR_NUMBER;
1701 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703 else
1704 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001705 argvars[i].v_type = VAR_STRING;
1706 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
1708 }
1709
1710 if (safe)
1711 {
1712 save_funccalp = save_funccal();
1713 ++sandbox;
1714 }
1715
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1717 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001719 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if (safe)
1721 {
1722 --sandbox;
1723 restore_funccal(save_funccalp);
1724 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725 vim_free(argvars);
1726
1727 if (ret == FAIL)
1728 clear_tv(rettv);
1729
1730 return ret;
1731}
1732
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001733/*
1734 * Call vimL function "func" and return the result as a number.
1735 * Returns -1 when calling the function fails.
1736 * Uses argv[argc] for the function arguments.
1737 */
1738 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001739call_func_retnr(
1740 char_u *func,
1741 int argc,
1742 char_u **argv,
1743 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001744{
1745 typval_T rettv;
1746 long retval;
1747
1748 /* All arguments are passed as strings, no conversion to number. */
1749 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1750 return -1;
1751
1752 retval = get_tv_number_chk(&rettv, NULL);
1753 clear_tv(&rettv);
1754 return retval;
1755}
1756
1757#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1758 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1759
Bram Moolenaar4f688582007-07-24 12:34:30 +00001760# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001762 * Call vimL function "func" and return the result as a string.
1763 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001764 * Uses argv[argc] for the function arguments.
1765 */
1766 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001767call_func_retstr(
1768 char_u *func,
1769 int argc,
1770 char_u **argv,
1771 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772{
1773 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001774 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001775
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001776 /* All arguments are passed as strings, no conversion to number. */
1777 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001778 return NULL;
1779
1780 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001781 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 return retval;
1783}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001784# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001785
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001786/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001787 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001788 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001789 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001790 */
1791 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001792call_func_retlist(
1793 char_u *func,
1794 int argc,
1795 char_u **argv,
1796 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001797{
1798 typval_T rettv;
1799
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001800 /* All arguments are passed as strings, no conversion to number. */
1801 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001802 return NULL;
1803
1804 if (rettv.v_type != VAR_LIST)
1805 {
1806 clear_tv(&rettv);
1807 return NULL;
1808 }
1809
1810 return rettv.vval.v_list;
1811}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#endif
1813
1814/*
1815 * Save the current function call pointer, and set it to NULL.
1816 * Used when executing autocommands and for ":source".
1817 */
1818 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001819save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001821 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 current_funccal = NULL;
1824 return (void *)fc;
1825}
1826
1827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001828restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001830 funccall_T *fc = (funccall_T *)vfc;
1831
1832 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833}
1834
Bram Moolenaar05159a02005-02-26 23:04:13 +00001835#if defined(FEAT_PROFILE) || defined(PROTO)
1836/*
1837 * Prepare profiling for entering a child or something else that is not
1838 * counted for the script/function itself.
1839 * Should always be called in pair with prof_child_exit().
1840 */
1841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001842prof_child_enter(
1843 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001844{
1845 funccall_T *fc = current_funccal;
1846
1847 if (fc != NULL && fc->func->uf_profiling)
1848 profile_start(&fc->prof_child);
1849 script_prof_save(tm);
1850}
1851
1852/*
1853 * Take care of time spent in a child.
1854 * Should always be called after prof_child_enter().
1855 */
1856 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001857prof_child_exit(
1858 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001859{
1860 funccall_T *fc = current_funccal;
1861
1862 if (fc != NULL && fc->func->uf_profiling)
1863 {
1864 profile_end(&fc->prof_child);
1865 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1866 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1867 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1868 }
1869 script_prof_restore(tm);
1870}
1871#endif
1872
1873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874#ifdef FEAT_FOLDING
1875/*
1876 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1877 * it in "*cp". Doesn't give error messages.
1878 */
1879 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001880eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 int retval;
1884 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001885 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1886 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
1888 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001889 if (use_sandbox)
1890 ++sandbox;
1891 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 retval = 0;
1895 else
1896 {
1897 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 if (tv.v_type == VAR_NUMBER)
1899 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001900 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 retval = 0;
1902 else
1903 {
1904 /* If the result is a string, check if there is a non-digit before
1905 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (!VIM_ISDIGIT(*s) && *s != '-')
1908 *cp = *s++;
1909 retval = atol((char *)s);
1910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001914 if (use_sandbox)
1915 --sandbox;
1916 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
1918 return retval;
1919}
1920#endif
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 * ":let" list all variable values
1924 * ":let var1 var2" list variable values
1925 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 * ":let var += expr" assignment command.
1927 * ":let var -= expr" assignment command.
1928 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 */
1931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001932ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
1934 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001936 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 int var_count = 0;
1939 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001941 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001942 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
Bram Moolenaardb552d602006-03-23 22:59:57 +00001944 argend = skip_var_list(arg, &var_count, &semicolon);
1945 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001947 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1948 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001949 expr = skipwhite(argend);
1950 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1951 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001953 /*
1954 * ":let" without "=": list variables
1955 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 if (*arg == '[')
1957 EMSG(_(e_invarg));
1958 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001960 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001964 list_glob_vars(&first);
1965 list_buf_vars(&first);
1966 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001967#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001968 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001969#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001970 list_script_vars(&first);
1971 list_func_vars(&first);
1972 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 eap->nextcmd = check_nextcmd(arg);
1975 }
1976 else
1977 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001978 op[0] = '=';
1979 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001980 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001982 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1983 op[0] = *expr; /* +=, -= or .= */
1984 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001986 else
1987 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (eap->skip)
1990 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (eap->skip)
1993 {
1994 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 --emsg_skip;
1997 }
1998 else if (i != FAIL)
1999 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004 }
2005}
2006
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007/*
2008 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2009 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002010 * When "nextchars" is not NULL it points to a string with characters that
2011 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2012 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 * Returns OK or FAIL;
2014 */
2015 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002016ex_let_vars(
2017 char_u *arg_start,
2018 typval_T *tv,
2019 int copy, /* copy values from "tv", don't move */
2020 int semicolon, /* from skip_var_list() */
2021 int var_count, /* from skip_var_list() */
2022 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023{
2024 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002025 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002027 listitem_T *item;
2028 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029
2030 if (*arg != '[')
2031 {
2032 /*
2033 * ":let var = expr" or ":for var in list"
2034 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002036 return FAIL;
2037 return OK;
2038 }
2039
2040 /*
2041 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2042 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002043 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 {
2045 EMSG(_(e_listreq));
2046 return FAIL;
2047 }
2048
2049 i = list_len(l);
2050 if (semicolon == 0 && var_count < i)
2051 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002052 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002053 return FAIL;
2054 }
2055 if (var_count - semicolon > i)
2056 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002057 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 return FAIL;
2059 }
2060
2061 item = l->lv_first;
2062 while (*arg != ']')
2063 {
2064 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002065 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 item = item->li_next;
2067 if (arg == NULL)
2068 return FAIL;
2069
2070 arg = skipwhite(arg);
2071 if (*arg == ';')
2072 {
2073 /* Put the rest of the list (may be empty) in the var after ';'.
2074 * Create a new list for this. */
2075 l = list_alloc();
2076 if (l == NULL)
2077 return FAIL;
2078 while (item != NULL)
2079 {
2080 list_append_tv(l, &item->li_tv);
2081 item = item->li_next;
2082 }
2083
2084 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002085 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 ltv.vval.v_list = l;
2087 l->lv_refcount = 1;
2088
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2090 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002091 clear_tv(&ltv);
2092 if (arg == NULL)
2093 return FAIL;
2094 break;
2095 }
2096 else if (*arg != ',' && *arg != ']')
2097 {
2098 EMSG2(_(e_intern2), "ex_let_vars()");
2099 return FAIL;
2100 }
2101 }
2102
2103 return OK;
2104}
2105
2106/*
2107 * Skip over assignable variable "var" or list of variables "[var, var]".
2108 * Used for ":let varvar = expr" and ":for varvar in expr".
2109 * For "[var, var]" increment "*var_count" for each variable.
2110 * for "[var, var; var]" set "semicolon".
2111 * Return NULL for an error.
2112 */
2113 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002114skip_var_list(
2115 char_u *arg,
2116 int *var_count,
2117 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002118{
2119 char_u *p, *s;
2120
2121 if (*arg == '[')
2122 {
2123 /* "[var, var]": find the matching ']'. */
2124 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002125 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126 {
2127 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2128 s = skip_var_one(p);
2129 if (s == p)
2130 {
2131 EMSG2(_(e_invarg2), p);
2132 return NULL;
2133 }
2134 ++*var_count;
2135
2136 p = skipwhite(s);
2137 if (*p == ']')
2138 break;
2139 else if (*p == ';')
2140 {
2141 if (*semicolon == 1)
2142 {
2143 EMSG(_("Double ; in list of variables"));
2144 return NULL;
2145 }
2146 *semicolon = 1;
2147 }
2148 else if (*p != ',')
2149 {
2150 EMSG2(_(e_invarg2), p);
2151 return NULL;
2152 }
2153 }
2154 return p + 1;
2155 }
2156 else
2157 return skip_var_one(arg);
2158}
2159
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002160/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002161 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002162 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002163 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002165skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002167 if (*arg == '@' && arg[1] != NUL)
2168 return arg + 2;
2169 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2170 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002171}
2172
Bram Moolenaara7043832005-01-21 11:56:39 +00002173/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002174 * List variables for hashtab "ht" with prefix "prefix".
2175 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002176 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178list_hashtable_vars(
2179 hashtab_T *ht,
2180 char_u *prefix,
2181 int empty,
2182 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002183{
Bram Moolenaar33570922005-01-25 22:26:29 +00002184 hashitem_T *hi;
2185 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002186 int todo;
2187
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002188 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002189 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2190 {
2191 if (!HASHITEM_EMPTY(hi))
2192 {
2193 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002194 di = HI2DI(hi);
2195 if (empty || di->di_tv.v_type != VAR_STRING
2196 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002198 }
2199 }
2200}
2201
2202/*
2203 * List global variables.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002207{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002209}
2210
2211/*
2212 * List buffer variables.
2213 */
2214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002215list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002216{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 char_u numbuf[NUMBUFLEN];
2218
Bram Moolenaar429fa852013-04-15 12:27:36 +02002219 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221
2222 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2224 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002225}
2226
2227/*
2228 * List window variables.
2229 */
2230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002231list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002232{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002233 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002234 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002235}
2236
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237#ifdef FEAT_WINDOWS
2238/*
2239 * List tab page variables.
2240 */
2241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002242list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002243{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002244 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002246}
2247#endif
2248
Bram Moolenaara7043832005-01-21 11:56:39 +00002249/*
2250 * List Vim variables.
2251 */
2252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002253list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256}
2257
2258/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002259 * List script-local variables, if there is a script.
2260 */
2261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002262list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002263{
2264 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002265 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2266 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002267}
2268
2269/*
2270 * List function variables, if there is a function.
2271 */
2272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002273list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002274{
2275 if (current_funccal != NULL)
2276 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002278}
2279
2280/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 * List variables in "arg".
2282 */
2283 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002284list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285{
2286 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 char_u *name_start;
2290 char_u *arg_subsc;
2291 char_u *tofree;
2292 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293
2294 while (!ends_excmd(*arg) && !got_int)
2295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002298 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2300 {
2301 emsg_severe = TRUE;
2302 EMSG(_(e_trailing));
2303 break;
2304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002306 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 /* get_name_len() takes care of expanding curly braces */
2309 name_start = name = arg;
2310 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2311 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 /* This is mainly to keep test 49 working: when expanding
2314 * curly braces fails overrule the exception error message. */
2315 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 emsg_severe = TRUE;
2318 EMSG2(_(e_invarg2), arg);
2319 break;
2320 }
2321 error = TRUE;
2322 }
2323 else
2324 {
2325 if (tofree != NULL)
2326 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002327 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 else
2330 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002331 /* handle d.key, l[idx], f(expr) */
2332 arg_subsc = arg;
2333 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002334 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002340 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002341 case 'g': list_glob_vars(first); break;
2342 case 'b': list_buf_vars(first); break;
2343 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002344#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002345 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002346#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002347 case 'v': list_vim_vars(first); break;
2348 case 's': list_script_vars(first); break;
2349 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002350 default:
2351 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002352 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002353 }
2354 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002355 {
2356 char_u numbuf[NUMBUFLEN];
2357 char_u *tf;
2358 int c;
2359 char_u *s;
2360
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002361 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002362 c = *arg;
2363 *arg = NUL;
2364 list_one_var_a((char_u *)"",
2365 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002366 tv.v_type,
2367 s == NULL ? (char_u *)"" : s,
2368 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002369 *arg = c;
2370 vim_free(tf);
2371 }
2372 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002373 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 }
2375 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002376
2377 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002379
2380 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 }
2382
2383 return arg;
2384}
2385
2386/*
2387 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2388 * Returns a pointer to the char just after the var name.
2389 * Returns NULL if there is an error.
2390 */
2391 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002392ex_let_one(
2393 char_u *arg, /* points to variable name */
2394 typval_T *tv, /* value to assign to variable */
2395 int copy, /* copy value from "tv" */
2396 char_u *endchars, /* valid chars after variable name or NULL */
2397 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398{
2399 int c1;
2400 char_u *name;
2401 char_u *p;
2402 char_u *arg_end = NULL;
2403 int len;
2404 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406
2407 /*
2408 * ":let $VAR = expr": Set environment variable.
2409 */
2410 if (*arg == '$')
2411 {
2412 /* Find the end of the name. */
2413 ++arg;
2414 name = arg;
2415 len = get_env_len(&arg);
2416 if (len == 0)
2417 EMSG2(_(e_invarg2), name - 1);
2418 else
2419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 if (op != NULL && (*op == '+' || *op == '-'))
2421 EMSG2(_(e_letwrong), op);
2422 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002423 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002425 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 {
2427 c1 = name[len];
2428 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 p = get_tv_string_chk(tv);
2430 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 {
2432 int mustfree = FALSE;
2433 char_u *s = vim_getenv(name, &mustfree);
2434
2435 if (s != NULL)
2436 {
2437 p = tofree = concat_str(s, p);
2438 if (mustfree)
2439 vim_free(s);
2440 }
2441 }
2442 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 if (STRICMP(name, "HOME") == 0)
2446 init_homedir();
2447 else if (didset_vim && STRICMP(name, "VIM") == 0)
2448 didset_vim = FALSE;
2449 else if (didset_vimruntime
2450 && STRICMP(name, "VIMRUNTIME") == 0)
2451 didset_vimruntime = FALSE;
2452 arg_end = arg;
2453 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 }
2457 }
2458 }
2459
2460 /*
2461 * ":let &option = expr": Set option value.
2462 * ":let &l:option = expr": Set local option value.
2463 * ":let &g:option = expr": Set global option value.
2464 */
2465 else if (*arg == '&')
2466 {
2467 /* Find the end of the name. */
2468 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002469 if (p == NULL || (endchars != NULL
2470 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 EMSG(_(e_letunexp));
2472 else
2473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 long n;
2475 int opt_type;
2476 long numval;
2477 char_u *stringval = NULL;
2478 char_u *s;
2479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 c1 = *p;
2481 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482
2483 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 s = get_tv_string_chk(tv); /* != NULL if number or string */
2485 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 {
2487 opt_type = get_option_value(arg, &numval,
2488 &stringval, opt_flags);
2489 if ((opt_type == 1 && *op == '.')
2490 || (opt_type == 0 && *op != '.'))
2491 EMSG2(_(e_letwrong), op);
2492 else
2493 {
2494 if (opt_type == 1) /* number */
2495 {
2496 if (*op == '+')
2497 n = numval + n;
2498 else
2499 n = numval - n;
2500 }
2501 else if (opt_type == 0 && stringval != NULL) /* string */
2502 {
2503 s = concat_str(stringval, s);
2504 vim_free(stringval);
2505 stringval = s;
2506 }
2507 }
2508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002509 if (s != NULL)
2510 {
2511 set_option_value(arg, n, s, opt_flags);
2512 arg_end = p;
2513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 }
2517 }
2518
2519 /*
2520 * ":let @r = expr": Set register contents.
2521 */
2522 else if (*arg == '@')
2523 {
2524 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002525 if (op != NULL && (*op == '+' || *op == '-'))
2526 EMSG2(_(e_letwrong), op);
2527 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002528 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 EMSG(_(e_letunexp));
2530 else
2531 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002532 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 char_u *s;
2534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002535 p = get_tv_string_chk(tv);
2536 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002538 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002539 if (s != NULL)
2540 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002541 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 vim_free(s);
2543 }
2544 }
2545 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002546 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002548 arg_end = arg + 1;
2549 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002550 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551 }
2552 }
2553
2554 /*
2555 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002558 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002560 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002562 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2566 EMSG(_(e_letunexp));
2567 else
2568 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002569 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 arg_end = p;
2571 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 }
2575
2576 else
2577 EMSG2(_(e_invarg2), arg);
2578
2579 return arg_end;
2580}
2581
2582/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2584 */
2585 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002586check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587{
2588 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2589 {
2590 EMSG2(_(e_readonlyvar), arg);
2591 return TRUE;
2592 }
2593 return FALSE;
2594}
2595
2596/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 * Get an lval: variable, Dict item or List item that can be assigned a value
2598 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2599 * "name.key", "name.key[expr]" etc.
2600 * Indexing only works if "name" is an existing List or Dictionary.
2601 * "name" points to the start of the name.
2602 * If "rettv" is not NULL it points to the value to be assigned.
2603 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2604 * wrong; must end in space or cmd separator.
2605 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002606 * flags:
2607 * GLV_QUIET: do not give error messages
2608 * GLV_NO_AUTOLOAD: do not use script autoloading
2609 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002611 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 */
2614 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002615get_lval(
2616 char_u *name,
2617 typval_T *rettv,
2618 lval_T *lp,
2619 int unlet,
2620 int skip,
2621 int flags, /* GLV_ values */
2622 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 char_u *expr_start, *expr_end;
2626 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 dictitem_T *v;
2628 typval_T var1;
2629 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002631 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002634 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002635 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002638 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639
2640 if (skip)
2641 {
2642 /* When skipping just find the end of the name. */
2643 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002644 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 }
2646
2647 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002648 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (expr_start != NULL)
2650 {
2651 /* Don't expand the name when we already know there is an error. */
2652 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2653 && *p != '[' && *p != '.')
2654 {
2655 EMSG(_(e_trailing));
2656 return NULL;
2657 }
2658
2659 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2660 if (lp->ll_exp_name == NULL)
2661 {
2662 /* Report an invalid expression in braces, unless the
2663 * expression evaluation has been cancelled due to an
2664 * aborting error, an interrupt, or an exception. */
2665 if (!aborting() && !quiet)
2666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002667 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 EMSG2(_(e_invarg2), name);
2669 return NULL;
2670 }
2671 }
2672 lp->ll_name = lp->ll_exp_name;
2673 }
2674 else
2675 lp->ll_name = name;
2676
2677 /* Without [idx] or .key we are done. */
2678 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2679 return p;
2680
2681 cc = *p;
2682 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002683 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (v == NULL && !quiet)
2685 EMSG2(_(e_undefvar), lp->ll_name);
2686 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687 if (v == NULL)
2688 return NULL;
2689
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 /*
2691 * Loop until no more [idx] or .key is following.
2692 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002693 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2697 && !(lp->ll_tv->v_type == VAR_DICT
2698 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (!quiet)
2701 EMSG(_("E689: Can only index a List or Dictionary"));
2702 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (!quiet)
2707 EMSG(_("E708: [:] must come last"));
2708 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002709 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 len = -1;
2712 if (*p == '.')
2713 {
2714 key = p + 1;
2715 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2716 ;
2717 if (len == 0)
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_emptykey));
2721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 p = key + len;
2724 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 else
2726 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (*p == ':')
2730 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002731 else
2732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 empty1 = FALSE;
2734 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736 if (get_tv_string_chk(&var1) == NULL)
2737 {
2738 /* not a number or string */
2739 clear_tv(&var1);
2740 return NULL;
2741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
2743
2744 /* Optionally get the second index [ :expr]. */
2745 if (*p == ':')
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002750 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 if (!empty1)
2752 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (rettv != NULL && (rettv->v_type != VAR_LIST
2756 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (!quiet)
2759 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 if (!empty1)
2761 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 }
2764 p = skipwhite(p + 1);
2765 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 else
2768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2771 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (!empty1)
2773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002776 if (get_tv_string_chk(&var2) == NULL)
2777 {
2778 /* not a number or string */
2779 if (!empty1)
2780 clear_tv(&var1);
2781 clear_tv(&var2);
2782 return NULL;
2783 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002789
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (!quiet)
2793 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 if (!empty1)
2795 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800
2801 /* Skip to past ']'. */
2802 ++p;
2803 }
2804
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
2807 if (len == -1)
2808 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002810 key = get_tv_string_chk(&var1); /* is number or string */
2811 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 }
2816 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 lp->ll_list = NULL;
2818 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002819 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002821 /* When assigning to a scope dictionary check that a function and
2822 * variable name is valid (only variable name unless it is l: or
2823 * g: dictionary). Disallow overwriting a builtin function. */
2824 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002825 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002826 int prevval;
2827 int wrong;
2828
2829 if (len != -1)
2830 {
2831 prevval = key[len];
2832 key[len] = NUL;
2833 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002834 else
2835 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002836 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2837 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002839 || !valid_varname(key);
2840 if (len != -1)
2841 key[len] = prevval;
2842 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002843 return NULL;
2844 }
2845
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002848 /* Can't add "v:" variable. */
2849 if (lp->ll_dict == &vimvardict)
2850 {
2851 EMSG2(_(e_illvar), name);
2852 return NULL;
2853 }
2854
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002855 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002859 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 if (len == -1)
2861 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 }
2864 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 if (len == -1)
2869 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 p = NULL;
2872 break;
2873 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002874 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002875 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002876 return NULL;
2877
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 if (len == -1)
2879 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 }
2882 else
2883 {
2884 /*
2885 * Get the number and item for the only or first index of the List.
2886 */
2887 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 else
2890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002891 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 clear_tv(&var1);
2893 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_list = lp->ll_tv->vval.v_list;
2896 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2897 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002898 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002899 if (lp->ll_n1 < 0)
2900 {
2901 lp->ll_n1 = 0;
2902 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2903 }
2904 }
2905 if (lp->ll_li == NULL)
2906 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002908 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002909 if (!quiet)
2910 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002912 }
2913
2914 /*
2915 * May need to find the item or absolute index for the second
2916 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 * When no index given: "lp->ll_empty2" is TRUE.
2918 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002919 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002922 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002927 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002928 {
2929 if (!quiet)
2930 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002932 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002934 }
2935
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2937 if (lp->ll_n1 < 0)
2938 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2939 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002940 {
2941 if (!quiet)
2942 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002944 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002945 }
2946
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002949 }
2950
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 return p;
2952}
2953
2954/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002955 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 */
2957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002958clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959{
2960 vim_free(lp->ll_exp_name);
2961 vim_free(lp->ll_newkey);
2962}
2963
2964/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002965 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002967 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 */
2969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002970set_var_lval(
2971 lval_T *lp,
2972 char_u *endp,
2973 typval_T *rettv,
2974 int copy,
2975 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976{
2977 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002978 listitem_T *ri;
2979 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980
2981 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002982 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 cc = *endp;
2986 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 if (op != NULL && *op != '=')
2988 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002989 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002991 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002992 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002993 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002994 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002995 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002996 if ((di == NULL
2997 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2998 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2999 FALSE)))
3000 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 set_var(lp->ll_name, &tv, FALSE);
3002 clear_tv(&tv);
3003 }
3004 }
3005 else
3006 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003008 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003010 else if (tv_check_lock(lp->ll_newkey == NULL
3011 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003012 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003013 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 else if (lp->ll_range)
3015 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003016 listitem_T *ll_li = lp->ll_li;
3017 int ll_n1 = lp->ll_n1;
3018
3019 /*
3020 * Check whether any of the list items is locked
3021 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003022 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003023 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003024 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003025 return;
3026 ri = ri->li_next;
3027 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3028 break;
3029 ll_li = ll_li->li_next;
3030 ++ll_n1;
3031 }
3032
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 /*
3034 * Assign the List values to the list items.
3035 */
3036 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003037 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003038 if (op != NULL && *op != '=')
3039 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3040 else
3041 {
3042 clear_tv(&lp->ll_li->li_tv);
3043 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 ri = ri->li_next;
3046 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3047 break;
3048 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003051 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003052 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 ri = NULL;
3054 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003055 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003056 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 lp->ll_li = lp->ll_li->li_next;
3058 ++lp->ll_n1;
3059 }
3060 if (ri != NULL)
3061 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 else if (lp->ll_empty2
3063 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 : lp->ll_n1 != lp->ll_n2)
3065 EMSG(_("E711: List value has not enough items"));
3066 }
3067 else
3068 {
3069 /*
3070 * Assign to a List or Dictionary item.
3071 */
3072 if (lp->ll_newkey != NULL)
3073 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 if (op != NULL && *op != '=')
3075 {
3076 EMSG2(_(e_letwrong), op);
3077 return;
3078 }
3079
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003080 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003081 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003082 if (di == NULL)
3083 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003084 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3085 {
3086 vim_free(di);
3087 return;
3088 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003089 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003090 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 else if (op != NULL && *op != '=')
3092 {
3093 tv_op(lp->ll_tv, rettv, op);
3094 return;
3095 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003096 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003097 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003098
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003099 /*
3100 * Assign the value to the variable or list item.
3101 */
3102 if (copy)
3103 copy_tv(rettv, lp->ll_tv);
3104 else
3105 {
3106 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003107 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003108 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003109 }
3110 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003111}
3112
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003113/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3115 * Returns OK or FAIL.
3116 */
3117 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003118tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119{
3120 long n;
3121 char_u numbuf[NUMBUFLEN];
3122 char_u *s;
3123
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003124 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3125 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3126 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 {
3128 switch (tv1->v_type)
3129 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003130 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 case VAR_DICT:
3132 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003133 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003134 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003135 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003136 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 break;
3138
3139 case VAR_LIST:
3140 if (*op != '+' || tv2->v_type != VAR_LIST)
3141 break;
3142 /* List += List */
3143 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3144 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3145 return OK;
3146
3147 case VAR_NUMBER:
3148 case VAR_STRING:
3149 if (tv2->v_type == VAR_LIST)
3150 break;
3151 if (*op == '+' || *op == '-')
3152 {
3153 /* nr += nr or nr -= nr*/
3154 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155#ifdef FEAT_FLOAT
3156 if (tv2->v_type == VAR_FLOAT)
3157 {
3158 float_T f = n;
3159
3160 if (*op == '+')
3161 f += tv2->vval.v_float;
3162 else
3163 f -= tv2->vval.v_float;
3164 clear_tv(tv1);
3165 tv1->v_type = VAR_FLOAT;
3166 tv1->vval.v_float = f;
3167 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003168 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169#endif
3170 {
3171 if (*op == '+')
3172 n += get_tv_number(tv2);
3173 else
3174 n -= get_tv_number(tv2);
3175 clear_tv(tv1);
3176 tv1->v_type = VAR_NUMBER;
3177 tv1->vval.v_number = n;
3178 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003179 }
3180 else
3181 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182 if (tv2->v_type == VAR_FLOAT)
3183 break;
3184
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003185 /* str .= str */
3186 s = get_tv_string(tv1);
3187 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3188 clear_tv(tv1);
3189 tv1->v_type = VAR_STRING;
3190 tv1->vval.v_string = s;
3191 }
3192 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003193
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003194 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003195#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196 {
3197 float_T f;
3198
3199 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3200 && tv2->v_type != VAR_NUMBER
3201 && tv2->v_type != VAR_STRING))
3202 break;
3203 if (tv2->v_type == VAR_FLOAT)
3204 f = tv2->vval.v_float;
3205 else
3206 f = get_tv_number(tv2);
3207 if (*op == '+')
3208 tv1->vval.v_float += f;
3209 else
3210 tv1->vval.v_float -= f;
3211 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003212#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003213 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003214 }
3215 }
3216
3217 EMSG2(_(e_letwrong), op);
3218 return FAIL;
3219}
3220
3221/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 * Add a watcher to a list.
3223 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003224 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003225list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226{
3227 lw->lw_next = l->lv_watch;
3228 l->lv_watch = lw;
3229}
3230
3231/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003232 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 * No warning when it isn't found...
3234 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003236list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237{
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239
3240 lwp = &l->lv_watch;
3241 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3242 {
3243 if (lw == lwrem)
3244 {
3245 *lwp = lw->lw_next;
3246 break;
3247 }
3248 lwp = &lw->lw_next;
3249 }
3250}
3251
3252/*
3253 * Just before removing an item from a list: advance watchers to the next
3254 * item.
3255 */
3256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003257list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258{
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260
3261 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3262 if (lw->lw_item == item)
3263 lw->lw_item = item->li_next;
3264}
3265
3266/*
3267 * Evaluate the expression used in a ":for var in expr" command.
3268 * "arg" points to "var".
3269 * Set "*errp" to TRUE for an error, FALSE otherwise;
3270 * Return a pointer that holds the info. Null when there is an error.
3271 */
3272 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003273eval_for_line(
3274 char_u *arg,
3275 int *errp,
3276 char_u **nextcmdp,
3277 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278{
Bram Moolenaar33570922005-01-25 22:26:29 +00003279 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 typval_T tv;
3282 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283
3284 *errp = TRUE; /* default: there is an error */
3285
Bram Moolenaar33570922005-01-25 22:26:29 +00003286 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 if (fi == NULL)
3288 return NULL;
3289
3290 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3291 if (expr == NULL)
3292 return fi;
3293
3294 expr = skipwhite(expr);
3295 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3296 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003297 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 return fi;
3299 }
3300
3301 if (skip)
3302 ++emsg_skip;
3303 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3304 {
3305 *errp = FALSE;
3306 if (!skip)
3307 {
3308 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003309 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003310 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003312 clear_tv(&tv);
3313 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003314 else if (l == NULL)
3315 {
3316 /* a null list is like an empty list: do nothing */
3317 clear_tv(&tv);
3318 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 else
3320 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003321 /* No need to increment the refcount, it's already set for the
3322 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 fi->fi_list = l;
3324 list_add_watch(l, &fi->fi_lw);
3325 fi->fi_lw.lw_item = l->lv_first;
3326 }
3327 }
3328 }
3329 if (skip)
3330 --emsg_skip;
3331
3332 return fi;
3333}
3334
3335/*
3336 * Use the first item in a ":for" list. Advance to the next.
3337 * Assign the values to the variable (list). "arg" points to the first one.
3338 * Return TRUE when a valid item was found, FALSE when at end of list or
3339 * something wrong.
3340 */
3341 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003344 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003346 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347
3348 item = fi->fi_lw.lw_item;
3349 if (item == NULL)
3350 result = FALSE;
3351 else
3352 {
3353 fi->fi_lw.lw_item = item->li_next;
3354 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3355 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3356 }
3357 return result;
3358}
3359
3360/*
3361 * Free the structure used to store info used by ":for".
3362 */
3363 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003364free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365{
Bram Moolenaar33570922005-01-25 22:26:29 +00003366 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003367
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003368 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003369 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003370 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003371 list_unref(fi->fi_list);
3372 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003373 vim_free(fi);
3374}
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3377
3378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003379set_context_for_expression(
3380 expand_T *xp,
3381 char_u *arg,
3382 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
3384 int got_eq = FALSE;
3385 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003386 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003388 if (cmdidx == CMD_let)
3389 {
3390 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003391 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003392 {
3393 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003394 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003395 {
3396 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003397 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003398 if (vim_iswhite(*p))
3399 break;
3400 }
3401 return;
3402 }
3403 }
3404 else
3405 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3406 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 while ((xp->xp_pattern = vim_strpbrk(arg,
3408 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3409 {
3410 c = *xp->xp_pattern;
3411 if (c == '&')
3412 {
3413 c = xp->xp_pattern[1];
3414 if (c == '&')
3415 {
3416 ++xp->xp_pattern;
3417 xp->xp_context = cmdidx != CMD_let || got_eq
3418 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3419 }
3420 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003423 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3424 xp->xp_pattern += 2;
3425
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428 else if (c == '$')
3429 {
3430 /* environment variable */
3431 xp->xp_context = EXPAND_ENV_VARS;
3432 }
3433 else if (c == '=')
3434 {
3435 got_eq = TRUE;
3436 xp->xp_context = EXPAND_EXPRESSION;
3437 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003438 else if (c == '#'
3439 && xp->xp_context == EXPAND_EXPRESSION)
3440 {
3441 /* Autoload function/variable contains '#'. */
3442 break;
3443 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003444 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 && xp->xp_context == EXPAND_FUNCTIONS
3446 && vim_strchr(xp->xp_pattern, '(') == NULL)
3447 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003448 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 break;
3450 }
3451 else if (cmdidx != CMD_let || got_eq)
3452 {
3453 if (c == '"') /* string */
3454 {
3455 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3456 if (c == '\\' && xp->xp_pattern[1] != NUL)
3457 ++xp->xp_pattern;
3458 xp->xp_context = EXPAND_NOTHING;
3459 }
3460 else if (c == '\'') /* literal string */
3461 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003462 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3464 /* skip */ ;
3465 xp->xp_context = EXPAND_NOTHING;
3466 }
3467 else if (c == '|')
3468 {
3469 if (xp->xp_pattern[1] == '|')
3470 {
3471 ++xp->xp_pattern;
3472 xp->xp_context = EXPAND_EXPRESSION;
3473 }
3474 else
3475 xp->xp_context = EXPAND_COMMANDS;
3476 }
3477 else
3478 xp->xp_context = EXPAND_EXPRESSION;
3479 }
3480 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003481 /* Doesn't look like something valid, expand as an expression
3482 * anyway. */
3483 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 arg = xp->xp_pattern;
3485 if (*arg != NUL)
3486 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3487 /* skip */ ;
3488 }
3489 xp->xp_pattern = arg;
3490}
3491
3492#endif /* FEAT_CMDL_COMPL */
3493
3494/*
3495 * ":1,25call func(arg1, arg2)" function call.
3496 */
3497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003498ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
3500 char_u *arg = eap->arg;
3501 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003503 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003505 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 linenr_T lnum;
3507 int doesrange;
3508 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003509 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003510 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003512 if (eap->skip)
3513 {
3514 /* trans_function_name() doesn't work well when skipping, use eval0()
3515 * instead to skip to any following command, e.g. for:
3516 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003517 ++emsg_skip;
3518 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3519 clear_tv(&rettv);
3520 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003521 return;
3522 }
3523
Bram Moolenaar65639032016-03-16 21:40:30 +01003524 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003525 if (fudi.fd_newkey != NULL)
3526 {
3527 /* Still need to give an error message for missing key. */
3528 EMSG2(_(e_dictkey), fudi.fd_newkey);
3529 vim_free(fudi.fd_newkey);
3530 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003531 if (tofree == NULL)
3532 return;
3533
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003534 /* Increase refcount on dictionary, it could get deleted when evaluating
3535 * the arguments. */
3536 if (fudi.fd_dict != NULL)
3537 ++fudi.fd_dict->dv_refcount;
3538
Bram Moolenaar65639032016-03-16 21:40:30 +01003539 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3540 * contents. For VAR_PARTIAL get its partial, unless we already have one
3541 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003542 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003543 name = deref_func_name(tofree, &len,
3544 partial != NULL ? NULL : &partial, FALSE);
3545
Bram Moolenaar532c7802005-01-27 14:44:31 +00003546 /* Skip white space to allow ":call func ()". Not good, but required for
3547 * backward compatibility. */
3548 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003549 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
3551 if (*startarg != '(')
3552 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003553 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 goto end;
3555 }
3556
3557 /*
3558 * When skipping, evaluate the function once, to find the end of the
3559 * arguments.
3560 * When the function takes a range, this is discovered after the first
3561 * call, and the loop is broken.
3562 */
3563 if (eap->skip)
3564 {
3565 ++emsg_skip;
3566 lnum = eap->line2; /* do it once, also with an invalid range */
3567 }
3568 else
3569 lnum = eap->line1;
3570 for ( ; lnum <= eap->line2; ++lnum)
3571 {
3572 if (!eap->skip && eap->addr_count > 0)
3573 {
3574 curwin->w_cursor.lnum = lnum;
3575 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003576#ifdef FEAT_VIRTUALEDIT
3577 curwin->w_cursor.coladd = 0;
3578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003581 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003582 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003583 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 failed = TRUE;
3586 break;
3587 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003588
3589 /* Handle a function returning a Funcref, Dictionary or List. */
3590 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3591 {
3592 failed = TRUE;
3593 break;
3594 }
3595
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003596 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (doesrange || eap->skip)
3598 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003601 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003602 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003603 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (aborting())
3605 break;
3606 }
3607 if (eap->skip)
3608 --emsg_skip;
3609
3610 if (!failed)
3611 {
3612 /* Check for trailing illegal characters and a following command. */
3613 if (!ends_excmd(*arg))
3614 {
3615 emsg_severe = TRUE;
3616 EMSG(_(e_trailing));
3617 }
3618 else
3619 eap->nextcmd = check_nextcmd(arg);
3620 }
3621
3622end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003623 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003624 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625}
3626
3627/*
3628 * ":unlet[!] var1 ... " command.
3629 */
3630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003631ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003633 ex_unletlock(eap, eap->arg, 0);
3634}
3635
3636/*
3637 * ":lockvar" and ":unlockvar" commands
3638 */
3639 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003640ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 int deep = 2;
3644
3645 if (eap->forceit)
3646 deep = -1;
3647 else if (vim_isdigit(*arg))
3648 {
3649 deep = getdigits(&arg);
3650 arg = skipwhite(arg);
3651 }
3652
3653 ex_unletlock(eap, arg, deep);
3654}
3655
3656/*
3657 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3658 */
3659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003660ex_unletlock(
3661 exarg_T *eap,
3662 char_u *argstart,
3663 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664{
3665 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 do
3671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003673 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003674 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 if (lv.ll_name == NULL)
3676 error = TRUE; /* error but continue parsing */
3677 if (name_end == NULL || (!vim_iswhite(*name_end)
3678 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 if (name_end != NULL)
3681 {
3682 emsg_severe = TRUE;
3683 EMSG(_(e_trailing));
3684 }
3685 if (!(eap->skip || error))
3686 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 break;
3688 }
3689
3690 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003691 {
3692 if (eap->cmdidx == CMD_unlet)
3693 {
3694 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3695 error = TRUE;
3696 }
3697 else
3698 {
3699 if (do_lock_var(&lv, name_end, deep,
3700 eap->cmdidx == CMD_lockvar) == FAIL)
3701 error = TRUE;
3702 }
3703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003705 if (!eap->skip)
3706 clear_lval(&lv);
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 arg = skipwhite(name_end);
3709 } while (!ends_excmd(*arg));
3710
3711 eap->nextcmd = check_nextcmd(arg);
3712}
3713
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003715do_unlet_var(
3716 lval_T *lp,
3717 char_u *name_end,
3718 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 int ret = OK;
3721 int cc;
3722
3723 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003725 cc = *name_end;
3726 *name_end = NUL;
3727
3728 /* Normal name or expanded name. */
3729 if (check_changedtick(lp->ll_name))
3730 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003731 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003732 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003733 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003734 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003735 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003736 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003737 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003738 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003739 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003740 else if (lp->ll_range)
3741 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003743 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003744 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003745
3746 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3747 {
3748 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003749 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003750 return FAIL;
3751 ll_li = li;
3752 ++ll_n1;
3753 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003754
3755 /* Delete a range of List items. */
3756 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3757 {
3758 li = lp->ll_li->li_next;
3759 listitem_remove(lp->ll_list, lp->ll_li);
3760 lp->ll_li = li;
3761 ++lp->ll_n1;
3762 }
3763 }
3764 else
3765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003766 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003767 /* unlet a List item. */
3768 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003769 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003770 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003771 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003772 }
3773
3774 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003775}
3776
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777/*
3778 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 */
3781 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783{
Bram Moolenaar33570922005-01-25 22:26:29 +00003784 hashtab_T *ht;
3785 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003786 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003787 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003788 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
Bram Moolenaar33570922005-01-25 22:26:29 +00003790 ht = find_var_ht(name, &varname);
3791 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003793 if (ht == &globvarht)
3794 d = &globvardict;
3795 else if (current_funccal != NULL
3796 && ht == &current_funccal->l_vars.dv_hashtab)
3797 d = &current_funccal->l_vars;
3798 else if (ht == &compat_hashtab)
3799 d = &vimvardict;
3800 else
3801 {
3802 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3803 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3804 }
3805 if (d == NULL)
3806 {
3807 EMSG2(_(e_intern2), "do_unlet()");
3808 return FAIL;
3809 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003810 hi = hash_find(ht, varname);
3811 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003812 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003813 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003814 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003815 || var_check_ro(di->di_flags, name, FALSE)
3816 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003817 return FAIL;
3818
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003819 delete_var(ht, hi);
3820 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003823 if (forceit)
3824 return OK;
3825 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 return FAIL;
3827}
3828
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003829/*
3830 * Lock or unlock variable indicated by "lp".
3831 * "deep" is the levels to go (-1 for unlimited);
3832 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3833 */
3834 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003835do_lock_var(
3836 lval_T *lp,
3837 char_u *name_end,
3838 int deep,
3839 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003840{
3841 int ret = OK;
3842 int cc;
3843 dictitem_T *di;
3844
3845 if (deep == 0) /* nothing to do */
3846 return OK;
3847
3848 if (lp->ll_tv == NULL)
3849 {
3850 cc = *name_end;
3851 *name_end = NUL;
3852
3853 /* Normal name or expanded name. */
3854 if (check_changedtick(lp->ll_name))
3855 ret = FAIL;
3856 else
3857 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003858 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003859 if (di == NULL)
3860 ret = FAIL;
3861 else
3862 {
3863 if (lock)
3864 di->di_flags |= DI_FLAGS_LOCK;
3865 else
3866 di->di_flags &= ~DI_FLAGS_LOCK;
3867 item_lock(&di->di_tv, deep, lock);
3868 }
3869 }
3870 *name_end = cc;
3871 }
3872 else if (lp->ll_range)
3873 {
3874 listitem_T *li = lp->ll_li;
3875
3876 /* (un)lock a range of List items. */
3877 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3878 {
3879 item_lock(&li->li_tv, deep, lock);
3880 li = li->li_next;
3881 ++lp->ll_n1;
3882 }
3883 }
3884 else if (lp->ll_list != NULL)
3885 /* (un)lock a List item. */
3886 item_lock(&lp->ll_li->li_tv, deep, lock);
3887 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003888 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003889 item_lock(&lp->ll_di->di_tv, deep, lock);
3890
3891 return ret;
3892}
3893
3894/*
3895 * Lock or unlock an item. "deep" is nr of levels to go.
3896 */
3897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003898item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003899{
3900 static int recurse = 0;
3901 list_T *l;
3902 listitem_T *li;
3903 dict_T *d;
3904 hashitem_T *hi;
3905 int todo;
3906
3907 if (recurse >= DICT_MAXNEST)
3908 {
3909 EMSG(_("E743: variable nested too deep for (un)lock"));
3910 return;
3911 }
3912 if (deep == 0)
3913 return;
3914 ++recurse;
3915
3916 /* lock/unlock the item itself */
3917 if (lock)
3918 tv->v_lock |= VAR_LOCKED;
3919 else
3920 tv->v_lock &= ~VAR_LOCKED;
3921
3922 switch (tv->v_type)
3923 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003924 case VAR_UNKNOWN:
3925 case VAR_NUMBER:
3926 case VAR_STRING:
3927 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003928 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003929 case VAR_FLOAT:
3930 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003931 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003932 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003933 break;
3934
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003935 case VAR_LIST:
3936 if ((l = tv->vval.v_list) != NULL)
3937 {
3938 if (lock)
3939 l->lv_lock |= VAR_LOCKED;
3940 else
3941 l->lv_lock &= ~VAR_LOCKED;
3942 if (deep < 0 || deep > 1)
3943 /* recursive: lock/unlock the items the List contains */
3944 for (li = l->lv_first; li != NULL; li = li->li_next)
3945 item_lock(&li->li_tv, deep - 1, lock);
3946 }
3947 break;
3948 case VAR_DICT:
3949 if ((d = tv->vval.v_dict) != NULL)
3950 {
3951 if (lock)
3952 d->dv_lock |= VAR_LOCKED;
3953 else
3954 d->dv_lock &= ~VAR_LOCKED;
3955 if (deep < 0 || deep > 1)
3956 {
3957 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003958 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003959 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3960 {
3961 if (!HASHITEM_EMPTY(hi))
3962 {
3963 --todo;
3964 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3965 }
3966 }
3967 }
3968 }
3969 }
3970 --recurse;
3971}
3972
Bram Moolenaara40058a2005-07-11 22:42:07 +00003973/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003974 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3975 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003976 */
3977 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003978tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003979{
3980 return (tv->v_lock & VAR_LOCKED)
3981 || (tv->v_type == VAR_LIST
3982 && tv->vval.v_list != NULL
3983 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3984 || (tv->v_type == VAR_DICT
3985 && tv->vval.v_dict != NULL
3986 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3987}
3988
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3990/*
3991 * Delete all "menutrans_" variables.
3992 */
3993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003994del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003997 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
Bram Moolenaar33570922005-01-25 22:26:29 +00003999 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004000 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00004001 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004002 {
4003 if (!HASHITEM_EMPTY(hi))
4004 {
4005 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4007 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004008 }
4009 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011}
4012#endif
4013
4014#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4015
4016/*
4017 * Local string buffer for the next two functions to store a variable name
4018 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4019 * get_user_var_name().
4020 */
4021
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004022static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
4024static char_u *varnamebuf = NULL;
4025static int varnamebuflen = 0;
4026
4027/*
4028 * Function to concatenate a prefix and a variable name.
4029 */
4030 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004031cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032{
4033 int len;
4034
4035 len = (int)STRLEN(name) + 3;
4036 if (len > varnamebuflen)
4037 {
4038 vim_free(varnamebuf);
4039 len += 10; /* some additional space */
4040 varnamebuf = alloc(len);
4041 if (varnamebuf == NULL)
4042 {
4043 varnamebuflen = 0;
4044 return NULL;
4045 }
4046 varnamebuflen = len;
4047 }
4048 *varnamebuf = prefix;
4049 varnamebuf[1] = ':';
4050 STRCPY(varnamebuf + 2, name);
4051 return varnamebuf;
4052}
4053
4054/*
4055 * Function given to ExpandGeneric() to obtain the list of user defined
4056 * (global/buffer/window/built-in) variable names.
4057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004059get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004061 static long_u gdone;
4062 static long_u bdone;
4063 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004064#ifdef FEAT_WINDOWS
4065 static long_u tdone;
4066#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004067 static int vidx;
4068 static hashitem_T *hi;
4069 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
4071 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004073 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004074#ifdef FEAT_WINDOWS
4075 tdone = 0;
4076#endif
4077 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004078
4079 /* Global variables */
4080 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004082 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004084 else
4085 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004086 while (HASHITEM_EMPTY(hi))
4087 ++hi;
4088 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4089 return cat_prefix_varname('g', hi->hi_key);
4090 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004092
4093 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004094 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004095 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004097 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004098 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004099 else
4100 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004101 while (HASHITEM_EMPTY(hi))
4102 ++hi;
4103 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004105 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004107 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 return (char_u *)"b:changedtick";
4109 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004110
4111 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004112 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004113 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004115 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004116 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004117 else
4118 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004119 while (HASHITEM_EMPTY(hi))
4120 ++hi;
4121 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004123
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004124#ifdef FEAT_WINDOWS
4125 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004126 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004127 if (tdone < ht->ht_used)
4128 {
4129 if (tdone++ == 0)
4130 hi = ht->ht_array;
4131 else
4132 ++hi;
4133 while (HASHITEM_EMPTY(hi))
4134 ++hi;
4135 return cat_prefix_varname('t', hi->hi_key);
4136 }
4137#endif
4138
Bram Moolenaar33570922005-01-25 22:26:29 +00004139 /* v: variables */
4140 if (vidx < VV_LEN)
4141 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143 vim_free(varnamebuf);
4144 varnamebuf = NULL;
4145 varnamebuflen = 0;
4146 return NULL;
4147}
4148
4149#endif /* FEAT_CMDL_COMPL */
4150
4151/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004152 * Return TRUE if "pat" matches "text".
4153 * Does not use 'cpo' and always uses 'magic'.
4154 */
4155 static int
4156pattern_match(char_u *pat, char_u *text, int ic)
4157{
4158 int matches = FALSE;
4159 char_u *save_cpo;
4160 regmatch_T regmatch;
4161
4162 /* avoid 'l' flag in 'cpoptions' */
4163 save_cpo = p_cpo;
4164 p_cpo = (char_u *)"";
4165 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4166 if (regmatch.regprog != NULL)
4167 {
4168 regmatch.rm_ic = ic;
4169 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4170 vim_regfree(regmatch.regprog);
4171 }
4172 p_cpo = save_cpo;
4173 return matches;
4174}
4175
4176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 * types for expressions.
4178 */
4179typedef enum
4180{
4181 TYPE_UNKNOWN = 0
4182 , TYPE_EQUAL /* == */
4183 , TYPE_NEQUAL /* != */
4184 , TYPE_GREATER /* > */
4185 , TYPE_GEQUAL /* >= */
4186 , TYPE_SMALLER /* < */
4187 , TYPE_SEQUAL /* <= */
4188 , TYPE_MATCH /* =~ */
4189 , TYPE_NOMATCH /* !~ */
4190} exptype_T;
4191
4192/*
4193 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4196 */
4197
4198/*
4199 * Handle zero level expression.
4200 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004202 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 * Return OK or FAIL.
4204 */
4205 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004206eval0(
4207 char_u *arg,
4208 typval_T *rettv,
4209 char_u **nextcmd,
4210 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
4212 int ret;
4213 char_u *p;
4214
4215 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 if (ret == FAIL || !ends_excmd(*p))
4218 {
4219 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 /*
4222 * Report the invalid expression unless the expression evaluation has
4223 * been cancelled due to an aborting error, an interrupt, or an
4224 * exception.
4225 */
4226 if (!aborting())
4227 EMSG2(_(e_invexpr2), arg);
4228 ret = FAIL;
4229 }
4230 if (nextcmd != NULL)
4231 *nextcmd = check_nextcmd(p);
4232
4233 return ret;
4234}
4235
4236/*
4237 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004238 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 *
4240 * "arg" must point to the first non-white of the expression.
4241 * "arg" is advanced to the next non-white after the recognized expression.
4242 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004243 * Note: "rettv.v_lock" is not set.
4244 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 * Return OK or FAIL.
4246 */
4247 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004248eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
4250 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004251 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
4253 /*
4254 * Get the first variable.
4255 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 return FAIL;
4258
4259 if ((*arg)[0] == '?')
4260 {
4261 result = FALSE;
4262 if (evaluate)
4263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 int error = FALSE;
4265
4266 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (error)
4270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272
4273 /*
4274 * Get the second variable.
4275 */
4276 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 return FAIL;
4279
4280 /*
4281 * Check for the ":".
4282 */
4283 if ((*arg)[0] != ':')
4284 {
4285 EMSG(_("E109: Missing ':' after '?'"));
4286 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return FAIL;
4289 }
4290
4291 /*
4292 * Get the third variable.
4293 */
4294 *arg = skipwhite(*arg + 1);
4295 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4296 {
4297 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return FAIL;
4300 }
4301 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304
4305 return OK;
4306}
4307
4308/*
4309 * Handle first level expression:
4310 * expr2 || expr2 || expr2 logical OR
4311 *
4312 * "arg" must point to the first non-white of the expression.
4313 * "arg" is advanced to the next non-white after the recognized expression.
4314 *
4315 * Return OK or FAIL.
4316 */
4317 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004318eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319{
Bram Moolenaar33570922005-01-25 22:26:29 +00004320 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 long result;
4322 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
4325 /*
4326 * Get the first variable.
4327 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 return FAIL;
4330
4331 /*
4332 * Repeat until there is no following "||".
4333 */
4334 first = TRUE;
4335 result = FALSE;
4336 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4337 {
4338 if (evaluate && first)
4339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (error)
4344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 first = FALSE;
4346 }
4347
4348 /*
4349 * Get the second variable.
4350 */
4351 *arg = skipwhite(*arg + 2);
4352 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4353 return FAIL;
4354
4355 /*
4356 * Compute the result.
4357 */
4358 if (evaluate && !result)
4359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004362 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004363 if (error)
4364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 if (evaluate)
4367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 rettv->v_type = VAR_NUMBER;
4369 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 }
4372
4373 return OK;
4374}
4375
4376/*
4377 * Handle second level expression:
4378 * expr3 && expr3 && expr3 logical AND
4379 *
4380 * "arg" must point to the first non-white of the expression.
4381 * "arg" is advanced to the next non-white after the recognized expression.
4382 *
4383 * Return OK or FAIL.
4384 */
4385 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004386eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387{
Bram Moolenaar33570922005-01-25 22:26:29 +00004388 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 long result;
4390 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 /*
4394 * Get the first variable.
4395 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
4398
4399 /*
4400 * Repeat until there is no following "&&".
4401 */
4402 first = TRUE;
4403 result = TRUE;
4404 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4405 {
4406 if (evaluate && first)
4407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004408 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004410 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411 if (error)
4412 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 first = FALSE;
4414 }
4415
4416 /*
4417 * Get the second variable.
4418 */
4419 *arg = skipwhite(*arg + 2);
4420 if (eval4(arg, &var2, evaluate && result) == FAIL)
4421 return FAIL;
4422
4423 /*
4424 * Compute the result.
4425 */
4426 if (evaluate && result)
4427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004428 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004431 if (error)
4432 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 if (evaluate)
4435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 rettv->v_type = VAR_NUMBER;
4437 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439 }
4440
4441 return OK;
4442}
4443
4444/*
4445 * Handle third level expression:
4446 * var1 == var2
4447 * var1 =~ var2
4448 * var1 != var2
4449 * var1 !~ var2
4450 * var1 > var2
4451 * var1 >= var2
4452 * var1 < var2
4453 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004454 * var1 is var2
4455 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 *
4457 * "arg" must point to the first non-white of the expression.
4458 * "arg" is advanced to the next non-white after the recognized expression.
4459 *
4460 * Return OK or FAIL.
4461 */
4462 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004463eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
Bram Moolenaar33570922005-01-25 22:26:29 +00004465 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 char_u *p;
4467 int i;
4468 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004469 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 int len = 2;
4471 long n1, n2;
4472 char_u *s1, *s2;
4473 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
4476 /*
4477 * Get the first variable.
4478 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004479 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 return FAIL;
4481
4482 p = *arg;
4483 switch (p[0])
4484 {
4485 case '=': if (p[1] == '=')
4486 type = TYPE_EQUAL;
4487 else if (p[1] == '~')
4488 type = TYPE_MATCH;
4489 break;
4490 case '!': if (p[1] == '=')
4491 type = TYPE_NEQUAL;
4492 else if (p[1] == '~')
4493 type = TYPE_NOMATCH;
4494 break;
4495 case '>': if (p[1] != '=')
4496 {
4497 type = TYPE_GREATER;
4498 len = 1;
4499 }
4500 else
4501 type = TYPE_GEQUAL;
4502 break;
4503 case '<': if (p[1] != '=')
4504 {
4505 type = TYPE_SMALLER;
4506 len = 1;
4507 }
4508 else
4509 type = TYPE_SEQUAL;
4510 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004511 case 'i': if (p[1] == 's')
4512 {
4513 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4514 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004515 i = p[len];
4516 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004517 {
4518 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4519 type_is = TRUE;
4520 }
4521 }
4522 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524
4525 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004526 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 */
4528 if (type != TYPE_UNKNOWN)
4529 {
4530 /* extra question mark appended: ignore case */
4531 if (p[len] == '?')
4532 {
4533 ic = TRUE;
4534 ++len;
4535 }
4536 /* extra '#' appended: match case */
4537 else if (p[len] == '#')
4538 {
4539 ic = FALSE;
4540 ++len;
4541 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004542 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 else
4544 ic = p_ic;
4545
4546 /*
4547 * Get the second variable.
4548 */
4549 *arg = skipwhite(p + len);
4550 if (eval5(arg, &var2, evaluate) == FAIL)
4551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004552 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 return FAIL;
4554 }
4555
4556 if (evaluate)
4557 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004558 if (type_is && rettv->v_type != var2.v_type)
4559 {
4560 /* For "is" a different type always means FALSE, for "notis"
4561 * it means TRUE. */
4562 n1 = (type == TYPE_NEQUAL);
4563 }
4564 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4565 {
4566 if (type_is)
4567 {
4568 n1 = (rettv->v_type == var2.v_type
4569 && rettv->vval.v_list == var2.vval.v_list);
4570 if (type == TYPE_NEQUAL)
4571 n1 = !n1;
4572 }
4573 else if (rettv->v_type != var2.v_type
4574 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4575 {
4576 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004577 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004578 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004579 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004580 clear_tv(rettv);
4581 clear_tv(&var2);
4582 return FAIL;
4583 }
4584 else
4585 {
4586 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004587 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4588 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004589 if (type == TYPE_NEQUAL)
4590 n1 = !n1;
4591 }
4592 }
4593
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004594 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4595 {
4596 if (type_is)
4597 {
4598 n1 = (rettv->v_type == var2.v_type
4599 && rettv->vval.v_dict == var2.vval.v_dict);
4600 if (type == TYPE_NEQUAL)
4601 n1 = !n1;
4602 }
4603 else if (rettv->v_type != var2.v_type
4604 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4605 {
4606 if (rettv->v_type != var2.v_type)
4607 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4608 else
4609 EMSG(_("E736: Invalid operation for Dictionary"));
4610 clear_tv(rettv);
4611 clear_tv(&var2);
4612 return FAIL;
4613 }
4614 else
4615 {
4616 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004617 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4618 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004619 if (type == TYPE_NEQUAL)
4620 n1 = !n1;
4621 }
4622 }
4623
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004624 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4625 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004626 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004627 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004628 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004629 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004630 clear_tv(rettv);
4631 clear_tv(&var2);
4632 return FAIL;
4633 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004634 if ((rettv->v_type == VAR_PARTIAL
4635 && rettv->vval.v_partial == NULL)
4636 || (var2.v_type == VAR_PARTIAL
4637 && var2.vval.v_partial == NULL))
4638 /* when a partial is NULL assume not equal */
4639 n1 = FALSE;
4640 else if (type_is)
4641 {
4642 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4643 /* strings are considered the same if their value is
4644 * the same */
4645 n1 = tv_equal(rettv, &var2, ic, FALSE);
4646 else if (rettv->v_type == VAR_PARTIAL
4647 && var2.v_type == VAR_PARTIAL)
4648 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4649 else
4650 n1 = FALSE;
4651 }
4652 else
4653 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004654 if (type == TYPE_NEQUAL)
4655 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004656 }
4657
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004658#ifdef FEAT_FLOAT
4659 /*
4660 * If one of the two variables is a float, compare as a float.
4661 * When using "=~" or "!~", always compare as string.
4662 */
4663 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4664 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4665 {
4666 float_T f1, f2;
4667
4668 if (rettv->v_type == VAR_FLOAT)
4669 f1 = rettv->vval.v_float;
4670 else
4671 f1 = get_tv_number(rettv);
4672 if (var2.v_type == VAR_FLOAT)
4673 f2 = var2.vval.v_float;
4674 else
4675 f2 = get_tv_number(&var2);
4676 n1 = FALSE;
4677 switch (type)
4678 {
4679 case TYPE_EQUAL: n1 = (f1 == f2); break;
4680 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4681 case TYPE_GREATER: n1 = (f1 > f2); break;
4682 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4683 case TYPE_SMALLER: n1 = (f1 < f2); break;
4684 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4685 case TYPE_UNKNOWN:
4686 case TYPE_MATCH:
4687 case TYPE_NOMATCH: break; /* avoid gcc warning */
4688 }
4689 }
4690#endif
4691
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 /*
4693 * If one of the two variables is a number, compare as a number.
4694 * When using "=~" or "!~", always compare as string.
4695 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004696 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004699 n1 = get_tv_number(rettv);
4700 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 switch (type)
4702 {
4703 case TYPE_EQUAL: n1 = (n1 == n2); break;
4704 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4705 case TYPE_GREATER: n1 = (n1 > n2); break;
4706 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4707 case TYPE_SMALLER: n1 = (n1 < n2); break;
4708 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4709 case TYPE_UNKNOWN:
4710 case TYPE_MATCH:
4711 case TYPE_NOMATCH: break; /* avoid gcc warning */
4712 }
4713 }
4714 else
4715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004716 s1 = get_tv_string_buf(rettv, buf1);
4717 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4719 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4720 else
4721 i = 0;
4722 n1 = FALSE;
4723 switch (type)
4724 {
4725 case TYPE_EQUAL: n1 = (i == 0); break;
4726 case TYPE_NEQUAL: n1 = (i != 0); break;
4727 case TYPE_GREATER: n1 = (i > 0); break;
4728 case TYPE_GEQUAL: n1 = (i >= 0); break;
4729 case TYPE_SMALLER: n1 = (i < 0); break;
4730 case TYPE_SEQUAL: n1 = (i <= 0); break;
4731
4732 case TYPE_MATCH:
4733 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004734 n1 = pattern_match(s2, s1, ic);
4735 if (type == TYPE_NOMATCH)
4736 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 break;
4738
4739 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4740 }
4741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004742 clear_tv(rettv);
4743 clear_tv(&var2);
4744 rettv->v_type = VAR_NUMBER;
4745 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
4747 }
4748
4749 return OK;
4750}
4751
4752/*
4753 * Handle fourth level expression:
4754 * + number addition
4755 * - number subtraction
4756 * . string concatenation
4757 *
4758 * "arg" must point to the first non-white of the expression.
4759 * "arg" is advanced to the next non-white after the recognized expression.
4760 *
4761 * Return OK or FAIL.
4762 */
4763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004764eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
Bram Moolenaar33570922005-01-25 22:26:29 +00004766 typval_T var2;
4767 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 int op;
4769 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770#ifdef FEAT_FLOAT
4771 float_T f1 = 0, f2 = 0;
4772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 char_u *s1, *s2;
4774 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4775 char_u *p;
4776
4777 /*
4778 * Get the first variable.
4779 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004780 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 return FAIL;
4782
4783 /*
4784 * Repeat computing, until no '+', '-' or '.' is following.
4785 */
4786 for (;;)
4787 {
4788 op = **arg;
4789 if (op != '+' && op != '-' && op != '.')
4790 break;
4791
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 if ((op != '+' || rettv->v_type != VAR_LIST)
4793#ifdef FEAT_FLOAT
4794 && (op == '.' || rettv->v_type != VAR_FLOAT)
4795#endif
4796 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 {
4798 /* For "list + ...", an illegal use of the first operand as
4799 * a number cannot be determined before evaluating the 2nd
4800 * operand: if this is also a list, all is ok.
4801 * For "something . ...", "something - ..." or "non-list + ...",
4802 * we know that the first operand needs to be a string or number
4803 * without evaluating the 2nd operand. So check before to avoid
4804 * side effects after an error. */
4805 if (evaluate && get_tv_string_chk(rettv) == NULL)
4806 {
4807 clear_tv(rettv);
4808 return FAIL;
4809 }
4810 }
4811
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 /*
4813 * Get the second variable.
4814 */
4815 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004816 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004818 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 return FAIL;
4820 }
4821
4822 if (evaluate)
4823 {
4824 /*
4825 * Compute the result.
4826 */
4827 if (op == '.')
4828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4830 s2 = get_tv_string_buf_chk(&var2, buf2);
4831 if (s2 == NULL) /* type error ? */
4832 {
4833 clear_tv(rettv);
4834 clear_tv(&var2);
4835 return FAIL;
4836 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004837 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 clear_tv(rettv);
4839 rettv->v_type = VAR_STRING;
4840 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004842 else if (op == '+' && rettv->v_type == VAR_LIST
4843 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004844 {
4845 /* concatenate Lists */
4846 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4847 &var3) == FAIL)
4848 {
4849 clear_tv(rettv);
4850 clear_tv(&var2);
4851 return FAIL;
4852 }
4853 clear_tv(rettv);
4854 *rettv = var3;
4855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 else
4857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004858 int error = FALSE;
4859
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860#ifdef FEAT_FLOAT
4861 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004862 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004863 f1 = rettv->vval.v_float;
4864 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004865 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866 else
4867#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004868 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004869 n1 = get_tv_number_chk(rettv, &error);
4870 if (error)
4871 {
4872 /* This can only happen for "list + non-list". For
4873 * "non-list + ..." or "something - ...", we returned
4874 * before evaluating the 2nd operand. */
4875 clear_tv(rettv);
4876 return FAIL;
4877 }
4878#ifdef FEAT_FLOAT
4879 if (var2.v_type == VAR_FLOAT)
4880 f1 = n1;
4881#endif
4882 }
4883#ifdef FEAT_FLOAT
4884 if (var2.v_type == VAR_FLOAT)
4885 {
4886 f2 = var2.vval.v_float;
4887 n2 = 0;
4888 }
4889 else
4890#endif
4891 {
4892 n2 = get_tv_number_chk(&var2, &error);
4893 if (error)
4894 {
4895 clear_tv(rettv);
4896 clear_tv(&var2);
4897 return FAIL;
4898 }
4899#ifdef FEAT_FLOAT
4900 if (rettv->v_type == VAR_FLOAT)
4901 f2 = n2;
4902#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905
4906#ifdef FEAT_FLOAT
4907 /* If there is a float on either side the result is a float. */
4908 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4909 {
4910 if (op == '+')
4911 f1 = f1 + f2;
4912 else
4913 f1 = f1 - f2;
4914 rettv->v_type = VAR_FLOAT;
4915 rettv->vval.v_float = f1;
4916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918#endif
4919 {
4920 if (op == '+')
4921 n1 = n1 + n2;
4922 else
4923 n1 = n1 - n2;
4924 rettv->v_type = VAR_NUMBER;
4925 rettv->vval.v_number = n1;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004928 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930 }
4931 return OK;
4932}
4933
4934/*
4935 * Handle fifth level expression:
4936 * * number multiplication
4937 * / number division
4938 * % number modulo
4939 *
4940 * "arg" must point to the first non-white of the expression.
4941 * "arg" is advanced to the next non-white after the recognized expression.
4942 *
4943 * Return OK or FAIL.
4944 */
4945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004946eval6(
4947 char_u **arg,
4948 typval_T *rettv,
4949 int evaluate,
4950 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
Bram Moolenaar33570922005-01-25 22:26:29 +00004952 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 int op;
4954 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 int use_float = FALSE;
4957 float_T f1 = 0, f2;
4958#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004959 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960
4961 /*
4962 * Get the first variable.
4963 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004964 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 return FAIL;
4966
4967 /*
4968 * Repeat computing, until no '*', '/' or '%' is following.
4969 */
4970 for (;;)
4971 {
4972 op = **arg;
4973 if (op != '*' && op != '/' && op != '%')
4974 break;
4975
4976 if (evaluate)
4977 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978#ifdef FEAT_FLOAT
4979 if (rettv->v_type == VAR_FLOAT)
4980 {
4981 f1 = rettv->vval.v_float;
4982 use_float = TRUE;
4983 n1 = 0;
4984 }
4985 else
4986#endif
4987 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004988 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004989 if (error)
4990 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
4993 n1 = 0;
4994
4995 /*
4996 * Get the second variable.
4997 */
4998 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004999 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 return FAIL;
5001
5002 if (evaluate)
5003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004#ifdef FEAT_FLOAT
5005 if (var2.v_type == VAR_FLOAT)
5006 {
5007 if (!use_float)
5008 {
5009 f1 = n1;
5010 use_float = TRUE;
5011 }
5012 f2 = var2.vval.v_float;
5013 n2 = 0;
5014 }
5015 else
5016#endif
5017 {
5018 n2 = get_tv_number_chk(&var2, &error);
5019 clear_tv(&var2);
5020 if (error)
5021 return FAIL;
5022#ifdef FEAT_FLOAT
5023 if (use_float)
5024 f2 = n2;
5025#endif
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
5028 /*
5029 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005032#ifdef FEAT_FLOAT
5033 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 if (op == '*')
5036 f1 = f1 * f2;
5037 else if (op == '/')
5038 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005039# ifdef VMS
5040 /* VMS crashes on divide by zero, work around it */
5041 if (f2 == 0.0)
5042 {
5043 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005044 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005045 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005046 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005047 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005048 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005049 }
5050 else
5051 f1 = f1 / f2;
5052# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005053 /* We rely on the floating point library to handle divide
5054 * by zero to result in "inf" and not a crash. */
5055 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005056# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005059 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005060 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061 return FAIL;
5062 }
5063 rettv->v_type = VAR_FLOAT;
5064 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005069 if (op == '*')
5070 n1 = n1 * n2;
5071 else if (op == '/')
5072 {
5073 if (n2 == 0) /* give an error message? */
5074 {
5075 if (n1 == 0)
5076 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5077 else if (n1 < 0)
5078 n1 = -0x7fffffffL;
5079 else
5080 n1 = 0x7fffffffL;
5081 }
5082 else
5083 n1 = n1 / n2;
5084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005086 {
5087 if (n2 == 0) /* give an error message? */
5088 n1 = 0;
5089 else
5090 n1 = n1 % n2;
5091 }
5092 rettv->v_type = VAR_NUMBER;
5093 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 }
5097
5098 return OK;
5099}
5100
5101/*
5102 * Handle sixth level expression:
5103 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005104 * "string" string constant
5105 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 * &option-name option value
5107 * @r register contents
5108 * identifier variable value
5109 * function() function call
5110 * $VAR environment variable
5111 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005112 * [expr, expr] List
5113 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 *
5115 * Also handle:
5116 * ! in front logical NOT
5117 * - in front unary minus
5118 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 * trailing [] subscript in String or List
5120 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 *
5122 * "arg" must point to the first non-white of the expression.
5123 * "arg" is advanced to the next non-white after the recognized expression.
5124 *
5125 * Return OK or FAIL.
5126 */
5127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005128eval7(
5129 char_u **arg,
5130 typval_T *rettv,
5131 int evaluate,
5132 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 long n;
5135 int len;
5136 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 char_u *start_leader, *end_leader;
5138 int ret = OK;
5139 char_u *alias;
5140
5141 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005143 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
5147 /*
5148 * Skip '!' and '-' characters. They are handled later.
5149 */
5150 start_leader = *arg;
5151 while (**arg == '!' || **arg == '-' || **arg == '+')
5152 *arg = skipwhite(*arg + 1);
5153 end_leader = *arg;
5154
5155 switch (**arg)
5156 {
5157 /*
5158 * Number constant.
5159 */
5160 case '0':
5161 case '1':
5162 case '2':
5163 case '3':
5164 case '4':
5165 case '5':
5166 case '6':
5167 case '7':
5168 case '8':
5169 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005170 {
5171#ifdef FEAT_FLOAT
5172 char_u *p = skipdigits(*arg + 1);
5173 int get_float = FALSE;
5174
5175 /* We accept a float when the format matches
5176 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005177 * strict to avoid backwards compatibility problems.
5178 * Don't look for a float after the "." operator, so that
5179 * ":let vers = 1.2.3" doesn't fail. */
5180 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005182 get_float = TRUE;
5183 p = skipdigits(p + 2);
5184 if (*p == 'e' || *p == 'E')
5185 {
5186 ++p;
5187 if (*p == '-' || *p == '+')
5188 ++p;
5189 if (!vim_isdigit(*p))
5190 get_float = FALSE;
5191 else
5192 p = skipdigits(p + 1);
5193 }
5194 if (ASCII_ISALPHA(*p) || *p == '.')
5195 get_float = FALSE;
5196 }
5197 if (get_float)
5198 {
5199 float_T f;
5200
5201 *arg += string2float(*arg, &f);
5202 if (evaluate)
5203 {
5204 rettv->v_type = VAR_FLOAT;
5205 rettv->vval.v_float = f;
5206 }
5207 }
5208 else
5209#endif
5210 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005211 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005212 *arg += len;
5213 if (evaluate)
5214 {
5215 rettv->v_type = VAR_NUMBER;
5216 rettv->vval.v_number = n;
5217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
5222 /*
5223 * String constant: "string".
5224 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005225 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 break;
5227
5228 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005231 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005232 break;
5233
5234 /*
5235 * List: [expr, expr]
5236 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005237 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 break;
5239
5240 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 * Dictionary: {key: val, key: val}
5242 */
5243 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5244 break;
5245
5246 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005247 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005249 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 break;
5251
5252 /*
5253 * Environment variable: $VAR.
5254 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005255 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 break;
5257
5258 /*
5259 * Register contents: @r.
5260 */
5261 case '@': ++*arg;
5262 if (evaluate)
5263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005264 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005265 rettv->vval.v_string = get_reg_contents(**arg,
5266 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
5268 if (**arg != NUL)
5269 ++*arg;
5270 break;
5271
5272 /*
5273 * nested expression: (expression).
5274 */
5275 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005276 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 if (**arg == ')')
5278 ++*arg;
5279 else if (ret == OK)
5280 {
5281 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005282 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 ret = FAIL;
5284 }
5285 break;
5286
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 break;
5289 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290
5291 if (ret == NOTDONE)
5292 {
5293 /*
5294 * Must be a variable or function name.
5295 * Can also be a curly-braces kind of name: {expr}.
5296 */
5297 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 if (alias != NULL)
5300 s = alias;
5301
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005302 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 ret = FAIL;
5304 else
5305 {
5306 if (**arg == '(') /* recursive! */
5307 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005308 partial_T *partial;
5309
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 /* If "s" is the name of a variable of type VAR_FUNC
5311 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005312 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313
5314 /* Invoke the function. */
5315 ret = get_func_tv(s, len, rettv, arg,
5316 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005317 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005318
5319 /* If evaluate is FALSE rettv->v_type was not set in
5320 * get_func_tv, but it's needed in handle_subscript() to parse
5321 * what follows. So set it here. */
5322 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5323 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005324 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005325 rettv->v_type = VAR_FUNC;
5326 }
5327
Bram Moolenaar8c711452005-01-14 21:53:12 +00005328 /* Stop the expression evaluation when immediately
5329 * aborting on error, or when an interrupt occurred or
5330 * an exception was thrown but not caught. */
5331 if (aborting())
5332 {
5333 if (ret == OK)
5334 clear_tv(rettv);
5335 ret = FAIL;
5336 }
5337 }
5338 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005339 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005340 else
5341 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005343 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005344 }
5345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 *arg = skipwhite(*arg);
5347
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005348 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5349 * expr(expr). */
5350 if (ret == OK)
5351 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352
5353 /*
5354 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5355 */
5356 if (ret == OK && evaluate && end_leader > start_leader)
5357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005358 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005359 int val = 0;
5360#ifdef FEAT_FLOAT
5361 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005362
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005363 if (rettv->v_type == VAR_FLOAT)
5364 f = rettv->vval.v_float;
5365 else
5366#endif
5367 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005368 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005370 clear_tv(rettv);
5371 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005373 else
5374 {
5375 while (end_leader > start_leader)
5376 {
5377 --end_leader;
5378 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005379 {
5380#ifdef FEAT_FLOAT
5381 if (rettv->v_type == VAR_FLOAT)
5382 f = !f;
5383 else
5384#endif
5385 val = !val;
5386 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005387 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005388 {
5389#ifdef FEAT_FLOAT
5390 if (rettv->v_type == VAR_FLOAT)
5391 f = -f;
5392 else
5393#endif
5394 val = -val;
5395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005396 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005397#ifdef FEAT_FLOAT
5398 if (rettv->v_type == VAR_FLOAT)
5399 {
5400 clear_tv(rettv);
5401 rettv->vval.v_float = f;
5402 }
5403 else
5404#endif
5405 {
5406 clear_tv(rettv);
5407 rettv->v_type = VAR_NUMBER;
5408 rettv->vval.v_number = val;
5409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 }
5412
5413 return ret;
5414}
5415
5416/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005417 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5418 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5420 */
5421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005422eval_index(
5423 char_u **arg,
5424 typval_T *rettv,
5425 int evaluate,
5426 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427{
5428 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005429 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 long len = -1;
5432 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435
Bram Moolenaara03f2332016-02-06 18:09:59 +01005436 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005438 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005439 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005440 if (verbose)
5441 EMSG(_("E695: Cannot index a Funcref"));
5442 return FAIL;
5443 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005444#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005445 if (verbose)
5446 EMSG(_(e_float_as_string));
5447 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005448#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005449 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005450 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005451 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005452 if (verbose)
5453 EMSG(_("E909: Cannot index a special variable"));
5454 return FAIL;
5455 case VAR_UNKNOWN:
5456 if (evaluate)
5457 return FAIL;
5458 /* FALLTHROUGH */
5459
5460 case VAR_STRING:
5461 case VAR_NUMBER:
5462 case VAR_LIST:
5463 case VAR_DICT:
5464 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005465 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005467 init_tv(&var1);
5468 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005471 /*
5472 * dict.name
5473 */
5474 key = *arg + 1;
5475 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5476 ;
5477 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005479 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 }
5481 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005483 /*
5484 * something[idx]
5485 *
5486 * Get the (first) variable from inside the [].
5487 */
5488 *arg = skipwhite(*arg + 1);
5489 if (**arg == ':')
5490 empty1 = TRUE;
5491 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5492 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005493 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5494 {
5495 /* not a number or string */
5496 clear_tv(&var1);
5497 return FAIL;
5498 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005499
5500 /*
5501 * Get the second variable from inside the [:].
5502 */
5503 if (**arg == ':')
5504 {
5505 range = TRUE;
5506 *arg = skipwhite(*arg + 1);
5507 if (**arg == ']')
5508 empty2 = TRUE;
5509 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005511 if (!empty1)
5512 clear_tv(&var1);
5513 return FAIL;
5514 }
5515 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5516 {
5517 /* not a number or string */
5518 if (!empty1)
5519 clear_tv(&var1);
5520 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005521 return FAIL;
5522 }
5523 }
5524
5525 /* Check for the ']'. */
5526 if (**arg != ']')
5527 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005528 if (verbose)
5529 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005530 clear_tv(&var1);
5531 if (range)
5532 clear_tv(&var2);
5533 return FAIL;
5534 }
5535 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 }
5537
5538 if (evaluate)
5539 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 n1 = 0;
5541 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543 n1 = get_tv_number(&var1);
5544 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 }
5546 if (range)
5547 {
5548 if (empty2)
5549 n2 = -1;
5550 else
5551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 n2 = get_tv_number(&var2);
5553 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 }
5555 }
5556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005558 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005559 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005560 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005561 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005562 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005563 case VAR_SPECIAL:
5564 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005565 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005566 break; /* not evaluating, skipping over subscript */
5567
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 case VAR_NUMBER:
5569 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 len = (long)STRLEN(s);
5572 if (range)
5573 {
5574 /* The resulting variable is a substring. If the indexes
5575 * are out of range the result is empty. */
5576 if (n1 < 0)
5577 {
5578 n1 = len + n1;
5579 if (n1 < 0)
5580 n1 = 0;
5581 }
5582 if (n2 < 0)
5583 n2 = len + n2;
5584 else if (n2 >= len)
5585 n2 = len;
5586 if (n1 >= len || n2 < 0 || n1 > n2)
5587 s = NULL;
5588 else
5589 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5590 }
5591 else
5592 {
5593 /* The resulting variable is a string of a single
5594 * character. If the index is too big or negative the
5595 * result is empty. */
5596 if (n1 >= len || n1 < 0)
5597 s = NULL;
5598 else
5599 s = vim_strnsave(s + n1, 1);
5600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005601 clear_tv(rettv);
5602 rettv->v_type = VAR_STRING;
5603 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 break;
5605
5606 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005608 if (n1 < 0)
5609 n1 = len + n1;
5610 if (!empty1 && (n1 < 0 || n1 >= len))
5611 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005612 /* For a range we allow invalid values and return an empty
5613 * list. A list index out of range is an error. */
5614 if (!range)
5615 {
5616 if (verbose)
5617 EMSGN(_(e_listidx), n1);
5618 return FAIL;
5619 }
5620 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621 }
5622 if (range)
5623 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 list_T *l;
5625 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626
5627 if (n2 < 0)
5628 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005629 else if (n2 >= len)
5630 n2 = len - 1;
5631 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005632 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 l = list_alloc();
5634 if (l == NULL)
5635 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005636 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005637 n1 <= n2; ++n1)
5638 {
5639 if (list_append_tv(l, &item->li_tv) == FAIL)
5640 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005641 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005642 return FAIL;
5643 }
5644 item = item->li_next;
5645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 clear_tv(rettv);
5647 rettv->v_type = VAR_LIST;
5648 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005649 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650 }
5651 else
5652 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005653 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654 clear_tv(rettv);
5655 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 }
5657 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658
5659 case VAR_DICT:
5660 if (range)
5661 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005662 if (verbose)
5663 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 if (len == -1)
5665 clear_tv(&var1);
5666 return FAIL;
5667 }
5668 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005669 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670
5671 if (len == -1)
5672 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005673 key = get_tv_string_chk(&var1);
5674 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 clear_tv(&var1);
5677 return FAIL;
5678 }
5679 }
5680
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005681 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005683 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005684 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 if (len == -1)
5686 clear_tv(&var1);
5687 if (item == NULL)
5688 return FAIL;
5689
5690 copy_tv(&item->di_tv, &var1);
5691 clear_tv(rettv);
5692 *rettv = var1;
5693 }
5694 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695 }
5696 }
5697
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 return OK;
5699}
5700
5701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 * Get an option value.
5703 * "arg" points to the '&' or '+' before the option name.
5704 * "arg" is advanced to character after the option name.
5705 * Return OK or FAIL.
5706 */
5707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005708get_option_tv(
5709 char_u **arg,
5710 typval_T *rettv, /* when NULL, only check if option exists */
5711 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 char_u *option_end;
5714 long numval;
5715 char_u *stringval;
5716 int opt_type;
5717 int c;
5718 int working = (**arg == '+'); /* has("+option") */
5719 int ret = OK;
5720 int opt_flags;
5721
5722 /*
5723 * Isolate the option name and find its value.
5724 */
5725 option_end = find_option_end(arg, &opt_flags);
5726 if (option_end == NULL)
5727 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005728 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 EMSG2(_("E112: Option name missing: %s"), *arg);
5730 return FAIL;
5731 }
5732
5733 if (!evaluate)
5734 {
5735 *arg = option_end;
5736 return OK;
5737 }
5738
5739 c = *option_end;
5740 *option_end = NUL;
5741 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005742 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
5744 if (opt_type == -3) /* invalid name */
5745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005746 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 EMSG2(_("E113: Unknown option: %s"), *arg);
5748 ret = FAIL;
5749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005750 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 if (opt_type == -2) /* hidden string option */
5753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005754 rettv->v_type = VAR_STRING;
5755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757 else if (opt_type == -1) /* hidden number option */
5758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005759 rettv->v_type = VAR_NUMBER;
5760 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762 else if (opt_type == 1) /* number option */
5763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005764 rettv->v_type = VAR_NUMBER;
5765 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767 else /* string option */
5768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005769 rettv->v_type = VAR_STRING;
5770 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 }
5772 }
5773 else if (working && (opt_type == -2 || opt_type == -1))
5774 ret = FAIL;
5775
5776 *option_end = c; /* put back for error messages */
5777 *arg = option_end;
5778
5779 return ret;
5780}
5781
5782/*
5783 * Allocate a variable for a string constant.
5784 * Return OK or FAIL.
5785 */
5786 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005787get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788{
5789 char_u *p;
5790 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 int extra = 0;
5792
5793 /*
5794 * Find the end of the string, skipping backslashed characters.
5795 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
5798 if (*p == '\\' && p[1] != NUL)
5799 {
5800 ++p;
5801 /* A "\<x>" form occupies at least 4 characters, and produces up
5802 * to 6 characters: reserve space for 2 extra */
5803 if (*p == '<')
5804 extra += 2;
5805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807
5808 if (*p != '"')
5809 {
5810 EMSG2(_("E114: Missing quote: %s"), *arg);
5811 return FAIL;
5812 }
5813
5814 /* If only parsing, set *arg and return here */
5815 if (!evaluate)
5816 {
5817 *arg = p + 1;
5818 return OK;
5819 }
5820
5821 /*
5822 * Copy the string into allocated memory, handling backslashed
5823 * characters.
5824 */
5825 name = alloc((unsigned)(p - *arg + extra));
5826 if (name == NULL)
5827 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 rettv->v_type = VAR_STRING;
5829 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {
5833 if (*p == '\\')
5834 {
5835 switch (*++p)
5836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 case 'b': *name++ = BS; ++p; break;
5838 case 'e': *name++ = ESC; ++p; break;
5839 case 'f': *name++ = FF; ++p; break;
5840 case 'n': *name++ = NL; ++p; break;
5841 case 'r': *name++ = CAR; ++p; break;
5842 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
5844 case 'X': /* hex: "\x1", "\x12" */
5845 case 'x':
5846 case 'u': /* Unicode: "\u0023" */
5847 case 'U':
5848 if (vim_isxdigit(p[1]))
5849 {
5850 int n, nr;
5851 int c = toupper(*p);
5852
5853 if (c == 'X')
5854 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005855 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005857 else
5858 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 nr = 0;
5860 while (--n >= 0 && vim_isxdigit(p[1]))
5861 {
5862 ++p;
5863 nr = (nr << 4) + hex2nr(*p);
5864 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866#ifdef FEAT_MBYTE
5867 /* For "\u" store the number according to
5868 * 'encoding'. */
5869 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 else
5872#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 break;
5876
5877 /* octal: "\1", "\12", "\123" */
5878 case '0':
5879 case '1':
5880 case '2':
5881 case '3':
5882 case '4':
5883 case '5':
5884 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 case '7': *name = *p++ - '0';
5886 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 *name = (*name << 3) + *p++ - '0';
5889 if (*p >= '0' && *p <= '7')
5890 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 break;
5894
5895 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 if (extra != 0)
5898 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 break;
5901 }
5902 /* FALLTHROUGH */
5903
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 break;
5906 }
5907 }
5908 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005909 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 *arg = p + 1;
5914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 return OK;
5916}
5917
5918/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 * Return OK or FAIL.
5921 */
5922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005923get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 int reduce = 0;
5928
5929 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005930 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931 */
5932 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005937 break;
5938 ++reduce;
5939 ++p;
5940 }
5941 }
5942
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 return FAIL;
5947 }
5948
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 if (!evaluate)
5951 {
5952 *arg = p + 1;
5953 return OK;
5954 }
5955
5956 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005957 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005958 */
5959 str = alloc((unsigned)((p - *arg) - reduce));
5960 if (str == NULL)
5961 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005962 rettv->v_type = VAR_STRING;
5963 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005964
Bram Moolenaar8c711452005-01-14 21:53:12 +00005965 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005967 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005969 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005970 break;
5971 ++p;
5972 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005973 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005974 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005975 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005976 *arg = p + 1;
5977
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005978 return OK;
5979}
5980
Bram Moolenaarddecc252016-04-06 22:59:37 +02005981 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005982partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005983{
5984 int i;
5985
5986 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005987 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005988 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005989 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005990 func_unref(pt->pt_name);
5991 vim_free(pt->pt_name);
5992 vim_free(pt);
5993}
5994
5995/*
5996 * Unreference a closure: decrement the reference count and free it when it
5997 * becomes zero.
5998 */
5999 void
6000partial_unref(partial_T *pt)
6001{
6002 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006003 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006004}
6005
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006006/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 * Allocate a variable for a List and fill it from "*arg".
6008 * Return OK or FAIL.
6009 */
6010 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006011get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012{
Bram Moolenaar33570922005-01-25 22:26:29 +00006013 list_T *l = NULL;
6014 typval_T tv;
6015 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016
6017 if (evaluate)
6018 {
6019 l = list_alloc();
6020 if (l == NULL)
6021 return FAIL;
6022 }
6023
6024 *arg = skipwhite(*arg + 1);
6025 while (**arg != ']' && **arg != NUL)
6026 {
6027 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6028 goto failret;
6029 if (evaluate)
6030 {
6031 item = listitem_alloc();
6032 if (item != NULL)
6033 {
6034 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006035 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 list_append(l, item);
6037 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006038 else
6039 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040 }
6041
6042 if (**arg == ']')
6043 break;
6044 if (**arg != ',')
6045 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006046 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 goto failret;
6048 }
6049 *arg = skipwhite(*arg + 1);
6050 }
6051
6052 if (**arg != ']')
6053 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006054 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055failret:
6056 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006057 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 return FAIL;
6059 }
6060
6061 *arg = skipwhite(*arg + 1);
6062 if (evaluate)
6063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006064 rettv->v_type = VAR_LIST;
6065 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 ++l->lv_refcount;
6067 }
6068
6069 return OK;
6070}
6071
6072/*
6073 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006074 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006076 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006077list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006079 list_T *l;
6080
6081 l = (list_T *)alloc_clear(sizeof(list_T));
6082 if (l != NULL)
6083 {
6084 /* Prepend the list to the list of lists for garbage collection. */
6085 if (first_list != NULL)
6086 first_list->lv_used_prev = l;
6087 l->lv_used_prev = NULL;
6088 l->lv_used_next = first_list;
6089 first_list = l;
6090 }
6091 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092}
6093
6094/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006095 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006096 * Returns OK or FAIL.
6097 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006098 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006100{
6101 list_T *l = list_alloc();
6102
6103 if (l == NULL)
6104 return FAIL;
6105
6106 rettv->vval.v_list = l;
6107 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006108 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006109 ++l->lv_refcount;
6110 return OK;
6111}
6112
6113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114 * Unreference a list: decrement the reference count and free it when it
6115 * becomes zero.
6116 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006119{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006120 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006121 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122}
6123
6124/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006125 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006126 * Ignores the reference count.
6127 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006128 static void
6129list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130{
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006132
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006133 for (item = l->lv_first; item != NULL; item = l->lv_first)
6134 {
6135 /* Remove the item before deleting it. */
6136 l->lv_first = item->li_next;
6137 clear_tv(&item->li_tv);
6138 vim_free(item);
6139 }
6140}
6141
6142 static void
6143list_free_list(list_T *l)
6144{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006145 /* Remove the list from the list of lists for garbage collection. */
6146 if (l->lv_used_prev == NULL)
6147 first_list = l->lv_used_next;
6148 else
6149 l->lv_used_prev->lv_used_next = l->lv_used_next;
6150 if (l->lv_used_next != NULL)
6151 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6152
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 vim_free(l);
6154}
6155
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006156 void
6157list_free(list_T *l)
6158{
6159 if (!in_free_unref_items)
6160 {
6161 list_free_contents(l);
6162 list_free_list(l);
6163 }
6164}
6165
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006166/*
6167 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006168 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006170 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006171listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172{
Bram Moolenaar33570922005-01-25 22:26:29 +00006173 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174}
6175
6176/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006177 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006179 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006180listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006182 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 vim_free(item);
6184}
6185
6186/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006187 * Remove a list item from a List and free it. Also clears the value.
6188 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006190listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006191{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006192 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006193 listitem_free(item);
6194}
6195
6196/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 * Get the number of items in a list.
6198 */
6199 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006200list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 if (l == NULL)
6203 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006204 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006205}
6206
6207/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006208 * Return TRUE when two lists have exactly the same values.
6209 */
6210 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006211list_equal(
6212 list_T *l1,
6213 list_T *l2,
6214 int ic, /* ignore case for strings */
6215 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006216{
Bram Moolenaar33570922005-01-25 22:26:29 +00006217 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006218
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006219 if (l1 == NULL || l2 == NULL)
6220 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006221 if (l1 == l2)
6222 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006223 if (list_len(l1) != list_len(l2))
6224 return FALSE;
6225
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006226 for (item1 = l1->lv_first, item2 = l2->lv_first;
6227 item1 != NULL && item2 != NULL;
6228 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006229 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 return FALSE;
6231 return item1 == NULL && item2 == NULL;
6232}
6233
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006234/*
6235 * Return the dictitem that an entry in a hashtable points to.
6236 */
6237 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006238dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006239{
6240 return HI2DI(hi);
6241}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006242
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006243/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006244 * Return TRUE when two dictionaries have exactly the same key/values.
6245 */
6246 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006247dict_equal(
6248 dict_T *d1,
6249 dict_T *d2,
6250 int ic, /* ignore case for strings */
6251 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006252{
Bram Moolenaar33570922005-01-25 22:26:29 +00006253 hashitem_T *hi;
6254 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006255 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006256
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006257 if (d1 == NULL && d2 == NULL)
6258 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006259 if (d1 == NULL || d2 == NULL)
6260 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006261 if (d1 == d2)
6262 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006263 if (dict_len(d1) != dict_len(d2))
6264 return FALSE;
6265
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006266 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006267 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006268 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006269 if (!HASHITEM_EMPTY(hi))
6270 {
6271 item2 = dict_find(d2, hi->hi_key, -1);
6272 if (item2 == NULL)
6273 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006274 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006275 return FALSE;
6276 --todo;
6277 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006278 }
6279 return TRUE;
6280}
6281
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006282static int tv_equal_recurse_limit;
6283
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006284 static int
6285func_equal(
6286 typval_T *tv1,
6287 typval_T *tv2,
6288 int ic) /* ignore case */
6289{
6290 char_u *s1, *s2;
6291 dict_T *d1, *d2;
6292 int a1, a2;
6293 int i;
6294
6295 /* empty and NULL function name considered the same */
6296 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6297 : tv1->vval.v_partial->pt_name;
6298 if (s1 != NULL && *s1 == NUL)
6299 s1 = NULL;
6300 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6301 : tv2->vval.v_partial->pt_name;
6302 if (s2 != NULL && *s2 == NUL)
6303 s2 = NULL;
6304 if (s1 == NULL || s2 == NULL)
6305 {
6306 if (s1 != s2)
6307 return FALSE;
6308 }
6309 else if (STRCMP(s1, s2) != 0)
6310 return FALSE;
6311
6312 /* empty dict and NULL dict is different */
6313 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6314 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6315 if (d1 == NULL || d2 == NULL)
6316 {
6317 if (d1 != d2)
6318 return FALSE;
6319 }
6320 else if (!dict_equal(d1, d2, ic, TRUE))
6321 return FALSE;
6322
6323 /* empty list and no list considered the same */
6324 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6325 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6326 if (a1 != a2)
6327 return FALSE;
6328 for (i = 0; i < a1; ++i)
6329 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6330 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6331 return FALSE;
6332
6333 return TRUE;
6334}
6335
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006336/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006337 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006338 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006339 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006340 */
6341 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006342tv_equal(
6343 typval_T *tv1,
6344 typval_T *tv2,
6345 int ic, /* ignore case */
6346 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006347{
6348 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006349 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006350 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006351 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006352
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006353 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006354 * recursiveness to a limit. We guess they are equal then.
6355 * A fixed limit has the problem of still taking an awful long time.
6356 * Reduce the limit every time running into it. That should work fine for
6357 * deeply linked structures that are not recursively linked and catch
6358 * recursiveness quickly. */
6359 if (!recursive)
6360 tv_equal_recurse_limit = 1000;
6361 if (recursive_cnt >= tv_equal_recurse_limit)
6362 {
6363 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006364 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006365 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006366
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006367 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6368 if ((tv1->v_type == VAR_FUNC
6369 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6370 && (tv2->v_type == VAR_FUNC
6371 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6372 {
6373 ++recursive_cnt;
6374 r = func_equal(tv1, tv2, ic);
6375 --recursive_cnt;
6376 return r;
6377 }
6378
6379 if (tv1->v_type != tv2->v_type)
6380 return FALSE;
6381
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006382 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006383 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006384 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006385 ++recursive_cnt;
6386 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6387 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006388 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006389
6390 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006391 ++recursive_cnt;
6392 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6393 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006394 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006395
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006396 case VAR_NUMBER:
6397 return tv1->vval.v_number == tv2->vval.v_number;
6398
6399 case VAR_STRING:
6400 s1 = get_tv_string_buf(tv1, buf1);
6401 s2 = get_tv_string_buf(tv2, buf2);
6402 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006403
6404 case VAR_SPECIAL:
6405 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006406
6407 case VAR_FLOAT:
6408#ifdef FEAT_FLOAT
6409 return tv1->vval.v_float == tv2->vval.v_float;
6410#endif
6411 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006412#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006413 return tv1->vval.v_job == tv2->vval.v_job;
6414#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006415 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006416#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006417 return tv1->vval.v_channel == tv2->vval.v_channel;
6418#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006419 case VAR_FUNC:
6420 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006421 case VAR_UNKNOWN:
6422 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006424
Bram Moolenaara03f2332016-02-06 18:09:59 +01006425 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6426 * does not equal anything, not even itself. */
6427 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428}
6429
6430/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 * Locate item with index "n" in list "l" and return it.
6432 * A negative index is counted from the end; -1 is the last item.
6433 * Returns NULL when "n" is out of range.
6434 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006435 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006436list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006437{
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439 long idx;
6440
6441 if (l == NULL)
6442 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006443
6444 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006446 n = l->lv_len + n;
6447
6448 /* Check for index out of range. */
6449 if (n < 0 || n >= l->lv_len)
6450 return NULL;
6451
6452 /* When there is a cached index may start search from there. */
6453 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006455 if (n < l->lv_idx / 2)
6456 {
6457 /* closest to the start of the list */
6458 item = l->lv_first;
6459 idx = 0;
6460 }
6461 else if (n > (l->lv_idx + l->lv_len) / 2)
6462 {
6463 /* closest to the end of the list */
6464 item = l->lv_last;
6465 idx = l->lv_len - 1;
6466 }
6467 else
6468 {
6469 /* closest to the cached index */
6470 item = l->lv_idx_item;
6471 idx = l->lv_idx;
6472 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006473 }
6474 else
6475 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006476 if (n < l->lv_len / 2)
6477 {
6478 /* closest to the start of the list */
6479 item = l->lv_first;
6480 idx = 0;
6481 }
6482 else
6483 {
6484 /* closest to the end of the list */
6485 item = l->lv_last;
6486 idx = l->lv_len - 1;
6487 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006489
6490 while (n > idx)
6491 {
6492 /* search forward */
6493 item = item->li_next;
6494 ++idx;
6495 }
6496 while (n < idx)
6497 {
6498 /* search backward */
6499 item = item->li_prev;
6500 --idx;
6501 }
6502
6503 /* cache the used index */
6504 l->lv_idx = idx;
6505 l->lv_idx_item = item;
6506
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507 return item;
6508}
6509
6510/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006511 * Get list item "l[idx]" as a number.
6512 */
6513 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006514list_find_nr(
6515 list_T *l,
6516 long idx,
6517 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006518{
6519 listitem_T *li;
6520
6521 li = list_find(l, idx);
6522 if (li == NULL)
6523 {
6524 if (errorp != NULL)
6525 *errorp = TRUE;
6526 return -1L;
6527 }
6528 return get_tv_number_chk(&li->li_tv, errorp);
6529}
6530
6531/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006532 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6533 */
6534 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006535list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006536{
6537 listitem_T *li;
6538
6539 li = list_find(l, idx - 1);
6540 if (li == NULL)
6541 {
6542 EMSGN(_(e_listidx), idx);
6543 return NULL;
6544 }
6545 return get_tv_string(&li->li_tv);
6546}
6547
6548/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006549 * Locate "item" list "l" and return its index.
6550 * Returns -1 when "item" is not in the list.
6551 */
6552 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006553list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006554{
6555 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006557
6558 if (l == NULL)
6559 return -1;
6560 idx = 0;
6561 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6562 ++idx;
6563 if (li == NULL)
6564 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006565 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006566}
6567
6568/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569 * Append item "item" to the end of list "l".
6570 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006572list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573{
6574 if (l->lv_last == NULL)
6575 {
6576 /* empty list */
6577 l->lv_first = item;
6578 l->lv_last = item;
6579 item->li_prev = NULL;
6580 }
6581 else
6582 {
6583 l->lv_last->li_next = item;
6584 item->li_prev = l->lv_last;
6585 l->lv_last = item;
6586 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006587 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588 item->li_next = NULL;
6589}
6590
6591/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006595 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006596list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006598 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599
Bram Moolenaar05159a02005-02-26 23:04:13 +00006600 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006602 copy_tv(tv, &li->li_tv);
6603 list_append(l, li);
6604 return OK;
6605}
6606
6607/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006608 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006609 * Return FAIL when out of memory.
6610 */
6611 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006612list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006613{
6614 listitem_T *li = listitem_alloc();
6615
6616 if (li == NULL)
6617 return FAIL;
6618 li->li_tv.v_type = VAR_DICT;
6619 li->li_tv.v_lock = 0;
6620 li->li_tv.vval.v_dict = dict;
6621 list_append(list, li);
6622 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 return OK;
6624}
6625
6626/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006627 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006628 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006629 * Returns FAIL when out of memory.
6630 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006633{
6634 listitem_T *li = listitem_alloc();
6635
6636 if (li == NULL)
6637 return FAIL;
6638 list_append(l, li);
6639 li->li_tv.v_type = VAR_STRING;
6640 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006641 if (str == NULL)
6642 li->li_tv.vval.v_string = NULL;
6643 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006644 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006645 return FAIL;
6646 return OK;
6647}
6648
6649/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006650 * Append "n" to list "l".
6651 * Returns FAIL when out of memory.
6652 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006654list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006655{
6656 listitem_T *li;
6657
6658 li = listitem_alloc();
6659 if (li == NULL)
6660 return FAIL;
6661 li->li_tv.v_type = VAR_NUMBER;
6662 li->li_tv.v_lock = 0;
6663 li->li_tv.vval.v_number = n;
6664 list_append(l, li);
6665 return OK;
6666}
6667
6668/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006669 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006670 * If "item" is NULL append at the end.
6671 * Return FAIL when out of memory.
6672 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006675{
Bram Moolenaar33570922005-01-25 22:26:29 +00006676 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006677
6678 if (ni == NULL)
6679 return FAIL;
6680 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006681 list_insert(l, ni, item);
6682 return OK;
6683}
6684
6685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006686list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006687{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006688 if (item == NULL)
6689 /* Append new item at end of list. */
6690 list_append(l, ni);
6691 else
6692 {
6693 /* Insert new item before existing item. */
6694 ni->li_prev = item->li_prev;
6695 ni->li_next = item;
6696 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006697 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006698 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006699 ++l->lv_idx;
6700 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006701 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006702 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006703 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006704 l->lv_idx_item = NULL;
6705 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006706 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006707 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006708 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006709}
6710
6711/*
6712 * Extend "l1" with "l2".
6713 * If "bef" is NULL append at the end, otherwise insert before this item.
6714 * Returns FAIL when out of memory.
6715 */
6716 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006717list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006718{
Bram Moolenaar33570922005-01-25 22:26:29 +00006719 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006720 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006721
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006722 /* We also quit the loop when we have inserted the original item count of
6723 * the list, avoid a hang when we extend a list with itself. */
6724 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006725 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6726 return FAIL;
6727 return OK;
6728}
6729
6730/*
6731 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6732 * Return FAIL when out of memory.
6733 */
6734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006735list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006736{
Bram Moolenaar33570922005-01-25 22:26:29 +00006737 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006738
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006739 if (l1 == NULL || l2 == NULL)
6740 return FAIL;
6741
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006743 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006744 if (l == NULL)
6745 return FAIL;
6746 tv->v_type = VAR_LIST;
6747 tv->vval.v_list = l;
6748
6749 /* append all items from the second list */
6750 return list_extend(l, l2, NULL);
6751}
6752
6753/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006754 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006755 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006756 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006757 * Returns NULL when out of memory.
6758 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006759 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006760list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006761{
Bram Moolenaar33570922005-01-25 22:26:29 +00006762 list_T *copy;
6763 listitem_T *item;
6764 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006765
6766 if (orig == NULL)
6767 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006768
6769 copy = list_alloc();
6770 if (copy != NULL)
6771 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006772 if (copyID != 0)
6773 {
6774 /* Do this before adding the items, because one of the items may
6775 * refer back to this list. */
6776 orig->lv_copyID = copyID;
6777 orig->lv_copylist = copy;
6778 }
6779 for (item = orig->lv_first; item != NULL && !got_int;
6780 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781 {
6782 ni = listitem_alloc();
6783 if (ni == NULL)
6784 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006785 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006786 {
6787 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6788 {
6789 vim_free(ni);
6790 break;
6791 }
6792 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006793 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006794 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006795 list_append(copy, ni);
6796 }
6797 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006798 if (item != NULL)
6799 {
6800 list_unref(copy);
6801 copy = NULL;
6802 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006803 }
6804
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006805 return copy;
6806}
6807
6808/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006809 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006810 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006811 * This used to be called list_remove, but that conflicts with a Sun header
6812 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006813 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006814 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006815vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006816{
Bram Moolenaar33570922005-01-25 22:26:29 +00006817 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006818
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006819 /* notify watchers */
6820 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006821 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006822 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006823 list_fix_watch(l, ip);
6824 if (ip == item2)
6825 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006826 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006827
6828 if (item2->li_next == NULL)
6829 l->lv_last = item->li_prev;
6830 else
6831 item2->li_next->li_prev = item->li_prev;
6832 if (item->li_prev == NULL)
6833 l->lv_first = item2->li_next;
6834 else
6835 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006836 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006837}
6838
6839/*
6840 * Return an allocated string with the string representation of a list.
6841 * May return NULL.
6842 */
6843 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006844list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845{
6846 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006847
6848 if (tv->vval.v_list == NULL)
6849 return NULL;
6850 ga_init2(&ga, (int)sizeof(char), 80);
6851 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006852 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6853 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006854 {
6855 vim_free(ga.ga_data);
6856 return NULL;
6857 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006858 ga_append(&ga, ']');
6859 ga_append(&ga, NUL);
6860 return (char_u *)ga.ga_data;
6861}
6862
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006863typedef struct join_S {
6864 char_u *s;
6865 char_u *tofree;
6866} join_T;
6867
6868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006869list_join_inner(
6870 garray_T *gap, /* to store the result in */
6871 list_T *l,
6872 char_u *sep,
6873 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006874 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006875 int copyID,
6876 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006877{
6878 int i;
6879 join_T *p;
6880 int len;
6881 int sumlen = 0;
6882 int first = TRUE;
6883 char_u *tofree;
6884 char_u numbuf[NUMBUFLEN];
6885 listitem_T *item;
6886 char_u *s;
6887
6888 /* Stringify each item in the list. */
6889 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6890 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006891 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6892 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006893 if (s == NULL)
6894 return FAIL;
6895
6896 len = (int)STRLEN(s);
6897 sumlen += len;
6898
Bram Moolenaarcde88542015-08-11 19:14:00 +02006899 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006900 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6901 if (tofree != NULL || s != numbuf)
6902 {
6903 p->s = s;
6904 p->tofree = tofree;
6905 }
6906 else
6907 {
6908 p->s = vim_strnsave(s, len);
6909 p->tofree = p->s;
6910 }
6911
6912 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006913 if (did_echo_string_emsg) /* recursion error, bail out */
6914 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006915 }
6916
6917 /* Allocate result buffer with its total size, avoid re-allocation and
6918 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6919 if (join_gap->ga_len >= 2)
6920 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6921 if (ga_grow(gap, sumlen + 2) == FAIL)
6922 return FAIL;
6923
6924 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6925 {
6926 if (first)
6927 first = FALSE;
6928 else
6929 ga_concat(gap, sep);
6930 p = ((join_T *)join_gap->ga_data) + i;
6931
6932 if (p->s != NULL)
6933 ga_concat(gap, p->s);
6934 line_breakcheck();
6935 }
6936
6937 return OK;
6938}
6939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006940/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006941 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006942 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006943 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006944 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006946list_join(
6947 garray_T *gap,
6948 list_T *l,
6949 char_u *sep,
6950 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006951 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006952 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006953{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006954 garray_T join_ga;
6955 int retval;
6956 join_T *p;
6957 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006958
Bram Moolenaard39a7512015-04-16 22:51:22 +02006959 if (l->lv_len < 1)
6960 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006961 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006962 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6963 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006964
6965 /* Dispose each item in join_ga. */
6966 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006967 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006968 p = (join_T *)join_ga.ga_data;
6969 for (i = 0; i < join_ga.ga_len; ++i)
6970 {
6971 vim_free(p->tofree);
6972 ++p;
6973 }
6974 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006975 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006976
6977 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006978}
6979
6980/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006981 * Return the next (unique) copy ID.
6982 * Used for serializing nested structures.
6983 */
6984 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006985get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006986{
6987 current_copyID += COPYID_INC;
6988 return current_copyID;
6989}
6990
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006991/* Used by get_func_tv() */
6992static garray_T funcargs = GA_EMPTY;
6993
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006994/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 * Garbage collection for lists and dictionaries.
6996 *
6997 * We use reference counts to be able to free most items right away when they
6998 * are no longer used. But for composite items it's possible that it becomes
6999 * unused while the reference count is > 0: When there is a recursive
7000 * reference. Example:
7001 * :let l = [1, 2, 3]
7002 * :let d = {9: l}
7003 * :let l[1] = d
7004 *
7005 * Since this is quite unusual we handle this with garbage collection: every
7006 * once in a while find out which lists and dicts are not referenced from any
7007 * variable.
7008 *
7009 * Here is a good reference text about garbage collection (refers to Python
7010 * but it applies to all reference-counting mechanisms):
7011 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007012 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007013
7014/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007016 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007018 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007020garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007021{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007022 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 buf_T *buf;
7025 win_T *wp;
7026 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007027 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007028 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007029 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007030#ifdef FEAT_WINDOWS
7031 tabpage_T *tp;
7032#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007034 if (!testing)
7035 {
7036 /* Only do this once. */
7037 want_garbage_collect = FALSE;
7038 may_garbage_collect = FALSE;
7039 garbage_collect_at_exit = FALSE;
7040 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007041
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007042 /* We advance by two because we add one for items referenced through
7043 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007044 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007045
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 /*
7047 * 1. Go through all accessible variables and mark all lists and dicts
7048 * with copyID.
7049 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007050
7051 /* Don't free variables in the previous_funccal list unless they are only
7052 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007053 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007054 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7055 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7057 NULL);
7058 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7059 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007060 }
7061
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062 /* script-local variables */
7063 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007065
7066 /* buffer-local variables */
7067 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7069 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070
7071 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007072 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7074 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007075#ifdef FEAT_AUTOCMD
7076 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007077 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7078 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007079#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007081#ifdef FEAT_WINDOWS
7082 /* tabpage-local variables */
7083 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7085 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007086#endif
7087
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007089 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090
7091 /* function-local variables */
7092 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7093 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007094 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7095 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096 }
7097
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007098 /* function call arguments, if v:testing is set. */
7099 for (i = 0; i < funcargs.ga_len; ++i)
7100 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7101 copyID, NULL, NULL);
7102
Bram Moolenaard812df62008-11-09 12:46:09 +00007103 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007105
Bram Moolenaar1dced572012-04-05 16:54:08 +02007106#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007108#endif
7109
Bram Moolenaardb913952012-06-29 12:54:53 +02007110#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007112#endif
7113
7114#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007115 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007116#endif
7117
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007118#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007119 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007120 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007121#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007122#ifdef FEAT_NETBEANS_INTG
7123 abort = abort || set_ref_in_nb_channel(copyID);
7124#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007125
Bram Moolenaare3188e22016-05-31 21:13:04 +02007126#ifdef FEAT_TIMERS
7127 abort = abort || set_ref_in_timer(copyID);
7128#endif
7129
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007131 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 /*
7133 * 2. Free lists and dictionaries that are not referenced.
7134 */
7135 did_free = free_unref_items(copyID);
7136
7137 /*
7138 * 3. Check if any funccal can be freed now.
7139 */
7140 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007141 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007142 if (can_free_funccal(*pfc, copyID))
7143 {
7144 fc = *pfc;
7145 *pfc = fc->caller;
7146 free_funccal(fc, TRUE);
7147 did_free = TRUE;
7148 did_free_funccal = TRUE;
7149 }
7150 else
7151 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007152 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007153 if (did_free_funccal)
7154 /* When a funccal was freed some more items might be garbage
7155 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007156 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007157 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007158 else if (p_verbose > 0)
7159 {
7160 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7161 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007162
7163 return did_free;
7164}
7165
7166/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007167 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007168 */
7169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007170free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007171{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007172 dict_T *dd, *dd_next;
7173 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007174 int did_free = FALSE;
7175
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007176 /* Let all "free" functions know that we are here. This means no
7177 * dictionaries, lists, channels or jobs are to be freed, because we will
7178 * do that here. */
7179 in_free_unref_items = TRUE;
7180
7181 /*
7182 * PASS 1: free the contents of the items. We don't free the items
7183 * themselves yet, so that it is possible to decrement refcount counters
7184 */
7185
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007186 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007187 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007189 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007190 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007191 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007192 /* Free the Dictionary and ordinary items it contains, but don't
7193 * recurse into Lists and Dictionaries, they will be in the list
7194 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007195 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007196 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007197 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007198
7199 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007200 * Go through the list of lists and free items without the copyID.
7201 * But don't free a list that has a watcher (used in a for loop), these
7202 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007203 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007204 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7205 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7206 && ll->lv_watch == NULL)
7207 {
7208 /* Free the List and ordinary items it contains, but don't recurse
7209 * into Lists and Dictionaries, they will be in the list of dicts
7210 * or list of lists. */
7211 list_free_contents(ll);
7212 did_free = TRUE;
7213 }
7214
7215#ifdef FEAT_JOB_CHANNEL
7216 /* Go through the list of jobs and free items without the copyID. This
7217 * must happen before doing channels, because jobs refer to channels, but
7218 * the reference from the channel to the job isn't tracked. */
7219 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7220
7221 /* Go through the list of channels and free items without the copyID. */
7222 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7223#endif
7224
7225 /*
7226 * PASS 2: free the items themselves.
7227 */
7228 for (dd = first_dict; dd != NULL; dd = dd_next)
7229 {
7230 dd_next = dd->dv_used_next;
7231 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7232 dict_free_dict(dd);
7233 }
7234
7235 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007236 {
7237 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007238 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7239 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007240 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007241 /* Free the List and ordinary items it contains, but don't recurse
7242 * into Lists and Dictionaries, they will be in the list of dicts
7243 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007244 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007245 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007246 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007247
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007248#ifdef FEAT_JOB_CHANNEL
7249 /* Go through the list of jobs and free items without the copyID. This
7250 * must happen before doing channels, because jobs refer to channels, but
7251 * the reference from the channel to the job isn't tracked. */
7252 free_unused_jobs(copyID, COPYID_MASK);
7253
7254 /* Go through the list of channels and free items without the copyID. */
7255 free_unused_channels(copyID, COPYID_MASK);
7256#endif
7257
7258 in_free_unref_items = FALSE;
7259
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007260 return did_free;
7261}
7262
7263/*
7264 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007265 * "list_stack" is used to add lists to be marked. Can be NULL.
7266 *
7267 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007268 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007270set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007271{
7272 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007273 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007274 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007275 hashtab_T *cur_ht;
7276 ht_stack_T *ht_stack = NULL;
7277 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007278
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007279 cur_ht = ht;
7280 for (;;)
7281 {
7282 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007283 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007284 /* Mark each item in the hashtab. If the item contains a hashtab
7285 * it is added to ht_stack, if it contains a list it is added to
7286 * list_stack. */
7287 todo = (int)cur_ht->ht_used;
7288 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7289 if (!HASHITEM_EMPTY(hi))
7290 {
7291 --todo;
7292 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7293 &ht_stack, list_stack);
7294 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007295 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007296
7297 if (ht_stack == NULL)
7298 break;
7299
7300 /* take an item from the stack */
7301 cur_ht = ht_stack->ht;
7302 tempitem = ht_stack;
7303 ht_stack = ht_stack->prev;
7304 free(tempitem);
7305 }
7306
7307 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007308}
7309
7310/*
7311 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007312 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7313 *
7314 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007315 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007317set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007318{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007319 listitem_T *li;
7320 int abort = FALSE;
7321 list_T *cur_l;
7322 list_stack_T *list_stack = NULL;
7323 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007324
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007325 cur_l = l;
7326 for (;;)
7327 {
7328 if (!abort)
7329 /* Mark each item in the list. If the item contains a hashtab
7330 * it is added to ht_stack, if it contains a list it is added to
7331 * list_stack. */
7332 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7333 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7334 ht_stack, &list_stack);
7335 if (list_stack == NULL)
7336 break;
7337
7338 /* take an item from the stack */
7339 cur_l = list_stack->list;
7340 tempitem = list_stack;
7341 list_stack = list_stack->prev;
7342 free(tempitem);
7343 }
7344
7345 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007346}
7347
7348/*
7349 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007350 * "list_stack" is used to add lists to be marked. Can be NULL.
7351 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7352 *
7353 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007354 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007356set_ref_in_item(
7357 typval_T *tv,
7358 int copyID,
7359 ht_stack_T **ht_stack,
7360 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007361{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007362 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007363
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007364 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007365 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007366 dict_T *dd = tv->vval.v_dict;
7367
Bram Moolenaara03f2332016-02-06 18:09:59 +01007368 if (dd != NULL && dd->dv_copyID != copyID)
7369 {
7370 /* Didn't see this dict yet. */
7371 dd->dv_copyID = copyID;
7372 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007373 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007374 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7375 }
7376 else
7377 {
7378 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7379 if (newitem == NULL)
7380 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007381 else
7382 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007383 newitem->ht = &dd->dv_hashtab;
7384 newitem->prev = *ht_stack;
7385 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007386 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007387 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007388 }
7389 }
7390 else if (tv->v_type == VAR_LIST)
7391 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007392 list_T *ll = tv->vval.v_list;
7393
Bram Moolenaara03f2332016-02-06 18:09:59 +01007394 if (ll != NULL && ll->lv_copyID != copyID)
7395 {
7396 /* Didn't see this list yet. */
7397 ll->lv_copyID = copyID;
7398 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007399 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007400 abort = set_ref_in_list(ll, copyID, ht_stack);
7401 }
7402 else
7403 {
7404 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007405 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007406 if (newitem == NULL)
7407 abort = TRUE;
7408 else
7409 {
7410 newitem->list = ll;
7411 newitem->prev = *list_stack;
7412 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007413 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007414 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007415 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007416 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007417 else if (tv->v_type == VAR_PARTIAL)
7418 {
7419 partial_T *pt = tv->vval.v_partial;
7420 int i;
7421
7422 /* A partial does not have a copyID, because it cannot contain itself.
7423 */
7424 if (pt != NULL)
7425 {
7426 if (pt->pt_dict != NULL)
7427 {
7428 typval_T dtv;
7429
7430 dtv.v_type = VAR_DICT;
7431 dtv.vval.v_dict = pt->pt_dict;
7432 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7433 }
7434
7435 for (i = 0; i < pt->pt_argc; ++i)
7436 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7437 ht_stack, list_stack);
7438 }
7439 }
7440#ifdef FEAT_JOB_CHANNEL
7441 else if (tv->v_type == VAR_JOB)
7442 {
7443 job_T *job = tv->vval.v_job;
7444 typval_T dtv;
7445
7446 if (job != NULL && job->jv_copyID != copyID)
7447 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007448 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007449 if (job->jv_channel != NULL)
7450 {
7451 dtv.v_type = VAR_CHANNEL;
7452 dtv.vval.v_channel = job->jv_channel;
7453 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7454 }
7455 if (job->jv_exit_partial != NULL)
7456 {
7457 dtv.v_type = VAR_PARTIAL;
7458 dtv.vval.v_partial = job->jv_exit_partial;
7459 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7460 }
7461 }
7462 }
7463 else if (tv->v_type == VAR_CHANNEL)
7464 {
7465 channel_T *ch =tv->vval.v_channel;
7466 int part;
7467 typval_T dtv;
7468 jsonq_T *jq;
7469 cbq_T *cq;
7470
7471 if (ch != NULL && ch->ch_copyID != copyID)
7472 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007473 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007474 for (part = PART_SOCK; part <= PART_IN; ++part)
7475 {
7476 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7477 jq = jq->jq_next)
7478 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7479 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7480 cq = cq->cq_next)
7481 if (cq->cq_partial != NULL)
7482 {
7483 dtv.v_type = VAR_PARTIAL;
7484 dtv.vval.v_partial = cq->cq_partial;
7485 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7486 }
7487 if (ch->ch_part[part].ch_partial != NULL)
7488 {
7489 dtv.v_type = VAR_PARTIAL;
7490 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7491 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7492 }
7493 }
7494 if (ch->ch_partial != NULL)
7495 {
7496 dtv.v_type = VAR_PARTIAL;
7497 dtv.vval.v_partial = ch->ch_partial;
7498 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7499 }
7500 if (ch->ch_close_partial != NULL)
7501 {
7502 dtv.v_type = VAR_PARTIAL;
7503 dtv.vval.v_partial = ch->ch_close_partial;
7504 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7505 }
7506 }
7507 }
7508#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007509 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007510}
7511
7512/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 * Allocate an empty header for a dictionary.
7514 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007515 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007516dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517{
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007522 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007523 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007524 if (first_dict != NULL)
7525 first_dict->dv_used_prev = d;
7526 d->dv_used_next = first_dict;
7527 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007528 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007529
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007531 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007532 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007533 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007534 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007535 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007536 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537}
7538
7539/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007540 * Allocate an empty dict for a return value.
7541 * Returns OK or FAIL.
7542 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007543 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007544rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007545{
7546 dict_T *d = dict_alloc();
7547
7548 if (d == NULL)
7549 return FAIL;
7550
7551 rettv->vval.v_dict = d;
7552 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007553 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007554 ++d->dv_refcount;
7555 return OK;
7556}
7557
7558
7559/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 * Unreference a Dictionary: decrement the reference count and free it when it
7561 * becomes zero.
7562 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007566 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007567 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568}
7569
7570/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007571 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 * Ignores the reference count.
7573 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007574 static void
7575dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007579 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007581 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007582 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007583 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586 if (!HASHITEM_EMPTY(hi))
7587 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007588 /* Remove the item before deleting it, just in case there is
7589 * something recursive causing trouble. */
7590 di = HI2DI(hi);
7591 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007592 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007593 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 --todo;
7595 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007598}
7599
7600 static void
7601dict_free_dict(dict_T *d)
7602{
7603 /* Remove the dict from the list of dicts for garbage collection. */
7604 if (d->dv_used_prev == NULL)
7605 first_dict = d->dv_used_next;
7606 else
7607 d->dv_used_prev->dv_used_next = d->dv_used_next;
7608 if (d->dv_used_next != NULL)
7609 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 vim_free(d);
7611}
7612
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007613 void
7614dict_free(dict_T *d)
7615{
7616 if (!in_free_unref_items)
7617 {
7618 dict_free_contents(d);
7619 dict_free_dict(d);
7620 }
7621}
7622
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623/*
7624 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007625 * The "key" is copied to the new item.
7626 * Note that the value of the item "di_tv" still needs to be initialized!
7627 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007629 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007630dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631{
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007634 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007635 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007636 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007637 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007638 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007639 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007640 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641}
7642
7643/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007644 * Make a copy of a Dictionary item.
7645 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007647dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648{
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007650
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007651 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7652 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007653 if (di != NULL)
7654 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007656 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 copy_tv(&org->di_tv, &di->di_tv);
7658 }
7659 return di;
7660}
7661
7662/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007663 * Remove item "item" from Dictionary "dict" and free it.
7664 */
7665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007666dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667{
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007669
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007671 if (HASHITEM_EMPTY(hi))
7672 EMSG2(_(e_intern2), "dictitem_remove()");
7673 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007675 dictitem_free(item);
7676}
7677
7678/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679 * Free a dict item. Also clears the value.
7680 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007681 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007682dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007685 if (item->di_flags & DI_FLAGS_ALLOC)
7686 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687}
7688
7689/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007690 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7691 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007692 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007693 * Returns NULL when out of memory.
7694 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007696dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007697{
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 dict_T *copy;
7699 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007700 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007702
7703 if (orig == NULL)
7704 return NULL;
7705
7706 copy = dict_alloc();
7707 if (copy != NULL)
7708 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007709 if (copyID != 0)
7710 {
7711 orig->dv_copyID = copyID;
7712 orig->dv_copydict = copy;
7713 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007714 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007715 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007717 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007718 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007719 --todo;
7720
7721 di = dictitem_alloc(hi->hi_key);
7722 if (di == NULL)
7723 break;
7724 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007725 {
7726 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7727 copyID) == FAIL)
7728 {
7729 vim_free(di);
7730 break;
7731 }
7732 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007733 else
7734 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7735 if (dict_add(copy, di) == FAIL)
7736 {
7737 dictitem_free(di);
7738 break;
7739 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007740 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007742
Bram Moolenaare9a41262005-01-15 22:18:47 +00007743 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007744 if (todo > 0)
7745 {
7746 dict_unref(copy);
7747 copy = NULL;
7748 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007749 }
7750
7751 return copy;
7752}
7753
7754/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007756 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007757 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007758 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007759dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007760{
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762}
7763
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007765 * Add a number or string entry to dictionary "d".
7766 * When "str" is NULL use number "nr", otherwise use "str".
7767 * Returns FAIL when out of memory and when key already exists.
7768 */
7769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007770dict_add_nr_str(
7771 dict_T *d,
7772 char *key,
7773 long nr,
7774 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007775{
7776 dictitem_T *item;
7777
7778 item = dictitem_alloc((char_u *)key);
7779 if (item == NULL)
7780 return FAIL;
7781 item->di_tv.v_lock = 0;
7782 if (str == NULL)
7783 {
7784 item->di_tv.v_type = VAR_NUMBER;
7785 item->di_tv.vval.v_number = nr;
7786 }
7787 else
7788 {
7789 item->di_tv.v_type = VAR_STRING;
7790 item->di_tv.vval.v_string = vim_strsave(str);
7791 }
7792 if (dict_add(d, item) == FAIL)
7793 {
7794 dictitem_free(item);
7795 return FAIL;
7796 }
7797 return OK;
7798}
7799
7800/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007801 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007802 * Returns FAIL when out of memory and when key already exists.
7803 */
7804 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007805dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007806{
7807 dictitem_T *item;
7808
7809 item = dictitem_alloc((char_u *)key);
7810 if (item == NULL)
7811 return FAIL;
7812 item->di_tv.v_lock = 0;
7813 item->di_tv.v_type = VAR_LIST;
7814 item->di_tv.vval.v_list = list;
7815 if (dict_add(d, item) == FAIL)
7816 {
7817 dictitem_free(item);
7818 return FAIL;
7819 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007820 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007821 return OK;
7822}
7823
7824/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825 * Get the number of items in a Dictionary.
7826 */
7827 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007828dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007830 if (d == NULL)
7831 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007832 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007833}
7834
7835/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007836 * Find item "key[len]" in Dictionary "d".
7837 * If "len" is negative use strlen(key).
7838 * Returns NULL when not found.
7839 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007840 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007841dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007842{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007843#define AKEYLEN 200
7844 char_u buf[AKEYLEN];
7845 char_u *akey;
7846 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007849 if (d == NULL)
7850 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007851 if (len < 0)
7852 akey = key;
7853 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007854 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007855 tofree = akey = vim_strnsave(key, len);
7856 if (akey == NULL)
7857 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007858 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007859 else
7860 {
7861 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007862 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007863 akey = buf;
7864 }
7865
Bram Moolenaar33570922005-01-25 22:26:29 +00007866 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007867 vim_free(tofree);
7868 if (HASHITEM_EMPTY(hi))
7869 return NULL;
7870 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007871}
7872
7873/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007874 * Get a string item from a dictionary.
7875 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007876 * Returns NULL if the entry doesn't exist or out of memory.
7877 */
7878 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007879get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007880{
7881 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007882 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007883
7884 di = dict_find(d, key, -1);
7885 if (di == NULL)
7886 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007887 s = get_tv_string(&di->di_tv);
7888 if (save && s != NULL)
7889 s = vim_strsave(s);
7890 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007891}
7892
7893/*
7894 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007895 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007896 */
7897 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007898get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007899{
7900 dictitem_T *di;
7901
7902 di = dict_find(d, key, -1);
7903 if (di == NULL)
7904 return 0;
7905 return get_tv_number(&di->di_tv);
7906}
7907
7908/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007909 * Return an allocated string with the string representation of a Dictionary.
7910 * May return NULL.
7911 */
7912 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007913dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007914{
7915 garray_T ga;
7916 int first = TRUE;
7917 char_u *tofree;
7918 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007920 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007921 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007922 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007923
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007924 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007925 return NULL;
7926 ga_init2(&ga, (int)sizeof(char), 80);
7927 ga_append(&ga, '{');
7928
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007929 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007930 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007932 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007934 --todo;
7935
7936 if (first)
7937 first = FALSE;
7938 else
7939 ga_concat(&ga, (char_u *)", ");
7940
7941 tofree = string_quote(hi->hi_key, FALSE);
7942 if (tofree != NULL)
7943 {
7944 ga_concat(&ga, tofree);
7945 vim_free(tofree);
7946 }
7947 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007948 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7949 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007950 if (s != NULL)
7951 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007952 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007953 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007954 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007955 line_breakcheck();
7956
Bram Moolenaar8c711452005-01-14 21:53:12 +00007957 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007959 if (todo > 0)
7960 {
7961 vim_free(ga.ga_data);
7962 return NULL;
7963 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007964
7965 ga_append(&ga, '}');
7966 ga_append(&ga, NUL);
7967 return (char_u *)ga.ga_data;
7968}
7969
7970/*
7971 * Allocate a variable for a Dictionary and fill it from "*arg".
7972 * Return OK or FAIL. Returns NOTDONE for {expr}.
7973 */
7974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007975get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007976{
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 dict_T *d = NULL;
7978 typval_T tvkey;
7979 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007980 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007981 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007983 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007984
7985 /*
7986 * First check if it's not a curly-braces thing: {expr}.
7987 * Must do this without evaluating, otherwise a function may be called
7988 * twice. Unfortunately this means we need to call eval1() twice for the
7989 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007991 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007992 if (*start != '}')
7993 {
7994 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7995 return FAIL;
7996 if (*start == '}')
7997 return NOTDONE;
7998 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007999
8000 if (evaluate)
8001 {
8002 d = dict_alloc();
8003 if (d == NULL)
8004 return FAIL;
8005 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008006 tvkey.v_type = VAR_UNKNOWN;
8007 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008008
8009 *arg = skipwhite(*arg + 1);
8010 while (**arg != '}' && **arg != NUL)
8011 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008012 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008013 goto failret;
8014 if (**arg != ':')
8015 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008016 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008017 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008018 goto failret;
8019 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008020 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008021 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008022 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008023 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008024 {
8025 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008026 clear_tv(&tvkey);
8027 goto failret;
8028 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008029 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008030
8031 *arg = skipwhite(*arg + 1);
8032 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8033 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008034 if (evaluate)
8035 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008036 goto failret;
8037 }
8038 if (evaluate)
8039 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008040 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008041 if (item != NULL)
8042 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008043 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008044 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008045 clear_tv(&tv);
8046 goto failret;
8047 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008048 item = dictitem_alloc(key);
8049 clear_tv(&tvkey);
8050 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008051 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008052 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008053 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008054 if (dict_add(d, item) == FAIL)
8055 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008056 }
8057 }
8058
8059 if (**arg == '}')
8060 break;
8061 if (**arg != ',')
8062 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008063 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008064 goto failret;
8065 }
8066 *arg = skipwhite(*arg + 1);
8067 }
8068
8069 if (**arg != '}')
8070 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008071 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008072failret:
8073 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008074 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008075 return FAIL;
8076 }
8077
8078 *arg = skipwhite(*arg + 1);
8079 if (evaluate)
8080 {
8081 rettv->v_type = VAR_DICT;
8082 rettv->vval.v_dict = d;
8083 ++d->dv_refcount;
8084 }
8085
8086 return OK;
8087}
8088
Bram Moolenaar17a13432016-01-24 14:22:10 +01008089 static char *
8090get_var_special_name(int nr)
8091{
8092 switch (nr)
8093 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008094 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008095 case VVAL_TRUE: return "v:true";
8096 case VVAL_NONE: return "v:none";
8097 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008098 }
8099 EMSG2(_(e_intern2), "get_var_special_name()");
8100 return "42";
8101}
8102
Bram Moolenaar8c711452005-01-14 21:53:12 +00008103/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008104 * Return a string with the string representation of a variable.
8105 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008106 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008107 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008108 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8109 * "string()", otherwise does not put quotes around strings, as ":echo"
8110 * displays values.
8111 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8112 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008113 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008114 */
8115 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008116echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008117 typval_T *tv,
8118 char_u **tofree,
8119 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008120 int copyID,
8121 int echo_style,
8122 int restore_copyID,
8123 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008125 static int recurse = 0;
8126 char_u *r = NULL;
8127
Bram Moolenaar33570922005-01-25 22:26:29 +00008128 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008129 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008130 if (!did_echo_string_emsg)
8131 {
8132 /* Only give this message once for a recursive call to avoid
8133 * flooding the user with errors. And stop iterating over lists
8134 * and dicts. */
8135 did_echo_string_emsg = TRUE;
8136 EMSG(_("E724: variable nested too deep for displaying"));
8137 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008138 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008139 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008140 }
8141 ++recurse;
8142
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008143 switch (tv->v_type)
8144 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008145 case VAR_STRING:
8146 if (echo_style && !dict_val)
8147 {
8148 *tofree = NULL;
8149 r = get_tv_string_buf(tv, numbuf);
8150 }
8151 else
8152 {
8153 *tofree = string_quote(tv->vval.v_string, FALSE);
8154 r = *tofree;
8155 }
8156 break;
8157
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008158 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008159 if (echo_style)
8160 {
8161 *tofree = NULL;
8162 r = tv->vval.v_string;
8163 }
8164 else
8165 {
8166 *tofree = string_quote(tv->vval.v_string, TRUE);
8167 r = *tofree;
8168 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008169 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008170
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008171 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008172 {
8173 partial_T *pt = tv->vval.v_partial;
8174 char_u *fname = string_quote(pt == NULL ? NULL
8175 : pt->pt_name, FALSE);
8176 garray_T ga;
8177 int i;
8178 char_u *tf;
8179
8180 ga_init2(&ga, 1, 100);
8181 ga_concat(&ga, (char_u *)"function(");
8182 if (fname != NULL)
8183 {
8184 ga_concat(&ga, fname);
8185 vim_free(fname);
8186 }
8187 if (pt != NULL && pt->pt_argc > 0)
8188 {
8189 ga_concat(&ga, (char_u *)", [");
8190 for (i = 0; i < pt->pt_argc; ++i)
8191 {
8192 if (i > 0)
8193 ga_concat(&ga, (char_u *)", ");
8194 ga_concat(&ga,
8195 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8196 vim_free(tf);
8197 }
8198 ga_concat(&ga, (char_u *)"]");
8199 }
8200 if (pt != NULL && pt->pt_dict != NULL)
8201 {
8202 typval_T dtv;
8203
8204 ga_concat(&ga, (char_u *)", ");
8205 dtv.v_type = VAR_DICT;
8206 dtv.vval.v_dict = pt->pt_dict;
8207 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8208 vim_free(tf);
8209 }
8210 ga_concat(&ga, (char_u *)")");
8211
8212 *tofree = ga.ga_data;
8213 r = *tofree;
8214 break;
8215 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008216
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008217 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008218 if (tv->vval.v_list == NULL)
8219 {
8220 *tofree = NULL;
8221 r = NULL;
8222 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008223 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8224 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008225 {
8226 *tofree = NULL;
8227 r = (char_u *)"[...]";
8228 }
8229 else
8230 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008231 int old_copyID = tv->vval.v_list->lv_copyID;
8232
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008233 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008234 *tofree = list2string(tv, copyID, restore_copyID);
8235 if (restore_copyID)
8236 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008237 r = *tofree;
8238 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008239 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008240
Bram Moolenaar8c711452005-01-14 21:53:12 +00008241 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008242 if (tv->vval.v_dict == NULL)
8243 {
8244 *tofree = NULL;
8245 r = NULL;
8246 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008247 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8248 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008249 {
8250 *tofree = NULL;
8251 r = (char_u *)"{...}";
8252 }
8253 else
8254 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008255 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008256 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008257 *tofree = dict2string(tv, copyID, restore_copyID);
8258 if (restore_copyID)
8259 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008260 r = *tofree;
8261 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008262 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008263
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008265 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008266 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008267 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008268 *tofree = NULL;
8269 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008270 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008271
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008272 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008273#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008274 *tofree = NULL;
8275 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8276 r = numbuf;
8277 break;
8278#endif
8279
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008280 case VAR_SPECIAL:
8281 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008282 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008283 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008285
Bram Moolenaar8502c702014-06-17 12:51:16 +02008286 if (--recurse == 0)
8287 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008288 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008289}
8290
8291/*
8292 * Return a string with the string representation of a variable.
8293 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8294 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008295 * Does not put quotes around strings, as ":echo" displays values.
8296 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8297 * May return NULL.
8298 */
8299 static char_u *
8300echo_string(
8301 typval_T *tv,
8302 char_u **tofree,
8303 char_u *numbuf,
8304 int copyID)
8305{
8306 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8307}
8308
8309/*
8310 * Return a string with the string representation of a variable.
8311 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8312 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008313 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008314 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008315 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008316 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008317tv2string(
8318 typval_T *tv,
8319 char_u **tofree,
8320 char_u *numbuf,
8321 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008322{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008323 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008324}
8325
8326/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008327 * Return string "str" in ' quotes, doubling ' characters.
8328 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008329 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008330 */
8331 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008332string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008333{
Bram Moolenaar33570922005-01-25 22:26:29 +00008334 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008335 char_u *p, *r, *s;
8336
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 len = (function ? 13 : 3);
8338 if (str != NULL)
8339 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008340 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 for (p = str; *p != NUL; mb_ptr_adv(p))
8342 if (*p == '\'')
8343 ++len;
8344 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008345 s = r = alloc(len);
8346 if (r != NULL)
8347 {
8348 if (function)
8349 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008350 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008351 r += 10;
8352 }
8353 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008354 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008355 if (str != NULL)
8356 for (p = str; *p != NUL; )
8357 {
8358 if (*p == '\'')
8359 *r++ = '\'';
8360 MB_COPY_CHAR(p, r);
8361 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008362 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008363 if (function)
8364 *r++ = ')';
8365 *r++ = NUL;
8366 }
8367 return s;
8368}
8369
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008370#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371/*
8372 * Convert the string "text" to a floating point number.
8373 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8374 * this always uses a decimal point.
8375 * Returns the length of the text that was consumed.
8376 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008377 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008378string2float(
8379 char_u *text,
8380 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008381{
8382 char *s = (char *)text;
8383 float_T f;
8384
8385 f = strtod(s, &s);
8386 *value = f;
8387 return (int)((char_u *)s - text);
8388}
8389#endif
8390
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 * Get the value of an environment variable.
8393 * "arg" is pointing to the '$'. It is advanced to after the name.
8394 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008395 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 */
8397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008398get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
8400 char_u *string = NULL;
8401 int len;
8402 int cc;
8403 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008404 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405
8406 ++*arg;
8407 name = *arg;
8408 len = get_env_len(arg);
8409 if (evaluate)
8410 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008411 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008412 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008413
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008414 cc = name[len];
8415 name[len] = NUL;
8416 /* first try vim_getenv(), fast for normal environment vars */
8417 string = vim_getenv(name, &mustfree);
8418 if (string != NULL && *string != NUL)
8419 {
8420 if (!mustfree)
8421 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008423 else
8424 {
8425 if (mustfree)
8426 vim_free(string);
8427
8428 /* next try expanding things like $VIM and ${HOME} */
8429 string = expand_env_save(name - 1);
8430 if (string != NULL && *string == '$')
8431 {
8432 vim_free(string);
8433 string = NULL;
8434 }
8435 }
8436 name[len] = cc;
8437
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008438 rettv->v_type = VAR_STRING;
8439 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 }
8441
8442 return OK;
8443}
8444
8445/*
8446 * Array with names and number of arguments of all internal functions
8447 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8448 */
8449static struct fst
8450{
8451 char *f_name; /* function name */
8452 char f_min_argc; /* minimal number of arguments */
8453 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008454 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008455 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456} functions[] =
8457{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008458#ifdef FEAT_FLOAT
8459 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008460 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008461#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008462 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008463 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 {"append", 2, 2, f_append},
8465 {"argc", 0, 0, f_argc},
8466 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008467 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008468 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008469#ifdef FEAT_FLOAT
8470 {"asin", 1, 1, f_asin}, /* WJMc */
8471#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008472 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008473 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008474 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008475 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008476 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008477 {"assert_notequal", 2, 3, f_assert_notequal},
8478 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008479 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008480#ifdef FEAT_FLOAT
8481 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008482 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008485 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {"bufexists", 1, 1, f_bufexists},
8487 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8488 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8489 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8490 {"buflisted", 1, 1, f_buflisted},
8491 {"bufloaded", 1, 1, f_bufloaded},
8492 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008493 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008494 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 {"bufwinnr", 1, 1, f_bufwinnr},
8496 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008497 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008498 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008499 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008500#ifdef FEAT_FLOAT
8501 {"ceil", 1, 1, f_ceil},
8502#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008504 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008505 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8506 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008507 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008508 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008509 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008510 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008511 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008512 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008513 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008514 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008515 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8516 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008517 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008518 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008519#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008520 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008521 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008523 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008525#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008526 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008527 {"complete_add", 1, 1, f_complete_add},
8528 {"complete_check", 0, 0, f_complete_check},
8529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008532#ifdef FEAT_FLOAT
8533 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008534 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008535#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008536 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008538 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008539 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008540 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008542 {"diff_filler", 1, 1, f_diff_filler},
8543 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008544 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008546 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 {"eventhandler", 0, 0, f_eventhandler},
8548 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008549 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008551#ifdef FEAT_FLOAT
8552 {"exp", 1, 1, f_exp},
8553#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008554 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008555 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008556 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8558 {"filereadable", 1, 1, f_filereadable},
8559 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008560 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008561 {"finddir", 1, 3, f_finddir},
8562 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008563#ifdef FEAT_FLOAT
8564 {"float2nr", 1, 1, f_float2nr},
8565 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008566 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008567#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008568 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {"fnamemodify", 2, 2, f_fnamemodify},
8570 {"foldclosed", 1, 1, f_foldclosed},
8571 {"foldclosedend", 1, 1, f_foldclosedend},
8572 {"foldlevel", 1, 1, f_foldlevel},
8573 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008574 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008576 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008577 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008578 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008579 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008580 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 {"getchar", 0, 1, f_getchar},
8582 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008583 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 {"getcmdline", 0, 0, f_getcmdline},
8585 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008586 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008587 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008588 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008589 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008590 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008591 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 {"getfsize", 1, 1, f_getfsize},
8593 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008594 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008595 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008596 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008597 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008598 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008599 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008600 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008601 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008603 {"gettabvar", 2, 3, f_gettabvar},
8604 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 {"getwinposx", 0, 0, f_getwinposx},
8606 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008607 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008608 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008609 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008610 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008612 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008613 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008614 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8616 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8617 {"histadd", 2, 2, f_histadd},
8618 {"histdel", 1, 2, f_histdel},
8619 {"histget", 1, 2, f_histget},
8620 {"histnr", 1, 1, f_histnr},
8621 {"hlID", 1, 1, f_hlID},
8622 {"hlexists", 1, 1, f_hlexists},
8623 {"hostname", 0, 0, f_hostname},
8624 {"iconv", 3, 3, f_iconv},
8625 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008626 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008627 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008629 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"inputrestore", 0, 0, f_inputrestore},
8631 {"inputsave", 0, 0, f_inputsave},
8632 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008633 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008634 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008636 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008637#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8638 {"isnan", 1, 1, f_isnan},
8639#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008640 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008641#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008642 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008643 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008644 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008645 {"job_start", 1, 2, f_job_start},
8646 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008647 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008648#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008649 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008650 {"js_decode", 1, 1, f_js_decode},
8651 {"js_encode", 1, 1, f_js_encode},
8652 {"json_decode", 1, 1, f_json_decode},
8653 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008654 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {"libcall", 3, 3, f_libcall},
8658 {"libcallnr", 3, 3, f_libcallnr},
8659 {"line", 1, 1, f_line},
8660 {"line2byte", 1, 1, f_line2byte},
8661 {"lispindent", 1, 1, f_lispindent},
8662 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008663#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008664 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008665 {"log10", 1, 1, f_log10},
8666#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008667#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008668 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008669#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008670 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008671 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008672 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008673 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008674 {"matchadd", 2, 5, f_matchadd},
8675 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008676 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008677 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008678 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008679 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008680 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008681 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008682 {"max", 1, 1, f_max},
8683 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008684#ifdef vim_mkdir
8685 {"mkdir", 1, 3, f_mkdir},
8686#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008687 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008688#ifdef FEAT_MZSCHEME
8689 {"mzeval", 1, 1, f_mzeval},
8690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008692 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008693 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008694 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008695#ifdef FEAT_PERL
8696 {"perleval", 1, 1, f_perleval},
8697#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008698#ifdef FEAT_FLOAT
8699 {"pow", 2, 2, f_pow},
8700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008702 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008703 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008704#ifdef FEAT_PYTHON3
8705 {"py3eval", 1, 1, f_py3eval},
8706#endif
8707#ifdef FEAT_PYTHON
8708 {"pyeval", 1, 1, f_pyeval},
8709#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008710 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008711 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008712 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008713#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008714 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008715#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008716 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 {"remote_expr", 2, 3, f_remote_expr},
8718 {"remote_foreground", 1, 1, f_remote_foreground},
8719 {"remote_peek", 1, 2, f_remote_peek},
8720 {"remote_read", 1, 1, f_remote_read},
8721 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008722 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008724 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008726 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008727#ifdef FEAT_FLOAT
8728 {"round", 1, 1, f_round},
8729#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008730 {"screenattr", 2, 2, f_screenattr},
8731 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008732 {"screencol", 0, 0, f_screencol},
8733 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008734 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008735 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008736 {"searchpair", 3, 7, f_searchpair},
8737 {"searchpairpos", 3, 7, f_searchpairpos},
8738 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 {"server2client", 2, 2, f_server2client},
8740 {"serverlist", 0, 0, f_serverlist},
8741 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008742 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008744 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008746 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008747 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008748 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008749 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008751 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008752 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008754#ifdef FEAT_CRYPT
8755 {"sha256", 1, 1, f_sha256},
8756#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008757 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008758 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008760#ifdef FEAT_FLOAT
8761 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008762 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008763#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008764 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008765 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008766 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008767 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008768 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008769#ifdef FEAT_FLOAT
8770 {"sqrt", 1, 1, f_sqrt},
8771 {"str2float", 1, 1, f_str2float},
8772#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008773 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008774 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008775 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008776 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777#ifdef HAVE_STRFTIME
8778 {"strftime", 1, 2, f_strftime},
8779#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008780 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008781 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008782 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 {"strlen", 1, 1, f_strlen},
8784 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008785 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008787 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008788 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 {"substitute", 4, 4, f_substitute},
8790 {"synID", 3, 3, f_synID},
8791 {"synIDattr", 2, 3, f_synIDattr},
8792 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008793 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008794 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008795 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008796 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008797 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008798 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008799 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008800 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008801 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008802#ifdef FEAT_FLOAT
8803 {"tan", 1, 1, f_tan},
8804 {"tanh", 1, 1, f_tanh},
8805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008807 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8808 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008809 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8810#ifdef FEAT_JOB_CHANNEL
8811 {"test_null_channel", 0, 0, f_test_null_channel},
8812#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008813 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008814#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008815 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008816#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008817 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008818 {"test_null_partial", 0, 0, f_test_null_partial},
8819 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008820 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008821#ifdef FEAT_TIMERS
8822 {"timer_start", 2, 3, f_timer_start},
8823 {"timer_stop", 1, 1, f_timer_stop},
8824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 {"tolower", 1, 1, f_tolower},
8826 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008827 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008828#ifdef FEAT_FLOAT
8829 {"trunc", 1, 1, f_trunc},
8830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008832 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008833 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008834 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008835 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 {"virtcol", 1, 1, f_virtcol},
8837 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008838 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008839 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008840 {"win_getid", 0, 2, f_win_getid},
8841 {"win_gotoid", 1, 1, f_win_gotoid},
8842 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8843 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 {"winbufnr", 1, 1, f_winbufnr},
8845 {"wincol", 0, 0, f_wincol},
8846 {"winheight", 1, 1, f_winheight},
8847 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008848 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008850 {"winrestview", 1, 1, f_winrestview},
8851 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008853 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008854 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008855 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856};
8857
8858#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8859
8860/*
8861 * Function given to ExpandGeneric() to obtain the list of internal
8862 * or user defined function names.
8863 */
8864 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008865get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
8867 static int intidx = -1;
8868 char_u *name;
8869
8870 if (idx == 0)
8871 intidx = -1;
8872 if (intidx < 0)
8873 {
8874 name = get_user_func_name(xp, idx);
8875 if (name != NULL)
8876 return name;
8877 }
8878 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8879 {
8880 STRCPY(IObuff, functions[intidx].f_name);
8881 STRCAT(IObuff, "(");
8882 if (functions[intidx].f_max_argc == 0)
8883 STRCAT(IObuff, ")");
8884 return IObuff;
8885 }
8886
8887 return NULL;
8888}
8889
8890/*
8891 * Function given to ExpandGeneric() to obtain the list of internal or
8892 * user defined variable or function names.
8893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008895get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896{
8897 static int intidx = -1;
8898 char_u *name;
8899
8900 if (idx == 0)
8901 intidx = -1;
8902 if (intidx < 0)
8903 {
8904 name = get_function_name(xp, idx);
8905 if (name != NULL)
8906 return name;
8907 }
8908 return get_user_var_name(xp, ++intidx);
8909}
8910
8911#endif /* FEAT_CMDL_COMPL */
8912
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008913#if defined(EBCDIC) || defined(PROTO)
8914/*
8915 * Compare struct fst by function name.
8916 */
8917 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008918compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008919{
8920 struct fst *p1 = (struct fst *)s1;
8921 struct fst *p2 = (struct fst *)s2;
8922
8923 return STRCMP(p1->f_name, p2->f_name);
8924}
8925
8926/*
8927 * Sort the function table by function name.
8928 * The sorting of the table above is ASCII dependant.
8929 * On machines using EBCDIC we have to sort it.
8930 */
8931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008932sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008933{
8934 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8935
8936 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8937}
8938#endif
8939
8940
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941/*
8942 * Find internal function in table above.
8943 * Return index, or -1 if not found
8944 */
8945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008946find_internal_func(
8947 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948{
8949 int first = 0;
8950 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8951 int cmp;
8952 int x;
8953
8954 /*
8955 * Find the function name in the table. Binary search.
8956 */
8957 while (first <= last)
8958 {
8959 x = first + ((unsigned)(last - first) >> 1);
8960 cmp = STRCMP(name, functions[x].f_name);
8961 if (cmp < 0)
8962 last = x - 1;
8963 else if (cmp > 0)
8964 first = x + 1;
8965 else
8966 return x;
8967 }
8968 return -1;
8969}
8970
8971/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008972 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8973 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008974 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8975 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 */
8977 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008978deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008979{
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008981 int cc;
8982
Bram Moolenaar65639032016-03-16 21:40:30 +01008983 if (partialp != NULL)
8984 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008985
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008986 cc = name[*lenp];
8987 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008988 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008989 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008990 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008991 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 {
8994 *lenp = 0;
8995 return (char_u *)""; /* just in case */
8996 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008997 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008999 }
9000
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009001 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
9002 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009003 partial_T *pt = v->di_tv.vval.v_partial;
9004
9005 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009006 {
9007 *lenp = 0;
9008 return (char_u *)""; /* just in case */
9009 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009010 if (partialp != NULL)
9011 *partialp = pt;
9012 *lenp = (int)STRLEN(pt->pt_name);
9013 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009014 }
9015
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009016 return name;
9017}
9018
9019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 * Allocate a variable for the result of a function.
9021 * Return OK or FAIL.
9022 */
9023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009024get_func_tv(
9025 char_u *name, /* name of the function */
9026 int len, /* length of "name" */
9027 typval_T *rettv,
9028 char_u **arg, /* argument, pointing to the '(' */
9029 linenr_T firstline, /* first line of range */
9030 linenr_T lastline, /* last line of range */
9031 int *doesrange, /* return: function handled range */
9032 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009033 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009034 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035{
9036 char_u *argp;
9037 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009038 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 int argcount = 0; /* number of arguments found */
9040
9041 /*
9042 * Get the arguments.
9043 */
9044 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009045 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 {
9047 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9048 if (*argp == ')' || *argp == ',' || *argp == NUL)
9049 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9051 {
9052 ret = FAIL;
9053 break;
9054 }
9055 ++argcount;
9056 if (*argp != ',')
9057 break;
9058 }
9059 if (*argp == ')')
9060 ++argp;
9061 else
9062 ret = FAIL;
9063
9064 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009065 {
9066 int i = 0;
9067
9068 if (get_vim_var_nr(VV_TESTING))
9069 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009070 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009071 * what variables are used on the call stack. */
9072 if (funcargs.ga_itemsize == 0)
9073 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9074 for (i = 0; i < argcount; ++i)
9075 if (ga_grow(&funcargs, 1) == OK)
9076 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9077 &argvars[i];
9078 }
9079
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009081 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009082
9083 funcargs.ga_len -= i;
9084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 {
9087 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009088 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009090 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092
9093 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095
9096 *arg = skipwhite(argp);
9097 return ret;
9098}
9099
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009100#define ERROR_UNKNOWN 0
9101#define ERROR_TOOMANY 1
9102#define ERROR_TOOFEW 2
9103#define ERROR_SCRIPT 3
9104#define ERROR_DICT 4
9105#define ERROR_NONE 5
9106#define ERROR_OTHER 6
9107#define FLEN_FIXED 40
9108
9109/*
9110 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9111 * Change <SNR>123_name() to K_SNR 123_name().
9112 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9113 * (slow).
9114 */
9115 static char_u *
9116fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9117{
9118 int llen;
9119 char_u *fname;
9120 int i;
9121
9122 llen = eval_fname_script(name);
9123 if (llen > 0)
9124 {
9125 fname_buf[0] = K_SPECIAL;
9126 fname_buf[1] = KS_EXTRA;
9127 fname_buf[2] = (int)KE_SNR;
9128 i = 3;
9129 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9130 {
9131 if (current_SID <= 0)
9132 *error = ERROR_SCRIPT;
9133 else
9134 {
9135 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9136 i = (int)STRLEN(fname_buf);
9137 }
9138 }
9139 if (i + STRLEN(name + llen) < FLEN_FIXED)
9140 {
9141 STRCPY(fname_buf + i, name + llen);
9142 fname = fname_buf;
9143 }
9144 else
9145 {
9146 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9147 if (fname == NULL)
9148 *error = ERROR_OTHER;
9149 else
9150 {
9151 *tofree = fname;
9152 mch_memmove(fname, fname_buf, (size_t)i);
9153 STRCPY(fname + i, name + llen);
9154 }
9155 }
9156 }
9157 else
9158 fname = name;
9159 return fname;
9160}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161
9162/*
9163 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009164 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009165 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009167 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009168call_func(
9169 char_u *funcname, /* name of the function */
9170 int len, /* length of "name" */
9171 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009172 int argcount_in, /* number of "argvars" */
9173 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009174 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009175 linenr_T firstline, /* first line of range */
9176 linenr_T lastline, /* last line of range */
9177 int *doesrange, /* return: function handled range */
9178 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009179 partial_T *partial, /* optional, can be NULL */
9180 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181{
9182 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 int error = ERROR_NONE;
9184 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009187 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009189 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009190 int argcount = argcount_in;
9191 typval_T *argvars = argvars_in;
9192 dict_T *selfdict = selfdict_in;
9193 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9194 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009195
9196 /* Make a copy of the name, if it comes from a funcref variable it could
9197 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009198 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009199 if (name == NULL)
9200 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009202 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203
9204 *doesrange = FALSE;
9205
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009206 if (partial != NULL)
9207 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009208 /* When the function has a partial with a dict and there is a dict
9209 * argument, use the dict argument. That is backwards compatible.
9210 * When the dict was bound explicitly use the one from the partial. */
9211 if (partial->pt_dict != NULL
9212 && (selfdict_in == NULL || !partial->pt_auto))
9213 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009214 if (error == ERROR_NONE && partial->pt_argc > 0)
9215 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009216 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9217 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9218 for (i = 0; i < argcount_in; ++i)
9219 argv[i + argv_clear] = argvars_in[i];
9220 argvars = argv;
9221 argcount = partial->pt_argc + argcount_in;
9222 }
9223 }
9224
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225
9226 /* execute the function if no errors detected and executing */
9227 if (evaluate && error == ERROR_NONE)
9228 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009229 char_u *rfname = fname;
9230
9231 /* Ignore "g:" before a function name. */
9232 if (fname[0] == 'g' && fname[1] == ':')
9233 rfname = fname + 2;
9234
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009235 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9236 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 error = ERROR_UNKNOWN;
9238
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009239 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 {
9241 /*
9242 * User defined function.
9243 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009244 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009245
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009247 /* Trigger FuncUndefined event, may load the function. */
9248 if (fp == NULL
9249 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009250 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009251 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009253 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009254 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 }
9256#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009257 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009258 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009259 {
9260 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009261 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009262 }
9263
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 if (fp != NULL)
9265 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009266 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009268 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009270 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009272 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009273 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 else
9275 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009276 int did_save_redo = FALSE;
9277
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 /*
9279 * Call the user function.
9280 * Save and restore search patterns, script variables and
9281 * redo buffer.
9282 */
9283 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009284#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009285 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009286#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009287 {
9288 saveRedobuff();
9289 did_save_redo = TRUE;
9290 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009291 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009293 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009294 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9295 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9296 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009297 /* Function was unreferenced while being used, free it
9298 * now. */
9299 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009300 if (did_save_redo)
9301 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 restore_search_patterns();
9303 error = ERROR_NONE;
9304 }
9305 }
9306 }
9307 else
9308 {
9309 /*
9310 * Find the function name in the table, call its implementation.
9311 */
9312 i = find_internal_func(fname);
9313 if (i >= 0)
9314 {
9315 if (argcount < functions[i].f_min_argc)
9316 error = ERROR_TOOFEW;
9317 else if (argcount > functions[i].f_max_argc)
9318 error = ERROR_TOOMANY;
9319 else
9320 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009321 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009322 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 error = ERROR_NONE;
9324 }
9325 }
9326 }
9327 /*
9328 * The function call (or "FuncUndefined" autocommand sequence) might
9329 * have been aborted by an error, an interrupt, or an explicitly thrown
9330 * exception that has not been caught so far. This situation can be
9331 * tested for by calling aborting(). For an error in an internal
9332 * function or for the "E132" error in call_user_func(), however, the
9333 * throw point at which the "force_abort" flag (temporarily reset by
9334 * emsg()) is normally updated has not been reached yet. We need to
9335 * update that flag first to make aborting() reliable.
9336 */
9337 update_force_abort();
9338 }
9339 if (error == ERROR_NONE)
9340 ret = OK;
9341
9342 /*
9343 * Report an error unless the argument evaluation or function call has been
9344 * cancelled due to an aborting error, an interrupt, or an exception.
9345 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009346 if (!aborting())
9347 {
9348 switch (error)
9349 {
9350 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009351 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009352 break;
9353 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009354 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009355 break;
9356 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009357 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009358 name);
9359 break;
9360 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009361 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009362 name);
9363 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009364 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009365 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009366 name);
9367 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009368 }
9369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009371 while (argv_clear > 0)
9372 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009373 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009374 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375
9376 return ret;
9377}
9378
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009379/*
9380 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009381 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009382 */
9383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009384emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009385{
9386 char_u *p;
9387
9388 if (*name == K_SPECIAL)
9389 p = concat_str((char_u *)"<SNR>", name + 3);
9390 else
9391 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009392 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009393 if (p != name)
9394 vim_free(p);
9395}
9396
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009397/*
9398 * Return TRUE for a non-zero Number and a non-empty String.
9399 */
9400 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009401non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009402{
9403 return ((argvars[0].v_type == VAR_NUMBER
9404 && argvars[0].vval.v_number != 0)
9405 || (argvars[0].v_type == VAR_STRING
9406 && argvars[0].vval.v_string != NULL
9407 && *argvars[0].vval.v_string != NUL));
9408}
9409
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410/*********************************************
9411 * Implementation of the built-in functions
9412 */
9413
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009414#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009415static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009416
9417/*
9418 * Get the float value of "argvars[0]" into "f".
9419 * Returns FAIL when the argument is not a Number or Float.
9420 */
9421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009422get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009423{
9424 if (argvars[0].v_type == VAR_FLOAT)
9425 {
9426 *f = argvars[0].vval.v_float;
9427 return OK;
9428 }
9429 if (argvars[0].v_type == VAR_NUMBER)
9430 {
9431 *f = (float_T)argvars[0].vval.v_number;
9432 return OK;
9433 }
9434 EMSG(_("E808: Number or Float required"));
9435 return FAIL;
9436}
9437
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009438/*
9439 * "abs(expr)" function
9440 */
9441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009442f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009443{
9444 if (argvars[0].v_type == VAR_FLOAT)
9445 {
9446 rettv->v_type = VAR_FLOAT;
9447 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9448 }
9449 else
9450 {
9451 varnumber_T n;
9452 int error = FALSE;
9453
9454 n = get_tv_number_chk(&argvars[0], &error);
9455 if (error)
9456 rettv->vval.v_number = -1;
9457 else if (n > 0)
9458 rettv->vval.v_number = n;
9459 else
9460 rettv->vval.v_number = -n;
9461 }
9462}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009463
9464/*
9465 * "acos()" function
9466 */
9467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009468f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009469{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009470 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009471
9472 rettv->v_type = VAR_FLOAT;
9473 if (get_float_arg(argvars, &f) == OK)
9474 rettv->vval.v_float = acos(f);
9475 else
9476 rettv->vval.v_float = 0.0;
9477}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009478#endif
9479
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009481 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 */
9483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009484f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485{
Bram Moolenaar33570922005-01-25 22:26:29 +00009486 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009489 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009491 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009492 && !tv_check_lock(l->lv_lock,
9493 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009494 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009496 }
9497 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009498 EMSG(_(e_listreq));
9499}
9500
9501/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009502 * "and(expr, expr)" function
9503 */
9504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009505f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009506{
9507 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9508 & get_tv_number_chk(&argvars[1], NULL);
9509}
9510
9511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009512 * "append(lnum, string/list)" function
9513 */
9514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009515f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009516{
9517 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009518 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009519 list_T *l = NULL;
9520 listitem_T *li = NULL;
9521 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009522 long added = 0;
9523
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009524 /* When coming here from Insert mode, sync undo, so that this can be
9525 * undone separately from what was previously inserted. */
9526 if (u_sync_once == 2)
9527 {
9528 u_sync_once = 1; /* notify that u_sync() was called */
9529 u_sync(TRUE);
9530 }
9531
Bram Moolenaar0d660222005-01-07 21:51:51 +00009532 lnum = get_tv_lnum(argvars);
9533 if (lnum >= 0
9534 && lnum <= curbuf->b_ml.ml_line_count
9535 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009536 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009537 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009538 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009539 l = argvars[1].vval.v_list;
9540 if (l == NULL)
9541 return;
9542 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009543 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009544 for (;;)
9545 {
9546 if (l == NULL)
9547 tv = &argvars[1]; /* append a string */
9548 else if (li == NULL)
9549 break; /* end of list */
9550 else
9551 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 line = get_tv_string_chk(tv);
9553 if (line == NULL) /* type error */
9554 {
9555 rettv->vval.v_number = 1; /* Failed */
9556 break;
9557 }
9558 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009559 ++added;
9560 if (l == NULL)
9561 break;
9562 li = li->li_next;
9563 }
9564
9565 appended_lines_mark(lnum, added);
9566 if (curwin->w_cursor.lnum > lnum)
9567 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009569 else
9570 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571}
9572
9573/*
9574 * "argc()" function
9575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009577f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580}
9581
9582/*
9583 * "argidx()" function
9584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009586f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009588 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589}
9590
9591/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009592 * "arglistid()" function
9593 */
9594 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009595f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009596{
9597 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009598
9599 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009600 wp = find_tabwin(&argvars[0], &argvars[1]);
9601 if (wp != NULL)
9602 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009603}
9604
9605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 * "argv(nr)" function
9607 */
9608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009609f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
9611 int idx;
9612
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009613 if (argvars[0].v_type != VAR_UNKNOWN)
9614 {
9615 idx = get_tv_number_chk(&argvars[0], NULL);
9616 if (idx >= 0 && idx < ARGCOUNT)
9617 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9618 else
9619 rettv->vval.v_string = NULL;
9620 rettv->v_type = VAR_STRING;
9621 }
9622 else if (rettv_list_alloc(rettv) == OK)
9623 for (idx = 0; idx < ARGCOUNT; ++idx)
9624 list_append_string(rettv->vval.v_list,
9625 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626}
9627
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009628typedef enum
9629{
9630 ASSERT_EQUAL,
9631 ASSERT_NOTEQUAL,
9632 ASSERT_MATCH,
9633 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009634 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009635} assert_type_T;
9636
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009637static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009638static 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 +01009639static void assert_error(garray_T *gap);
9640static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009641
9642/*
9643 * Prepare "gap" for an assert error and add the sourcing position.
9644 */
9645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009646prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009647{
9648 char buf[NUMBUFLEN];
9649
9650 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009651 if (sourcing_name != NULL)
9652 {
9653 ga_concat(gap, sourcing_name);
9654 if (sourcing_lnum > 0)
9655 ga_concat(gap, (char_u *)" ");
9656 }
9657 if (sourcing_lnum > 0)
9658 {
9659 sprintf(buf, "line %ld", (long)sourcing_lnum);
9660 ga_concat(gap, (char_u *)buf);
9661 }
9662 if (sourcing_name != NULL || sourcing_lnum > 0)
9663 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009664}
9665
9666/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009667 * Append "str" to "gap", escaping unprintable characters.
9668 * Changes NL to \n, CR to \r, etc.
9669 */
9670 static void
9671ga_concat_esc(garray_T *gap, char_u *str)
9672{
9673 char_u *p;
9674 char_u buf[NUMBUFLEN];
9675
Bram Moolenaarf1551962016-03-15 12:55:58 +01009676 if (str == NULL)
9677 {
9678 ga_concat(gap, (char_u *)"NULL");
9679 return;
9680 }
9681
Bram Moolenaar23689172016-02-15 22:37:37 +01009682 for (p = str; *p != NUL; ++p)
9683 switch (*p)
9684 {
9685 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9686 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9687 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9688 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9689 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9690 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9691 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9692 default:
9693 if (*p < ' ')
9694 {
9695 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9696 ga_concat(gap, buf);
9697 }
9698 else
9699 ga_append(gap, *p);
9700 break;
9701 }
9702}
9703
9704/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009705 * Fill "gap" with information about an assert error.
9706 */
9707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009708fill_assert_error(
9709 garray_T *gap,
9710 typval_T *opt_msg_tv,
9711 char_u *exp_str,
9712 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009713 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009714 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009715{
9716 char_u numbuf[NUMBUFLEN];
9717 char_u *tofree;
9718
9719 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9720 {
9721 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9722 vim_free(tofree);
9723 }
9724 else
9725 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009726 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009727 ga_concat(gap, (char_u *)"Pattern ");
9728 else
9729 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009730 if (exp_str == NULL)
9731 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009732 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009733 vim_free(tofree);
9734 }
9735 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009736 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009737 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009738 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009739 else if (atype == ASSERT_NOTMATCH)
9740 ga_concat(gap, (char_u *)" does match ");
9741 else if (atype == ASSERT_NOTEQUAL)
9742 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009743 else
9744 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009745 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009746 vim_free(tofree);
9747 }
9748}
Bram Moolenaar43345542015-11-29 17:35:35 +01009749
9750/*
9751 * Add an assert error to v:errors.
9752 */
9753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009754assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009755{
9756 struct vimvar *vp = &vimvars[VV_ERRORS];
9757
9758 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9759 /* Make sure v:errors is a list. */
9760 set_vim_var_list(VV_ERRORS, list_alloc());
9761 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9762}
9763
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009764 static void
9765assert_equal_common(typval_T *argvars, assert_type_T atype)
9766{
9767 garray_T ga;
9768
9769 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9770 != (atype == ASSERT_EQUAL))
9771 {
9772 prepare_assert_error(&ga);
9773 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9774 atype);
9775 assert_error(&ga);
9776 ga_clear(&ga);
9777 }
9778}
9779
Bram Moolenaar43345542015-11-29 17:35:35 +01009780/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009781 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009782 */
9783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009784f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009785{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009786 assert_equal_common(argvars, ASSERT_EQUAL);
9787}
Bram Moolenaar43345542015-11-29 17:35:35 +01009788
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009789/*
9790 * "assert_notequal(expected, actual[, msg])" function
9791 */
9792 static void
9793f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9794{
9795 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009796}
9797
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009798/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009799 * "assert_exception(string[, msg])" function
9800 */
9801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009802f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009803{
9804 garray_T ga;
9805 char *error;
9806
9807 error = (char *)get_tv_string_chk(&argvars[0]);
9808 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9809 {
9810 prepare_assert_error(&ga);
9811 ga_concat(&ga, (char_u *)"v:exception is not set");
9812 assert_error(&ga);
9813 ga_clear(&ga);
9814 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009815 else if (error != NULL
9816 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009817 {
9818 prepare_assert_error(&ga);
9819 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009820 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009821 assert_error(&ga);
9822 ga_clear(&ga);
9823 }
9824}
9825
9826/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009827 * "assert_fails(cmd [, error])" function
9828 */
9829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009830f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009831{
9832 char_u *cmd = get_tv_string_chk(&argvars[0]);
9833 garray_T ga;
9834
9835 called_emsg = FALSE;
9836 suppress_errthrow = TRUE;
9837 emsg_silent = TRUE;
9838 do_cmdline_cmd(cmd);
9839 if (!called_emsg)
9840 {
9841 prepare_assert_error(&ga);
9842 ga_concat(&ga, (char_u *)"command did not fail: ");
9843 ga_concat(&ga, cmd);
9844 assert_error(&ga);
9845 ga_clear(&ga);
9846 }
9847 else if (argvars[1].v_type != VAR_UNKNOWN)
9848 {
9849 char_u buf[NUMBUFLEN];
9850 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9851
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009852 if (error == NULL
9853 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009854 {
9855 prepare_assert_error(&ga);
9856 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009857 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009858 assert_error(&ga);
9859 ga_clear(&ga);
9860 }
9861 }
9862
9863 called_emsg = FALSE;
9864 suppress_errthrow = FALSE;
9865 emsg_silent = FALSE;
9866 emsg_on_display = FALSE;
9867 set_vim_var_string(VV_ERRMSG, NULL, 0);
9868}
9869
9870/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009871 * Common for assert_true() and assert_false().
9872 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009874assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009875{
9876 int error = FALSE;
9877 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009878
Bram Moolenaar37127922016-02-06 20:29:28 +01009879 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009880 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009881 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009882 if (argvars[0].v_type != VAR_NUMBER
9883 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9884 || error)
9885 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009886 prepare_assert_error(&ga);
9887 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009888 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009889 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009890 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009891 ga_clear(&ga);
9892 }
9893}
9894
9895/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009896 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009897 */
9898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009899f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009900{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009901 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009902}
9903
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009904 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009905assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009906{
9907 garray_T ga;
9908 char_u buf1[NUMBUFLEN];
9909 char_u buf2[NUMBUFLEN];
9910 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9911 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9912
Bram Moolenaar72188e92016-03-28 22:48:29 +02009913 if (pat == NULL || text == NULL)
9914 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009915 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009916 {
9917 prepare_assert_error(&ga);
9918 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009919 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009920 assert_error(&ga);
9921 ga_clear(&ga);
9922 }
9923}
9924
9925/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009926 * "assert_match(pattern, actual[, msg])" function
9927 */
9928 static void
9929f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9930{
9931 assert_match_common(argvars, ASSERT_MATCH);
9932}
9933
9934/*
9935 * "assert_notmatch(pattern, actual[, msg])" function
9936 */
9937 static void
9938f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9939{
9940 assert_match_common(argvars, ASSERT_NOTMATCH);
9941}
9942
9943/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009944 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009945 */
9946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009947f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009948{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009949 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009950}
9951
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009952#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009953/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009954 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009955 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009957f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009958{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009959 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009960
9961 rettv->v_type = VAR_FLOAT;
9962 if (get_float_arg(argvars, &f) == OK)
9963 rettv->vval.v_float = asin(f);
9964 else
9965 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009966}
9967
9968/*
9969 * "atan()" function
9970 */
9971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009972f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009973{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009974 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009975
9976 rettv->v_type = VAR_FLOAT;
9977 if (get_float_arg(argvars, &f) == OK)
9978 rettv->vval.v_float = atan(f);
9979 else
9980 rettv->vval.v_float = 0.0;
9981}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009982
9983/*
9984 * "atan2()" function
9985 */
9986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009987f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009988{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009989 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009990
9991 rettv->v_type = VAR_FLOAT;
9992 if (get_float_arg(argvars, &fx) == OK
9993 && get_float_arg(&argvars[1], &fy) == OK)
9994 rettv->vval.v_float = atan2(fx, fy);
9995 else
9996 rettv->vval.v_float = 0.0;
9997}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009998#endif
9999
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000/*
10001 * "browse(save, title, initdir, default)" function
10002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010004f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
10006#ifdef FEAT_BROWSE
10007 int save;
10008 char_u *title;
10009 char_u *initdir;
10010 char_u *defname;
10011 char_u buf[NUMBUFLEN];
10012 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010013 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 save = get_tv_number_chk(&argvars[0], &error);
10016 title = get_tv_string_chk(&argvars[1]);
10017 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10018 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010020 if (error || title == NULL || initdir == NULL || defname == NULL)
10021 rettv->vval.v_string = NULL;
10022 else
10023 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010024 do_browse(save ? BROWSE_SAVE : 0,
10025 title, defname, NULL, initdir, NULL, curbuf);
10026#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010028#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010030}
10031
10032/*
10033 * "browsedir(title, initdir)" function
10034 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010036f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010037{
10038#ifdef FEAT_BROWSE
10039 char_u *title;
10040 char_u *initdir;
10041 char_u buf[NUMBUFLEN];
10042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010043 title = get_tv_string_chk(&argvars[0]);
10044 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010046 if (title == NULL || initdir == NULL)
10047 rettv->vval.v_string = NULL;
10048 else
10049 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010050 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055}
10056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010057static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010058
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059/*
10060 * Find a buffer by number or exact name.
10061 */
10062 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010063find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
10065 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010067 if (avar->v_type == VAR_NUMBER)
10068 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010069 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010071 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010072 if (buf == NULL)
10073 {
10074 /* No full path name match, try a match with a URL or a "nofile"
10075 * buffer, these don't use the full path. */
10076 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10077 if (buf->b_fname != NULL
10078 && (path_with_url(buf->b_fname)
10079#ifdef FEAT_QUICKFIX
10080 || bt_nofile(buf)
10081#endif
10082 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010083 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010084 break;
10085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 }
10087 return buf;
10088}
10089
10090/*
10091 * "bufexists(expr)" function
10092 */
10093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010094f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097}
10098
10099/*
10100 * "buflisted(expr)" function
10101 */
10102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010103f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104{
10105 buf_T *buf;
10106
10107 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109}
10110
10111/*
10112 * "bufloaded(expr)" function
10113 */
10114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010115f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116{
10117 buf_T *buf;
10118
10119 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121}
10122
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010123 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010124buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 int save_magic;
10127 char_u *save_cpo;
10128 buf_T *buf;
10129
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10131 save_magic = p_magic;
10132 p_magic = TRUE;
10133 save_cpo = p_cpo;
10134 p_cpo = (char_u *)"";
10135
10136 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010137 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138
10139 p_magic = save_magic;
10140 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010141 return buf;
10142}
10143
10144/*
10145 * Get buffer by number or pattern.
10146 */
10147 static buf_T *
10148get_buf_tv(typval_T *tv, int curtab_only)
10149{
10150 char_u *name = tv->vval.v_string;
10151 buf_T *buf;
10152
10153 if (tv->v_type == VAR_NUMBER)
10154 return buflist_findnr((int)tv->vval.v_number);
10155 if (tv->v_type != VAR_STRING)
10156 return NULL;
10157 if (name == NULL || *name == NUL)
10158 return curbuf;
10159 if (name[0] == '$' && name[1] == NUL)
10160 return lastbuf;
10161
10162 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163
10164 /* If not found, try expanding the name, like done for bufexists(). */
10165 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010166 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167
10168 return buf;
10169}
10170
10171/*
10172 * "bufname(expr)" function
10173 */
10174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010175f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176{
10177 buf_T *buf;
10178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010181 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 --emsg_off;
10188}
10189
10190/*
10191 * "bufnr(expr)" function
10192 */
10193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010194f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195{
10196 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010197 int error = FALSE;
10198 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010200 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010202 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010203 --emsg_off;
10204
10205 /* If the buffer isn't found and the second argument is not zero create a
10206 * new buffer. */
10207 if (buf == NULL
10208 && argvars[1].v_type != VAR_UNKNOWN
10209 && get_tv_number_chk(&argvars[1], &error) != 0
10210 && !error
10211 && (name = get_tv_string_chk(&argvars[0])) != NULL
10212 && !error)
10213 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10214
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010218 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219}
10220
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010222buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223{
10224#ifdef FEAT_WINDOWS
10225 win_T *wp;
10226 int winnr = 0;
10227#endif
10228 buf_T *buf;
10229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010232 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233#ifdef FEAT_WINDOWS
10234 for (wp = firstwin; wp; wp = wp->w_next)
10235 {
10236 ++winnr;
10237 if (wp->w_buffer == buf)
10238 break;
10239 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010240 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010242 rettv->vval.v_number = (curwin->w_buffer == buf
10243 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244#endif
10245 --emsg_off;
10246}
10247
10248/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010249 * "bufwinid(nr)" function
10250 */
10251 static void
10252f_bufwinid(typval_T *argvars, typval_T *rettv)
10253{
10254 buf_win_common(argvars, rettv, FALSE);
10255}
10256
10257/*
10258 * "bufwinnr(nr)" function
10259 */
10260 static void
10261f_bufwinnr(typval_T *argvars, typval_T *rettv)
10262{
10263 buf_win_common(argvars, rettv, TRUE);
10264}
10265
10266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 * "byte2line(byte)" function
10268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010270f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271{
10272#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274#else
10275 long boff = 0;
10276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010279 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010281 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 (linenr_T)0, &boff);
10283#endif
10284}
10285
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010287byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010288{
10289#ifdef FEAT_MBYTE
10290 char_u *t;
10291#endif
10292 char_u *str;
10293 long idx;
10294
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010295 str = get_tv_string_chk(&argvars[0]);
10296 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010297 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010298 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010299 return;
10300
10301#ifdef FEAT_MBYTE
10302 t = str;
10303 for ( ; idx > 0; idx--)
10304 {
10305 if (*t == NUL) /* EOL reached */
10306 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010307 if (enc_utf8 && comp)
10308 t += utf_ptr2len(t);
10309 else
10310 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010311 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010312 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010313#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010314 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010316#endif
10317}
10318
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010319/*
10320 * "byteidx()" function
10321 */
10322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010323f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010324{
10325 byteidx(argvars, rettv, FALSE);
10326}
10327
10328/*
10329 * "byteidxcomp()" function
10330 */
10331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010332f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010333{
10334 byteidx(argvars, rettv, TRUE);
10335}
10336
Bram Moolenaardb913952012-06-29 12:54:53 +020010337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010338func_call(
10339 char_u *name,
10340 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010341 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010342 dict_T *selfdict,
10343 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010344{
10345 listitem_T *item;
10346 typval_T argv[MAX_FUNC_ARGS + 1];
10347 int argc = 0;
10348 int dummy;
10349 int r = 0;
10350
10351 for (item = args->vval.v_list->lv_first; item != NULL;
10352 item = item->li_next)
10353 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010354 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010355 {
10356 EMSG(_("E699: Too many arguments"));
10357 break;
10358 }
10359 /* Make a copy of each argument. This is needed to be able to set
10360 * v_lock to VAR_FIXED in the copy without changing the original list.
10361 */
10362 copy_tv(&item->li_tv, &argv[argc++]);
10363 }
10364
10365 if (item == NULL)
10366 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10367 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010368 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010369
10370 /* Free the arguments. */
10371 while (argc > 0)
10372 clear_tv(&argv[--argc]);
10373
10374 return r;
10375}
10376
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010377/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010378 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010379 */
10380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010381f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010382{
10383 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010384 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010385 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010386
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010387 if (argvars[1].v_type != VAR_LIST)
10388 {
10389 EMSG(_(e_listreq));
10390 return;
10391 }
10392 if (argvars[1].vval.v_list == NULL)
10393 return;
10394
10395 if (argvars[0].v_type == VAR_FUNC)
10396 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010397 else if (argvars[0].v_type == VAR_PARTIAL)
10398 {
10399 partial = argvars[0].vval.v_partial;
10400 func = partial->pt_name;
10401 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010402 else
10403 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010404 if (*func == NUL)
10405 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010406
Bram Moolenaare9a41262005-01-15 22:18:47 +000010407 if (argvars[2].v_type != VAR_UNKNOWN)
10408 {
10409 if (argvars[2].v_type != VAR_DICT)
10410 {
10411 EMSG(_(e_dictreq));
10412 return;
10413 }
10414 selfdict = argvars[2].vval.v_dict;
10415 }
10416
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010417 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010418}
10419
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010420#ifdef FEAT_FLOAT
10421/*
10422 * "ceil({float})" function
10423 */
10424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010425f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010426{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010427 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010428
10429 rettv->v_type = VAR_FLOAT;
10430 if (get_float_arg(argvars, &f) == OK)
10431 rettv->vval.v_float = ceil(f);
10432 else
10433 rettv->vval.v_float = 0.0;
10434}
10435#endif
10436
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010437#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010438/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010439 * "ch_close()" function
10440 */
10441 static void
10442f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10443{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010444 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010445
10446 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010447 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010448 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010449 channel_clear(channel);
10450 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010451}
10452
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010453/*
10454 * "ch_getbufnr()" function
10455 */
10456 static void
10457f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10458{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010459 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010460
10461 rettv->vval.v_number = -1;
10462 if (channel != NULL)
10463 {
10464 char_u *what = get_tv_string(&argvars[1]);
10465 int part;
10466
10467 if (STRCMP(what, "err") == 0)
10468 part = PART_ERR;
10469 else if (STRCMP(what, "out") == 0)
10470 part = PART_OUT;
10471 else if (STRCMP(what, "in") == 0)
10472 part = PART_IN;
10473 else
10474 part = PART_SOCK;
10475 if (channel->ch_part[part].ch_buffer != NULL)
10476 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10477 }
10478}
10479
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010480/*
10481 * "ch_getjob()" function
10482 */
10483 static void
10484f_ch_getjob(typval_T *argvars, typval_T *rettv)
10485{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010486 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010487
10488 if (channel != NULL)
10489 {
10490 rettv->v_type = VAR_JOB;
10491 rettv->vval.v_job = channel->ch_job;
10492 if (channel->ch_job != NULL)
10493 ++channel->ch_job->jv_refcount;
10494 }
10495}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010496
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010497/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010498 * "ch_info()" function
10499 */
10500 static void
10501f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10502{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010503 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010504
10505 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10506 channel_info(channel, rettv->vval.v_dict);
10507}
10508
10509/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010510 * "ch_log()" function
10511 */
10512 static void
10513f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10514{
10515 char_u *msg = get_tv_string(&argvars[0]);
10516 channel_T *channel = NULL;
10517
10518 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010519 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010520
10521 ch_log(channel, (char *)msg);
10522}
10523
10524/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010525 * "ch_logfile()" function
10526 */
10527 static void
10528f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10529{
10530 char_u *fname;
10531 char_u *opt = (char_u *)"";
10532 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010533
10534 fname = get_tv_string(&argvars[0]);
10535 if (argvars[1].v_type == VAR_STRING)
10536 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010537 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010538}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010539
10540/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010541 * "ch_open()" function
10542 */
10543 static void
10544f_ch_open(typval_T *argvars, typval_T *rettv)
10545{
Bram Moolenaar77073442016-02-13 23:23:53 +010010546 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010547 if (check_restricted() || check_secure())
10548 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010549 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010550}
10551
10552/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010553 * "ch_read()" function
10554 */
10555 static void
10556f_ch_read(typval_T *argvars, typval_T *rettv)
10557{
10558 common_channel_read(argvars, rettv, FALSE);
10559}
10560
10561/*
10562 * "ch_readraw()" function
10563 */
10564 static void
10565f_ch_readraw(typval_T *argvars, typval_T *rettv)
10566{
10567 common_channel_read(argvars, rettv, TRUE);
10568}
10569
10570/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010571 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010572 */
10573 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010574f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10575{
10576 ch_expr_common(argvars, rettv, TRUE);
10577}
10578
10579/*
10580 * "ch_sendexpr()" function
10581 */
10582 static void
10583f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10584{
10585 ch_expr_common(argvars, rettv, FALSE);
10586}
10587
10588/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010589 * "ch_evalraw()" function
10590 */
10591 static void
10592f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10593{
10594 ch_raw_common(argvars, rettv, TRUE);
10595}
10596
10597/*
10598 * "ch_sendraw()" function
10599 */
10600 static void
10601f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10602{
10603 ch_raw_common(argvars, rettv, FALSE);
10604}
10605
10606/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010607 * "ch_setoptions()" function
10608 */
10609 static void
10610f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10611{
10612 channel_T *channel;
10613 jobopt_T opt;
10614
Bram Moolenaar437905c2016-04-26 19:01:05 +020010615 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010616 if (channel == NULL)
10617 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010618 clear_job_options(&opt);
10619 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010620 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10621 channel_set_options(channel, &opt);
10622 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010623}
10624
10625/*
10626 * "ch_status()" function
10627 */
10628 static void
10629f_ch_status(typval_T *argvars, typval_T *rettv)
10630{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010631 channel_T *channel;
10632
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010633 /* return an empty string by default */
10634 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010635 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010636
Bram Moolenaar437905c2016-04-26 19:01:05 +020010637 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010638 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010639}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010640#endif
10641
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010642/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010643 * "changenr()" function
10644 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010646f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010647{
10648 rettv->vval.v_number = curbuf->b_u_seq_cur;
10649}
10650
10651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 * "char2nr(string)" function
10653 */
10654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010655f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656{
10657#ifdef FEAT_MBYTE
10658 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010659 {
10660 int utf8 = 0;
10661
10662 if (argvars[1].v_type != VAR_UNKNOWN)
10663 utf8 = get_tv_number_chk(&argvars[1], NULL);
10664
10665 if (utf8)
10666 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10667 else
10668 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 else
10671#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010672 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673}
10674
10675/*
10676 * "cindent(lnum)" function
10677 */
10678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010679f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680{
10681#ifdef FEAT_CINDENT
10682 pos_T pos;
10683 linenr_T lnum;
10684
10685 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10688 {
10689 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 curwin->w_cursor = pos;
10692 }
10693 else
10694#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696}
10697
10698/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010699 * "clearmatches()" function
10700 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010702f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010703{
10704#ifdef FEAT_SEARCH_EXTRA
10705 clear_matches(curwin);
10706#endif
10707}
10708
10709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 * "col(string)" function
10711 */
10712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010713f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
10715 colnr_T col = 0;
10716 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010717 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010719 fp = var2fpos(&argvars[0], FALSE, &fnum);
10720 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 {
10722 if (fp->col == MAXCOL)
10723 {
10724 /* '> can be MAXCOL, get the length of the line then */
10725 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010726 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 else
10728 col = MAXCOL;
10729 }
10730 else
10731 {
10732 col = fp->col + 1;
10733#ifdef FEAT_VIRTUALEDIT
10734 /* col(".") when the cursor is on the NUL at the end of the line
10735 * because of "coladd" can be seen as an extra column. */
10736 if (virtual_active() && fp == &curwin->w_cursor)
10737 {
10738 char_u *p = ml_get_cursor();
10739
10740 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10741 curwin->w_virtcol - curwin->w_cursor.coladd))
10742 {
10743# ifdef FEAT_MBYTE
10744 int l;
10745
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010746 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 col += l;
10748# else
10749 if (*p != NUL && p[1] == NUL)
10750 ++col;
10751# endif
10752 }
10753 }
10754#endif
10755 }
10756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758}
10759
Bram Moolenaar572cb562005-08-05 21:35:02 +000010760#if defined(FEAT_INS_EXPAND)
10761/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010762 * "complete()" function
10763 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010765f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010766{
10767 int startcol;
10768
10769 if ((State & INSERT) == 0)
10770 {
10771 EMSG(_("E785: complete() can only be used in Insert mode"));
10772 return;
10773 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010774
10775 /* Check for undo allowed here, because if something was already inserted
10776 * the line was already saved for undo and this check isn't done. */
10777 if (!undo_allowed())
10778 return;
10779
Bram Moolenaarade00832006-03-10 21:46:58 +000010780 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10781 {
10782 EMSG(_(e_invarg));
10783 return;
10784 }
10785
10786 startcol = get_tv_number_chk(&argvars[0], NULL);
10787 if (startcol <= 0)
10788 return;
10789
10790 set_completion(startcol - 1, argvars[1].vval.v_list);
10791}
10792
10793/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010794 * "complete_add()" function
10795 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010797f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010798{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010799 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010800}
10801
10802/*
10803 * "complete_check()" function
10804 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010806f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010807{
10808 int saved = RedrawingDisabled;
10809
10810 RedrawingDisabled = 0;
10811 ins_compl_check_keys(0);
10812 rettv->vval.v_number = compl_interrupted;
10813 RedrawingDisabled = saved;
10814}
10815#endif
10816
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817/*
10818 * "confirm(message, buttons[, default [, type]])" function
10819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010821f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822{
10823#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10824 char_u *message;
10825 char_u *buttons = NULL;
10826 char_u buf[NUMBUFLEN];
10827 char_u buf2[NUMBUFLEN];
10828 int def = 1;
10829 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010830 char_u *typestr;
10831 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010833 message = get_tv_string_chk(&argvars[0]);
10834 if (message == NULL)
10835 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010836 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010838 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10839 if (buttons == NULL)
10840 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010841 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010843 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010844 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010846 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10847 if (typestr == NULL)
10848 error = TRUE;
10849 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 switch (TOUPPER_ASC(*typestr))
10852 {
10853 case 'E': type = VIM_ERROR; break;
10854 case 'Q': type = VIM_QUESTION; break;
10855 case 'I': type = VIM_INFO; break;
10856 case 'W': type = VIM_WARNING; break;
10857 case 'G': type = VIM_GENERIC; break;
10858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 }
10860 }
10861 }
10862 }
10863
10864 if (buttons == NULL || *buttons == NUL)
10865 buttons = (char_u *)_("&Ok");
10866
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010867 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010869 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870#endif
10871}
10872
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010873/*
10874 * "copy()" function
10875 */
10876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010877f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010878{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010879 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010880}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010882#ifdef FEAT_FLOAT
10883/*
10884 * "cos()" function
10885 */
10886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010887f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010888{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010889 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010890
10891 rettv->v_type = VAR_FLOAT;
10892 if (get_float_arg(argvars, &f) == OK)
10893 rettv->vval.v_float = cos(f);
10894 else
10895 rettv->vval.v_float = 0.0;
10896}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010897
10898/*
10899 * "cosh()" function
10900 */
10901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010902f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010903{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010904 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010905
10906 rettv->v_type = VAR_FLOAT;
10907 if (get_float_arg(argvars, &f) == OK)
10908 rettv->vval.v_float = cosh(f);
10909 else
10910 rettv->vval.v_float = 0.0;
10911}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010912#endif
10913
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010915 * "count()" function
10916 */
10917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010918f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010919{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010920 long n = 0;
10921 int ic = FALSE;
10922
Bram Moolenaare9a41262005-01-15 22:18:47 +000010923 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010924 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010925 listitem_T *li;
10926 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010927 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010928
Bram Moolenaare9a41262005-01-15 22:18:47 +000010929 if ((l = argvars[0].vval.v_list) != NULL)
10930 {
10931 li = l->lv_first;
10932 if (argvars[2].v_type != VAR_UNKNOWN)
10933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010934 int error = FALSE;
10935
10936 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 if (argvars[3].v_type != VAR_UNKNOWN)
10938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 idx = get_tv_number_chk(&argvars[3], &error);
10940 if (!error)
10941 {
10942 li = list_find(l, idx);
10943 if (li == NULL)
10944 EMSGN(_(e_listidx), idx);
10945 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010947 if (error)
10948 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010949 }
10950
10951 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010952 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010953 ++n;
10954 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010955 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956 else if (argvars[0].v_type == VAR_DICT)
10957 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010958 int todo;
10959 dict_T *d;
10960 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010962 if ((d = argvars[0].vval.v_dict) != NULL)
10963 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010964 int error = FALSE;
10965
Bram Moolenaare9a41262005-01-15 22:18:47 +000010966 if (argvars[2].v_type != VAR_UNKNOWN)
10967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010968 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010969 if (argvars[3].v_type != VAR_UNKNOWN)
10970 EMSG(_(e_invarg));
10971 }
10972
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010973 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010974 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010975 {
10976 if (!HASHITEM_EMPTY(hi))
10977 {
10978 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010979 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010980 ++n;
10981 }
10982 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010983 }
10984 }
10985 else
10986 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010987 rettv->vval.v_number = n;
10988}
10989
10990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10992 *
10993 * Checks the existence of a cscope connection.
10994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010996f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997{
10998#ifdef FEAT_CSCOPE
10999 int num = 0;
11000 char_u *dbpath = NULL;
11001 char_u *prepend = NULL;
11002 char_u buf[NUMBUFLEN];
11003
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011004 if (argvars[0].v_type != VAR_UNKNOWN
11005 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 num = (int)get_tv_number(&argvars[0]);
11008 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011009 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 }
11012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014#endif
11015}
11016
11017/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011018 * "cursor(lnum, col)" function, or
11019 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011021 * Moves the cursor to the specified line and column.
11022 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011025f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026{
11027 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011028#ifdef FEAT_VIRTUALEDIT
11029 long coladd = 0;
11030#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011031 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011033 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011034 if (argvars[1].v_type == VAR_UNKNOWN)
11035 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011036 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011037 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011038
Bram Moolenaar493c1782014-05-28 14:34:46 +020011039 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011040 {
11041 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011042 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011043 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011044 line = pos.lnum;
11045 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011046#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011047 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011048#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011049 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011050 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011051 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011052 set_curswant = FALSE;
11053 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011054 }
11055 else
11056 {
11057 line = get_tv_lnum(argvars);
11058 col = get_tv_number_chk(&argvars[1], NULL);
11059#ifdef FEAT_VIRTUALEDIT
11060 if (argvars[2].v_type != VAR_UNKNOWN)
11061 coladd = get_tv_number_chk(&argvars[2], NULL);
11062#endif
11063 }
11064 if (line < 0 || col < 0
11065#ifdef FEAT_VIRTUALEDIT
11066 || coladd < 0
11067#endif
11068 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011069 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 if (line > 0)
11071 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 if (col > 0)
11073 curwin->w_cursor.col = col - 1;
11074#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011075 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076#endif
11077
11078 /* Make sure the cursor is in a valid position. */
11079 check_cursor();
11080#ifdef FEAT_MBYTE
11081 /* Correct cursor for multi-byte character. */
11082 if (has_mbyte)
11083 mb_adjust_cursor();
11084#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011085
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011086 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011087 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088}
11089
11090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011091 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 */
11093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011094f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011096 int noref = 0;
11097
11098 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011099 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011100 if (noref < 0 || noref > 1)
11101 EMSG(_(e_invarg));
11102 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011103 {
11104 current_copyID += COPYID_INC;
11105 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107}
11108
11109/*
11110 * "delete()" function
11111 */
11112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011113f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011115 char_u nbuf[NUMBUFLEN];
11116 char_u *name;
11117 char_u *flags;
11118
11119 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011121 return;
11122
11123 name = get_tv_string(&argvars[0]);
11124 if (name == NULL || *name == NUL)
11125 {
11126 EMSG(_(e_invarg));
11127 return;
11128 }
11129
11130 if (argvars[1].v_type != VAR_UNKNOWN)
11131 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011133 flags = (char_u *)"";
11134
11135 if (*flags == NUL)
11136 /* delete a file */
11137 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11138 else if (STRCMP(flags, "d") == 0)
11139 /* delete an empty directory */
11140 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11141 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011142 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011143 rettv->vval.v_number = delete_recursive(name);
11144 else
11145 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146}
11147
11148/*
11149 * "did_filetype()" function
11150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011152f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153{
11154#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156#endif
11157}
11158
11159/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011160 * "diff_filler()" function
11161 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011163f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011164{
11165#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011166 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011167#endif
11168}
11169
11170/*
11171 * "diff_hlID()" function
11172 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011174f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011175{
11176#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011178 static linenr_T prev_lnum = 0;
11179 static int changedtick = 0;
11180 static int fnum = 0;
11181 static int change_start = 0;
11182 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011183 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011184 int filler_lines;
11185 int col;
11186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 if (lnum < 0) /* ignore type error in {lnum} arg */
11188 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011189 if (lnum != prev_lnum
11190 || changedtick != curbuf->b_changedtick
11191 || fnum != curbuf->b_fnum)
11192 {
11193 /* New line, buffer, change: need to get the values. */
11194 filler_lines = diff_check(curwin, lnum);
11195 if (filler_lines < 0)
11196 {
11197 if (filler_lines == -1)
11198 {
11199 change_start = MAXCOL;
11200 change_end = -1;
11201 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11202 hlID = HLF_ADD; /* added line */
11203 else
11204 hlID = HLF_CHD; /* changed line */
11205 }
11206 else
11207 hlID = HLF_ADD; /* added line */
11208 }
11209 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011210 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011211 prev_lnum = lnum;
11212 changedtick = curbuf->b_changedtick;
11213 fnum = curbuf->b_fnum;
11214 }
11215
11216 if (hlID == HLF_CHD || hlID == HLF_TXD)
11217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011219 if (col >= change_start && col <= change_end)
11220 hlID = HLF_TXD; /* changed text */
11221 else
11222 hlID = HLF_CHD; /* changed line */
11223 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011224 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011225#endif
11226}
11227
11228/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011229 * "empty({expr})" function
11230 */
11231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011232f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011233{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011234 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011235
11236 switch (argvars[0].v_type)
11237 {
11238 case VAR_STRING:
11239 case VAR_FUNC:
11240 n = argvars[0].vval.v_string == NULL
11241 || *argvars[0].vval.v_string == NUL;
11242 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011243 case VAR_PARTIAL:
11244 n = FALSE;
11245 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011246 case VAR_NUMBER:
11247 n = argvars[0].vval.v_number == 0;
11248 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011249 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011250#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011251 n = argvars[0].vval.v_float == 0.0;
11252 break;
11253#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011254 case VAR_LIST:
11255 n = argvars[0].vval.v_list == NULL
11256 || argvars[0].vval.v_list->lv_first == NULL;
11257 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011258 case VAR_DICT:
11259 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011261 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011262 case VAR_SPECIAL:
11263 n = argvars[0].vval.v_number != VVAL_TRUE;
11264 break;
11265
Bram Moolenaar835dc632016-02-07 14:27:38 +010011266 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011267#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011268 n = argvars[0].vval.v_job == NULL
11269 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11270 break;
11271#endif
11272 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011273#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011274 n = argvars[0].vval.v_channel == NULL
11275 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011276 break;
11277#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011278 case VAR_UNKNOWN:
11279 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11280 n = TRUE;
11281 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011282 }
11283
11284 rettv->vval.v_number = n;
11285}
11286
11287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 * "escape({string}, {chars})" function
11289 */
11290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011291f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292{
11293 char_u buf[NUMBUFLEN];
11294
Bram Moolenaar758711c2005-02-02 23:11:38 +000011295 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11296 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298}
11299
11300/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011301 * "eval()" function
11302 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011304f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011305{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011306 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 s = get_tv_string_chk(&argvars[0]);
11309 if (s != NULL)
11310 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011311
Bram Moolenaar615b9972015-01-14 17:15:05 +010011312 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011313 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11314 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011315 if (p != NULL && !aborting())
11316 EMSG2(_(e_invexpr2), p);
11317 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011319 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011321 else if (*s != NUL)
11322 EMSG(_(e_trailing));
11323}
11324
11325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 * "eventhandler()" function
11327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011329f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332}
11333
11334/*
11335 * "executable()" function
11336 */
11337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011338f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011340 char_u *name = get_tv_string(&argvars[0]);
11341
11342 /* Check in $PATH and also check directly if there is a directory name. */
11343 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11344 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011345}
11346
11347/*
11348 * "exepath()" function
11349 */
11350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011351f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011352{
11353 char_u *p = NULL;
11354
Bram Moolenaarb5971142015-03-21 17:32:19 +010011355 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011356 rettv->v_type = VAR_STRING;
11357 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358}
11359
11360/*
11361 * "exists()" function
11362 */
11363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011364f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365{
11366 char_u *p;
11367 char_u *name;
11368 int n = FALSE;
11369 int len = 0;
11370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 if (*p == '$') /* environment variable */
11373 {
11374 /* first try "normal" environment variables (fast) */
11375 if (mch_getenv(p + 1) != NULL)
11376 n = TRUE;
11377 else
11378 {
11379 /* try expanding things like $VIM and ${HOME} */
11380 p = expand_env_save(p);
11381 if (p != NULL && *p != '$')
11382 n = TRUE;
11383 vim_free(p);
11384 }
11385 }
11386 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011389 if (*skipwhite(p) != NUL)
11390 n = FALSE; /* trailing garbage */
11391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 else if (*p == '*') /* internal or user defined function */
11393 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011394 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 }
11396 else if (*p == ':')
11397 {
11398 n = cmd_exists(p + 1);
11399 }
11400 else if (*p == '#')
11401 {
11402#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011403 if (p[1] == '#')
11404 n = autocmd_supported(p + 2);
11405 else
11406 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407#endif
11408 }
11409 else /* internal variable */
11410 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011411 char_u *tofree;
11412 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011414 /* get_name_len() takes care of expanding curly braces */
11415 name = p;
11416 len = get_name_len(&p, &tofree, TRUE, FALSE);
11417 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011419 if (tofree != NULL)
11420 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011421 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011422 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011424 /* handle d.key, l[idx], f(expr) */
11425 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11426 if (n)
11427 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 }
11429 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011430 if (*p != NUL)
11431 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011433 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 }
11435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437}
11438
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011439#ifdef FEAT_FLOAT
11440/*
11441 * "exp()" function
11442 */
11443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011444f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011445{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011446 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011447
11448 rettv->v_type = VAR_FLOAT;
11449 if (get_float_arg(argvars, &f) == OK)
11450 rettv->vval.v_float = exp(f);
11451 else
11452 rettv->vval.v_float = 0.0;
11453}
11454#endif
11455
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456/*
11457 * "expand()" function
11458 */
11459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011460f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461{
11462 char_u *s;
11463 int len;
11464 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011465 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011467 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011468 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011471 if (argvars[1].v_type != VAR_UNKNOWN
11472 && argvars[2].v_type != VAR_UNKNOWN
11473 && get_tv_number_chk(&argvars[2], &error)
11474 && !error)
11475 {
11476 rettv->v_type = VAR_LIST;
11477 rettv->vval.v_list = NULL;
11478 }
11479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481 if (*s == '%' || *s == '#' || *s == '<')
11482 {
11483 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011484 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011486 if (rettv->v_type == VAR_LIST)
11487 {
11488 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11489 list_append_string(rettv->vval.v_list, result, -1);
11490 else
11491 vim_free(result);
11492 }
11493 else
11494 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 }
11496 else
11497 {
11498 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011499 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 if (argvars[1].v_type != VAR_UNKNOWN
11501 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011502 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011503 if (!error)
11504 {
11505 ExpandInit(&xpc);
11506 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011507 if (p_wic)
11508 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011509 if (rettv->v_type == VAR_STRING)
11510 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11511 options, WILD_ALL);
11512 else if (rettv_list_alloc(rettv) != FAIL)
11513 {
11514 int i;
11515
11516 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11517 for (i = 0; i < xpc.xp_numfiles; i++)
11518 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11519 ExpandCleanup(&xpc);
11520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011521 }
11522 else
11523 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 }
11525}
11526
11527/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011528 * Go over all entries in "d2" and add them to "d1".
11529 * When "action" is "error" then a duplicate key is an error.
11530 * When "action" is "force" then a duplicate key is overwritten.
11531 * Otherwise duplicate keys are ignored ("action" is "keep").
11532 */
11533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011534dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011535{
11536 dictitem_T *di1;
11537 hashitem_T *hi2;
11538 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011539 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011540
11541 todo = (int)d2->dv_hashtab.ht_used;
11542 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11543 {
11544 if (!HASHITEM_EMPTY(hi2))
11545 {
11546 --todo;
11547 di1 = dict_find(d1, hi2->hi_key, -1);
11548 if (d1->dv_scope != 0)
11549 {
11550 /* Disallow replacing a builtin function in l: and g:.
11551 * Check the key to be valid when adding to any
11552 * scope. */
11553 if (d1->dv_scope == VAR_DEF_SCOPE
11554 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11555 && var_check_func_name(hi2->hi_key,
11556 di1 == NULL))
11557 break;
11558 if (!valid_varname(hi2->hi_key))
11559 break;
11560 }
11561 if (di1 == NULL)
11562 {
11563 di1 = dictitem_copy(HI2DI(hi2));
11564 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11565 dictitem_free(di1);
11566 }
11567 else if (*action == 'e')
11568 {
11569 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11570 break;
11571 }
11572 else if (*action == 'f' && HI2DI(hi2) != di1)
11573 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011574 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11575 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011576 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011577 clear_tv(&di1->di_tv);
11578 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11579 }
11580 }
11581 }
11582}
11583
11584/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011585 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011586 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011587 */
11588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011589f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011590{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011591 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011592
Bram Moolenaare9a41262005-01-15 22:18:47 +000011593 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011594 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011595 list_T *l1, *l2;
11596 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011597 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011598 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011599
Bram Moolenaare9a41262005-01-15 22:18:47 +000011600 l1 = argvars[0].vval.v_list;
11601 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011602 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011603 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011604 {
11605 if (argvars[2].v_type != VAR_UNKNOWN)
11606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011607 before = get_tv_number_chk(&argvars[2], &error);
11608 if (error)
11609 return; /* type error; errmsg already given */
11610
Bram Moolenaar758711c2005-02-02 23:11:38 +000011611 if (before == l1->lv_len)
11612 item = NULL;
11613 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011614 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011615 item = list_find(l1, before);
11616 if (item == NULL)
11617 {
11618 EMSGN(_(e_listidx), before);
11619 return;
11620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011621 }
11622 }
11623 else
11624 item = NULL;
11625 list_extend(l1, l2, item);
11626
Bram Moolenaare9a41262005-01-15 22:18:47 +000011627 copy_tv(&argvars[0], rettv);
11628 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011629 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011630 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11631 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011632 dict_T *d1, *d2;
11633 char_u *action;
11634 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011635
11636 d1 = argvars[0].vval.v_dict;
11637 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011638 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011639 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011640 {
11641 /* Check the third argument. */
11642 if (argvars[2].v_type != VAR_UNKNOWN)
11643 {
11644 static char *(av[]) = {"keep", "force", "error"};
11645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011646 action = get_tv_string_chk(&argvars[2]);
11647 if (action == NULL)
11648 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011649 for (i = 0; i < 3; ++i)
11650 if (STRCMP(action, av[i]) == 0)
11651 break;
11652 if (i == 3)
11653 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011654 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011655 return;
11656 }
11657 }
11658 else
11659 action = (char_u *)"force";
11660
Bram Moolenaara9922d62013-05-30 13:01:18 +020011661 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011662
Bram Moolenaare9a41262005-01-15 22:18:47 +000011663 copy_tv(&argvars[0], rettv);
11664 }
11665 }
11666 else
11667 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011668}
11669
11670/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011671 * "feedkeys()" function
11672 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011674f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011675{
11676 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011677 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011678 char_u *keys, *flags;
11679 char_u nbuf[NUMBUFLEN];
11680 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011681 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011682 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011683 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011684
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011685 /* This is not allowed in the sandbox. If the commands would still be
11686 * executed in the sandbox it would be OK, but it probably happens later,
11687 * when "sandbox" is no longer set. */
11688 if (check_secure())
11689 return;
11690
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011691 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011692
11693 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011694 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011695 flags = get_tv_string_buf(&argvars[1], nbuf);
11696 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011697 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011698 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011699 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011700 case 'n': remap = FALSE; break;
11701 case 'm': remap = TRUE; break;
11702 case 't': typed = TRUE; break;
11703 case 'i': insert = TRUE; break;
11704 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011705 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011706 }
11707 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011708 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011709
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011710 if (*keys != NUL || execute)
11711 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011712 /* Need to escape K_SPECIAL and CSI before putting the string in the
11713 * typeahead buffer. */
11714 keys_esc = vim_strsave_escape_csi(keys);
11715 if (keys_esc != NULL)
11716 {
11717 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011718 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011719 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011720 if (vgetc_busy)
11721 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011722 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011723 {
11724 int save_msg_scroll = msg_scroll;
11725
11726 /* Avoid a 1 second delay when the keys start Insert mode. */
11727 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011728
Bram Moolenaar245c4102016-04-20 17:37:41 +020011729 if (!dangerous)
11730 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011731 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011732 if (!dangerous)
11733 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011734 msg_scroll |= save_msg_scroll;
11735 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011736 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011737 }
11738}
11739
11740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741 * "filereadable()" function
11742 */
11743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011744f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011746 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 char_u *p;
11748 int n;
11749
Bram Moolenaarc236c162008-07-13 17:41:49 +000011750#ifndef O_NONBLOCK
11751# define O_NONBLOCK 0
11752#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011754 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11755 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 {
11757 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011758 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 }
11760 else
11761 n = FALSE;
11762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764}
11765
11766/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011767 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 * rights to write into.
11769 */
11770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011771f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011773 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774}
11775
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011776 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011777findfilendir(
11778 typval_T *argvars UNUSED,
11779 typval_T *rettv,
11780 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011781{
11782#ifdef FEAT_SEARCHPATH
11783 char_u *fname;
11784 char_u *fresult = NULL;
11785 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11786 char_u *p;
11787 char_u pathbuf[NUMBUFLEN];
11788 int count = 1;
11789 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011790 int error = FALSE;
11791#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011792
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011793 rettv->vval.v_string = NULL;
11794 rettv->v_type = VAR_STRING;
11795
11796#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011798
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011799 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11802 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011803 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011804 else
11805 {
11806 if (*p != NUL)
11807 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011810 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011811 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011812 }
11813
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011814 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11815 error = TRUE;
11816
11817 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 do
11820 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011821 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011822 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 fresult = find_file_in_path_option(first ? fname : NULL,
11824 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011825 0, first, path,
11826 find_what,
11827 curbuf->b_ffname,
11828 find_what == FINDFILE_DIR
11829 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011830 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011831
11832 if (fresult != NULL && rettv->v_type == VAR_LIST)
11833 list_append_string(rettv->vval.v_list, fresult, -1);
11834
11835 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011836 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011837
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011838 if (rettv->v_type == VAR_STRING)
11839 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011840#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011841}
11842
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011843static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11844static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011845
11846/*
11847 * Implementation of map() and filter().
11848 */
11849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011850filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011851{
11852 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011853 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011854 listitem_T *li, *nli;
11855 list_T *l = NULL;
11856 dictitem_T *di;
11857 hashtab_T *ht;
11858 hashitem_T *hi;
11859 dict_T *d = NULL;
11860 typval_T save_val;
11861 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011862 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011863 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011864 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011865 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011866 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011867 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011868 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011869
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011871 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011872 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011873 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011874 return;
11875 }
11876 else if (argvars[0].v_type == VAR_DICT)
11877 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011878 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011879 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011880 return;
11881 }
11882 else
11883 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011884 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011885 return;
11886 }
11887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011888 expr = get_tv_string_buf_chk(&argvars[1], buf);
11889 /* On type errors, the preceding call has already displayed an error
11890 * message. Avoid a misleading error message for an empty string that
11891 * was not passed as argument. */
11892 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011894 prepare_vimvar(VV_VAL, &save_val);
11895 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011896
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011897 /* We reset "did_emsg" to be able to detect whether an error
11898 * occurred during evaluation of the expression. */
11899 save_did_emsg = did_emsg;
11900 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011901
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011902 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011905 vimvars[VV_KEY].vv_type = VAR_STRING;
11906
11907 ht = &d->dv_hashtab;
11908 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011909 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011910 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011912 if (!HASHITEM_EMPTY(hi))
11913 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011914 int r;
11915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011916 --todo;
11917 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011918 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011919 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11920 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011921 break;
11922 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011923 r = filter_map_one(&di->di_tv, expr, map, &rem);
11924 clear_tv(&vimvars[VV_KEY].vv_tv);
11925 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011926 break;
11927 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011928 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011929 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11930 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011931 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011932 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011933 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934 }
11935 }
11936 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011937 }
11938 else
11939 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011940 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011942 for (li = l->lv_first; li != NULL; li = nli)
11943 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011944 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011945 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011946 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011947 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011948 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011949 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011950 break;
11951 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011952 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011953 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011954 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011955 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011956
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011957 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011959
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011960 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011961 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011962
11963 copy_tv(&argvars[0], rettv);
11964}
11965
11966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011967filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011968{
Bram Moolenaar33570922005-01-25 22:26:29 +000011969 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011970 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011971 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011972
Bram Moolenaar33570922005-01-25 22:26:29 +000011973 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011974 s = expr;
11975 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011976 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011977 if (*s != NUL) /* check for trailing chars after expr */
11978 {
11979 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011980 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011981 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011982 }
11983 if (map)
11984 {
11985 /* map(): replace the list item value */
11986 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011987 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011988 *tv = rettv;
11989 }
11990 else
11991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011992 int error = FALSE;
11993
Bram Moolenaare9a41262005-01-15 22:18:47 +000011994 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011995 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011996 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 /* On type error, nothing has been removed; return FAIL to stop the
11998 * loop. The error message was given by get_tv_number_chk(). */
11999 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012000 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012001 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012002 retval = OK;
12003theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012004 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012005 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012006}
12007
12008/*
12009 * "filter()" function
12010 */
12011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012012f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012013{
12014 filter_map(argvars, rettv, FALSE);
12015}
12016
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012017/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012018 * "finddir({fname}[, {path}[, {count}]])" function
12019 */
12020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012021f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012022{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012023 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012024}
12025
12026/*
12027 * "findfile({fname}[, {path}[, {count}]])" function
12028 */
12029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012030f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012031{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012032 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012033}
12034
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012035#ifdef FEAT_FLOAT
12036/*
12037 * "float2nr({float})" function
12038 */
12039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012040f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012041{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012042 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012043
12044 if (get_float_arg(argvars, &f) == OK)
12045 {
12046 if (f < -0x7fffffff)
12047 rettv->vval.v_number = -0x7fffffff;
12048 else if (f > 0x7fffffff)
12049 rettv->vval.v_number = 0x7fffffff;
12050 else
12051 rettv->vval.v_number = (varnumber_T)f;
12052 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012053}
12054
12055/*
12056 * "floor({float})" function
12057 */
12058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012059f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012060{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012061 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012062
12063 rettv->v_type = VAR_FLOAT;
12064 if (get_float_arg(argvars, &f) == OK)
12065 rettv->vval.v_float = floor(f);
12066 else
12067 rettv->vval.v_float = 0.0;
12068}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012069
12070/*
12071 * "fmod()" function
12072 */
12073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012074f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012075{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012076 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012077
12078 rettv->v_type = VAR_FLOAT;
12079 if (get_float_arg(argvars, &fx) == OK
12080 && get_float_arg(&argvars[1], &fy) == OK)
12081 rettv->vval.v_float = fmod(fx, fy);
12082 else
12083 rettv->vval.v_float = 0.0;
12084}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012085#endif
12086
Bram Moolenaar0d660222005-01-07 21:51:51 +000012087/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012088 * "fnameescape({string})" function
12089 */
12090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012091f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012092{
12093 rettv->vval.v_string = vim_strsave_fnameescape(
12094 get_tv_string(&argvars[0]), FALSE);
12095 rettv->v_type = VAR_STRING;
12096}
12097
12098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099 * "fnamemodify({fname}, {mods})" function
12100 */
12101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012102f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103{
12104 char_u *fname;
12105 char_u *mods;
12106 int usedlen = 0;
12107 int len;
12108 char_u *fbuf = NULL;
12109 char_u buf[NUMBUFLEN];
12110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012111 fname = get_tv_string_chk(&argvars[0]);
12112 mods = get_tv_string_buf_chk(&argvars[1], buf);
12113 if (fname == NULL || mods == NULL)
12114 fname = NULL;
12115 else
12116 {
12117 len = (int)STRLEN(fname);
12118 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012125 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 vim_free(fbuf);
12127}
12128
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012129static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130
12131/*
12132 * "foldclosed()" function
12133 */
12134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012135foldclosed_both(
12136 typval_T *argvars UNUSED,
12137 typval_T *rettv,
12138 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139{
12140#ifdef FEAT_FOLDING
12141 linenr_T lnum;
12142 linenr_T first, last;
12143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12146 {
12147 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12148 {
12149 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012152 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 return;
12154 }
12155 }
12156#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012157 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158}
12159
12160/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161 * "foldclosed()" function
12162 */
12163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012164f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012165{
12166 foldclosed_both(argvars, rettv, FALSE);
12167}
12168
12169/*
12170 * "foldclosedend()" function
12171 */
12172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012173f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012174{
12175 foldclosed_both(argvars, rettv, TRUE);
12176}
12177
12178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 * "foldlevel()" function
12180 */
12181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012182f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183{
12184#ifdef FEAT_FOLDING
12185 linenr_T lnum;
12186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012187 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012189 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191}
12192
12193/*
12194 * "foldtext()" function
12195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012197f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198{
12199#ifdef FEAT_FOLDING
12200 linenr_T lnum;
12201 char_u *s;
12202 char_u *r;
12203 int len;
12204 char *txt;
12205#endif
12206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207 rettv->v_type = VAR_STRING;
12208 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012210 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12211 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12212 <= curbuf->b_ml.ml_line_count
12213 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214 {
12215 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012216 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12217 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 {
12219 if (!linewhite(lnum))
12220 break;
12221 ++lnum;
12222 }
12223
12224 /* Find interesting text in this line. */
12225 s = skipwhite(ml_get(lnum));
12226 /* skip C comment-start */
12227 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012228 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012230 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012231 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012232 {
12233 s = skipwhite(ml_get(lnum + 1));
12234 if (*s == '*')
12235 s = skipwhite(s + 1);
12236 }
12237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 txt = _("+-%s%3ld lines: ");
12239 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012240 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 + 20 /* for %3ld */
12242 + STRLEN(s))); /* concatenated */
12243 if (r != NULL)
12244 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012245 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12246 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12247 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 len = (int)STRLEN(r);
12249 STRCAT(r, s);
12250 /* remove 'foldmarker' and 'commentstring' */
12251 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 }
12254 }
12255#endif
12256}
12257
12258/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012259 * "foldtextresult(lnum)" function
12260 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012262f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012263{
12264#ifdef FEAT_FOLDING
12265 linenr_T lnum;
12266 char_u *text;
12267 char_u buf[51];
12268 foldinfo_T foldinfo;
12269 int fold_count;
12270#endif
12271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 rettv->v_type = VAR_STRING;
12273 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012274#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012276 /* treat illegal types and illegal string values for {lnum} the same */
12277 if (lnum < 0)
12278 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012279 fold_count = foldedCount(curwin, lnum, &foldinfo);
12280 if (fold_count > 0)
12281 {
12282 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12283 &foldinfo, buf);
12284 if (text == buf)
12285 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012287 }
12288#endif
12289}
12290
12291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 * "foreground()" function
12293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012295f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297#ifdef FEAT_GUI
12298 if (gui.in_use)
12299 gui_mch_set_foreground();
12300#else
12301# ifdef WIN32
12302 win32_set_foreground();
12303# endif
12304#endif
12305}
12306
12307/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012308 * "function()" function
12309 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012311f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012312{
12313 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012314 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012315 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012316 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012317
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012318 if (argvars[0].v_type == VAR_FUNC)
12319 {
12320 /* function(MyFunc, [arg], dict) */
12321 s = argvars[0].vval.v_string;
12322 }
12323 else if (argvars[0].v_type == VAR_PARTIAL
12324 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012325 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012326 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012327 arg_pt = argvars[0].vval.v_partial;
12328 s = arg_pt->pt_name;
12329 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012330 else
12331 {
12332 /* function('MyFunc', [arg], dict) */
12333 s = get_tv_string(&argvars[0]);
12334 use_string = TRUE;
12335 }
12336
12337 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012338 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012339 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012340 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12341 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012342 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012343 else
12344 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012345 int dict_idx = 0;
12346 int arg_idx = 0;
12347 list_T *list = NULL;
12348
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012349 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012350 {
12351 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012352 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012353
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012354 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12355 * also be called from another script. Using trans_function_name()
12356 * would also work, but some plugins depend on the name being
12357 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012358 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012359 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12360 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012361 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012362 STRCPY(name, sid_buf);
12363 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012364 }
12365 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012366 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012367 name = vim_strsave(s);
12368
12369 if (argvars[1].v_type != VAR_UNKNOWN)
12370 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012371 if (argvars[2].v_type != VAR_UNKNOWN)
12372 {
12373 /* function(name, [args], dict) */
12374 arg_idx = 1;
12375 dict_idx = 2;
12376 }
12377 else if (argvars[1].v_type == VAR_DICT)
12378 /* function(name, dict) */
12379 dict_idx = 1;
12380 else
12381 /* function(name, [args]) */
12382 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012383 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012384 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012385 if (argvars[dict_idx].v_type != VAR_DICT)
12386 {
12387 EMSG(_("E922: expected a dict"));
12388 vim_free(name);
12389 return;
12390 }
12391 if (argvars[dict_idx].vval.v_dict == NULL)
12392 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012393 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012394 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012395 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012396 if (argvars[arg_idx].v_type != VAR_LIST)
12397 {
12398 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12399 vim_free(name);
12400 return;
12401 }
12402 list = argvars[arg_idx].vval.v_list;
12403 if (list == NULL || list->lv_len == 0)
12404 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012405 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012406 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012407 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012408 {
12409 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012410
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012411 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012412 if (pt == NULL)
12413 vim_free(name);
12414 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012415 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012416 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012417 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012418 listitem_T *li;
12419 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012420 int arg_len = 0;
12421 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012422
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012423 if (arg_pt != NULL)
12424 arg_len = arg_pt->pt_argc;
12425 if (list != NULL)
12426 lv_len = list->lv_len;
12427 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012428 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012429 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012430 if (pt->pt_argv == NULL)
12431 {
12432 vim_free(pt);
12433 vim_free(name);
12434 return;
12435 }
12436 else
12437 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012438 for (i = 0; i < arg_len; i++)
12439 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12440 if (lv_len > 0)
12441 for (li = list->lv_first; li != NULL;
12442 li = li->li_next)
12443 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012444 }
12445 }
12446
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012447 /* For "function(dict.func, [], dict)" and "func" is a partial
12448 * use "dict". That is backwards compatible. */
12449 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012450 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012451 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012452 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12453 ++pt->pt_dict->dv_refcount;
12454 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012455 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012456 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012457 /* If the dict was bound automatically the result is also
12458 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012459 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012460 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012461 if (pt->pt_dict != NULL)
12462 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012463 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012464
12465 pt->pt_refcount = 1;
12466 pt->pt_name = name;
12467 func_ref(pt->pt_name);
12468 }
12469 rettv->v_type = VAR_PARTIAL;
12470 rettv->vval.v_partial = pt;
12471 }
12472 else
12473 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012474 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012475 rettv->v_type = VAR_FUNC;
12476 rettv->vval.v_string = name;
12477 func_ref(name);
12478 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012479 }
12480}
12481
12482/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012483 * "garbagecollect()" function
12484 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012486f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012487{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012488 /* This is postponed until we are back at the toplevel, because we may be
12489 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12490 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012491
12492 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12493 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012494}
12495
12496/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012497 * "get()" function
12498 */
12499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012500f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012501{
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 listitem_T *li;
12503 list_T *l;
12504 dictitem_T *di;
12505 dict_T *d;
12506 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012507
Bram Moolenaare9a41262005-01-15 22:18:47 +000012508 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012509 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012510 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012512 int error = FALSE;
12513
12514 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12515 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012516 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012517 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012518 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012519 else if (argvars[0].v_type == VAR_DICT)
12520 {
12521 if ((d = argvars[0].vval.v_dict) != NULL)
12522 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012523 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012524 if (di != NULL)
12525 tv = &di->di_tv;
12526 }
12527 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012528 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012529 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012530 partial_T *pt;
12531 partial_T fref_pt;
12532
12533 if (argvars[0].v_type == VAR_PARTIAL)
12534 pt = argvars[0].vval.v_partial;
12535 else
12536 {
12537 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12538 fref_pt.pt_name = argvars[0].vval.v_string;
12539 pt = &fref_pt;
12540 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012541
12542 if (pt != NULL)
12543 {
12544 char_u *what = get_tv_string(&argvars[1]);
12545
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012546 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012547 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012548 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012549 if (pt->pt_name == NULL)
12550 rettv->vval.v_string = NULL;
12551 else
12552 rettv->vval.v_string = vim_strsave(pt->pt_name);
12553 }
12554 else if (STRCMP(what, "dict") == 0)
12555 {
12556 rettv->v_type = VAR_DICT;
12557 rettv->vval.v_dict = pt->pt_dict;
12558 if (pt->pt_dict != NULL)
12559 ++pt->pt_dict->dv_refcount;
12560 }
12561 else if (STRCMP(what, "args") == 0)
12562 {
12563 rettv->v_type = VAR_LIST;
12564 if (rettv_list_alloc(rettv) == OK)
12565 {
12566 int i;
12567
12568 for (i = 0; i < pt->pt_argc; ++i)
12569 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12570 }
12571 }
12572 else
12573 EMSG2(_(e_invarg2), what);
12574 return;
12575 }
12576 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012577 else
12578 EMSG2(_(e_listdictarg), "get()");
12579
12580 if (tv == NULL)
12581 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012583 copy_tv(&argvars[2], rettv);
12584 }
12585 else
12586 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587}
12588
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012589static 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 +000012590
12591/*
12592 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012593 * Return a range (from start to end) of lines in rettv from the specified
12594 * buffer.
12595 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012596 */
12597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012598get_buffer_lines(
12599 buf_T *buf,
12600 linenr_T start,
12601 linenr_T end,
12602 int retlist,
12603 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012604{
12605 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012606
Bram Moolenaar959a1432013-12-14 12:17:38 +010012607 rettv->v_type = VAR_STRING;
12608 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012609 if (retlist && rettv_list_alloc(rettv) == FAIL)
12610 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012611
12612 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12613 return;
12614
12615 if (!retlist)
12616 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012617 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12618 p = ml_get_buf(buf, start, FALSE);
12619 else
12620 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012621 rettv->vval.v_string = vim_strsave(p);
12622 }
12623 else
12624 {
12625 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012626 return;
12627
12628 if (start < 1)
12629 start = 1;
12630 if (end > buf->b_ml.ml_line_count)
12631 end = buf->b_ml.ml_line_count;
12632 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012633 if (list_append_string(rettv->vval.v_list,
12634 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012635 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012636 }
12637}
12638
12639/*
12640 * "getbufline()" function
12641 */
12642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012643f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012644{
12645 linenr_T lnum;
12646 linenr_T end;
12647 buf_T *buf;
12648
12649 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12650 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012651 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012652 --emsg_off;
12653
Bram Moolenaar661b1822005-07-28 22:36:45 +000012654 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012655 if (argvars[2].v_type == VAR_UNKNOWN)
12656 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012657 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012658 end = get_tv_lnum_buf(&argvars[2], buf);
12659
Bram Moolenaar342337a2005-07-21 21:11:17 +000012660 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012661}
12662
Bram Moolenaar0d660222005-01-07 21:51:51 +000012663/*
12664 * "getbufvar()" function
12665 */
12666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012667f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012668{
12669 buf_T *buf;
12670 buf_T *save_curbuf;
12671 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012672 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012673 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012675 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12676 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012677 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012678 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012679
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012680 rettv->v_type = VAR_STRING;
12681 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012682
12683 if (buf != NULL && varname != NULL)
12684 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012685 /* set curbuf to be our buf, temporarily */
12686 save_curbuf = curbuf;
12687 curbuf = buf;
12688
Bram Moolenaar0d660222005-01-07 21:51:51 +000012689 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012690 {
12691 if (get_option_tv(&varname, rettv, TRUE) == OK)
12692 done = TRUE;
12693 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012694 else if (STRCMP(varname, "changedtick") == 0)
12695 {
12696 rettv->v_type = VAR_NUMBER;
12697 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012698 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012699 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012700 else
12701 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012702 /* Look up the variable. */
12703 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12704 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12705 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012706 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012707 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012708 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012709 done = TRUE;
12710 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012711 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012712
12713 /* restore previous notion of curbuf */
12714 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012715 }
12716
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012717 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12718 /* use the default value */
12719 copy_tv(&argvars[2], rettv);
12720
Bram Moolenaar0d660222005-01-07 21:51:51 +000012721 --emsg_off;
12722}
12723
12724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725 * "getchar()" function
12726 */
12727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012728f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729{
12730 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012733 /* Position the cursor. Needed after a message that ends in a space. */
12734 windgoto(msg_row, msg_col);
12735
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 ++no_mapping;
12737 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012738 for (;;)
12739 {
12740 if (argvars[0].v_type == VAR_UNKNOWN)
12741 /* getchar(): blocking wait. */
12742 n = safe_vgetc();
12743 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12744 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012745 n = vpeekc_any();
12746 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012747 /* illegal argument or getchar(0) and no char avail: return zero */
12748 n = 0;
12749 else
12750 /* getchar(0) and char avail: return char */
12751 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012752
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012753 if (n == K_IGNORE)
12754 continue;
12755 break;
12756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 --no_mapping;
12758 --allow_keys;
12759
Bram Moolenaar219b8702006-11-01 14:32:36 +000012760 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012761 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012762 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12763 vimvars[VV_MOUSE_COL].vv_nr = 0;
12764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 if (IS_SPECIAL(n) || mod_mask != 0)
12767 {
12768 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12769 int i = 0;
12770
12771 /* Turn a special key into three bytes, plus modifier. */
12772 if (mod_mask != 0)
12773 {
12774 temp[i++] = K_SPECIAL;
12775 temp[i++] = KS_MODIFIER;
12776 temp[i++] = mod_mask;
12777 }
12778 if (IS_SPECIAL(n))
12779 {
12780 temp[i++] = K_SPECIAL;
12781 temp[i++] = K_SECOND(n);
12782 temp[i++] = K_THIRD(n);
12783 }
12784#ifdef FEAT_MBYTE
12785 else if (has_mbyte)
12786 i += (*mb_char2bytes)(n, temp + i);
12787#endif
12788 else
12789 temp[i++] = n;
12790 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 rettv->v_type = VAR_STRING;
12792 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012793
12794#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012795 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012796 {
12797 int row = mouse_row;
12798 int col = mouse_col;
12799 win_T *win;
12800 linenr_T lnum;
12801# ifdef FEAT_WINDOWS
12802 win_T *wp;
12803# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012804 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012805
12806 if (row >= 0 && col >= 0)
12807 {
12808 /* Find the window at the mouse coordinates and compute the
12809 * text position. */
12810 win = mouse_find_win(&row, &col);
12811 (void)mouse_comp_pos(win, &row, &col, &lnum);
12812# ifdef FEAT_WINDOWS
12813 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012814 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012815# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012816 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012817 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012818 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12819 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12820 }
12821 }
12822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 }
12824}
12825
12826/*
12827 * "getcharmod()" function
12828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012830f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833}
12834
12835/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012836 * "getcharsearch()" function
12837 */
12838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012839f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012840{
12841 if (rettv_dict_alloc(rettv) != FAIL)
12842 {
12843 dict_T *dict = rettv->vval.v_dict;
12844
12845 dict_add_nr_str(dict, "char", 0L, last_csearch());
12846 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12847 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12848 }
12849}
12850
12851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 * "getcmdline()" function
12853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012855f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 rettv->v_type = VAR_STRING;
12858 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859}
12860
12861/*
12862 * "getcmdpos()" function
12863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012865f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868}
12869
12870/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012871 * "getcmdtype()" function
12872 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012874f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012875{
12876 rettv->v_type = VAR_STRING;
12877 rettv->vval.v_string = alloc(2);
12878 if (rettv->vval.v_string != NULL)
12879 {
12880 rettv->vval.v_string[0] = get_cmdline_type();
12881 rettv->vval.v_string[1] = NUL;
12882 }
12883}
12884
12885/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012886 * "getcmdwintype()" function
12887 */
12888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012889f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012890{
12891 rettv->v_type = VAR_STRING;
12892 rettv->vval.v_string = NULL;
12893#ifdef FEAT_CMDWIN
12894 rettv->vval.v_string = alloc(2);
12895 if (rettv->vval.v_string != NULL)
12896 {
12897 rettv->vval.v_string[0] = cmdwin_type;
12898 rettv->vval.v_string[1] = NUL;
12899 }
12900#endif
12901}
12902
12903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 * "getcwd()" function
12905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012907f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012909 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012910 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012913 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012914
12915 wp = find_tabwin(&argvars[0], &argvars[1]);
12916 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012918 if (wp->w_localdir != NULL)
12919 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12920 else if(globaldir != NULL)
12921 rettv->vval.v_string = vim_strsave(globaldir);
12922 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012923 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012924 cwd = alloc(MAXPATHL);
12925 if (cwd != NULL)
12926 {
12927 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12928 rettv->vval.v_string = vim_strsave(cwd);
12929 vim_free(cwd);
12930 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012931 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012932#ifdef BACKSLASH_IN_FILENAME
12933 if (rettv->vval.v_string != NULL)
12934 slash_adjust(rettv->vval.v_string);
12935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 }
12937}
12938
12939/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012940 * "getfontname()" function
12941 */
12942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012943f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012944{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945 rettv->v_type = VAR_STRING;
12946 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012947#ifdef FEAT_GUI
12948 if (gui.in_use)
12949 {
12950 GuiFont font;
12951 char_u *name = NULL;
12952
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012953 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012954 {
12955 /* Get the "Normal" font. Either the name saved by
12956 * hl_set_font_name() or from the font ID. */
12957 font = gui.norm_font;
12958 name = hl_get_font_name();
12959 }
12960 else
12961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012963 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12964 return;
12965 font = gui_mch_get_font(name, FALSE);
12966 if (font == NOFONT)
12967 return; /* Invalid font name, return empty string. */
12968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012969 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012970 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012971 gui_mch_free_font(font);
12972 }
12973#endif
12974}
12975
12976/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012977 * "getfperm({fname})" function
12978 */
12979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012980f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012981{
12982 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020012983 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012984 char_u *perm = NULL;
12985 char_u flags[] = "rwx";
12986 int i;
12987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012988 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012990 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012991 if (mch_stat((char *)fname, &st) >= 0)
12992 {
12993 perm = vim_strsave((char_u *)"---------");
12994 if (perm != NULL)
12995 {
12996 for (i = 0; i < 9; i++)
12997 {
12998 if (st.st_mode & (1 << (8 - i)))
12999 perm[i] = flags[i % 3];
13000 }
13001 }
13002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013003 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013004}
13005
13006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 * "getfsize({fname})" function
13008 */
13009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013010f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011{
13012 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013013 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013015 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013017 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018
13019 if (mch_stat((char *)fname, &st) >= 0)
13020 {
13021 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013025 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013026
13027 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013028 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013029 rettv->vval.v_number = -2;
13030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 }
13032 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013033 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034}
13035
13036/*
13037 * "getftime({fname})" function
13038 */
13039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013040f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041{
13042 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013043 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013045 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046
13047 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013048 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013050 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051}
13052
13053/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013054 * "getftype({fname})" function
13055 */
13056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013057f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013058{
13059 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013060 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013061 char_u *type = NULL;
13062 char *t;
13063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013066 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013067 if (mch_lstat((char *)fname, &st) >= 0)
13068 {
13069#ifdef S_ISREG
13070 if (S_ISREG(st.st_mode))
13071 t = "file";
13072 else if (S_ISDIR(st.st_mode))
13073 t = "dir";
13074# ifdef S_ISLNK
13075 else if (S_ISLNK(st.st_mode))
13076 t = "link";
13077# endif
13078# ifdef S_ISBLK
13079 else if (S_ISBLK(st.st_mode))
13080 t = "bdev";
13081# endif
13082# ifdef S_ISCHR
13083 else if (S_ISCHR(st.st_mode))
13084 t = "cdev";
13085# endif
13086# ifdef S_ISFIFO
13087 else if (S_ISFIFO(st.st_mode))
13088 t = "fifo";
13089# endif
13090# ifdef S_ISSOCK
13091 else if (S_ISSOCK(st.st_mode))
13092 t = "fifo";
13093# endif
13094 else
13095 t = "other";
13096#else
13097# ifdef S_IFMT
13098 switch (st.st_mode & S_IFMT)
13099 {
13100 case S_IFREG: t = "file"; break;
13101 case S_IFDIR: t = "dir"; break;
13102# ifdef S_IFLNK
13103 case S_IFLNK: t = "link"; break;
13104# endif
13105# ifdef S_IFBLK
13106 case S_IFBLK: t = "bdev"; break;
13107# endif
13108# ifdef S_IFCHR
13109 case S_IFCHR: t = "cdev"; break;
13110# endif
13111# ifdef S_IFIFO
13112 case S_IFIFO: t = "fifo"; break;
13113# endif
13114# ifdef S_IFSOCK
13115 case S_IFSOCK: t = "socket"; break;
13116# endif
13117 default: t = "other";
13118 }
13119# else
13120 if (mch_isdir(fname))
13121 t = "dir";
13122 else
13123 t = "file";
13124# endif
13125#endif
13126 type = vim_strsave((char_u *)t);
13127 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013129}
13130
13131/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013132 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013133 */
13134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013135f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013136{
13137 linenr_T lnum;
13138 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013139 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013140
13141 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013142 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013143 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013144 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013145 retlist = FALSE;
13146 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013147 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013148 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013149 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013150 retlist = TRUE;
13151 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013152
Bram Moolenaar342337a2005-07-21 21:11:17 +000013153 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013154}
13155
13156/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013157 * "getmatches()" function
13158 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013160f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013161{
13162#ifdef FEAT_SEARCH_EXTRA
13163 dict_T *dict;
13164 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013165 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013166
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013167 if (rettv_list_alloc(rettv) == OK)
13168 {
13169 while (cur != NULL)
13170 {
13171 dict = dict_alloc();
13172 if (dict == NULL)
13173 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013174 if (cur->match.regprog == NULL)
13175 {
13176 /* match added with matchaddpos() */
13177 for (i = 0; i < MAXPOSMATCH; ++i)
13178 {
13179 llpos_T *llpos;
13180 char buf[6];
13181 list_T *l;
13182
13183 llpos = &cur->pos.pos[i];
13184 if (llpos->lnum == 0)
13185 break;
13186 l = list_alloc();
13187 if (l == NULL)
13188 break;
13189 list_append_number(l, (varnumber_T)llpos->lnum);
13190 if (llpos->col > 0)
13191 {
13192 list_append_number(l, (varnumber_T)llpos->col);
13193 list_append_number(l, (varnumber_T)llpos->len);
13194 }
13195 sprintf(buf, "pos%d", i + 1);
13196 dict_add_list(dict, buf, l);
13197 }
13198 }
13199 else
13200 {
13201 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13202 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013203 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013204 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13205 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013206# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013207 if (cur->conceal_char)
13208 {
13209 char_u buf[MB_MAXBYTES + 1];
13210
13211 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13212 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13213 }
13214# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013215 list_append_dict(rettv->vval.v_list, dict);
13216 cur = cur->next;
13217 }
13218 }
13219#endif
13220}
13221
13222/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013223 * "getpid()" function
13224 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013226f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013227{
13228 rettv->vval.v_number = mch_get_pid();
13229}
13230
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013232getpos_both(
13233 typval_T *argvars,
13234 typval_T *rettv,
13235 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013236{
Bram Moolenaara5525202006-03-02 22:52:09 +000013237 pos_T *fp;
13238 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013239 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013240
13241 if (rettv_list_alloc(rettv) == OK)
13242 {
13243 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013244 if (getcurpos)
13245 fp = &curwin->w_cursor;
13246 else
13247 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013248 if (fnum != -1)
13249 list_append_number(l, (varnumber_T)fnum);
13250 else
13251 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013252 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13253 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013254 list_append_number(l, (fp != NULL)
13255 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013256 : (varnumber_T)0);
13257 list_append_number(l,
13258#ifdef FEAT_VIRTUALEDIT
13259 (fp != NULL) ? (varnumber_T)fp->coladd :
13260#endif
13261 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013262 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013263 {
13264 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013265 list_append_number(l, curwin->w_curswant == MAXCOL ?
13266 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013267 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013268 }
13269 else
13270 rettv->vval.v_number = FALSE;
13271}
13272
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013273
13274/*
13275 * "getcurpos()" function
13276 */
13277 static void
13278f_getcurpos(typval_T *argvars, typval_T *rettv)
13279{
13280 getpos_both(argvars, rettv, TRUE);
13281}
13282
13283/*
13284 * "getpos(string)" function
13285 */
13286 static void
13287f_getpos(typval_T *argvars, typval_T *rettv)
13288{
13289 getpos_both(argvars, rettv, FALSE);
13290}
13291
Bram Moolenaara5525202006-03-02 22:52:09 +000013292/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013293 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013294 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013296f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013297{
13298#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013299 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013300#endif
13301
Bram Moolenaar2641f772005-03-25 21:58:17 +000013302#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013303 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013304 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013305 wp = NULL;
13306 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13307 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013308 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013309 if (wp == NULL)
13310 return;
13311 }
13312
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013313 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013314 }
13315#endif
13316}
13317
13318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 * "getreg()" function
13320 */
13321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013322f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323{
13324 char_u *strregname;
13325 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013326 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013327 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013328 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013330 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013332 strregname = get_tv_string_chk(&argvars[0]);
13333 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013334 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013336 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013337 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13338 return_list = get_tv_number_chk(&argvars[2], &error);
13339 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013342 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013343
13344 if (error)
13345 return;
13346
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 regname = (strregname == NULL ? '"' : *strregname);
13348 if (regname == 0)
13349 regname = '"';
13350
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013351 if (return_list)
13352 {
13353 rettv->v_type = VAR_LIST;
13354 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13355 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013356 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013357 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013358 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013359 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013360 }
13361 else
13362 {
13363 rettv->v_type = VAR_STRING;
13364 rettv->vval.v_string = get_reg_contents(regname,
13365 arg2 ? GREG_EXPR_SRC : 0);
13366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367}
13368
13369/*
13370 * "getregtype()" function
13371 */
13372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013373f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374{
13375 char_u *strregname;
13376 int regname;
13377 char_u buf[NUMBUFLEN + 2];
13378 long reglen = 0;
13379
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013380 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013381 {
13382 strregname = get_tv_string_chk(&argvars[0]);
13383 if (strregname == NULL) /* type error; errmsg already given */
13384 {
13385 rettv->v_type = VAR_STRING;
13386 rettv->vval.v_string = NULL;
13387 return;
13388 }
13389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 else
13391 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013392 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393
13394 regname = (strregname == NULL ? '"' : *strregname);
13395 if (regname == 0)
13396 regname = '"';
13397
13398 buf[0] = NUL;
13399 buf[1] = NUL;
13400 switch (get_reg_type(regname, &reglen))
13401 {
13402 case MLINE: buf[0] = 'V'; break;
13403 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 case MBLOCK:
13405 buf[0] = Ctrl_V;
13406 sprintf((char *)buf + 1, "%ld", reglen + 1);
13407 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013409 rettv->v_type = VAR_STRING;
13410 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411}
13412
13413/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013414 * "gettabvar()" function
13415 */
13416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013417f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013418{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013419 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013420 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013421 dictitem_T *v;
13422 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013423 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013424
13425 rettv->v_type = VAR_STRING;
13426 rettv->vval.v_string = NULL;
13427
13428 varname = get_tv_string_chk(&argvars[1]);
13429 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13430 if (tp != NULL && varname != NULL)
13431 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013432 /* Set tp to be our tabpage, temporarily. Also set the window to the
13433 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013434 if (switch_win(&oldcurwin, &oldtabpage,
13435 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013436 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013437 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013438 /* look up the variable */
13439 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13440 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13441 if (v != NULL)
13442 {
13443 copy_tv(&v->di_tv, rettv);
13444 done = TRUE;
13445 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013446 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013447
13448 /* restore previous notion of curwin */
13449 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013450 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013451
13452 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013453 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013454}
13455
13456/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013457 * "gettabwinvar()" function
13458 */
13459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013460f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013461{
13462 getwinvar(argvars, rettv, 1);
13463}
13464
13465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 * "getwinposx()" function
13467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013469f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013471 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472#ifdef FEAT_GUI
13473 if (gui.in_use)
13474 {
13475 int x, y;
13476
13477 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013478 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 }
13480#endif
13481}
13482
13483/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013484 * "win_findbuf()" function
13485 */
13486 static void
13487f_win_findbuf(typval_T *argvars, typval_T *rettv)
13488{
13489 if (rettv_list_alloc(rettv) != FAIL)
13490 win_findbuf(argvars, rettv->vval.v_list);
13491}
13492
13493/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013494 * "win_getid()" function
13495 */
13496 static void
13497f_win_getid(typval_T *argvars, typval_T *rettv)
13498{
13499 rettv->vval.v_number = win_getid(argvars);
13500}
13501
13502/*
13503 * "win_gotoid()" function
13504 */
13505 static void
13506f_win_gotoid(typval_T *argvars, typval_T *rettv)
13507{
13508 rettv->vval.v_number = win_gotoid(argvars);
13509}
13510
13511/*
13512 * "win_id2tabwin()" function
13513 */
13514 static void
13515f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13516{
13517 if (rettv_list_alloc(rettv) != FAIL)
13518 win_id2tabwin(argvars, rettv->vval.v_list);
13519}
13520
13521/*
13522 * "win_id2win()" function
13523 */
13524 static void
13525f_win_id2win(typval_T *argvars, typval_T *rettv)
13526{
13527 rettv->vval.v_number = win_id2win(argvars);
13528}
13529
13530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 * "getwinposy()" function
13532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013534f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013536 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537#ifdef FEAT_GUI
13538 if (gui.in_use)
13539 {
13540 int x, y;
13541
13542 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013543 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 }
13545#endif
13546}
13547
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013548/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013549 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013550 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013551 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013552find_win_by_nr(
13553 typval_T *vp,
13554 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013555{
13556#ifdef FEAT_WINDOWS
13557 win_T *wp;
13558#endif
13559 int nr;
13560
13561 nr = get_tv_number_chk(vp, NULL);
13562
13563#ifdef FEAT_WINDOWS
13564 if (nr < 0)
13565 return NULL;
13566 if (nr == 0)
13567 return curwin;
13568
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013569 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13570 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013571 if (nr >= LOWEST_WIN_ID)
13572 {
13573 if (wp->w_id == nr)
13574 return wp;
13575 }
13576 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013577 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013578 if (nr >= LOWEST_WIN_ID)
13579 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013580 return wp;
13581#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013582 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013583 return curwin;
13584 return NULL;
13585#endif
13586}
13587
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013589 * Find window specified by "wvp" in tabpage "tvp".
13590 */
13591 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013592find_tabwin(
13593 typval_T *wvp, /* VAR_UNKNOWN for current window */
13594 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013595{
13596 win_T *wp = NULL;
13597 tabpage_T *tp = NULL;
13598 long n;
13599
13600 if (wvp->v_type != VAR_UNKNOWN)
13601 {
13602 if (tvp->v_type != VAR_UNKNOWN)
13603 {
13604 n = get_tv_number(tvp);
13605 if (n >= 0)
13606 tp = find_tabpage(n);
13607 }
13608 else
13609 tp = curtab;
13610
13611 if (tp != NULL)
13612 wp = find_win_by_nr(wvp, tp);
13613 }
13614 else
13615 wp = curwin;
13616
13617 return wp;
13618}
13619
13620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 * "getwinvar()" function
13622 */
13623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013624f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013626 getwinvar(argvars, rettv, 0);
13627}
13628
13629/*
13630 * getwinvar() and gettabwinvar()
13631 */
13632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013633getwinvar(
13634 typval_T *argvars,
13635 typval_T *rettv,
13636 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013637{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013638 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013641 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013642 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013643#ifdef FEAT_WINDOWS
13644 win_T *oldcurwin;
13645 tabpage_T *oldtabpage;
13646 int need_switch_win;
13647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013649#ifdef FEAT_WINDOWS
13650 if (off == 1)
13651 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13652 else
13653 tp = curtab;
13654#endif
13655 win = find_win_by_nr(&argvars[off], tp);
13656 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013659 rettv->v_type = VAR_STRING;
13660 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661
13662 if (win != NULL && varname != NULL)
13663 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013664#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013665 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013666 * otherwise the window is not valid. Only do this when needed,
13667 * autocommands get blocked. */
13668 need_switch_win = !(tp == curtab && win == curwin);
13669 if (!need_switch_win
13670 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13671#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013672 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013673 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013674 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013675 if (get_option_tv(&varname, rettv, 1) == OK)
13676 done = TRUE;
13677 }
13678 else
13679 {
13680 /* Look up the variable. */
13681 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13682 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13683 varname, FALSE);
13684 if (v != NULL)
13685 {
13686 copy_tv(&v->di_tv, rettv);
13687 done = TRUE;
13688 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013691
Bram Moolenaarba117c22015-09-29 16:53:22 +020013692#ifdef FEAT_WINDOWS
13693 if (need_switch_win)
13694 /* restore previous notion of curwin */
13695 restore_win(oldcurwin, oldtabpage, TRUE);
13696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 }
13698
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013699 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13700 /* use the default return value */
13701 copy_tv(&argvars[off + 2], rettv);
13702
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703 --emsg_off;
13704}
13705
13706/*
13707 * "glob()" function
13708 */
13709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013710f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013712 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013714 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013716 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013717 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013719 if (argvars[1].v_type != VAR_UNKNOWN)
13720 {
13721 if (get_tv_number_chk(&argvars[1], &error))
13722 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013723 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013724 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013725 if (get_tv_number_chk(&argvars[2], &error))
13726 {
13727 rettv->v_type = VAR_LIST;
13728 rettv->vval.v_list = NULL;
13729 }
13730 if (argvars[3].v_type != VAR_UNKNOWN
13731 && get_tv_number_chk(&argvars[3], &error))
13732 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013733 }
13734 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013735 if (!error)
13736 {
13737 ExpandInit(&xpc);
13738 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013739 if (p_wic)
13740 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013741 if (rettv->v_type == VAR_STRING)
13742 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013743 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013744 else if (rettv_list_alloc(rettv) != FAIL)
13745 {
13746 int i;
13747
13748 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13749 NULL, options, WILD_ALL_KEEP);
13750 for (i = 0; i < xpc.xp_numfiles; i++)
13751 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13752
13753 ExpandCleanup(&xpc);
13754 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013755 }
13756 else
13757 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758}
13759
13760/*
13761 * "globpath()" function
13762 */
13763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013764f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013766 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013768 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013769 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013770 garray_T ga;
13771 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013773 /* When the optional second argument is non-zero, don't remove matches
13774 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013775 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013776 if (argvars[2].v_type != VAR_UNKNOWN)
13777 {
13778 if (get_tv_number_chk(&argvars[2], &error))
13779 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013780 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013781 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013782 if (get_tv_number_chk(&argvars[3], &error))
13783 {
13784 rettv->v_type = VAR_LIST;
13785 rettv->vval.v_list = NULL;
13786 }
13787 if (argvars[4].v_type != VAR_UNKNOWN
13788 && get_tv_number_chk(&argvars[4], &error))
13789 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013790 }
13791 }
13792 if (file != NULL && !error)
13793 {
13794 ga_init2(&ga, (int)sizeof(char_u *), 10);
13795 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13796 if (rettv->v_type == VAR_STRING)
13797 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13798 else if (rettv_list_alloc(rettv) != FAIL)
13799 for (i = 0; i < ga.ga_len; ++i)
13800 list_append_string(rettv->vval.v_list,
13801 ((char_u **)(ga.ga_data))[i], -1);
13802 ga_clear_strings(&ga);
13803 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013804 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013805 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806}
13807
13808/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013809 * "glob2regpat()" function
13810 */
13811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013812f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013813{
13814 char_u *pat = get_tv_string_chk(&argvars[0]);
13815
13816 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013817 rettv->vval.v_string = (pat == NULL)
13818 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013819}
13820
13821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822 * "has()" function
13823 */
13824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013825f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826{
13827 int i;
13828 char_u *name;
13829 int n = FALSE;
13830 static char *(has_list[]) =
13831 {
13832#ifdef AMIGA
13833 "amiga",
13834# ifdef FEAT_ARP
13835 "arp",
13836# endif
13837#endif
13838#ifdef __BEOS__
13839 "beos",
13840#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013841#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 "mac",
13843#endif
13844#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013845 "macunix", /* built with 'darwin' enabled */
13846#endif
13847#if defined(__APPLE__) && __APPLE__ == 1
13848 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850#ifdef __QNX__
13851 "qnx",
13852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853#ifdef UNIX
13854 "unix",
13855#endif
13856#ifdef VMS
13857 "vms",
13858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859#ifdef WIN32
13860 "win32",
13861#endif
13862#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13863 "win32unix",
13864#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013865#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 "win64",
13867#endif
13868#ifdef EBCDIC
13869 "ebcdic",
13870#endif
13871#ifndef CASE_INSENSITIVE_FILENAME
13872 "fname_case",
13873#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013874#ifdef HAVE_ACL
13875 "acl",
13876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877#ifdef FEAT_ARABIC
13878 "arabic",
13879#endif
13880#ifdef FEAT_AUTOCMD
13881 "autocmd",
13882#endif
13883#ifdef FEAT_BEVAL
13884 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013885# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13886 "balloon_multiline",
13887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888#endif
13889#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13890 "builtin_terms",
13891# ifdef ALL_BUILTIN_TCAPS
13892 "all_builtin_terms",
13893# endif
13894#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013895#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13896 || defined(FEAT_GUI_W32) \
13897 || defined(FEAT_GUI_MOTIF))
13898 "browsefilter",
13899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900#ifdef FEAT_BYTEOFF
13901 "byte_offset",
13902#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013903#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013904 "channel",
13905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906#ifdef FEAT_CINDENT
13907 "cindent",
13908#endif
13909#ifdef FEAT_CLIENTSERVER
13910 "clientserver",
13911#endif
13912#ifdef FEAT_CLIPBOARD
13913 "clipboard",
13914#endif
13915#ifdef FEAT_CMDL_COMPL
13916 "cmdline_compl",
13917#endif
13918#ifdef FEAT_CMDHIST
13919 "cmdline_hist",
13920#endif
13921#ifdef FEAT_COMMENTS
13922 "comments",
13923#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013924#ifdef FEAT_CONCEAL
13925 "conceal",
13926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927#ifdef FEAT_CRYPT
13928 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013929 "crypt-blowfish",
13930 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931#endif
13932#ifdef FEAT_CSCOPE
13933 "cscope",
13934#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013935#ifdef FEAT_CURSORBIND
13936 "cursorbind",
13937#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013938#ifdef CURSOR_SHAPE
13939 "cursorshape",
13940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941#ifdef DEBUG
13942 "debug",
13943#endif
13944#ifdef FEAT_CON_DIALOG
13945 "dialog_con",
13946#endif
13947#ifdef FEAT_GUI_DIALOG
13948 "dialog_gui",
13949#endif
13950#ifdef FEAT_DIFF
13951 "diff",
13952#endif
13953#ifdef FEAT_DIGRAPHS
13954 "digraphs",
13955#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013956#ifdef FEAT_DIRECTX
13957 "directx",
13958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959#ifdef FEAT_DND
13960 "dnd",
13961#endif
13962#ifdef FEAT_EMACS_TAGS
13963 "emacs_tags",
13964#endif
13965 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013966 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967#ifdef FEAT_SEARCH_EXTRA
13968 "extra_search",
13969#endif
13970#ifdef FEAT_FKMAP
13971 "farsi",
13972#endif
13973#ifdef FEAT_SEARCHPATH
13974 "file_in_path",
13975#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013976#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013977 "filterpipe",
13978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979#ifdef FEAT_FIND_ID
13980 "find_in_path",
13981#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013982#ifdef FEAT_FLOAT
13983 "float",
13984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985#ifdef FEAT_FOLDING
13986 "folding",
13987#endif
13988#ifdef FEAT_FOOTER
13989 "footer",
13990#endif
13991#if !defined(USE_SYSTEM) && defined(UNIX)
13992 "fork",
13993#endif
13994#ifdef FEAT_GETTEXT
13995 "gettext",
13996#endif
13997#ifdef FEAT_GUI
13998 "gui",
13999#endif
14000#ifdef FEAT_GUI_ATHENA
14001# ifdef FEAT_GUI_NEXTAW
14002 "gui_neXtaw",
14003# else
14004 "gui_athena",
14005# endif
14006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007#ifdef FEAT_GUI_GTK
14008 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014009# ifdef USE_GTK3
14010 "gui_gtk3",
14011# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014015#ifdef FEAT_GUI_GNOME
14016 "gui_gnome",
14017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018#ifdef FEAT_GUI_MAC
14019 "gui_mac",
14020#endif
14021#ifdef FEAT_GUI_MOTIF
14022 "gui_motif",
14023#endif
14024#ifdef FEAT_GUI_PHOTON
14025 "gui_photon",
14026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027#ifdef FEAT_GUI_W32
14028 "gui_win32",
14029#endif
14030#ifdef FEAT_HANGULIN
14031 "hangul_input",
14032#endif
14033#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14034 "iconv",
14035#endif
14036#ifdef FEAT_INS_EXPAND
14037 "insert_expand",
14038#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014039#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014040 "job",
14041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042#ifdef FEAT_JUMPLIST
14043 "jumplist",
14044#endif
14045#ifdef FEAT_KEYMAP
14046 "keymap",
14047#endif
14048#ifdef FEAT_LANGMAP
14049 "langmap",
14050#endif
14051#ifdef FEAT_LIBCALL
14052 "libcall",
14053#endif
14054#ifdef FEAT_LINEBREAK
14055 "linebreak",
14056#endif
14057#ifdef FEAT_LISP
14058 "lispindent",
14059#endif
14060#ifdef FEAT_LISTCMDS
14061 "listcmds",
14062#endif
14063#ifdef FEAT_LOCALMAP
14064 "localmap",
14065#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014066#ifdef FEAT_LUA
14067# ifndef DYNAMIC_LUA
14068 "lua",
14069# endif
14070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071#ifdef FEAT_MENU
14072 "menu",
14073#endif
14074#ifdef FEAT_SESSION
14075 "mksession",
14076#endif
14077#ifdef FEAT_MODIFY_FNAME
14078 "modify_fname",
14079#endif
14080#ifdef FEAT_MOUSE
14081 "mouse",
14082#endif
14083#ifdef FEAT_MOUSESHAPE
14084 "mouseshape",
14085#endif
14086#if defined(UNIX) || defined(VMS)
14087# ifdef FEAT_MOUSE_DEC
14088 "mouse_dec",
14089# endif
14090# ifdef FEAT_MOUSE_GPM
14091 "mouse_gpm",
14092# endif
14093# ifdef FEAT_MOUSE_JSB
14094 "mouse_jsbterm",
14095# endif
14096# ifdef FEAT_MOUSE_NET
14097 "mouse_netterm",
14098# endif
14099# ifdef FEAT_MOUSE_PTERM
14100 "mouse_pterm",
14101# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014102# ifdef FEAT_MOUSE_SGR
14103 "mouse_sgr",
14104# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014105# ifdef FEAT_SYSMOUSE
14106 "mouse_sysmouse",
14107# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014108# ifdef FEAT_MOUSE_URXVT
14109 "mouse_urxvt",
14110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111# ifdef FEAT_MOUSE_XTERM
14112 "mouse_xterm",
14113# endif
14114#endif
14115#ifdef FEAT_MBYTE
14116 "multi_byte",
14117#endif
14118#ifdef FEAT_MBYTE_IME
14119 "multi_byte_ime",
14120#endif
14121#ifdef FEAT_MULTI_LANG
14122 "multi_lang",
14123#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014124#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014125#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014126 "mzscheme",
14127#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129#ifdef FEAT_OLE
14130 "ole",
14131#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014132 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133#ifdef FEAT_PATH_EXTRA
14134 "path_extra",
14135#endif
14136#ifdef FEAT_PERL
14137#ifndef DYNAMIC_PERL
14138 "perl",
14139#endif
14140#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014141#ifdef FEAT_PERSISTENT_UNDO
14142 "persistent_undo",
14143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144#ifdef FEAT_PYTHON
14145#ifndef DYNAMIC_PYTHON
14146 "python",
14147#endif
14148#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014149#ifdef FEAT_PYTHON3
14150#ifndef DYNAMIC_PYTHON3
14151 "python3",
14152#endif
14153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154#ifdef FEAT_POSTSCRIPT
14155 "postscript",
14156#endif
14157#ifdef FEAT_PRINTER
14158 "printer",
14159#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014160#ifdef FEAT_PROFILE
14161 "profile",
14162#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014163#ifdef FEAT_RELTIME
14164 "reltime",
14165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166#ifdef FEAT_QUICKFIX
14167 "quickfix",
14168#endif
14169#ifdef FEAT_RIGHTLEFT
14170 "rightleft",
14171#endif
14172#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14173 "ruby",
14174#endif
14175#ifdef FEAT_SCROLLBIND
14176 "scrollbind",
14177#endif
14178#ifdef FEAT_CMDL_INFO
14179 "showcmd",
14180 "cmdline_info",
14181#endif
14182#ifdef FEAT_SIGNS
14183 "signs",
14184#endif
14185#ifdef FEAT_SMARTINDENT
14186 "smartindent",
14187#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014188#ifdef STARTUPTIME
14189 "startuptime",
14190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191#ifdef FEAT_STL_OPT
14192 "statusline",
14193#endif
14194#ifdef FEAT_SUN_WORKSHOP
14195 "sun_workshop",
14196#endif
14197#ifdef FEAT_NETBEANS_INTG
14198 "netbeans_intg",
14199#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014200#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014201 "spell",
14202#endif
14203#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204 "syntax",
14205#endif
14206#if defined(USE_SYSTEM) || !defined(UNIX)
14207 "system",
14208#endif
14209#ifdef FEAT_TAG_BINS
14210 "tag_binary",
14211#endif
14212#ifdef FEAT_TAG_OLDSTATIC
14213 "tag_old_static",
14214#endif
14215#ifdef FEAT_TAG_ANYWHITE
14216 "tag_any_white",
14217#endif
14218#ifdef FEAT_TCL
14219# ifndef DYNAMIC_TCL
14220 "tcl",
14221# endif
14222#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014223#ifdef FEAT_TERMGUICOLORS
14224 "termguicolors",
14225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226#ifdef TERMINFO
14227 "terminfo",
14228#endif
14229#ifdef FEAT_TERMRESPONSE
14230 "termresponse",
14231#endif
14232#ifdef FEAT_TEXTOBJ
14233 "textobjects",
14234#endif
14235#ifdef HAVE_TGETENT
14236 "tgetent",
14237#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014238#ifdef FEAT_TIMERS
14239 "timers",
14240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241#ifdef FEAT_TITLE
14242 "title",
14243#endif
14244#ifdef FEAT_TOOLBAR
14245 "toolbar",
14246#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014247#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14248 "unnamedplus",
14249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250#ifdef FEAT_USR_CMDS
14251 "user-commands", /* was accidentally included in 5.4 */
14252 "user_commands",
14253#endif
14254#ifdef FEAT_VIMINFO
14255 "viminfo",
14256#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014257#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258 "vertsplit",
14259#endif
14260#ifdef FEAT_VIRTUALEDIT
14261 "virtualedit",
14262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264#ifdef FEAT_VISUALEXTRA
14265 "visualextra",
14266#endif
14267#ifdef FEAT_VREPLACE
14268 "vreplace",
14269#endif
14270#ifdef FEAT_WILDIGN
14271 "wildignore",
14272#endif
14273#ifdef FEAT_WILDMENU
14274 "wildmenu",
14275#endif
14276#ifdef FEAT_WINDOWS
14277 "windows",
14278#endif
14279#ifdef FEAT_WAK
14280 "winaltkeys",
14281#endif
14282#ifdef FEAT_WRITEBACKUP
14283 "writebackup",
14284#endif
14285#ifdef FEAT_XIM
14286 "xim",
14287#endif
14288#ifdef FEAT_XFONTSET
14289 "xfontset",
14290#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014291#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014292 "xpm",
14293 "xpm_w32", /* for backward compatibility */
14294#else
14295# if defined(HAVE_XPM)
14296 "xpm",
14297# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299#ifdef USE_XSMP
14300 "xsmp",
14301#endif
14302#ifdef USE_XSMP_INTERACT
14303 "xsmp_interact",
14304#endif
14305#ifdef FEAT_XCLIPBOARD
14306 "xterm_clipboard",
14307#endif
14308#ifdef FEAT_XTERM_SAVE
14309 "xterm_save",
14310#endif
14311#if defined(UNIX) && defined(FEAT_X11)
14312 "X11",
14313#endif
14314 NULL
14315 };
14316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 for (i = 0; has_list[i] != NULL; ++i)
14319 if (STRICMP(name, has_list[i]) == 0)
14320 {
14321 n = TRUE;
14322 break;
14323 }
14324
14325 if (n == FALSE)
14326 {
14327 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014328 {
14329 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014330 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014331 && vim_isdigit(name[6])
14332 && vim_isdigit(name[8])
14333 && vim_isdigit(name[10]))
14334 {
14335 int major = atoi((char *)name + 6);
14336 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014337
14338 /* Expect "patch-9.9.01234". */
14339 n = (major < VIM_VERSION_MAJOR
14340 || (major == VIM_VERSION_MAJOR
14341 && (minor < VIM_VERSION_MINOR
14342 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014343 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014344 }
14345 else
14346 n = has_patch(atoi((char *)name + 5));
14347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 else if (STRICMP(name, "vim_starting") == 0)
14349 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014350#ifdef FEAT_MBYTE
14351 else if (STRICMP(name, "multi_byte_encoding") == 0)
14352 n = has_mbyte;
14353#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014354#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14355 else if (STRICMP(name, "balloon_multiline") == 0)
14356 n = multiline_balloon_available();
14357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358#ifdef DYNAMIC_TCL
14359 else if (STRICMP(name, "tcl") == 0)
14360 n = tcl_enabled(FALSE);
14361#endif
14362#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14363 else if (STRICMP(name, "iconv") == 0)
14364 n = iconv_enabled(FALSE);
14365#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014366#ifdef DYNAMIC_LUA
14367 else if (STRICMP(name, "lua") == 0)
14368 n = lua_enabled(FALSE);
14369#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014370#ifdef DYNAMIC_MZSCHEME
14371 else if (STRICMP(name, "mzscheme") == 0)
14372 n = mzscheme_enabled(FALSE);
14373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374#ifdef DYNAMIC_RUBY
14375 else if (STRICMP(name, "ruby") == 0)
14376 n = ruby_enabled(FALSE);
14377#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014378#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379#ifdef DYNAMIC_PYTHON
14380 else if (STRICMP(name, "python") == 0)
14381 n = python_enabled(FALSE);
14382#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014383#endif
14384#ifdef FEAT_PYTHON3
14385#ifdef DYNAMIC_PYTHON3
14386 else if (STRICMP(name, "python3") == 0)
14387 n = python3_enabled(FALSE);
14388#endif
14389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390#ifdef DYNAMIC_PERL
14391 else if (STRICMP(name, "perl") == 0)
14392 n = perl_enabled(FALSE);
14393#endif
14394#ifdef FEAT_GUI
14395 else if (STRICMP(name, "gui_running") == 0)
14396 n = (gui.in_use || gui.starting);
14397# ifdef FEAT_GUI_W32
14398 else if (STRICMP(name, "gui_win32s") == 0)
14399 n = gui_is_win32s();
14400# endif
14401# ifdef FEAT_BROWSE
14402 else if (STRICMP(name, "browse") == 0)
14403 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14404# endif
14405#endif
14406#ifdef FEAT_SYN_HL
14407 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014408 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409#endif
14410#if defined(WIN3264)
14411 else if (STRICMP(name, "win95") == 0)
14412 n = mch_windows95();
14413#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014414#ifdef FEAT_NETBEANS_INTG
14415 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014416 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 }
14419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014420 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421}
14422
14423/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014424 * "has_key()" function
14425 */
14426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014427f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014428{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014429 if (argvars[0].v_type != VAR_DICT)
14430 {
14431 EMSG(_(e_dictreq));
14432 return;
14433 }
14434 if (argvars[0].vval.v_dict == NULL)
14435 return;
14436
14437 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014438 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014439}
14440
14441/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014442 * "haslocaldir()" function
14443 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014445f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014446{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014447 win_T *wp = NULL;
14448
14449 wp = find_tabwin(&argvars[0], &argvars[1]);
14450 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014451}
14452
14453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 * "hasmapto()" function
14455 */
14456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014457f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458{
14459 char_u *name;
14460 char_u *mode;
14461 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014462 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014465 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466 mode = (char_u *)"nvo";
14467 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014470 if (argvars[2].v_type != VAR_UNKNOWN)
14471 abbr = get_tv_number(&argvars[2]);
14472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473
Bram Moolenaar2c932302006-03-18 21:42:09 +000014474 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478}
14479
14480/*
14481 * "histadd()" function
14482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014484f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485{
14486#ifdef FEAT_CMDHIST
14487 int histype;
14488 char_u *str;
14489 char_u buf[NUMBUFLEN];
14490#endif
14491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014492 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 if (check_restricted() || check_secure())
14494 return;
14495#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14497 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 if (histype >= 0)
14499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 if (*str != NUL)
14502 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014503 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014505 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 return;
14507 }
14508 }
14509#endif
14510}
14511
14512/*
14513 * "histdel()" function
14514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014516f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517{
14518#ifdef FEAT_CMDHIST
14519 int n;
14520 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014523 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14524 if (str == NULL)
14525 n = 0;
14526 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014528 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014529 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014531 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014532 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533 else
14534 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014535 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536 get_tv_string_buf(&argvars[1], buf));
14537 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538#endif
14539}
14540
14541/*
14542 * "histget()" function
14543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014545f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546{
14547#ifdef FEAT_CMDHIST
14548 int type;
14549 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14553 if (str == NULL)
14554 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014556 {
14557 type = get_histtype(str);
14558 if (argvars[1].v_type == VAR_UNKNOWN)
14559 idx = get_history_idx(type);
14560 else
14561 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14562 /* -1 on type error */
14563 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014568 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569}
14570
14571/*
14572 * "histnr()" function
14573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014575f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576{
14577 int i;
14578
14579#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 char_u *history = get_tv_string_chk(&argvars[0]);
14581
14582 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 if (i >= HIST_CMD && i < HIST_COUNT)
14584 i = get_history_idx(i);
14585 else
14586#endif
14587 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589}
14590
14591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 * "highlightID(name)" function
14593 */
14594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014595f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014597 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598}
14599
14600/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014601 * "highlight_exists()" function
14602 */
14603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014604f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014605{
14606 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14607}
14608
14609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 * "hostname()" function
14611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014613f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614{
14615 char_u hostname[256];
14616
14617 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014618 rettv->v_type = VAR_STRING;
14619 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620}
14621
14622/*
14623 * iconv() function
14624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014626f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627{
14628#ifdef FEAT_MBYTE
14629 char_u buf1[NUMBUFLEN];
14630 char_u buf2[NUMBUFLEN];
14631 char_u *from, *to, *str;
14632 vimconv_T vimconv;
14633#endif
14634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 rettv->v_type = VAR_STRING;
14636 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637
14638#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014639 str = get_tv_string(&argvars[0]);
14640 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14641 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 vimconv.vc_type = CONV_NONE;
14643 convert_setup(&vimconv, from, to);
14644
14645 /* If the encodings are equal, no conversion needed. */
14646 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014649 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650
14651 convert_setup(&vimconv, NULL, NULL);
14652 vim_free(from);
14653 vim_free(to);
14654#endif
14655}
14656
14657/*
14658 * "indent()" function
14659 */
14660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014661f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662{
14663 linenr_T lnum;
14664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014665 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014667 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014669 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670}
14671
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014672/*
14673 * "index()" function
14674 */
14675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014676f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014677{
Bram Moolenaar33570922005-01-25 22:26:29 +000014678 list_T *l;
14679 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014680 long idx = 0;
14681 int ic = FALSE;
14682
14683 rettv->vval.v_number = -1;
14684 if (argvars[0].v_type != VAR_LIST)
14685 {
14686 EMSG(_(e_listreq));
14687 return;
14688 }
14689 l = argvars[0].vval.v_list;
14690 if (l != NULL)
14691 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014692 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014693 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014695 int error = FALSE;
14696
Bram Moolenaar758711c2005-02-02 23:11:38 +000014697 /* Start at specified item. Use the cached index that list_find()
14698 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014699 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014700 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014701 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 ic = get_tv_number_chk(&argvars[3], &error);
14703 if (error)
14704 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014705 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014706
Bram Moolenaar758711c2005-02-02 23:11:38 +000014707 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014708 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014709 {
14710 rettv->vval.v_number = idx;
14711 break;
14712 }
14713 }
14714}
14715
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716static int inputsecret_flag = 0;
14717
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014718static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014719
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014721 * This function is used by f_input() and f_inputdialog() functions. The third
14722 * argument to f_input() specifies the type of completion to use at the
14723 * prompt. The third argument to f_inputdialog() specifies the value to return
14724 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 */
14726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014727get_user_input(
14728 typval_T *argvars,
14729 typval_T *rettv,
14730 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014732 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 char_u *p = NULL;
14734 int c;
14735 char_u buf[NUMBUFLEN];
14736 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014737 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014738 int xp_type = EXPAND_NOTHING;
14739 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014741 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014742 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743
14744#ifdef NO_CONSOLE_INPUT
14745 /* While starting up, there is no place to enter text. */
14746 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748#endif
14749
14750 cmd_silent = FALSE; /* Want to see the prompt. */
14751 if (prompt != NULL)
14752 {
14753 /* Only the part of the message after the last NL is considered as
14754 * prompt for the command line */
14755 p = vim_strrchr(prompt, '\n');
14756 if (p == NULL)
14757 p = prompt;
14758 else
14759 {
14760 ++p;
14761 c = *p;
14762 *p = NUL;
14763 msg_start();
14764 msg_clr_eos();
14765 msg_puts_attr(prompt, echo_attr);
14766 msg_didout = FALSE;
14767 msg_starthere();
14768 *p = c;
14769 }
14770 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014772 if (argvars[1].v_type != VAR_UNKNOWN)
14773 {
14774 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14775 if (defstr != NULL)
14776 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014778 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014779 {
14780 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014781 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014782 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014783
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014784 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014785 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014786
Bram Moolenaar4463f292005-09-25 22:20:24 +000014787 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14788 if (xp_name == NULL)
14789 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014790
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014791 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014792
Bram Moolenaar4463f292005-09-25 22:20:24 +000014793 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14794 &xp_arg) == FAIL)
14795 return;
14796 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014797 }
14798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014800 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014801 int save_ex_normal_busy = ex_normal_busy;
14802 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014803 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014804 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14805 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014806 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014807 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014808 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014809 && argvars[1].v_type != VAR_UNKNOWN
14810 && argvars[2].v_type != VAR_UNKNOWN)
14811 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14812 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014813
14814 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014816 /* since the user typed this, no need to wait for return */
14817 need_wait_return = FALSE;
14818 msg_didout = FALSE;
14819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 cmd_silent = cmd_silent_save;
14821}
14822
14823/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014824 * "input()" function
14825 * Also handles inputsecret() when inputsecret is set.
14826 */
14827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014828f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014829{
14830 get_user_input(argvars, rettv, FALSE);
14831}
14832
14833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 * "inputdialog()" function
14835 */
14836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014837f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838{
14839#if defined(FEAT_GUI_TEXTDIALOG)
14840 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14841 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14842 {
14843 char_u *message;
14844 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014845 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014847 message = get_tv_string_chk(&argvars[0]);
14848 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014849 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014850 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 else
14852 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014853 if (message != NULL && defstr != NULL
14854 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014855 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014856 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857 else
14858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014859 if (message != NULL && defstr != NULL
14860 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014861 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014862 rettv->vval.v_string = vim_strsave(
14863 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014865 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014867 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868 }
14869 else
14870#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014871 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872}
14873
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014874/*
14875 * "inputlist()" function
14876 */
14877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014878f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014879{
14880 listitem_T *li;
14881 int selected;
14882 int mouse_used;
14883
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014884#ifdef NO_CONSOLE_INPUT
14885 /* While starting up, there is no place to enter text. */
14886 if (no_console_input())
14887 return;
14888#endif
14889 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14890 {
14891 EMSG2(_(e_listarg), "inputlist()");
14892 return;
14893 }
14894
14895 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014896 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014897 lines_left = Rows; /* avoid more prompt */
14898 msg_scroll = TRUE;
14899 msg_clr_eos();
14900
14901 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14902 {
14903 msg_puts(get_tv_string(&li->li_tv));
14904 msg_putchar('\n');
14905 }
14906
14907 /* Ask for choice. */
14908 selected = prompt_for_number(&mouse_used);
14909 if (mouse_used)
14910 selected -= lines_left;
14911
14912 rettv->vval.v_number = selected;
14913}
14914
14915
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14917
14918/*
14919 * "inputrestore()" function
14920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014922f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923{
14924 if (ga_userinput.ga_len > 0)
14925 {
14926 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14928 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014929 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 }
14931 else if (p_verbose > 1)
14932 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014933 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014934 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 }
14936}
14937
14938/*
14939 * "inputsave()" function
14940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014942f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014944 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 if (ga_grow(&ga_userinput, 1) == OK)
14946 {
14947 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14948 + ga_userinput.ga_len);
14949 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014950 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 }
14952 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954}
14955
14956/*
14957 * "inputsecret()" function
14958 */
14959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014960f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961{
14962 ++cmdline_star;
14963 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014964 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965 --cmdline_star;
14966 --inputsecret_flag;
14967}
14968
14969/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014970 * "insert()" function
14971 */
14972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014973f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014974{
14975 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 listitem_T *item;
14977 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014978 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014979
14980 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014981 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014982 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014983 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014984 {
14985 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014986 before = get_tv_number_chk(&argvars[2], &error);
14987 if (error)
14988 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014989
Bram Moolenaar758711c2005-02-02 23:11:38 +000014990 if (before == l->lv_len)
14991 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014992 else
14993 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014994 item = list_find(l, before);
14995 if (item == NULL)
14996 {
14997 EMSGN(_(e_listidx), before);
14998 l = NULL;
14999 }
15000 }
15001 if (l != NULL)
15002 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015003 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015004 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015005 }
15006 }
15007}
15008
15009/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015010 * "invert(expr)" function
15011 */
15012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015013f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015014{
15015 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15016}
15017
15018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019 * "isdirectory()" function
15020 */
15021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015022f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015024 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025}
15026
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015027/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015028 * "islocked()" function
15029 */
15030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015031f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015032{
15033 lval_T lv;
15034 char_u *end;
15035 dictitem_T *di;
15036
15037 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015038 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15039 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015040 if (end != NULL && lv.ll_name != NULL)
15041 {
15042 if (*end != NUL)
15043 EMSG(_(e_trailing));
15044 else
15045 {
15046 if (lv.ll_tv == NULL)
15047 {
15048 if (check_changedtick(lv.ll_name))
15049 rettv->vval.v_number = 1; /* always locked */
15050 else
15051 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015052 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015053 if (di != NULL)
15054 {
15055 /* Consider a variable locked when:
15056 * 1. the variable itself is locked
15057 * 2. the value of the variable is locked.
15058 * 3. the List or Dict value is locked.
15059 */
15060 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15061 || tv_islocked(&di->di_tv));
15062 }
15063 }
15064 }
15065 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015066 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015067 else if (lv.ll_newkey != NULL)
15068 EMSG2(_(e_dictkey), lv.ll_newkey);
15069 else if (lv.ll_list != NULL)
15070 /* List item. */
15071 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15072 else
15073 /* Dictionary item. */
15074 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15075 }
15076 }
15077
15078 clear_lval(&lv);
15079}
15080
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015081#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15082/*
15083 * "isnan()" function
15084 */
15085 static void
15086f_isnan(typval_T *argvars, typval_T *rettv)
15087{
15088 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15089 && isnan(argvars[0].vval.v_float);
15090}
15091#endif
15092
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015093static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015094
15095/*
15096 * Turn a dict into a list:
15097 * "what" == 0: list of keys
15098 * "what" == 1: list of values
15099 * "what" == 2: list of items
15100 */
15101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015102dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015103{
Bram Moolenaar33570922005-01-25 22:26:29 +000015104 list_T *l2;
15105 dictitem_T *di;
15106 hashitem_T *hi;
15107 listitem_T *li;
15108 listitem_T *li2;
15109 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015110 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015111
Bram Moolenaar8c711452005-01-14 21:53:12 +000015112 if (argvars[0].v_type != VAR_DICT)
15113 {
15114 EMSG(_(e_dictreq));
15115 return;
15116 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015117 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015118 return;
15119
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015120 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015121 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015122
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015123 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015124 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015125 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015126 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015127 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015128 --todo;
15129 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015130
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015131 li = listitem_alloc();
15132 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015133 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015134 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015135
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015136 if (what == 0)
15137 {
15138 /* keys() */
15139 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015140 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015141 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15142 }
15143 else if (what == 1)
15144 {
15145 /* values() */
15146 copy_tv(&di->di_tv, &li->li_tv);
15147 }
15148 else
15149 {
15150 /* items() */
15151 l2 = list_alloc();
15152 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015153 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015154 li->li_tv.vval.v_list = l2;
15155 if (l2 == NULL)
15156 break;
15157 ++l2->lv_refcount;
15158
15159 li2 = listitem_alloc();
15160 if (li2 == NULL)
15161 break;
15162 list_append(l2, li2);
15163 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015164 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015165 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15166
15167 li2 = listitem_alloc();
15168 if (li2 == NULL)
15169 break;
15170 list_append(l2, li2);
15171 copy_tv(&di->di_tv, &li2->li_tv);
15172 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015173 }
15174 }
15175}
15176
15177/*
15178 * "items(dict)" function
15179 */
15180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015181f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015182{
15183 dict_list(argvars, rettv, 2);
15184}
15185
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015186#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015187/*
15188 * Get the job from the argument.
15189 * Returns NULL if the job is invalid.
15190 */
15191 static job_T *
15192get_job_arg(typval_T *tv)
15193{
15194 job_T *job;
15195
15196 if (tv->v_type != VAR_JOB)
15197 {
15198 EMSG2(_(e_invarg2), get_tv_string(tv));
15199 return NULL;
15200 }
15201 job = tv->vval.v_job;
15202
15203 if (job == NULL)
15204 EMSG(_("E916: not a valid job"));
15205 return job;
15206}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015207
Bram Moolenaar835dc632016-02-07 14:27:38 +010015208/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015209 * "job_getchannel()" function
15210 */
15211 static void
15212f_job_getchannel(typval_T *argvars, typval_T *rettv)
15213{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015214 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015215
Bram Moolenaar65edff82016-02-21 16:40:11 +010015216 if (job != NULL)
15217 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015218 rettv->v_type = VAR_CHANNEL;
15219 rettv->vval.v_channel = job->jv_channel;
15220 if (job->jv_channel != NULL)
15221 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015222 }
15223}
15224
15225/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015226 * "job_info()" function
15227 */
15228 static void
15229f_job_info(typval_T *argvars, typval_T *rettv)
15230{
15231 job_T *job = get_job_arg(&argvars[0]);
15232
15233 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15234 job_info(job, rettv->vval.v_dict);
15235}
15236
15237/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015238 * "job_setoptions()" function
15239 */
15240 static void
15241f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15242{
15243 job_T *job = get_job_arg(&argvars[0]);
15244 jobopt_T opt;
15245
15246 if (job == NULL)
15247 return;
15248 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015249 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15250 job_set_options(job, &opt);
15251 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015252}
15253
15254/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015255 * "job_start()" function
15256 */
15257 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015258f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015259{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015260 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015261 if (check_restricted() || check_secure())
15262 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015263 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015264}
15265
15266/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015267 * "job_status()" function
15268 */
15269 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015270f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015271{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015272 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015273
Bram Moolenaar65edff82016-02-21 16:40:11 +010015274 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015275 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015276 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015277 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015278 }
15279}
15280
15281/*
15282 * "job_stop()" function
15283 */
15284 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015285f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015286{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015287 job_T *job = get_job_arg(&argvars[0]);
15288
15289 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015290 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015291}
15292#endif
15293
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015295 * "join()" function
15296 */
15297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015298f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015299{
15300 garray_T ga;
15301 char_u *sep;
15302
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015303 if (argvars[0].v_type != VAR_LIST)
15304 {
15305 EMSG(_(e_listreq));
15306 return;
15307 }
15308 if (argvars[0].vval.v_list == NULL)
15309 return;
15310 if (argvars[1].v_type == VAR_UNKNOWN)
15311 sep = (char_u *)" ";
15312 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015313 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015314
15315 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015316
15317 if (sep != NULL)
15318 {
15319 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015320 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015321 ga_append(&ga, NUL);
15322 rettv->vval.v_string = (char_u *)ga.ga_data;
15323 }
15324 else
15325 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015326}
15327
15328/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015329 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015330 */
15331 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015332f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015333{
15334 js_read_T reader;
15335
15336 reader.js_buf = get_tv_string(&argvars[0]);
15337 reader.js_fill = NULL;
15338 reader.js_used = 0;
15339 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15340 EMSG(_(e_invarg));
15341}
15342
15343/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015344 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015345 */
15346 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015347f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015348{
15349 rettv->v_type = VAR_STRING;
15350 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15351}
15352
15353/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015354 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015355 */
15356 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015357f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015358{
15359 js_read_T reader;
15360
15361 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015362 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015363 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015364 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015365 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015366}
15367
15368/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015369 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015370 */
15371 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015372f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015373{
15374 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015375 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015376}
15377
15378/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015379 * "keys()" function
15380 */
15381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015382f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015383{
15384 dict_list(argvars, rettv, 0);
15385}
15386
15387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 * "last_buffer_nr()" function.
15389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015391f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392{
15393 int n = 0;
15394 buf_T *buf;
15395
15396 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15397 if (n < buf->b_fnum)
15398 n = buf->b_fnum;
15399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015401}
15402
15403/*
15404 * "len()" function
15405 */
15406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015407f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015408{
15409 switch (argvars[0].v_type)
15410 {
15411 case VAR_STRING:
15412 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413 rettv->vval.v_number = (varnumber_T)STRLEN(
15414 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015415 break;
15416 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015418 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015419 case VAR_DICT:
15420 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15421 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015422 case VAR_UNKNOWN:
15423 case VAR_SPECIAL:
15424 case VAR_FLOAT:
15425 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015426 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015427 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015428 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015429 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015430 break;
15431 }
15432}
15433
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015434static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015435
15436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015437libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015438{
15439#ifdef FEAT_LIBCALL
15440 char_u *string_in;
15441 char_u **string_result;
15442 int nr_result;
15443#endif
15444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015445 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015446 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015448
15449 if (check_restricted() || check_secure())
15450 return;
15451
15452#ifdef FEAT_LIBCALL
15453 /* The first two args must be strings, otherwise its meaningless */
15454 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15455 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015456 string_in = NULL;
15457 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015458 string_in = argvars[2].vval.v_string;
15459 if (type == VAR_NUMBER)
15460 string_result = NULL;
15461 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015462 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015463 if (mch_libcall(argvars[0].vval.v_string,
15464 argvars[1].vval.v_string,
15465 string_in,
15466 argvars[2].vval.v_number,
15467 string_result,
15468 &nr_result) == OK
15469 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015470 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015471 }
15472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473}
15474
15475/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015476 * "libcall()" function
15477 */
15478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015479f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015480{
15481 libcall_common(argvars, rettv, VAR_STRING);
15482}
15483
15484/*
15485 * "libcallnr()" function
15486 */
15487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015488f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015489{
15490 libcall_common(argvars, rettv, VAR_NUMBER);
15491}
15492
15493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 * "line(string)" function
15495 */
15496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015497f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498{
15499 linenr_T lnum = 0;
15500 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015501 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015503 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 if (fp != NULL)
15505 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015506 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507}
15508
15509/*
15510 * "line2byte(lnum)" function
15511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015513f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514{
15515#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015516 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517#else
15518 linenr_T lnum;
15519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015522 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015524 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15525 if (rettv->vval.v_number >= 0)
15526 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527#endif
15528}
15529
15530/*
15531 * "lispindent(lnum)" function
15532 */
15533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015534f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535{
15536#ifdef FEAT_LISP
15537 pos_T pos;
15538 linenr_T lnum;
15539
15540 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015541 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15543 {
15544 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015545 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 curwin->w_cursor = pos;
15547 }
15548 else
15549#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015550 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551}
15552
15553/*
15554 * "localtime()" function
15555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015557f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560}
15561
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015562static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563
15564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015565get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566{
15567 char_u *keys;
15568 char_u *which;
15569 char_u buf[NUMBUFLEN];
15570 char_u *keys_buf = NULL;
15571 char_u *rhs;
15572 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015573 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015574 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015575 mapblock_T *mp;
15576 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577
15578 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015579 rettv->v_type = VAR_STRING;
15580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015582 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 if (*keys == NUL)
15584 return;
15585
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015586 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015588 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015590 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015591 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015592 if (argvars[3].v_type != VAR_UNKNOWN)
15593 get_dict = get_tv_number(&argvars[3]);
15594 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596 else
15597 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015598 if (which == NULL)
15599 return;
15600
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 mode = get_map_mode(&which, 0);
15602
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015603 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015604 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015606
15607 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015609 /* Return a string. */
15610 if (rhs != NULL)
15611 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612
Bram Moolenaarbd743252010-10-20 21:23:33 +020015613 }
15614 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15615 {
15616 /* Return a dictionary. */
15617 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15618 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15619 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620
Bram Moolenaarbd743252010-10-20 21:23:33 +020015621 dict_add_nr_str(dict, "lhs", 0L, lhs);
15622 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15623 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15624 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15625 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15626 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15627 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015628 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015629 dict_add_nr_str(dict, "mode", 0L, mapmode);
15630
15631 vim_free(lhs);
15632 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 }
15634}
15635
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015636#ifdef FEAT_FLOAT
15637/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015638 * "log()" function
15639 */
15640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015641f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015642{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015643 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015644
15645 rettv->v_type = VAR_FLOAT;
15646 if (get_float_arg(argvars, &f) == OK)
15647 rettv->vval.v_float = log(f);
15648 else
15649 rettv->vval.v_float = 0.0;
15650}
15651
15652/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015653 * "log10()" function
15654 */
15655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015656f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015657{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015658 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015659
15660 rettv->v_type = VAR_FLOAT;
15661 if (get_float_arg(argvars, &f) == OK)
15662 rettv->vval.v_float = log10(f);
15663 else
15664 rettv->vval.v_float = 0.0;
15665}
15666#endif
15667
Bram Moolenaar1dced572012-04-05 16:54:08 +020015668#ifdef FEAT_LUA
15669/*
15670 * "luaeval()" function
15671 */
15672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015673f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015674{
15675 char_u *str;
15676 char_u buf[NUMBUFLEN];
15677
15678 str = get_tv_string_buf(&argvars[0], buf);
15679 do_luaeval(str, argvars + 1, rettv);
15680}
15681#endif
15682
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015684 * "map()" function
15685 */
15686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015687f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015688{
15689 filter_map(argvars, rettv, TRUE);
15690}
15691
15692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015693 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 */
15695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015696f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015698 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699}
15700
15701/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015702 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703 */
15704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015705f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015707 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708}
15709
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015710static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711
15712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015713find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015715 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015716 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015717 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 char_u *pat;
15719 regmatch_T regmatch;
15720 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015721 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 char_u *save_cpo;
15723 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015724 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015725 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015726 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015727 list_T *l = NULL;
15728 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015729 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015730 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731
15732 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15733 save_cpo = p_cpo;
15734 p_cpo = (char_u *)"";
15735
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015736 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015737 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015738 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015739 /* type 3: return empty list when there are no matches.
15740 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015741 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015742 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015743 if (type == 4
15744 && (list_append_string(rettv->vval.v_list,
15745 (char_u *)"", 0) == 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 || list_append_number(rettv->vval.v_list,
15751 (varnumber_T)-1) == FAIL))
15752 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015753 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015754 rettv->vval.v_list = NULL;
15755 goto theend;
15756 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015757 }
15758 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015760 rettv->v_type = VAR_STRING;
15761 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015764 if (argvars[0].v_type == VAR_LIST)
15765 {
15766 if ((l = argvars[0].vval.v_list) == NULL)
15767 goto theend;
15768 li = l->lv_first;
15769 }
15770 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015771 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015772 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015773 len = (long)STRLEN(str);
15774 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015776 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15777 if (pat == NULL)
15778 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015779
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015780 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015782 int error = FALSE;
15783
15784 start = get_tv_number_chk(&argvars[2], &error);
15785 if (error)
15786 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015787 if (l != NULL)
15788 {
15789 li = list_find(l, start);
15790 if (li == NULL)
15791 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015792 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015793 }
15794 else
15795 {
15796 if (start < 0)
15797 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015798 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015799 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015800 /* When "count" argument is there ignore matches before "start",
15801 * otherwise skip part of the string. Differs when pattern is "^"
15802 * or "\<". */
15803 if (argvars[3].v_type != VAR_UNKNOWN)
15804 startcol = start;
15805 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015806 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015807 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015808 len -= start;
15809 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015810 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015811
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015812 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015813 nth = get_tv_number_chk(&argvars[3], &error);
15814 if (error)
15815 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 }
15817
15818 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15819 if (regmatch.regprog != NULL)
15820 {
15821 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015822
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015823 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015824 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015825 if (l != NULL)
15826 {
15827 if (li == NULL)
15828 {
15829 match = FALSE;
15830 break;
15831 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015832 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015833 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015834 if (str == NULL)
15835 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015836 }
15837
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015838 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015839
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015840 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015841 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015842 if (l == NULL && !match)
15843 break;
15844
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015845 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015846 if (l != NULL)
15847 {
15848 li = li->li_next;
15849 ++idx;
15850 }
15851 else
15852 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015853#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015854 startcol = (colnr_T)(regmatch.startp[0]
15855 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015856#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015857 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015858#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015859 if (startcol > (colnr_T)len
15860 || str + startcol <= regmatch.startp[0])
15861 {
15862 match = FALSE;
15863 break;
15864 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015865 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015866 }
15867
15868 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015870 if (type == 4)
15871 {
15872 listitem_T *li1 = rettv->vval.v_list->lv_first;
15873 listitem_T *li2 = li1->li_next;
15874 listitem_T *li3 = li2->li_next;
15875 listitem_T *li4 = li3->li_next;
15876
Bram Moolenaar3c809342016-06-01 22:34:48 +020015877 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015878 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15879 (int)(regmatch.endp[0] - regmatch.startp[0]));
15880 li3->li_tv.vval.v_number =
15881 (varnumber_T)(regmatch.startp[0] - expr);
15882 li4->li_tv.vval.v_number =
15883 (varnumber_T)(regmatch.endp[0] - expr);
15884 if (l != NULL)
15885 li2->li_tv.vval.v_number = (varnumber_T)idx;
15886 }
15887 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015888 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015889 int i;
15890
15891 /* return list with matched string and submatches */
15892 for (i = 0; i < NSUBEXP; ++i)
15893 {
15894 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015895 {
15896 if (list_append_string(rettv->vval.v_list,
15897 (char_u *)"", 0) == FAIL)
15898 break;
15899 }
15900 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015901 regmatch.startp[i],
15902 (int)(regmatch.endp[i] - regmatch.startp[i]))
15903 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015904 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015905 }
15906 }
15907 else if (type == 2)
15908 {
15909 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015910 if (l != NULL)
15911 copy_tv(&li->li_tv, rettv);
15912 else
15913 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015915 }
15916 else if (l != NULL)
15917 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918 else
15919 {
15920 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015921 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 (varnumber_T)(regmatch.startp[0] - str);
15923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015924 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015926 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927 }
15928 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015929 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 }
15931
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015932 if (type == 4 && l == NULL)
15933 /* matchstrpos() without a list: drop the second item. */
15934 listitem_remove(rettv->vval.v_list,
15935 rettv->vval.v_list->lv_first->li_next);
15936
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015938 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939 p_cpo = save_cpo;
15940}
15941
15942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015943 * "match()" function
15944 */
15945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015946f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015947{
15948 find_some_match(argvars, rettv, 1);
15949}
15950
15951/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015952 * "matchadd()" function
15953 */
15954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015955f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015956{
15957#ifdef FEAT_SEARCH_EXTRA
15958 char_u buf[NUMBUFLEN];
15959 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15960 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15961 int prio = 10; /* default priority */
15962 int id = -1;
15963 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015964 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015965
15966 rettv->vval.v_number = -1;
15967
15968 if (grp == NULL || pat == NULL)
15969 return;
15970 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015971 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015972 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015973 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015974 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015975 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015976 if (argvars[4].v_type != VAR_UNKNOWN)
15977 {
15978 if (argvars[4].v_type != VAR_DICT)
15979 {
15980 EMSG(_(e_dictreq));
15981 return;
15982 }
15983 if (dict_find(argvars[4].vval.v_dict,
15984 (char_u *)"conceal", -1) != NULL)
15985 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15986 (char_u *)"conceal", FALSE);
15987 }
15988 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015989 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015990 if (error == TRUE)
15991 return;
15992 if (id >= 1 && id <= 3)
15993 {
15994 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15995 return;
15996 }
15997
Bram Moolenaar6561d522015-07-21 15:48:27 +020015998 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15999 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016000#endif
16001}
16002
16003/*
16004 * "matchaddpos()" function
16005 */
16006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016007f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016008{
16009#ifdef FEAT_SEARCH_EXTRA
16010 char_u buf[NUMBUFLEN];
16011 char_u *group;
16012 int prio = 10;
16013 int id = -1;
16014 int error = FALSE;
16015 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016016 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016017
16018 rettv->vval.v_number = -1;
16019
16020 group = get_tv_string_buf_chk(&argvars[0], buf);
16021 if (group == NULL)
16022 return;
16023
16024 if (argvars[1].v_type != VAR_LIST)
16025 {
16026 EMSG2(_(e_listarg), "matchaddpos()");
16027 return;
16028 }
16029 l = argvars[1].vval.v_list;
16030 if (l == NULL)
16031 return;
16032
16033 if (argvars[2].v_type != VAR_UNKNOWN)
16034 {
16035 prio = get_tv_number_chk(&argvars[2], &error);
16036 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016037 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016038 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016039 if (argvars[4].v_type != VAR_UNKNOWN)
16040 {
16041 if (argvars[4].v_type != VAR_DICT)
16042 {
16043 EMSG(_(e_dictreq));
16044 return;
16045 }
16046 if (dict_find(argvars[4].vval.v_dict,
16047 (char_u *)"conceal", -1) != NULL)
16048 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16049 (char_u *)"conceal", FALSE);
16050 }
16051 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016052 }
16053 if (error == TRUE)
16054 return;
16055
16056 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16057 if (id == 1 || id == 2)
16058 {
16059 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16060 return;
16061 }
16062
Bram Moolenaar6561d522015-07-21 15:48:27 +020016063 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16064 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016065#endif
16066}
16067
16068/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016069 * "matcharg()" function
16070 */
16071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016072f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016073{
16074 if (rettv_list_alloc(rettv) == OK)
16075 {
16076#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016077 int id = get_tv_number(&argvars[0]);
16078 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016079
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016080 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016081 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016082 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16083 {
16084 list_append_string(rettv->vval.v_list,
16085 syn_id2name(m->hlg_id), -1);
16086 list_append_string(rettv->vval.v_list, m->pattern, -1);
16087 }
16088 else
16089 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016090 list_append_string(rettv->vval.v_list, NULL, -1);
16091 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016092 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016093 }
16094#endif
16095 }
16096}
16097
16098/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016099 * "matchdelete()" function
16100 */
16101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016102f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016103{
16104#ifdef FEAT_SEARCH_EXTRA
16105 rettv->vval.v_number = match_delete(curwin,
16106 (int)get_tv_number(&argvars[0]), TRUE);
16107#endif
16108}
16109
16110/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016111 * "matchend()" function
16112 */
16113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016114f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016115{
16116 find_some_match(argvars, rettv, 0);
16117}
16118
16119/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016120 * "matchlist()" function
16121 */
16122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016123f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016124{
16125 find_some_match(argvars, rettv, 3);
16126}
16127
16128/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016129 * "matchstr()" function
16130 */
16131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016132f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016133{
16134 find_some_match(argvars, rettv, 2);
16135}
16136
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016137/*
16138 * "matchstrpos()" function
16139 */
16140 static void
16141f_matchstrpos(typval_T *argvars, typval_T *rettv)
16142{
16143 find_some_match(argvars, rettv, 4);
16144}
16145
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016146static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016147
16148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016149max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016150{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016151 long n = 0;
16152 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016153 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016154
16155 if (argvars[0].v_type == VAR_LIST)
16156 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016157 list_T *l;
16158 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016159
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016160 l = argvars[0].vval.v_list;
16161 if (l != NULL)
16162 {
16163 li = l->lv_first;
16164 if (li != NULL)
16165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016166 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016167 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016168 {
16169 li = li->li_next;
16170 if (li == NULL)
16171 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016172 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016173 if (domax ? i > n : i < n)
16174 n = i;
16175 }
16176 }
16177 }
16178 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016179 else if (argvars[0].v_type == VAR_DICT)
16180 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016182 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016183 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016184 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016185
16186 d = argvars[0].vval.v_dict;
16187 if (d != NULL)
16188 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016189 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016190 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016191 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016192 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016193 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016194 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016195 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016196 if (first)
16197 {
16198 n = i;
16199 first = FALSE;
16200 }
16201 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016202 n = i;
16203 }
16204 }
16205 }
16206 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016207 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016208 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016209 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016210}
16211
16212/*
16213 * "max()" function
16214 */
16215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016216f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016217{
16218 max_min(argvars, rettv, TRUE);
16219}
16220
16221/*
16222 * "min()" function
16223 */
16224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016225f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016226{
16227 max_min(argvars, rettv, FALSE);
16228}
16229
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016230static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016231
16232/*
16233 * Create the directory in which "dir" is located, and higher levels when
16234 * needed.
16235 */
16236 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016237mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016238{
16239 char_u *p;
16240 char_u *updir;
16241 int r = FAIL;
16242
16243 /* Get end of directory name in "dir".
16244 * We're done when it's "/" or "c:/". */
16245 p = gettail_sep(dir);
16246 if (p <= get_past_head(dir))
16247 return OK;
16248
16249 /* If the directory exists we're done. Otherwise: create it.*/
16250 updir = vim_strnsave(dir, (int)(p - dir));
16251 if (updir == NULL)
16252 return FAIL;
16253 if (mch_isdir(updir))
16254 r = OK;
16255 else if (mkdir_recurse(updir, prot) == OK)
16256 r = vim_mkdir_emsg(updir, prot);
16257 vim_free(updir);
16258 return r;
16259}
16260
16261#ifdef vim_mkdir
16262/*
16263 * "mkdir()" function
16264 */
16265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016266f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016267{
16268 char_u *dir;
16269 char_u buf[NUMBUFLEN];
16270 int prot = 0755;
16271
16272 rettv->vval.v_number = FAIL;
16273 if (check_restricted() || check_secure())
16274 return;
16275
16276 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016277 if (*dir == NUL)
16278 rettv->vval.v_number = FAIL;
16279 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016280 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016281 if (*gettail(dir) == NUL)
16282 /* remove trailing slashes */
16283 *gettail_sep(dir) = NUL;
16284
16285 if (argvars[1].v_type != VAR_UNKNOWN)
16286 {
16287 if (argvars[2].v_type != VAR_UNKNOWN)
16288 prot = get_tv_number_chk(&argvars[2], NULL);
16289 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16290 mkdir_recurse(dir, prot);
16291 }
16292 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016293 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016294}
16295#endif
16296
Bram Moolenaar0d660222005-01-07 21:51:51 +000016297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 * "mode()" function
16299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016301f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016303 char_u buf[3];
16304
16305 buf[1] = NUL;
16306 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 if (VIsual_active)
16309 {
16310 if (VIsual_select)
16311 buf[0] = VIsual_mode + 's' - 'v';
16312 else
16313 buf[0] = VIsual_mode;
16314 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016315 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016316 || State == CONFIRM)
16317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016319 if (State == ASKMORE)
16320 buf[1] = 'm';
16321 else if (State == CONFIRM)
16322 buf[1] = '?';
16323 }
16324 else if (State == EXTERNCMD)
16325 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326 else if (State & INSERT)
16327 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016328#ifdef FEAT_VREPLACE
16329 if (State & VREPLACE_FLAG)
16330 {
16331 buf[0] = 'R';
16332 buf[1] = 'v';
16333 }
16334 else
16335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 if (State & REPLACE_FLAG)
16337 buf[0] = 'R';
16338 else
16339 buf[0] = 'i';
16340 }
16341 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016344 if (exmode_active)
16345 buf[1] = 'v';
16346 }
16347 else if (exmode_active)
16348 {
16349 buf[0] = 'c';
16350 buf[1] = 'e';
16351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016355 if (finish_op)
16356 buf[1] = 'o';
16357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016359 /* Clear out the minor mode when the argument is not a non-zero number or
16360 * non-empty string. */
16361 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016362 buf[1] = NUL;
16363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016364 rettv->vval.v_string = vim_strsave(buf);
16365 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366}
16367
Bram Moolenaar429fa852013-04-15 12:27:36 +020016368#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016369/*
16370 * "mzeval()" function
16371 */
16372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016373f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016374{
16375 char_u *str;
16376 char_u buf[NUMBUFLEN];
16377
16378 str = get_tv_string_buf(&argvars[0], buf);
16379 do_mzeval(str, rettv);
16380}
Bram Moolenaar75676462013-01-30 14:55:42 +010016381
16382 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016383mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016384{
16385 typval_T argvars[3];
16386
16387 argvars[0].v_type = VAR_STRING;
16388 argvars[0].vval.v_string = name;
16389 copy_tv(args, &argvars[1]);
16390 argvars[2].v_type = VAR_UNKNOWN;
16391 f_call(argvars, rettv);
16392 clear_tv(&argvars[1]);
16393}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016394#endif
16395
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397 * "nextnonblank()" function
16398 */
16399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016400f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016401{
16402 linenr_T lnum;
16403
16404 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016406 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016407 {
16408 lnum = 0;
16409 break;
16410 }
16411 if (*skipwhite(ml_get(lnum)) != NUL)
16412 break;
16413 }
16414 rettv->vval.v_number = lnum;
16415}
16416
16417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 * "nr2char()" function
16419 */
16420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016421f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422{
16423 char_u buf[NUMBUFLEN];
16424
16425#ifdef FEAT_MBYTE
16426 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016427 {
16428 int utf8 = 0;
16429
16430 if (argvars[1].v_type != VAR_UNKNOWN)
16431 utf8 = get_tv_number_chk(&argvars[1], NULL);
16432 if (utf8)
16433 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16434 else
16435 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437 else
16438#endif
16439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016440 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441 buf[1] = NUL;
16442 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016443 rettv->v_type = VAR_STRING;
16444 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016445}
16446
16447/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016448 * "or(expr, expr)" function
16449 */
16450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016451f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016452{
16453 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16454 | get_tv_number_chk(&argvars[1], NULL);
16455}
16456
16457/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016458 * "pathshorten()" function
16459 */
16460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016461f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016462{
16463 char_u *p;
16464
16465 rettv->v_type = VAR_STRING;
16466 p = get_tv_string_chk(&argvars[0]);
16467 if (p == NULL)
16468 rettv->vval.v_string = NULL;
16469 else
16470 {
16471 p = vim_strsave(p);
16472 rettv->vval.v_string = p;
16473 if (p != NULL)
16474 shorten_dir(p);
16475 }
16476}
16477
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016478#ifdef FEAT_PERL
16479/*
16480 * "perleval()" function
16481 */
16482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016483f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016484{
16485 char_u *str;
16486 char_u buf[NUMBUFLEN];
16487
16488 str = get_tv_string_buf(&argvars[0], buf);
16489 do_perleval(str, rettv);
16490}
16491#endif
16492
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016493#ifdef FEAT_FLOAT
16494/*
16495 * "pow()" function
16496 */
16497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016498f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016499{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016500 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016501
16502 rettv->v_type = VAR_FLOAT;
16503 if (get_float_arg(argvars, &fx) == OK
16504 && get_float_arg(&argvars[1], &fy) == OK)
16505 rettv->vval.v_float = pow(fx, fy);
16506 else
16507 rettv->vval.v_float = 0.0;
16508}
16509#endif
16510
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512 * "prevnonblank()" function
16513 */
16514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016515f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016516{
16517 linenr_T lnum;
16518
16519 lnum = get_tv_lnum(argvars);
16520 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16521 lnum = 0;
16522 else
16523 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16524 --lnum;
16525 rettv->vval.v_number = lnum;
16526}
16527
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016528/* This dummy va_list is here because:
16529 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16530 * - locally in the function results in a "used before set" warning
16531 * - using va_start() to initialize it gives "function with fixed args" error */
16532static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016533
Bram Moolenaar8c711452005-01-14 21:53:12 +000016534/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016535 * "printf()" function
16536 */
16537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016538f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016539{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016540 char_u buf[NUMBUFLEN];
16541 int len;
16542 char_u *s;
16543 int saved_did_emsg = did_emsg;
16544 char *fmt;
16545
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016546 rettv->v_type = VAR_STRING;
16547 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016548
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016549 /* Get the required length, allocate the buffer and do it for real. */
16550 did_emsg = FALSE;
16551 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16552 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16553 if (!did_emsg)
16554 {
16555 s = alloc(len + 1);
16556 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016557 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016558 rettv->vval.v_string = s;
16559 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016560 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016561 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016562 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016563}
16564
16565/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016566 * "pumvisible()" function
16567 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016569f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016570{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016571#ifdef FEAT_INS_EXPAND
16572 if (pum_visible())
16573 rettv->vval.v_number = 1;
16574#endif
16575}
16576
Bram Moolenaardb913952012-06-29 12:54:53 +020016577#ifdef FEAT_PYTHON3
16578/*
16579 * "py3eval()" function
16580 */
16581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016582f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016583{
16584 char_u *str;
16585 char_u buf[NUMBUFLEN];
16586
16587 str = get_tv_string_buf(&argvars[0], buf);
16588 do_py3eval(str, rettv);
16589}
16590#endif
16591
16592#ifdef FEAT_PYTHON
16593/*
16594 * "pyeval()" function
16595 */
16596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016597f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016598{
16599 char_u *str;
16600 char_u buf[NUMBUFLEN];
16601
16602 str = get_tv_string_buf(&argvars[0], buf);
16603 do_pyeval(str, rettv);
16604}
16605#endif
16606
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016607/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016608 * "range()" function
16609 */
16610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016611f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016612{
16613 long start;
16614 long end;
16615 long stride = 1;
16616 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016617 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016619 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016620 if (argvars[1].v_type == VAR_UNKNOWN)
16621 {
16622 end = start - 1;
16623 start = 0;
16624 }
16625 else
16626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016627 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016628 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016629 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016630 }
16631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016632 if (error)
16633 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016634 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016635 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016636 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016637 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016638 else
16639 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016640 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016641 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016642 if (list_append_number(rettv->vval.v_list,
16643 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016644 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016645 }
16646}
16647
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016648/*
16649 * "readfile()" function
16650 */
16651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016652f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016653{
16654 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016655 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016656 char_u *fname;
16657 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016658 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16659 int io_size = sizeof(buf);
16660 int readlen; /* size of last fread() */
16661 char_u *prev = NULL; /* previously read bytes, if any */
16662 long prevlen = 0; /* length of data in prev */
16663 long prevsize = 0; /* size of prev buffer */
16664 long maxline = MAXLNUM;
16665 long cnt = 0;
16666 char_u *p; /* position in buf */
16667 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016668
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016669 if (argvars[1].v_type != VAR_UNKNOWN)
16670 {
16671 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16672 binary = TRUE;
16673 if (argvars[2].v_type != VAR_UNKNOWN)
16674 maxline = get_tv_number(&argvars[2]);
16675 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016676
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016677 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016678 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016679
16680 /* Always open the file in binary mode, library functions have a mind of
16681 * their own about CR-LF conversion. */
16682 fname = get_tv_string(&argvars[0]);
16683 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16684 {
16685 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16686 return;
16687 }
16688
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016689 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016690 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016691 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016692
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016693 /* This for loop processes what was read, but is also entered at end
16694 * of file so that either:
16695 * - an incomplete line gets written
16696 * - a "binary" file gets an empty line at the end if it ends in a
16697 * newline. */
16698 for (p = buf, start = buf;
16699 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16700 ++p)
16701 {
16702 if (*p == '\n' || readlen <= 0)
16703 {
16704 listitem_T *li;
16705 char_u *s = NULL;
16706 long_u len = p - start;
16707
16708 /* Finished a line. Remove CRs before NL. */
16709 if (readlen > 0 && !binary)
16710 {
16711 while (len > 0 && start[len - 1] == '\r')
16712 --len;
16713 /* removal may cross back to the "prev" string */
16714 if (len == 0)
16715 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16716 --prevlen;
16717 }
16718 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016719 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016720 else
16721 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016722 /* Change "prev" buffer to be the right size. This way
16723 * the bytes are only copied once, and very long lines are
16724 * allocated only once. */
16725 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016726 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016727 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016728 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016729 prev = NULL; /* the list will own the string */
16730 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016731 }
16732 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016733 if (s == NULL)
16734 {
16735 do_outofmem_msg((long_u) prevlen + len + 1);
16736 failed = TRUE;
16737 break;
16738 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016739
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016740 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016741 {
16742 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016743 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016744 break;
16745 }
16746 li->li_tv.v_type = VAR_STRING;
16747 li->li_tv.v_lock = 0;
16748 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016749 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016750
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016751 start = p + 1; /* step over newline */
16752 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016753 break;
16754 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016755 else if (*p == NUL)
16756 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016757#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016758 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16759 * when finding the BF and check the previous two bytes. */
16760 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016761 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016762 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16763 * + 1, these may be in the "prev" string. */
16764 char_u back1 = p >= buf + 1 ? p[-1]
16765 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16766 char_u back2 = p >= buf + 2 ? p[-2]
16767 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16768 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016769
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016770 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016771 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016772 char_u *dest = p - 2;
16773
16774 /* Usually a BOM is at the beginning of a file, and so at
16775 * the beginning of a line; then we can just step over it.
16776 */
16777 if (start == dest)
16778 start = p + 1;
16779 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016780 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016781 /* have to shuffle buf to close gap */
16782 int adjust_prevlen = 0;
16783
16784 if (dest < buf)
16785 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016786 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016787 dest = buf;
16788 }
16789 if (readlen > p - buf + 1)
16790 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16791 readlen -= 3 - adjust_prevlen;
16792 prevlen -= adjust_prevlen;
16793 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016794 }
16795 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016796 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016797#endif
16798 } /* for */
16799
16800 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16801 break;
16802 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016803 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016804 /* There's part of a line in buf, store it in "prev". */
16805 if (p - start + prevlen >= prevsize)
16806 {
16807 /* need bigger "prev" buffer */
16808 char_u *newprev;
16809
16810 /* A common use case is ordinary text files and "prev" gets a
16811 * fragment of a line, so the first allocation is made
16812 * small, to avoid repeatedly 'allocing' large and
16813 * 'reallocing' small. */
16814 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016815 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016816 else
16817 {
16818 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016819 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016820 prevsize = grow50pc > growmin ? grow50pc : growmin;
16821 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016822 newprev = prev == NULL ? alloc(prevsize)
16823 : vim_realloc(prev, prevsize);
16824 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016825 {
16826 do_outofmem_msg((long_u)prevsize);
16827 failed = TRUE;
16828 break;
16829 }
16830 prev = newprev;
16831 }
16832 /* Add the line part to end of "prev". */
16833 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016834 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016835 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016836 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016837
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016838 /*
16839 * For a negative line count use only the lines at the end of the file,
16840 * free the rest.
16841 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016842 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016843 while (cnt > -maxline)
16844 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016845 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016846 --cnt;
16847 }
16848
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016849 if (failed)
16850 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016851 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016852 /* readfile doc says an empty list is returned on error */
16853 rettv->vval.v_list = list_alloc();
16854 }
16855
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016856 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016857 fclose(fd);
16858}
16859
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016860#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016861static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016862
16863/*
16864 * Convert a List to proftime_T.
16865 * Return FAIL when there is something wrong.
16866 */
16867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016868list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016869{
16870 long n1, n2;
16871 int error = FALSE;
16872
16873 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16874 || arg->vval.v_list->lv_len != 2)
16875 return FAIL;
16876 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16877 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16878# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016879 tm->HighPart = n1;
16880 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016881# else
16882 tm->tv_sec = n1;
16883 tm->tv_usec = n2;
16884# endif
16885 return error ? FAIL : OK;
16886}
16887#endif /* FEAT_RELTIME */
16888
16889/*
16890 * "reltime()" function
16891 */
16892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016893f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016894{
16895#ifdef FEAT_RELTIME
16896 proftime_T res;
16897 proftime_T start;
16898
16899 if (argvars[0].v_type == VAR_UNKNOWN)
16900 {
16901 /* No arguments: get current time. */
16902 profile_start(&res);
16903 }
16904 else if (argvars[1].v_type == VAR_UNKNOWN)
16905 {
16906 if (list2proftime(&argvars[0], &res) == FAIL)
16907 return;
16908 profile_end(&res);
16909 }
16910 else
16911 {
16912 /* Two arguments: compute the difference. */
16913 if (list2proftime(&argvars[0], &start) == FAIL
16914 || list2proftime(&argvars[1], &res) == FAIL)
16915 return;
16916 profile_sub(&res, &start);
16917 }
16918
16919 if (rettv_list_alloc(rettv) == OK)
16920 {
16921 long n1, n2;
16922
16923# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016924 n1 = res.HighPart;
16925 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016926# else
16927 n1 = res.tv_sec;
16928 n2 = res.tv_usec;
16929# endif
16930 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16931 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16932 }
16933#endif
16934}
16935
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016936#ifdef FEAT_FLOAT
16937/*
16938 * "reltimefloat()" function
16939 */
16940 static void
16941f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16942{
16943# ifdef FEAT_RELTIME
16944 proftime_T tm;
16945# endif
16946
16947 rettv->v_type = VAR_FLOAT;
16948 rettv->vval.v_float = 0;
16949# ifdef FEAT_RELTIME
16950 if (list2proftime(&argvars[0], &tm) == OK)
16951 rettv->vval.v_float = profile_float(&tm);
16952# endif
16953}
16954#endif
16955
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016956/*
16957 * "reltimestr()" function
16958 */
16959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016960f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016961{
16962#ifdef FEAT_RELTIME
16963 proftime_T tm;
16964#endif
16965
16966 rettv->v_type = VAR_STRING;
16967 rettv->vval.v_string = NULL;
16968#ifdef FEAT_RELTIME
16969 if (list2proftime(&argvars[0], &tm) == OK)
16970 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16971#endif
16972}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016973
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016975static void make_connection(void);
16976static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977
16978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016979make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980{
16981 if (X_DISPLAY == NULL
16982# ifdef FEAT_GUI
16983 && !gui.in_use
16984# endif
16985 )
16986 {
16987 x_force_connect = TRUE;
16988 setup_term_clip();
16989 x_force_connect = FALSE;
16990 }
16991}
16992
16993 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016994check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995{
16996 make_connection();
16997 if (X_DISPLAY == NULL)
16998 {
16999 EMSG(_("E240: No connection to Vim server"));
17000 return FAIL;
17001 }
17002 return OK;
17003}
17004#endif
17005
17006#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017008remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009{
17010 char_u *server_name;
17011 char_u *keys;
17012 char_u *r = NULL;
17013 char_u buf[NUMBUFLEN];
17014# ifdef WIN32
17015 HWND w;
17016# else
17017 Window w;
17018# endif
17019
17020 if (check_restricted() || check_secure())
17021 return;
17022
17023# ifdef FEAT_X11
17024 if (check_connection() == FAIL)
17025 return;
17026# endif
17027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017028 server_name = get_tv_string_chk(&argvars[0]);
17029 if (server_name == NULL)
17030 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031 keys = get_tv_string_buf(&argvars[1], buf);
17032# ifdef WIN32
17033 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17034# else
17035 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17036 < 0)
17037# endif
17038 {
17039 if (r != NULL)
17040 EMSG(r); /* sending worked but evaluation failed */
17041 else
17042 EMSG2(_("E241: Unable to send to %s"), server_name);
17043 return;
17044 }
17045
17046 rettv->vval.v_string = r;
17047
17048 if (argvars[2].v_type != VAR_UNKNOWN)
17049 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017050 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017051 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017052 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017053
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017054 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017055 v.di_tv.v_type = VAR_STRING;
17056 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017057 idvar = get_tv_string_chk(&argvars[2]);
17058 if (idvar != NULL)
17059 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017060 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061 }
17062}
17063#endif
17064
17065/*
17066 * "remote_expr()" function
17067 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017069f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017070{
17071 rettv->v_type = VAR_STRING;
17072 rettv->vval.v_string = NULL;
17073#ifdef FEAT_CLIENTSERVER
17074 remote_common(argvars, rettv, TRUE);
17075#endif
17076}
17077
17078/*
17079 * "remote_foreground()" function
17080 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017082f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017083{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084#ifdef FEAT_CLIENTSERVER
17085# ifdef WIN32
17086 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017087 {
17088 char_u *server_name = get_tv_string_chk(&argvars[0]);
17089
17090 if (server_name != NULL)
17091 serverForeground(server_name);
17092 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017093# else
17094 /* Send a foreground() expression to the server. */
17095 argvars[1].v_type = VAR_STRING;
17096 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17097 argvars[2].v_type = VAR_UNKNOWN;
17098 remote_common(argvars, rettv, TRUE);
17099 vim_free(argvars[1].vval.v_string);
17100# endif
17101#endif
17102}
17103
Bram Moolenaar0d660222005-01-07 21:51:51 +000017104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017105f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106{
17107#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017108 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017109 char_u *s = NULL;
17110# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017111 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017112# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017113 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017114
17115 if (check_restricted() || check_secure())
17116 {
17117 rettv->vval.v_number = -1;
17118 return;
17119 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 serverid = get_tv_string_chk(&argvars[0]);
17121 if (serverid == NULL)
17122 {
17123 rettv->vval.v_number = -1;
17124 return; /* type error; errmsg already given */
17125 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017127 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128 if (n == 0)
17129 rettv->vval.v_number = -1;
17130 else
17131 {
17132 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17133 rettv->vval.v_number = (s != NULL);
17134 }
17135# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017136 if (check_connection() == FAIL)
17137 return;
17138
17139 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017140 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141# endif
17142
17143 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017145 char_u *retvar;
17146
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 v.di_tv.v_type = VAR_STRING;
17148 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017149 retvar = get_tv_string_chk(&argvars[1]);
17150 if (retvar != NULL)
17151 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017152 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017153 }
17154#else
17155 rettv->vval.v_number = -1;
17156#endif
17157}
17158
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017160f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161{
17162 char_u *r = NULL;
17163
17164#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017165 char_u *serverid = get_tv_string_chk(&argvars[0]);
17166
17167 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017168 {
17169# ifdef WIN32
17170 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017171 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017172
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017173 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017174 if (n != 0)
17175 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17176 if (r == NULL)
17177# else
17178 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017179 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180# endif
17181 EMSG(_("E277: Unable to read a server reply"));
17182 }
17183#endif
17184 rettv->v_type = VAR_STRING;
17185 rettv->vval.v_string = r;
17186}
17187
17188/*
17189 * "remote_send()" function
17190 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017192f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017193{
17194 rettv->v_type = VAR_STRING;
17195 rettv->vval.v_string = NULL;
17196#ifdef FEAT_CLIENTSERVER
17197 remote_common(argvars, rettv, FALSE);
17198#endif
17199}
17200
17201/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017202 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017203 */
17204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017205f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017206{
Bram Moolenaar33570922005-01-25 22:26:29 +000017207 list_T *l;
17208 listitem_T *item, *item2;
17209 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017210 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017211 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017212 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017213 dict_T *d;
17214 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017215 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017216
Bram Moolenaar8c711452005-01-14 21:53:12 +000017217 if (argvars[0].v_type == VAR_DICT)
17218 {
17219 if (argvars[2].v_type != VAR_UNKNOWN)
17220 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017221 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017222 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017224 key = get_tv_string_chk(&argvars[1]);
17225 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017227 di = dict_find(d, key, -1);
17228 if (di == NULL)
17229 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017230 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17231 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017232 {
17233 *rettv = di->di_tv;
17234 init_tv(&di->di_tv);
17235 dictitem_remove(d, di);
17236 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017237 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017238 }
17239 }
17240 else if (argvars[0].v_type != VAR_LIST)
17241 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017242 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017243 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017245 int error = FALSE;
17246
17247 idx = get_tv_number_chk(&argvars[1], &error);
17248 if (error)
17249 ; /* type error: do nothing, errmsg already given */
17250 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017251 EMSGN(_(e_listidx), idx);
17252 else
17253 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017254 if (argvars[2].v_type == VAR_UNKNOWN)
17255 {
17256 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017257 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017258 *rettv = item->li_tv;
17259 vim_free(item);
17260 }
17261 else
17262 {
17263 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017264 end = get_tv_number_chk(&argvars[2], &error);
17265 if (error)
17266 ; /* type error: do nothing */
17267 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017268 EMSGN(_(e_listidx), end);
17269 else
17270 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017271 int cnt = 0;
17272
17273 for (li = item; li != NULL; li = li->li_next)
17274 {
17275 ++cnt;
17276 if (li == item2)
17277 break;
17278 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017279 if (li == NULL) /* didn't find "item2" after "item" */
17280 EMSG(_(e_invrange));
17281 else
17282 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017283 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017284 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017285 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017286 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017287 l->lv_first = item;
17288 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017289 item->li_prev = NULL;
17290 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017291 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017292 }
17293 }
17294 }
17295 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017296 }
17297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298}
17299
17300/*
17301 * "rename({from}, {to})" function
17302 */
17303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017304f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305{
17306 char_u buf[NUMBUFLEN];
17307
17308 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017309 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017311 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17312 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313}
17314
17315/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017316 * "repeat()" function
17317 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017319f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017320{
17321 char_u *p;
17322 int n;
17323 int slen;
17324 int len;
17325 char_u *r;
17326 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017327
17328 n = get_tv_number(&argvars[1]);
17329 if (argvars[0].v_type == VAR_LIST)
17330 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017331 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017332 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017333 if (list_extend(rettv->vval.v_list,
17334 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017335 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017336 }
17337 else
17338 {
17339 p = get_tv_string(&argvars[0]);
17340 rettv->v_type = VAR_STRING;
17341 rettv->vval.v_string = NULL;
17342
17343 slen = (int)STRLEN(p);
17344 len = slen * n;
17345 if (len <= 0)
17346 return;
17347
17348 r = alloc(len + 1);
17349 if (r != NULL)
17350 {
17351 for (i = 0; i < n; i++)
17352 mch_memmove(r + i * slen, p, (size_t)slen);
17353 r[len] = NUL;
17354 }
17355
17356 rettv->vval.v_string = r;
17357 }
17358}
17359
17360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361 * "resolve()" function
17362 */
17363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017364f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365{
17366 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017367#ifdef HAVE_READLINK
17368 char_u *buf = NULL;
17369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017371 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372#ifdef FEAT_SHORTCUT
17373 {
17374 char_u *v = NULL;
17375
17376 v = mch_resolve_shortcut(p);
17377 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017378 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 }
17382#else
17383# ifdef HAVE_READLINK
17384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 char_u *cpy;
17386 int len;
17387 char_u *remain = NULL;
17388 char_u *q;
17389 int is_relative_to_current = FALSE;
17390 int has_trailing_pathsep = FALSE;
17391 int limit = 100;
17392
17393 p = vim_strsave(p);
17394
17395 if (p[0] == '.' && (vim_ispathsep(p[1])
17396 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17397 is_relative_to_current = TRUE;
17398
17399 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017400 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017403 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405
17406 q = getnextcomp(p);
17407 if (*q != NUL)
17408 {
17409 /* Separate the first path component in "p", and keep the
17410 * remainder (beginning with the path separator). */
17411 remain = vim_strsave(q - 1);
17412 q[-1] = NUL;
17413 }
17414
Bram Moolenaard9462e32011-04-11 21:35:11 +020017415 buf = alloc(MAXPATHL + 1);
17416 if (buf == NULL)
17417 goto fail;
17418
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 for (;;)
17420 {
17421 for (;;)
17422 {
17423 len = readlink((char *)p, (char *)buf, MAXPATHL);
17424 if (len <= 0)
17425 break;
17426 buf[len] = NUL;
17427
17428 if (limit-- == 0)
17429 {
17430 vim_free(p);
17431 vim_free(remain);
17432 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017433 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 goto fail;
17435 }
17436
17437 /* Ensure that the result will have a trailing path separator
17438 * if the argument has one. */
17439 if (remain == NULL && has_trailing_pathsep)
17440 add_pathsep(buf);
17441
17442 /* Separate the first path component in the link value and
17443 * concatenate the remainders. */
17444 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17445 if (*q != NUL)
17446 {
17447 if (remain == NULL)
17448 remain = vim_strsave(q - 1);
17449 else
17450 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017451 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 if (cpy != NULL)
17453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454 vim_free(remain);
17455 remain = cpy;
17456 }
17457 }
17458 q[-1] = NUL;
17459 }
17460
17461 q = gettail(p);
17462 if (q > p && *q == NUL)
17463 {
17464 /* Ignore trailing path separator. */
17465 q[-1] = NUL;
17466 q = gettail(p);
17467 }
17468 if (q > p && !mch_isFullName(buf))
17469 {
17470 /* symlink is relative to directory of argument */
17471 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17472 if (cpy != NULL)
17473 {
17474 STRCPY(cpy, p);
17475 STRCPY(gettail(cpy), buf);
17476 vim_free(p);
17477 p = cpy;
17478 }
17479 }
17480 else
17481 {
17482 vim_free(p);
17483 p = vim_strsave(buf);
17484 }
17485 }
17486
17487 if (remain == NULL)
17488 break;
17489
17490 /* Append the first path component of "remain" to "p". */
17491 q = getnextcomp(remain + 1);
17492 len = q - remain - (*q != NUL);
17493 cpy = vim_strnsave(p, STRLEN(p) + len);
17494 if (cpy != NULL)
17495 {
17496 STRNCAT(cpy, remain, len);
17497 vim_free(p);
17498 p = cpy;
17499 }
17500 /* Shorten "remain". */
17501 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017502 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 else
17504 {
17505 vim_free(remain);
17506 remain = NULL;
17507 }
17508 }
17509
17510 /* If the result is a relative path name, make it explicitly relative to
17511 * the current directory if and only if the argument had this form. */
17512 if (!vim_ispathsep(*p))
17513 {
17514 if (is_relative_to_current
17515 && *p != NUL
17516 && !(p[0] == '.'
17517 && (p[1] == NUL
17518 || vim_ispathsep(p[1])
17519 || (p[1] == '.'
17520 && (p[2] == NUL
17521 || vim_ispathsep(p[2]))))))
17522 {
17523 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017524 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 if (cpy != NULL)
17526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 vim_free(p);
17528 p = cpy;
17529 }
17530 }
17531 else if (!is_relative_to_current)
17532 {
17533 /* Strip leading "./". */
17534 q = p;
17535 while (q[0] == '.' && vim_ispathsep(q[1]))
17536 q += 2;
17537 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017538 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 }
17540 }
17541
17542 /* Ensure that the result will have no trailing path separator
17543 * if the argument had none. But keep "/" or "//". */
17544 if (!has_trailing_pathsep)
17545 {
17546 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017547 if (after_pathsep(p, q))
17548 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549 }
17550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017551 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 }
17553# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017554 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555# endif
17556#endif
17557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559
17560#ifdef HAVE_READLINK
17561fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017562 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017564 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565}
17566
17567/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017568 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 */
17570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017571f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572{
Bram Moolenaar33570922005-01-25 22:26:29 +000017573 list_T *l;
17574 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575
Bram Moolenaar0d660222005-01-07 21:51:51 +000017576 if (argvars[0].v_type != VAR_LIST)
17577 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017578 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017579 && !tv_check_lock(l->lv_lock,
17580 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017581 {
17582 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017583 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017584 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017585 while (li != NULL)
17586 {
17587 ni = li->li_prev;
17588 list_append(l, li);
17589 li = ni;
17590 }
17591 rettv->vval.v_list = l;
17592 rettv->v_type = VAR_LIST;
17593 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017594 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596}
17597
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017598#define SP_NOMOVE 0x01 /* don't move cursor */
17599#define SP_REPEAT 0x02 /* repeat to find outer pair */
17600#define SP_RETCOUNT 0x04 /* return matchcount */
17601#define SP_SETPCMARK 0x08 /* set previous context mark */
17602#define SP_START 0x10 /* accept match at start position */
17603#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17604#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017605#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017606
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017607static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017608
17609/*
17610 * Get flags for a search function.
17611 * Possibly sets "p_ws".
17612 * Returns BACKWARD, FORWARD or zero (for an error).
17613 */
17614 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017615get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017616{
17617 int dir = FORWARD;
17618 char_u *flags;
17619 char_u nbuf[NUMBUFLEN];
17620 int mask;
17621
17622 if (varp->v_type != VAR_UNKNOWN)
17623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017624 flags = get_tv_string_buf_chk(varp, nbuf);
17625 if (flags == NULL)
17626 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017627 while (*flags != NUL)
17628 {
17629 switch (*flags)
17630 {
17631 case 'b': dir = BACKWARD; break;
17632 case 'w': p_ws = TRUE; break;
17633 case 'W': p_ws = FALSE; break;
17634 default: mask = 0;
17635 if (flagsp != NULL)
17636 switch (*flags)
17637 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017638 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017639 case 'e': mask = SP_END; break;
17640 case 'm': mask = SP_RETCOUNT; break;
17641 case 'n': mask = SP_NOMOVE; break;
17642 case 'p': mask = SP_SUBPAT; break;
17643 case 'r': mask = SP_REPEAT; break;
17644 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017645 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017646 }
17647 if (mask == 0)
17648 {
17649 EMSG2(_(e_invarg2), flags);
17650 dir = 0;
17651 }
17652 else
17653 *flagsp |= mask;
17654 }
17655 if (dir == 0)
17656 break;
17657 ++flags;
17658 }
17659 }
17660 return dir;
17661}
17662
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017664 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017667search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017669 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 char_u *pat;
17671 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017672 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 int save_p_ws = p_ws;
17674 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017675 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017676 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017677 proftime_T tm;
17678#ifdef FEAT_RELTIME
17679 long time_limit = 0;
17680#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017681 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017682 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017684 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017685 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017686 if (dir == 0)
17687 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017688 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017689 if (flags & SP_START)
17690 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017691 if (flags & SP_END)
17692 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017693 if (flags & SP_COLUMN)
17694 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017695
Bram Moolenaar76929292008-01-06 19:07:36 +000017696 /* Optional arguments: line number to stop searching and timeout. */
17697 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017698 {
17699 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17700 if (lnum_stop < 0)
17701 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017702#ifdef FEAT_RELTIME
17703 if (argvars[3].v_type != VAR_UNKNOWN)
17704 {
17705 time_limit = get_tv_number_chk(&argvars[3], NULL);
17706 if (time_limit < 0)
17707 goto theend;
17708 }
17709#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017710 }
17711
Bram Moolenaar76929292008-01-06 19:07:36 +000017712#ifdef FEAT_RELTIME
17713 /* Set the time limit, if there is one. */
17714 profile_setlimit(time_limit, &tm);
17715#endif
17716
Bram Moolenaar231334e2005-07-25 20:46:57 +000017717 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017718 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017719 * Check to make sure only those flags are set.
17720 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17721 * flags cannot be set. Check for that condition also.
17722 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017723 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017724 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017726 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017727 goto theend;
17728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017730 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017731 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017732 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017733 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017735 if (flags & SP_SUBPAT)
17736 retval = subpatnum;
17737 else
17738 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017739 if (flags & SP_SETPCMARK)
17740 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017742 if (match_pos != NULL)
17743 {
17744 /* Store the match cursor position */
17745 match_pos->lnum = pos.lnum;
17746 match_pos->col = pos.col + 1;
17747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 /* "/$" will put the cursor after the end of the line, may need to
17749 * correct that here */
17750 check_cursor();
17751 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017752
17753 /* If 'n' flag is used: restore cursor position. */
17754 if (flags & SP_NOMOVE)
17755 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017756 else
17757 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017758theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017760
17761 return retval;
17762}
17763
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017764#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017765
17766/*
17767 * round() is not in C90, use ceil() or floor() instead.
17768 */
17769 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017770vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017771{
17772 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17773}
17774
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017775/*
17776 * "round({float})" function
17777 */
17778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017779f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017780{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017781 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017782
17783 rettv->v_type = VAR_FLOAT;
17784 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017785 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017786 else
17787 rettv->vval.v_float = 0.0;
17788}
17789#endif
17790
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017791/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017792 * "screenattr()" function
17793 */
17794 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017795f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017796{
17797 int row;
17798 int col;
17799 int c;
17800
17801 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17802 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17803 if (row < 0 || row >= screen_Rows
17804 || col < 0 || col >= screen_Columns)
17805 c = -1;
17806 else
17807 c = ScreenAttrs[LineOffset[row] + col];
17808 rettv->vval.v_number = c;
17809}
17810
17811/*
17812 * "screenchar()" function
17813 */
17814 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017815f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017816{
17817 int row;
17818 int col;
17819 int off;
17820 int c;
17821
17822 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17823 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17824 if (row < 0 || row >= screen_Rows
17825 || col < 0 || col >= screen_Columns)
17826 c = -1;
17827 else
17828 {
17829 off = LineOffset[row] + col;
17830#ifdef FEAT_MBYTE
17831 if (enc_utf8 && ScreenLinesUC[off] != 0)
17832 c = ScreenLinesUC[off];
17833 else
17834#endif
17835 c = ScreenLines[off];
17836 }
17837 rettv->vval.v_number = c;
17838}
17839
17840/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017841 * "screencol()" function
17842 *
17843 * First column is 1 to be consistent with virtcol().
17844 */
17845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017846f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017847{
17848 rettv->vval.v_number = screen_screencol() + 1;
17849}
17850
17851/*
17852 * "screenrow()" function
17853 */
17854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017855f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017856{
17857 rettv->vval.v_number = screen_screenrow() + 1;
17858}
17859
17860/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017861 * "search()" function
17862 */
17863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017864f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017865{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017866 int flags = 0;
17867
17868 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869}
17870
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017872 * "searchdecl()" function
17873 */
17874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017875f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017876{
17877 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017878 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017879 int error = FALSE;
17880 char_u *name;
17881
17882 rettv->vval.v_number = 1; /* default: FAIL */
17883
17884 name = get_tv_string_chk(&argvars[0]);
17885 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017886 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017887 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017888 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17889 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17890 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017891 if (!error && name != NULL)
17892 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017893 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017894}
17895
17896/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017897 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017899 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017900searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901{
17902 char_u *spat, *mpat, *epat;
17903 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 int dir;
17906 int flags = 0;
17907 char_u nbuf1[NUMBUFLEN];
17908 char_u nbuf2[NUMBUFLEN];
17909 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017910 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017911 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017912 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017915 spat = get_tv_string_chk(&argvars[0]);
17916 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17917 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17918 if (spat == NULL || mpat == NULL || epat == NULL)
17919 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 /* Handle the optional fourth argument: flags */
17922 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017923 if (dir == 0)
17924 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017925
17926 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017927 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17928 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017929 if ((flags & (SP_END | SP_SUBPAT)) != 0
17930 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017931 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017932 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017933 goto theend;
17934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017936 /* Using 'r' implies 'W', otherwise it doesn't work. */
17937 if (flags & SP_REPEAT)
17938 p_ws = FALSE;
17939
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017940 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017941 if (argvars[3].v_type == VAR_UNKNOWN
17942 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 skip = (char_u *)"";
17944 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017946 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017947 if (argvars[5].v_type != VAR_UNKNOWN)
17948 {
17949 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17950 if (lnum_stop < 0)
17951 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017952#ifdef FEAT_RELTIME
17953 if (argvars[6].v_type != VAR_UNKNOWN)
17954 {
17955 time_limit = get_tv_number_chk(&argvars[6], NULL);
17956 if (time_limit < 0)
17957 goto theend;
17958 }
17959#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017960 }
17961 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017962 if (skip == NULL)
17963 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017965 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017966 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017967
17968theend:
17969 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017970
17971 return retval;
17972}
17973
17974/*
17975 * "searchpair()" function
17976 */
17977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017978f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017979{
17980 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17981}
17982
17983/*
17984 * "searchpairpos()" function
17985 */
17986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017987f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017988{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017989 pos_T match_pos;
17990 int lnum = 0;
17991 int col = 0;
17992
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017993 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017994 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017995
17996 if (searchpair_cmn(argvars, &match_pos) > 0)
17997 {
17998 lnum = match_pos.lnum;
17999 col = match_pos.col;
18000 }
18001
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018002 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18003 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018004}
18005
18006/*
18007 * Search for a start/middle/end thing.
18008 * Used by searchpair(), see its documentation for the details.
18009 * Returns 0 or -1 for no match,
18010 */
18011 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018012do_searchpair(
18013 char_u *spat, /* start pattern */
18014 char_u *mpat, /* middle pattern */
18015 char_u *epat, /* end pattern */
18016 int dir, /* BACKWARD or FORWARD */
18017 char_u *skip, /* skip expression */
18018 int flags, /* SP_SETPCMARK and other SP_ values */
18019 pos_T *match_pos,
18020 linenr_T lnum_stop, /* stop at this line if not zero */
18021 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018022{
18023 char_u *save_cpo;
18024 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18025 long retval = 0;
18026 pos_T pos;
18027 pos_T firstpos;
18028 pos_T foundpos;
18029 pos_T save_cursor;
18030 pos_T save_pos;
18031 int n;
18032 int r;
18033 int nest = 1;
18034 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018035 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018036 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018037
18038 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18039 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018040 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018041
Bram Moolenaar76929292008-01-06 19:07:36 +000018042#ifdef FEAT_RELTIME
18043 /* Set the time limit, if there is one. */
18044 profile_setlimit(time_limit, &tm);
18045#endif
18046
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018047 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18048 * start/middle/end (pat3, for the top pair). */
18049 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18050 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18051 if (pat2 == NULL || pat3 == NULL)
18052 goto theend;
18053 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18054 if (*mpat == NUL)
18055 STRCPY(pat3, pat2);
18056 else
18057 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18058 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018059 if (flags & SP_START)
18060 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018061
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 save_cursor = curwin->w_cursor;
18063 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018064 clearpos(&firstpos);
18065 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 pat = pat3;
18067 for (;;)
18068 {
18069 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018070 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18072 /* didn't find it or found the first match again: FAIL */
18073 break;
18074
18075 if (firstpos.lnum == 0)
18076 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018077 if (equalpos(pos, foundpos))
18078 {
18079 /* Found the same position again. Can happen with a pattern that
18080 * has "\zs" at the end and searching backwards. Advance one
18081 * character and try again. */
18082 if (dir == BACKWARD)
18083 decl(&pos);
18084 else
18085 incl(&pos);
18086 }
18087 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018089 /* clear the start flag to avoid getting stuck here */
18090 options &= ~SEARCH_START;
18091
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 /* If the skip pattern matches, ignore this match. */
18093 if (*skip != NUL)
18094 {
18095 save_pos = curwin->w_cursor;
18096 curwin->w_cursor = pos;
18097 r = eval_to_bool(skip, &err, NULL, FALSE);
18098 curwin->w_cursor = save_pos;
18099 if (err)
18100 {
18101 /* Evaluating {skip} caused an error, break here. */
18102 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018103 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 break;
18105 }
18106 if (r)
18107 continue;
18108 }
18109
18110 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18111 {
18112 /* Found end when searching backwards or start when searching
18113 * forward: nested pair. */
18114 ++nest;
18115 pat = pat2; /* nested, don't search for middle */
18116 }
18117 else
18118 {
18119 /* Found end when searching forward or start when searching
18120 * backward: end of (nested) pair; or found middle in outer pair. */
18121 if (--nest == 1)
18122 pat = pat3; /* outer level, search for middle */
18123 }
18124
18125 if (nest == 0)
18126 {
18127 /* Found the match: return matchcount or line number. */
18128 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018129 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018131 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018132 if (flags & SP_SETPCMARK)
18133 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 curwin->w_cursor = pos;
18135 if (!(flags & SP_REPEAT))
18136 break;
18137 nest = 1; /* search for next unmatched */
18138 }
18139 }
18140
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018141 if (match_pos != NULL)
18142 {
18143 /* Store the match cursor position */
18144 match_pos->lnum = curwin->w_cursor.lnum;
18145 match_pos->col = curwin->w_cursor.col + 1;
18146 }
18147
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018149 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150 curwin->w_cursor = save_cursor;
18151
18152theend:
18153 vim_free(pat2);
18154 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018155 if (p_cpo == empty_option)
18156 p_cpo = save_cpo;
18157 else
18158 /* Darn, evaluating the {skip} expression changed the value. */
18159 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018160
18161 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162}
18163
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018164/*
18165 * "searchpos()" function
18166 */
18167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018168f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018169{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018170 pos_T match_pos;
18171 int lnum = 0;
18172 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018173 int n;
18174 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018175
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018176 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018177 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018178
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018179 n = search_cmn(argvars, &match_pos, &flags);
18180 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018181 {
18182 lnum = match_pos.lnum;
18183 col = match_pos.col;
18184 }
18185
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018186 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18187 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018188 if (flags & SP_SUBPAT)
18189 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018190}
18191
Bram Moolenaar0d660222005-01-07 21:51:51 +000018192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018193f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018195#ifdef FEAT_CLIENTSERVER
18196 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018197 char_u *server = get_tv_string_chk(&argvars[0]);
18198 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199
Bram Moolenaar0d660222005-01-07 21:51:51 +000018200 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018201 if (server == NULL || reply == NULL)
18202 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018203 if (check_restricted() || check_secure())
18204 return;
18205# ifdef FEAT_X11
18206 if (check_connection() == FAIL)
18207 return;
18208# endif
18209
18210 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018212 EMSG(_("E258: Unable to send to client"));
18213 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018215 rettv->vval.v_number = 0;
18216#else
18217 rettv->vval.v_number = -1;
18218#endif
18219}
18220
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018222f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018223{
18224 char_u *r = NULL;
18225
18226#ifdef FEAT_CLIENTSERVER
18227# ifdef WIN32
18228 r = serverGetVimNames();
18229# else
18230 make_connection();
18231 if (X_DISPLAY != NULL)
18232 r = serverGetVimNames(X_DISPLAY);
18233# endif
18234#endif
18235 rettv->v_type = VAR_STRING;
18236 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237}
18238
18239/*
18240 * "setbufvar()" function
18241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018243f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244{
18245 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018248 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 char_u nbuf[NUMBUFLEN];
18250
18251 if (check_restricted() || check_secure())
18252 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018253 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18254 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018255 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256 varp = &argvars[2];
18257
18258 if (buf != NULL && varname != NULL && varp != NULL)
18259 {
18260 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262
18263 if (*varname == '&')
18264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018265 long numval;
18266 char_u *strval;
18267 int error = FALSE;
18268
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018270 numval = get_tv_number_chk(varp, &error);
18271 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018272 if (!error && strval != NULL)
18273 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 }
18275 else
18276 {
18277 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18278 if (bufvarname != NULL)
18279 {
18280 STRCPY(bufvarname, "b:");
18281 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018282 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 vim_free(bufvarname);
18284 }
18285 }
18286
18287 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290}
18291
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018293f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018294{
18295 dict_T *d;
18296 dictitem_T *di;
18297 char_u *csearch;
18298
18299 if (argvars[0].v_type != VAR_DICT)
18300 {
18301 EMSG(_(e_dictreq));
18302 return;
18303 }
18304
18305 if ((d = argvars[0].vval.v_dict) != NULL)
18306 {
18307 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18308 if (csearch != NULL)
18309 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018310#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018311 if (enc_utf8)
18312 {
18313 int pcc[MAX_MCO];
18314 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018315
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018316 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18317 }
18318 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018319#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018320 set_last_csearch(PTR2CHAR(csearch),
18321 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018322 }
18323
18324 di = dict_find(d, (char_u *)"forward", -1);
18325 if (di != NULL)
18326 set_csearch_direction(get_tv_number(&di->di_tv)
18327 ? FORWARD : BACKWARD);
18328
18329 di = dict_find(d, (char_u *)"until", -1);
18330 if (di != NULL)
18331 set_csearch_until(!!get_tv_number(&di->di_tv));
18332 }
18333}
18334
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335/*
18336 * "setcmdpos()" function
18337 */
18338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018339f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018341 int pos = (int)get_tv_number(&argvars[0]) - 1;
18342
18343 if (pos >= 0)
18344 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345}
18346
18347/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018348 * "setfperm({fname}, {mode})" function
18349 */
18350 static void
18351f_setfperm(typval_T *argvars, typval_T *rettv)
18352{
18353 char_u *fname;
18354 char_u modebuf[NUMBUFLEN];
18355 char_u *mode_str;
18356 int i;
18357 int mask;
18358 int mode = 0;
18359
18360 rettv->vval.v_number = 0;
18361 fname = get_tv_string_chk(&argvars[0]);
18362 if (fname == NULL)
18363 return;
18364 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18365 if (mode_str == NULL)
18366 return;
18367 if (STRLEN(mode_str) != 9)
18368 {
18369 EMSG2(_(e_invarg2), mode_str);
18370 return;
18371 }
18372
18373 mask = 1;
18374 for (i = 8; i >= 0; --i)
18375 {
18376 if (mode_str[i] != '-')
18377 mode |= mask;
18378 mask = mask << 1;
18379 }
18380 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18381}
18382
18383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 * "setline()" function
18385 */
18386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018387f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388{
18389 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018390 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018391 list_T *l = NULL;
18392 listitem_T *li = NULL;
18393 long added = 0;
18394 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018396 lnum = get_tv_lnum(&argvars[0]);
18397 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018399 l = argvars[1].vval.v_list;
18400 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018402 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018403 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018404
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018405 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018406 for (;;)
18407 {
18408 if (l != NULL)
18409 {
18410 /* list argument, get next string */
18411 if (li == NULL)
18412 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018413 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018414 li = li->li_next;
18415 }
18416
18417 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018418 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018419 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018420
18421 /* When coming here from Insert mode, sync undo, so that this can be
18422 * undone separately from what was previously inserted. */
18423 if (u_sync_once == 2)
18424 {
18425 u_sync_once = 1; /* notify that u_sync() was called */
18426 u_sync(TRUE);
18427 }
18428
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018429 if (lnum <= curbuf->b_ml.ml_line_count)
18430 {
18431 /* existing line, replace it */
18432 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18433 {
18434 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018435 if (lnum == curwin->w_cursor.lnum)
18436 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018437 rettv->vval.v_number = 0; /* OK */
18438 }
18439 }
18440 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18441 {
18442 /* lnum is one past the last line, append the line */
18443 ++added;
18444 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18445 rettv->vval.v_number = 0; /* OK */
18446 }
18447
18448 if (l == NULL) /* only one string argument */
18449 break;
18450 ++lnum;
18451 }
18452
18453 if (added > 0)
18454 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455}
18456
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018457static 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 +000018458
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018460 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018461 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018463set_qf_ll_list(
18464 win_T *wp UNUSED,
18465 typval_T *list_arg UNUSED,
18466 typval_T *action_arg UNUSED,
18467 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018468{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018469#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018470 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018471 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018472 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018473#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018474
Bram Moolenaar2641f772005-03-25 21:58:17 +000018475 rettv->vval.v_number = -1;
18476
18477#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018478 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018479 EMSG(_(e_listreq));
18480 else
18481 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018482 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018483
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018484 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018485 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018486 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018487 if (act == NULL)
18488 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018489 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018490 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018491 else
18492 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018493 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018494 else if (action_arg->v_type == VAR_UNKNOWN)
18495 action = ' ';
18496 else
18497 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018498
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018499 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018500 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018501 rettv->vval.v_number = 0;
18502 }
18503#endif
18504}
18505
18506/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018507 * "setloclist()" function
18508 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018510f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018511{
18512 win_T *win;
18513
18514 rettv->vval.v_number = -1;
18515
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018516 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018517 if (win != NULL)
18518 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18519}
18520
18521/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018522 * "setmatches()" function
18523 */
18524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018525f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018526{
18527#ifdef FEAT_SEARCH_EXTRA
18528 list_T *l;
18529 listitem_T *li;
18530 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018531 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018532
18533 rettv->vval.v_number = -1;
18534 if (argvars[0].v_type != VAR_LIST)
18535 {
18536 EMSG(_(e_listreq));
18537 return;
18538 }
18539 if ((l = argvars[0].vval.v_list) != NULL)
18540 {
18541
18542 /* To some extent make sure that we are dealing with a list from
18543 * "getmatches()". */
18544 li = l->lv_first;
18545 while (li != NULL)
18546 {
18547 if (li->li_tv.v_type != VAR_DICT
18548 || (d = li->li_tv.vval.v_dict) == NULL)
18549 {
18550 EMSG(_(e_invarg));
18551 return;
18552 }
18553 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018554 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18555 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018556 && dict_find(d, (char_u *)"priority", -1) != NULL
18557 && dict_find(d, (char_u *)"id", -1) != NULL))
18558 {
18559 EMSG(_(e_invarg));
18560 return;
18561 }
18562 li = li->li_next;
18563 }
18564
18565 clear_matches(curwin);
18566 li = l->lv_first;
18567 while (li != NULL)
18568 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018569 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018570 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018571 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018572 char_u *group;
18573 int priority;
18574 int id;
18575 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018576
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018577 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018578 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18579 {
18580 if (s == NULL)
18581 {
18582 s = list_alloc();
18583 if (s == NULL)
18584 return;
18585 }
18586
18587 /* match from matchaddpos() */
18588 for (i = 1; i < 9; i++)
18589 {
18590 sprintf((char *)buf, (char *)"pos%d", i);
18591 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18592 {
18593 if (di->di_tv.v_type != VAR_LIST)
18594 return;
18595
18596 list_append_tv(s, &di->di_tv);
18597 s->lv_refcount++;
18598 }
18599 else
18600 break;
18601 }
18602 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018603
18604 group = get_dict_string(d, (char_u *)"group", FALSE);
18605 priority = (int)get_dict_number(d, (char_u *)"priority");
18606 id = (int)get_dict_number(d, (char_u *)"id");
18607 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18608 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18609 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018610 if (i == 0)
18611 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018612 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018613 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018614 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018615 }
18616 else
18617 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018618 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018619 list_unref(s);
18620 s = NULL;
18621 }
18622
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018623 li = li->li_next;
18624 }
18625 rettv->vval.v_number = 0;
18626 }
18627#endif
18628}
18629
18630/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018631 * "setpos()" function
18632 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018634f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018635{
18636 pos_T pos;
18637 int fnum;
18638 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018639 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018640
Bram Moolenaar08250432008-02-13 11:42:46 +000018641 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018642 name = get_tv_string_chk(argvars);
18643 if (name != NULL)
18644 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018645 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018646 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018647 if (--pos.col < 0)
18648 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018649 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018650 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018651 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018652 if (fnum == curbuf->b_fnum)
18653 {
18654 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018655 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018656 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018657 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018658 curwin->w_set_curswant = FALSE;
18659 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018660 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018661 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018662 }
18663 else
18664 EMSG(_(e_invarg));
18665 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018666 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18667 {
18668 /* set mark */
18669 if (setmark_pos(name[1], &pos, fnum) == OK)
18670 rettv->vval.v_number = 0;
18671 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018672 else
18673 EMSG(_(e_invarg));
18674 }
18675 }
18676}
18677
18678/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018679 * "setqflist()" function
18680 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018682f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018683{
18684 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18685}
18686
18687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 * "setreg()" function
18689 */
18690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018691f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692{
18693 int regname;
18694 char_u *strregname;
18695 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018696 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 int append;
18698 char_u yank_type;
18699 long block_len;
18700
18701 block_len = -1;
18702 yank_type = MAUTO;
18703 append = FALSE;
18704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018705 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018708 if (strregname == NULL)
18709 return; /* type error; errmsg already given */
18710 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711 if (regname == 0 || regname == '@')
18712 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018714 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018716 stropt = get_tv_string_chk(&argvars[2]);
18717 if (stropt == NULL)
18718 return; /* type error */
18719 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720 switch (*stropt)
18721 {
18722 case 'a': case 'A': /* append */
18723 append = TRUE;
18724 break;
18725 case 'v': case 'c': /* character-wise selection */
18726 yank_type = MCHAR;
18727 break;
18728 case 'V': case 'l': /* line-wise selection */
18729 yank_type = MLINE;
18730 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 case 'b': case Ctrl_V: /* block-wise selection */
18732 yank_type = MBLOCK;
18733 if (VIM_ISDIGIT(stropt[1]))
18734 {
18735 ++stropt;
18736 block_len = getdigits(&stropt) - 1;
18737 --stropt;
18738 }
18739 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 }
18741 }
18742
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018743 if (argvars[1].v_type == VAR_LIST)
18744 {
18745 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018746 char_u **allocval;
18747 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018748 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018749 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018750 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018751 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018752 int len;
18753
18754 /* If the list is NULL handle like an empty list. */
18755 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018756
Bram Moolenaar7d647822014-04-05 21:28:56 +020018757 /* First half: use for pointers to result lines; second half: use for
18758 * pointers to allocated copies. */
18759 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018760 if (lstval == NULL)
18761 return;
18762 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018763 allocval = lstval + len + 2;
18764 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018765
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018766 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018767 li = li->li_next)
18768 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018769 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018770 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018771 goto free_lstval;
18772 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018773 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018774 /* Need to make a copy, next get_tv_string_buf_chk() will
18775 * overwrite the string. */
18776 strval = vim_strsave(buf);
18777 if (strval == NULL)
18778 goto free_lstval;
18779 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018780 }
18781 *curval++ = strval;
18782 }
18783 *curval++ = NULL;
18784
18785 write_reg_contents_lst(regname, lstval, -1,
18786 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018787free_lstval:
18788 while (curallocval > allocval)
18789 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018790 vim_free(lstval);
18791 }
18792 else
18793 {
18794 strval = get_tv_string_chk(&argvars[1]);
18795 if (strval == NULL)
18796 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018797 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018799 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801}
18802
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018803/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018804 * "settabvar()" function
18805 */
18806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018807f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018808{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018809#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018810 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018811 tabpage_T *tp;
18812#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018813 char_u *varname, *tabvarname;
18814 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018815
18816 rettv->vval.v_number = 0;
18817
18818 if (check_restricted() || check_secure())
18819 return;
18820
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018821#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018822 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018823#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018824 varname = get_tv_string_chk(&argvars[1]);
18825 varp = &argvars[2];
18826
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018827 if (varname != NULL && varp != NULL
18828#ifdef FEAT_WINDOWS
18829 && tp != NULL
18830#endif
18831 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018832 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018833#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018834 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018835 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018836#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018837
18838 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18839 if (tabvarname != NULL)
18840 {
18841 STRCPY(tabvarname, "t:");
18842 STRCPY(tabvarname + 2, varname);
18843 set_var(tabvarname, varp, TRUE);
18844 vim_free(tabvarname);
18845 }
18846
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018847#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018848 /* Restore current tabpage */
18849 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018850 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018851#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018852 }
18853}
18854
18855/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018856 * "settabwinvar()" function
18857 */
18858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018859f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018860{
18861 setwinvar(argvars, rettv, 1);
18862}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863
18864/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018865 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018868f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018869{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018870 setwinvar(argvars, rettv, 0);
18871}
18872
18873/*
18874 * "setwinvar()" and "settabwinvar()" functions
18875 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018876
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018878setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018879{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 win_T *win;
18881#ifdef FEAT_WINDOWS
18882 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018883 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018884 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885#endif
18886 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018887 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018889 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890
18891 if (check_restricted() || check_secure())
18892 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018893
18894#ifdef FEAT_WINDOWS
18895 if (off == 1)
18896 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18897 else
18898 tp = curtab;
18899#endif
18900 win = find_win_by_nr(&argvars[off], tp);
18901 varname = get_tv_string_chk(&argvars[off + 1]);
18902 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903
18904 if (win != NULL && varname != NULL && varp != NULL)
18905 {
18906#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018907 need_switch_win = !(tp == curtab && win == curwin);
18908 if (!need_switch_win
18909 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018912 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018914 long numval;
18915 char_u *strval;
18916 int error = FALSE;
18917
18918 ++varname;
18919 numval = get_tv_number_chk(varp, &error);
18920 strval = get_tv_string_buf_chk(varp, nbuf);
18921 if (!error && strval != NULL)
18922 set_option_value(varname, numval, strval, OPT_LOCAL);
18923 }
18924 else
18925 {
18926 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18927 if (winvarname != NULL)
18928 {
18929 STRCPY(winvarname, "w:");
18930 STRCPY(winvarname + 2, varname);
18931 set_var(winvarname, varp, TRUE);
18932 vim_free(winvarname);
18933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 }
18935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018937 if (need_switch_win)
18938 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939#endif
18940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941}
18942
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018943#ifdef FEAT_CRYPT
18944/*
18945 * "sha256({string})" function
18946 */
18947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018948f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018949{
18950 char_u *p;
18951
18952 p = get_tv_string(&argvars[0]);
18953 rettv->vval.v_string = vim_strsave(
18954 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18955 rettv->v_type = VAR_STRING;
18956}
18957#endif /* FEAT_CRYPT */
18958
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018960 * "shellescape({string})" function
18961 */
18962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018963f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018964{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018965 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018966 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018967 rettv->v_type = VAR_STRING;
18968}
18969
18970/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018971 * shiftwidth() function
18972 */
18973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018974f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018975{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018976 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018977}
18978
18979/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018980 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 */
18982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018983f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018985 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986
Bram Moolenaar0d660222005-01-07 21:51:51 +000018987 p = get_tv_string(&argvars[0]);
18988 rettv->vval.v_string = vim_strsave(p);
18989 simplify_filename(rettv->vval.v_string); /* simplify in place */
18990 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991}
18992
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018993#ifdef FEAT_FLOAT
18994/*
18995 * "sin()" function
18996 */
18997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018998f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018999{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019000 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019001
19002 rettv->v_type = VAR_FLOAT;
19003 if (get_float_arg(argvars, &f) == OK)
19004 rettv->vval.v_float = sin(f);
19005 else
19006 rettv->vval.v_float = 0.0;
19007}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019008
19009/*
19010 * "sinh()" function
19011 */
19012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019013f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019014{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019015 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019016
19017 rettv->v_type = VAR_FLOAT;
19018 if (get_float_arg(argvars, &f) == OK)
19019 rettv->vval.v_float = sinh(f);
19020 else
19021 rettv->vval.v_float = 0.0;
19022}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019023#endif
19024
Bram Moolenaar0d660222005-01-07 21:51:51 +000019025static int
19026#ifdef __BORLANDC__
19027 _RTLENTRYF
19028#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019029 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019030static int
19031#ifdef __BORLANDC__
19032 _RTLENTRYF
19033#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019034 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019035
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019036/* struct used in the array that's given to qsort() */
19037typedef struct
19038{
19039 listitem_T *item;
19040 int idx;
19041} sortItem_T;
19042
Bram Moolenaar0b962472016-02-22 22:51:33 +010019043/* struct storing information about current sort */
19044typedef struct
19045{
19046 int item_compare_ic;
19047 int item_compare_numeric;
19048 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019049#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019050 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019051#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019052 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019053 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019054 dict_T *item_compare_selfdict;
19055 int item_compare_func_err;
19056 int item_compare_keep_zero;
19057} sortinfo_T;
19058static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019059static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019060#define ITEM_COMPARE_FAIL 999
19061
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019063 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019065 static int
19066#ifdef __BORLANDC__
19067_RTLENTRYF
19068#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019069item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019071 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019072 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019074 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019075 int res;
19076 char_u numbuf1[NUMBUFLEN];
19077 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019079 si1 = (sortItem_T *)s1;
19080 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019081 tv1 = &si1->item->li_tv;
19082 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019083
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019085 {
19086 long v1 = get_tv_number(tv1);
19087 long v2 = get_tv_number(tv2);
19088
19089 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19090 }
19091
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019092#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019093 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019094 {
19095 float_T v1 = get_tv_float(tv1);
19096 float_T v2 = get_tv_float(tv2);
19097
19098 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19099 }
19100#endif
19101
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019102 /* tv2string() puts quotes around a string and allocates memory. Don't do
19103 * that for string variables. Use a single quote when comparing with a
19104 * non-string to do what the docs promise. */
19105 if (tv1->v_type == VAR_STRING)
19106 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019107 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019108 p1 = (char_u *)"'";
19109 else
19110 p1 = tv1->vval.v_string;
19111 }
19112 else
19113 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19114 if (tv2->v_type == VAR_STRING)
19115 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019116 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019117 p2 = (char_u *)"'";
19118 else
19119 p2 = tv2->vval.v_string;
19120 }
19121 else
19122 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019123 if (p1 == NULL)
19124 p1 = (char_u *)"";
19125 if (p2 == NULL)
19126 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019127 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019128 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019129 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019130 res = STRICMP(p1, p2);
19131 else
19132 res = STRCMP(p1, p2);
19133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019135 {
19136 double n1, n2;
19137 n1 = strtod((char *)p1, (char **)&p1);
19138 n2 = strtod((char *)p2, (char **)&p2);
19139 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19140 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019141
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019142 /* When the result would be zero, compare the item indexes. Makes the
19143 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019144 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019145 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019146
Bram Moolenaar0d660222005-01-07 21:51:51 +000019147 vim_free(tofree1);
19148 vim_free(tofree2);
19149 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150}
19151
19152 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019153#ifdef __BORLANDC__
19154_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019156item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019157{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019158 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019159 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019160 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019161 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019162 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019163 char_u *func_name;
19164 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019166 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019167 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019168 return 0;
19169
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019170 si1 = (sortItem_T *)s1;
19171 si2 = (sortItem_T *)s2;
19172
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019173 if (partial == NULL)
19174 func_name = sortinfo->item_compare_func;
19175 else
19176 func_name = partial->pt_name;
19177
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019178 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019179 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019180 copy_tv(&si1->item->li_tv, &argv[0]);
19181 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019182
19183 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019184 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019185 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019186 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019187 clear_tv(&argv[0]);
19188 clear_tv(&argv[1]);
19189
19190 if (res == FAIL)
19191 res = ITEM_COMPARE_FAIL;
19192 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019193 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19194 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019195 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019196 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019197
19198 /* When the result would be zero, compare the pointers themselves. Makes
19199 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019200 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019201 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019202
Bram Moolenaar0d660222005-01-07 21:51:51 +000019203 return res;
19204}
19205
19206/*
19207 * "sort({list})" function
19208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019210do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211{
Bram Moolenaar33570922005-01-25 22:26:29 +000019212 list_T *l;
19213 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019214 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019215 sortinfo_T *old_sortinfo;
19216 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019217 long len;
19218 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219
Bram Moolenaar0b962472016-02-22 22:51:33 +010019220 /* Pointer to current info struct used in compare function. Save and
19221 * restore the current one for nested calls. */
19222 old_sortinfo = sortinfo;
19223 sortinfo = &info;
19224
Bram Moolenaar0d660222005-01-07 21:51:51 +000019225 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019226 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 else
19228 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019229 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019230 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019231 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19232 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019233 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019234 rettv->vval.v_list = l;
19235 rettv->v_type = VAR_LIST;
19236 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237
Bram Moolenaar0d660222005-01-07 21:51:51 +000019238 len = list_len(l);
19239 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019240 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241
Bram Moolenaar0b962472016-02-22 22:51:33 +010019242 info.item_compare_ic = FALSE;
19243 info.item_compare_numeric = FALSE;
19244 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019245#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019246 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019247#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019248 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019249 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019250 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019251 if (argvars[1].v_type != VAR_UNKNOWN)
19252 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019253 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019254 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019255 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019256 else if (argvars[1].v_type == VAR_PARTIAL)
19257 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019258 else
19259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019260 int error = FALSE;
19261
19262 i = get_tv_number_chk(&argvars[1], &error);
19263 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019264 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019265 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019266 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019267 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019268 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019269 else if (i != 0)
19270 {
19271 EMSG(_(e_invarg));
19272 goto theend;
19273 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019274 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019275 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019276 if (*info.item_compare_func == NUL)
19277 {
19278 /* empty string means default sort */
19279 info.item_compare_func = NULL;
19280 }
19281 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019282 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019283 info.item_compare_func = NULL;
19284 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019285 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019286 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019287 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019288 info.item_compare_func = NULL;
19289 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019290 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019291#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019292 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019293 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019294 info.item_compare_func = NULL;
19295 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019296 }
19297#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019298 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019299 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019300 info.item_compare_func = NULL;
19301 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019302 }
19303 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019304 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019305
19306 if (argvars[2].v_type != VAR_UNKNOWN)
19307 {
19308 /* optional third argument: {dict} */
19309 if (argvars[2].v_type != VAR_DICT)
19310 {
19311 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019312 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019313 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019314 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019315 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317
Bram Moolenaar0d660222005-01-07 21:51:51 +000019318 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019319 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019320 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019321 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322
Bram Moolenaar327aa022014-03-25 18:24:23 +010019323 i = 0;
19324 if (sort)
19325 {
19326 /* sort(): ptrs will be the list to sort */
19327 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019328 {
19329 ptrs[i].item = li;
19330 ptrs[i].idx = i;
19331 ++i;
19332 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019333
Bram Moolenaar0b962472016-02-22 22:51:33 +010019334 info.item_compare_func_err = FALSE;
19335 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019336 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019337 if ((info.item_compare_func != NULL
19338 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019339 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019340 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019341 EMSG(_("E702: Sort compare function failed"));
19342 else
19343 {
19344 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019345 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019346 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019347 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019348 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019349
Bram Moolenaar0b962472016-02-22 22:51:33 +010019350 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019351 {
19352 /* Clear the List and append the items in sorted order. */
19353 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19354 l->lv_len = 0;
19355 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019356 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019357 }
19358 }
19359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019361 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019362 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019363
19364 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019365 info.item_compare_func_err = FALSE;
19366 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019367 item_compare_func_ptr = info.item_compare_func != NULL
19368 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019369 ? item_compare2 : item_compare;
19370
19371 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19372 li = li->li_next)
19373 {
19374 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19375 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019376 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019377 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019378 {
19379 EMSG(_("E882: Uniq compare function failed"));
19380 break;
19381 }
19382 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019383
Bram Moolenaar0b962472016-02-22 22:51:33 +010019384 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019385 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019386 while (--i >= 0)
19387 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019388 li = ptrs[i].item->li_next;
19389 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019390 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019391 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019392 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019393 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019394 list_fix_watch(l, li);
19395 listitem_free(li);
19396 l->lv_len--;
19397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019398 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019399 }
19400
19401 vim_free(ptrs);
19402 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019403theend:
19404 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019405}
19406
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019407/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019408 * "sort({list})" function
19409 */
19410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019411f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019412{
19413 do_sort_uniq(argvars, rettv, TRUE);
19414}
19415
19416/*
19417 * "uniq({list})" function
19418 */
19419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019420f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019421{
19422 do_sort_uniq(argvars, rettv, FALSE);
19423}
19424
19425/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019426 * "soundfold({word})" function
19427 */
19428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019429f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019430{
19431 char_u *s;
19432
19433 rettv->v_type = VAR_STRING;
19434 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019435#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019436 rettv->vval.v_string = eval_soundfold(s);
19437#else
19438 rettv->vval.v_string = vim_strsave(s);
19439#endif
19440}
19441
19442/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019443 * "spellbadword()" function
19444 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019446f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019447{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019448 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019449 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019450 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019451
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019452 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019453 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019454
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019455#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019456 if (argvars[0].v_type == VAR_UNKNOWN)
19457 {
19458 /* Find the start and length of the badly spelled word. */
19459 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19460 if (len != 0)
19461 word = ml_get_cursor();
19462 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019463 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019464 {
19465 char_u *str = get_tv_string_chk(&argvars[0]);
19466 int capcol = -1;
19467
19468 if (str != NULL)
19469 {
19470 /* Check the argument for spelling. */
19471 while (*str != NUL)
19472 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019473 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019474 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019475 {
19476 word = str;
19477 break;
19478 }
19479 str += len;
19480 }
19481 }
19482 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019483#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019484
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019485 list_append_string(rettv->vval.v_list, word, len);
19486 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019487 attr == HLF_SPB ? "bad" :
19488 attr == HLF_SPR ? "rare" :
19489 attr == HLF_SPL ? "local" :
19490 attr == HLF_SPC ? "caps" :
19491 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019492}
19493
19494/*
19495 * "spellsuggest()" function
19496 */
19497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019498f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019499{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019500#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019501 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019502 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019503 int maxcount;
19504 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019505 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019506 listitem_T *li;
19507 int need_capital = FALSE;
19508#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019509
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019510 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019511 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019512
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019513#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019514 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019515 {
19516 str = get_tv_string(&argvars[0]);
19517 if (argvars[1].v_type != VAR_UNKNOWN)
19518 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019519 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019520 if (maxcount <= 0)
19521 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019522 if (argvars[2].v_type != VAR_UNKNOWN)
19523 {
19524 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19525 if (typeerr)
19526 return;
19527 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019528 }
19529 else
19530 maxcount = 25;
19531
Bram Moolenaar4770d092006-01-12 23:22:24 +000019532 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019533
19534 for (i = 0; i < ga.ga_len; ++i)
19535 {
19536 str = ((char_u **)ga.ga_data)[i];
19537
19538 li = listitem_alloc();
19539 if (li == NULL)
19540 vim_free(str);
19541 else
19542 {
19543 li->li_tv.v_type = VAR_STRING;
19544 li->li_tv.v_lock = 0;
19545 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019546 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019547 }
19548 }
19549 ga_clear(&ga);
19550 }
19551#endif
19552}
19553
Bram Moolenaar0d660222005-01-07 21:51:51 +000019554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019555f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019556{
19557 char_u *str;
19558 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019559 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019560 regmatch_T regmatch;
19561 char_u patbuf[NUMBUFLEN];
19562 char_u *save_cpo;
19563 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019564 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019565 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019566 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019567
19568 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19569 save_cpo = p_cpo;
19570 p_cpo = (char_u *)"";
19571
19572 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019573 if (argvars[1].v_type != VAR_UNKNOWN)
19574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019575 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19576 if (pat == NULL)
19577 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019578 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019579 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019580 }
19581 if (pat == NULL || *pat == NUL)
19582 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019583
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019584 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019586 if (typeerr)
19587 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588
Bram Moolenaar0d660222005-01-07 21:51:51 +000019589 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19590 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019592 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019593 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019594 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019595 if (*str == NUL)
19596 match = FALSE; /* empty item at the end */
19597 else
19598 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019599 if (match)
19600 end = regmatch.startp[0];
19601 else
19602 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019603 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19604 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019605 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019606 if (list_append_string(rettv->vval.v_list, str,
19607 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019608 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019609 }
19610 if (!match)
19611 break;
19612 /* Advance to just after the match. */
19613 if (regmatch.endp[0] > str)
19614 col = 0;
19615 else
19616 {
19617 /* Don't get stuck at the same match. */
19618#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019619 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019620#else
19621 col = 1;
19622#endif
19623 }
19624 str = regmatch.endp[0];
19625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626
Bram Moolenaar473de612013-06-08 18:19:48 +020019627 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629
Bram Moolenaar0d660222005-01-07 21:51:51 +000019630 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631}
19632
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019633#ifdef FEAT_FLOAT
19634/*
19635 * "sqrt()" function
19636 */
19637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019638f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019639{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019640 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019641
19642 rettv->v_type = VAR_FLOAT;
19643 if (get_float_arg(argvars, &f) == OK)
19644 rettv->vval.v_float = sqrt(f);
19645 else
19646 rettv->vval.v_float = 0.0;
19647}
19648
19649/*
19650 * "str2float()" function
19651 */
19652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019653f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019654{
19655 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19656
19657 if (*p == '+')
19658 p = skipwhite(p + 1);
19659 (void)string2float(p, &rettv->vval.v_float);
19660 rettv->v_type = VAR_FLOAT;
19661}
19662#endif
19663
Bram Moolenaar2c932302006-03-18 21:42:09 +000019664/*
19665 * "str2nr()" function
19666 */
19667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019668f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019669{
19670 int base = 10;
19671 char_u *p;
19672 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019673 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019674
19675 if (argvars[1].v_type != VAR_UNKNOWN)
19676 {
19677 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019678 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019679 {
19680 EMSG(_(e_invarg));
19681 return;
19682 }
19683 }
19684
19685 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019686 if (*p == '+')
19687 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019688 switch (base)
19689 {
19690 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19691 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19692 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19693 default: what = 0;
19694 }
19695 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019696 rettv->vval.v_number = n;
19697}
19698
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699#ifdef HAVE_STRFTIME
19700/*
19701 * "strftime({format}[, {time}])" function
19702 */
19703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019704f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705{
19706 char_u result_buf[256];
19707 struct tm *curtime;
19708 time_t seconds;
19709 char_u *p;
19710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019711 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019713 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019714 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 seconds = time(NULL);
19716 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 curtime = localtime(&seconds);
19719 /* MSVC returns NULL for an invalid value of seconds. */
19720 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019721 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 else
19723 {
19724# ifdef FEAT_MBYTE
19725 vimconv_T conv;
19726 char_u *enc;
19727
19728 conv.vc_type = CONV_NONE;
19729 enc = enc_locale();
19730 convert_setup(&conv, p_enc, enc);
19731 if (conv.vc_type != CONV_NONE)
19732 p = string_convert(&conv, p, NULL);
19733# endif
19734 if (p != NULL)
19735 (void)strftime((char *)result_buf, sizeof(result_buf),
19736 (char *)p, curtime);
19737 else
19738 result_buf[0] = NUL;
19739
19740# ifdef FEAT_MBYTE
19741 if (conv.vc_type != CONV_NONE)
19742 vim_free(p);
19743 convert_setup(&conv, enc, p_enc);
19744 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019745 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 else
19747# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019748 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749
19750# ifdef FEAT_MBYTE
19751 /* Release conversion descriptors */
19752 convert_setup(&conv, NULL, NULL);
19753 vim_free(enc);
19754# endif
19755 }
19756}
19757#endif
19758
19759/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019760 * "strgetchar()" function
19761 */
19762 static void
19763f_strgetchar(typval_T *argvars, typval_T *rettv)
19764{
19765 char_u *str;
19766 int len;
19767 int error = FALSE;
19768 int charidx;
19769
19770 rettv->vval.v_number = -1;
19771 str = get_tv_string_chk(&argvars[0]);
19772 if (str == NULL)
19773 return;
19774 len = (int)STRLEN(str);
19775 charidx = get_tv_number_chk(&argvars[1], &error);
19776 if (error)
19777 return;
19778#ifdef FEAT_MBYTE
19779 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019780 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019781
19782 while (charidx >= 0 && byteidx < len)
19783 {
19784 if (charidx == 0)
19785 {
19786 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19787 break;
19788 }
19789 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019790 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019791 }
19792 }
19793#else
19794 if (charidx < len)
19795 rettv->vval.v_number = str[charidx];
19796#endif
19797}
19798
19799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 * "stridx()" function
19801 */
19802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019803f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804{
19805 char_u buf[NUMBUFLEN];
19806 char_u *needle;
19807 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019808 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019812 needle = get_tv_string_chk(&argvars[1]);
19813 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019814 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019815 if (needle == NULL || haystack == NULL)
19816 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817
Bram Moolenaar33570922005-01-25 22:26:29 +000019818 if (argvars[2].v_type != VAR_UNKNOWN)
19819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019820 int error = FALSE;
19821
19822 start_idx = get_tv_number_chk(&argvars[2], &error);
19823 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019825 if (start_idx >= 0)
19826 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019827 }
19828
19829 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19830 if (pos != NULL)
19831 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832}
19833
19834/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019835 * "string()" function
19836 */
19837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019838f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019839{
19840 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019841 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019843 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019844 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19845 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019846 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019847 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019848 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849}
19850
19851/*
19852 * "strlen()" function
19853 */
19854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019855f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857 rettv->vval.v_number = (varnumber_T)(STRLEN(
19858 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859}
19860
19861/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019862 * "strchars()" function
19863 */
19864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019865f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019866{
19867 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019868 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019869#ifdef FEAT_MBYTE
19870 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019871 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019872#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019873
19874 if (argvars[1].v_type != VAR_UNKNOWN)
19875 skipcc = get_tv_number_chk(&argvars[1], NULL);
19876 if (skipcc < 0 || skipcc > 1)
19877 EMSG(_(e_invarg));
19878 else
19879 {
19880#ifdef FEAT_MBYTE
19881 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19882 while (*s != NUL)
19883 {
19884 func_mb_ptr2char_adv(&s);
19885 ++len;
19886 }
19887 rettv->vval.v_number = len;
19888#else
19889 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19890#endif
19891 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019892}
19893
19894/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019895 * "strdisplaywidth()" function
19896 */
19897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019898f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019899{
19900 char_u *s = get_tv_string(&argvars[0]);
19901 int col = 0;
19902
19903 if (argvars[1].v_type != VAR_UNKNOWN)
19904 col = get_tv_number(&argvars[1]);
19905
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019906 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019907}
19908
19909/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019910 * "strwidth()" function
19911 */
19912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019913f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019914{
19915 char_u *s = get_tv_string(&argvars[0]);
19916
19917 rettv->vval.v_number = (varnumber_T)(
19918#ifdef FEAT_MBYTE
19919 mb_string2cells(s, -1)
19920#else
19921 STRLEN(s)
19922#endif
19923 );
19924}
19925
19926/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019927 * "strcharpart()" function
19928 */
19929 static void
19930f_strcharpart(typval_T *argvars, typval_T *rettv)
19931{
19932#ifdef FEAT_MBYTE
19933 char_u *p;
19934 int nchar;
19935 int nbyte = 0;
19936 int charlen;
19937 int len = 0;
19938 int slen;
19939 int error = FALSE;
19940
19941 p = get_tv_string(&argvars[0]);
19942 slen = (int)STRLEN(p);
19943
19944 nchar = get_tv_number_chk(&argvars[1], &error);
19945 if (!error)
19946 {
19947 if (nchar > 0)
19948 while (nchar > 0 && nbyte < slen)
19949 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019950 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019951 --nchar;
19952 }
19953 else
19954 nbyte = nchar;
19955 if (argvars[2].v_type != VAR_UNKNOWN)
19956 {
19957 charlen = get_tv_number(&argvars[2]);
19958 while (charlen > 0 && nbyte + len < slen)
19959 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019960 int off = nbyte + len;
19961
19962 if (off < 0)
19963 len += 1;
19964 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019965 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019966 --charlen;
19967 }
19968 }
19969 else
19970 len = slen - nbyte; /* default: all bytes that are available. */
19971 }
19972
19973 /*
19974 * Only return the overlap between the specified part and the actual
19975 * string.
19976 */
19977 if (nbyte < 0)
19978 {
19979 len += nbyte;
19980 nbyte = 0;
19981 }
19982 else if (nbyte > slen)
19983 nbyte = slen;
19984 if (len < 0)
19985 len = 0;
19986 else if (nbyte + len > slen)
19987 len = slen - nbyte;
19988
19989 rettv->v_type = VAR_STRING;
19990 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19991#else
19992 f_strpart(argvars, rettv);
19993#endif
19994}
19995
19996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 * "strpart()" function
19998 */
19999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020000f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001{
20002 char_u *p;
20003 int n;
20004 int len;
20005 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020006 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020008 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 slen = (int)STRLEN(p);
20010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020011 n = get_tv_number_chk(&argvars[1], &error);
20012 if (error)
20013 len = 0;
20014 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020015 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 else
20017 len = slen - n; /* default len: all bytes that are available. */
20018
20019 /*
20020 * Only return the overlap between the specified part and the actual
20021 * string.
20022 */
20023 if (n < 0)
20024 {
20025 len += n;
20026 n = 0;
20027 }
20028 else if (n > slen)
20029 n = slen;
20030 if (len < 0)
20031 len = 0;
20032 else if (n + len > slen)
20033 len = slen - n;
20034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020035 rettv->v_type = VAR_STRING;
20036 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037}
20038
20039/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020040 * "strridx()" function
20041 */
20042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020043f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020044{
20045 char_u buf[NUMBUFLEN];
20046 char_u *needle;
20047 char_u *haystack;
20048 char_u *rest;
20049 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020050 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020052 needle = get_tv_string_chk(&argvars[1]);
20053 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020054
20055 rettv->vval.v_number = -1;
20056 if (needle == NULL || haystack == NULL)
20057 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020058
20059 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020060 if (argvars[2].v_type != VAR_UNKNOWN)
20061 {
20062 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020063 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020064 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020065 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020066 }
20067 else
20068 end_idx = haystack_len;
20069
Bram Moolenaar0d660222005-01-07 21:51:51 +000020070 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020071 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020072 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020073 lastmatch = haystack + end_idx;
20074 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020075 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020076 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020077 for (rest = haystack; *rest != '\0'; ++rest)
20078 {
20079 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020080 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020081 break;
20082 lastmatch = rest;
20083 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020084 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020085
20086 if (lastmatch == NULL)
20087 rettv->vval.v_number = -1;
20088 else
20089 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20090}
20091
20092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 * "strtrans()" function
20094 */
20095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020096f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 rettv->v_type = VAR_STRING;
20099 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100}
20101
20102/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020103 * "submatch()" function
20104 */
20105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020106f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020107{
Bram Moolenaar41571762014-04-02 19:00:58 +020020108 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020109 int no;
20110 int retList = 0;
20111
20112 no = (int)get_tv_number_chk(&argvars[0], &error);
20113 if (error)
20114 return;
20115 error = FALSE;
20116 if (argvars[1].v_type != VAR_UNKNOWN)
20117 retList = get_tv_number_chk(&argvars[1], &error);
20118 if (error)
20119 return;
20120
20121 if (retList == 0)
20122 {
20123 rettv->v_type = VAR_STRING;
20124 rettv->vval.v_string = reg_submatch(no);
20125 }
20126 else
20127 {
20128 rettv->v_type = VAR_LIST;
20129 rettv->vval.v_list = reg_submatch_list(no);
20130 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020131}
20132
20133/*
20134 * "substitute()" function
20135 */
20136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020137f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020138{
20139 char_u patbuf[NUMBUFLEN];
20140 char_u subbuf[NUMBUFLEN];
20141 char_u flagsbuf[NUMBUFLEN];
20142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020143 char_u *str = get_tv_string_chk(&argvars[0]);
20144 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20145 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20146 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20147
Bram Moolenaar0d660222005-01-07 21:51:51 +000020148 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020149 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20150 rettv->vval.v_string = NULL;
20151 else
20152 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020153}
20154
20155/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020156 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020159f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160{
20161 int id = 0;
20162#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020163 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 long col;
20165 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020166 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020168 lnum = get_tv_lnum(argvars); /* -1 on type error */
20169 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20170 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020172 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020173 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020174 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175#endif
20176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020177 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178}
20179
20180/*
20181 * "synIDattr(id, what [, mode])" function
20182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020184f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185{
20186 char_u *p = NULL;
20187#ifdef FEAT_SYN_HL
20188 int id;
20189 char_u *what;
20190 char_u *mode;
20191 char_u modebuf[NUMBUFLEN];
20192 int modec;
20193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020194 id = get_tv_number(&argvars[0]);
20195 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020196 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020198 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020200 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 modec = 0; /* replace invalid with current */
20202 }
20203 else
20204 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020205#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020206 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 modec = 'g';
20208 else
20209#endif
20210 if (t_colors > 1)
20211 modec = 'c';
20212 else
20213 modec = 't';
20214 }
20215
20216
20217 switch (TOLOWER_ASC(what[0]))
20218 {
20219 case 'b':
20220 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20221 p = highlight_color(id, what, modec);
20222 else /* bold */
20223 p = highlight_has_attr(id, HL_BOLD, modec);
20224 break;
20225
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020226 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 p = highlight_color(id, what, modec);
20228 break;
20229
20230 case 'i':
20231 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20232 p = highlight_has_attr(id, HL_INVERSE, modec);
20233 else /* italic */
20234 p = highlight_has_attr(id, HL_ITALIC, modec);
20235 break;
20236
20237 case 'n': /* name */
20238 p = get_highlight_name(NULL, id - 1);
20239 break;
20240
20241 case 'r': /* reverse */
20242 p = highlight_has_attr(id, HL_INVERSE, modec);
20243 break;
20244
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020245 case 's':
20246 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20247 p = highlight_color(id, what, modec);
20248 else /* standout */
20249 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 break;
20251
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020252 case 'u':
20253 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20254 /* underline */
20255 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20256 else
20257 /* undercurl */
20258 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259 break;
20260 }
20261
20262 if (p != NULL)
20263 p = vim_strsave(p);
20264#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020265 rettv->v_type = VAR_STRING;
20266 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267}
20268
20269/*
20270 * "synIDtrans(id)" function
20271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020273f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274{
20275 int id;
20276
20277#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020278 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279
20280 if (id > 0)
20281 id = syn_get_final_id(id);
20282 else
20283#endif
20284 id = 0;
20285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020286 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287}
20288
20289/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020290 * "synconcealed(lnum, col)" function
20291 */
20292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020293f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020294{
20295#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20296 long lnum;
20297 long col;
20298 int syntax_flags = 0;
20299 int cchar;
20300 int matchid = 0;
20301 char_u str[NUMBUFLEN];
20302#endif
20303
20304 rettv->v_type = VAR_LIST;
20305 rettv->vval.v_list = NULL;
20306
20307#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20308 lnum = get_tv_lnum(argvars); /* -1 on type error */
20309 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20310
20311 vim_memset(str, NUL, sizeof(str));
20312
20313 if (rettv_list_alloc(rettv) != FAIL)
20314 {
20315 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20316 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20317 && curwin->w_p_cole > 0)
20318 {
20319 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20320 syntax_flags = get_syntax_info(&matchid);
20321
20322 /* get the conceal character */
20323 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20324 {
20325 cchar = syn_get_sub_char();
20326 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20327 cchar = lcs_conceal;
20328 if (cchar != NUL)
20329 {
20330# ifdef FEAT_MBYTE
20331 if (has_mbyte)
20332 (*mb_char2bytes)(cchar, str);
20333 else
20334# endif
20335 str[0] = cchar;
20336 }
20337 }
20338 }
20339
20340 list_append_number(rettv->vval.v_list,
20341 (syntax_flags & HL_CONCEAL) != 0);
20342 /* -1 to auto-determine strlen */
20343 list_append_string(rettv->vval.v_list, str, -1);
20344 list_append_number(rettv->vval.v_list, matchid);
20345 }
20346#endif
20347}
20348
20349/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020350 * "synstack(lnum, col)" function
20351 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020353f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020354{
20355#ifdef FEAT_SYN_HL
20356 long lnum;
20357 long col;
20358 int i;
20359 int id;
20360#endif
20361
20362 rettv->v_type = VAR_LIST;
20363 rettv->vval.v_list = NULL;
20364
20365#ifdef FEAT_SYN_HL
20366 lnum = get_tv_lnum(argvars); /* -1 on type error */
20367 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20368
20369 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020370 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020371 && rettv_list_alloc(rettv) != FAIL)
20372 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020373 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020374 for (i = 0; ; ++i)
20375 {
20376 id = syn_get_stack_item(i);
20377 if (id < 0)
20378 break;
20379 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20380 break;
20381 }
20382 }
20383#endif
20384}
20385
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020387get_cmd_output_as_rettv(
20388 typval_T *argvars,
20389 typval_T *rettv,
20390 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020392 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020394 char_u *infile = NULL;
20395 char_u buf[NUMBUFLEN];
20396 int err = FALSE;
20397 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020398 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020399 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020401 rettv->v_type = VAR_STRING;
20402 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020403 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020404 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020405
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020406 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020407 {
20408 /*
20409 * Write the string to a temp file, to be used for input of the shell
20410 * command.
20411 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020412 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020413 {
20414 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020415 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020416 }
20417
20418 fd = mch_fopen((char *)infile, WRITEBIN);
20419 if (fd == NULL)
20420 {
20421 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020422 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020423 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020424 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020425 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020426 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20427 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020428 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020429 else
20430 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020431 size_t len;
20432
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020433 p = get_tv_string_buf_chk(&argvars[1], buf);
20434 if (p == NULL)
20435 {
20436 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020437 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020438 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020439 len = STRLEN(p);
20440 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020441 err = TRUE;
20442 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020443 if (fclose(fd) != 0)
20444 err = TRUE;
20445 if (err)
20446 {
20447 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020448 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020449 }
20450 }
20451
Bram Moolenaar52a72462014-08-29 15:53:52 +020020452 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20453 * echoes typeahead, that messes up the display. */
20454 if (!msg_silent)
20455 flags += SHELL_COOKED;
20456
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020457 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020459 int len;
20460 listitem_T *li;
20461 char_u *s = NULL;
20462 char_u *start;
20463 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020464 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465
Bram Moolenaar52a72462014-08-29 15:53:52 +020020466 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020467 if (res == NULL)
20468 goto errret;
20469
20470 list = list_alloc();
20471 if (list == NULL)
20472 goto errret;
20473
20474 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020476 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020477 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020478 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020479 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020480
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020481 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020482 if (s == NULL)
20483 goto errret;
20484
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020485 for (p = s; start < end; ++p, ++start)
20486 *p = *start == NUL ? NL : *start;
20487 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020488
20489 li = listitem_alloc();
20490 if (li == NULL)
20491 {
20492 vim_free(s);
20493 goto errret;
20494 }
20495 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020496 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020497 li->li_tv.vval.v_string = s;
20498 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020500
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020501 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020502 rettv->v_type = VAR_LIST;
20503 rettv->vval.v_list = list;
20504 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020506 else
20507 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020508 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020509#ifdef USE_CR
20510 /* translate <CR> into <NL> */
20511 if (res != NULL)
20512 {
20513 char_u *s;
20514
20515 for (s = res; *s; ++s)
20516 {
20517 if (*s == CAR)
20518 *s = NL;
20519 }
20520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521#else
20522# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020523 /* translate <CR><NL> into <NL> */
20524 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020526 char_u *s, *d;
20527
20528 d = res;
20529 for (s = res; *s; ++s)
20530 {
20531 if (s[0] == CAR && s[1] == NL)
20532 ++s;
20533 *d++ = *s;
20534 }
20535 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537# endif
20538#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020539 rettv->vval.v_string = res;
20540 res = NULL;
20541 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020542
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020543errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020544 if (infile != NULL)
20545 {
20546 mch_remove(infile);
20547 vim_free(infile);
20548 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020549 if (res != NULL)
20550 vim_free(res);
20551 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020552 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020553}
20554
20555/*
20556 * "system()" function
20557 */
20558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020559f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020560{
20561 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20562}
20563
20564/*
20565 * "systemlist()" function
20566 */
20567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020568f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020569{
20570 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571}
20572
20573/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020574 * "tabpagebuflist()" function
20575 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020577f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020578{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020579#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020580 tabpage_T *tp;
20581 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020582
20583 if (argvars[0].v_type == VAR_UNKNOWN)
20584 wp = firstwin;
20585 else
20586 {
20587 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20588 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020589 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020590 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020591 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020592 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020593 for (; wp != NULL; wp = wp->w_next)
20594 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020595 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020596 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020597 }
20598#endif
20599}
20600
20601
20602/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020603 * "tabpagenr()" function
20604 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020606f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020607{
20608 int nr = 1;
20609#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020610 char_u *arg;
20611
20612 if (argvars[0].v_type != VAR_UNKNOWN)
20613 {
20614 arg = get_tv_string_chk(&argvars[0]);
20615 nr = 0;
20616 if (arg != NULL)
20617 {
20618 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020619 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020620 else
20621 EMSG2(_(e_invexpr2), arg);
20622 }
20623 }
20624 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020625 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020626#endif
20627 rettv->vval.v_number = nr;
20628}
20629
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020630
20631#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020632static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020633
20634/*
20635 * Common code for tabpagewinnr() and winnr().
20636 */
20637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020638get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020639{
20640 win_T *twin;
20641 int nr = 1;
20642 win_T *wp;
20643 char_u *arg;
20644
20645 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20646 if (argvar->v_type != VAR_UNKNOWN)
20647 {
20648 arg = get_tv_string_chk(argvar);
20649 if (arg == NULL)
20650 nr = 0; /* type error; errmsg already given */
20651 else if (STRCMP(arg, "$") == 0)
20652 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20653 else if (STRCMP(arg, "#") == 0)
20654 {
20655 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20656 if (twin == NULL)
20657 nr = 0;
20658 }
20659 else
20660 {
20661 EMSG2(_(e_invexpr2), arg);
20662 nr = 0;
20663 }
20664 }
20665
20666 if (nr > 0)
20667 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20668 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020669 {
20670 if (wp == NULL)
20671 {
20672 /* didn't find it in this tabpage */
20673 nr = 0;
20674 break;
20675 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020676 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020677 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020678 return nr;
20679}
20680#endif
20681
20682/*
20683 * "tabpagewinnr()" function
20684 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020686f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020687{
20688 int nr = 1;
20689#ifdef FEAT_WINDOWS
20690 tabpage_T *tp;
20691
20692 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20693 if (tp == NULL)
20694 nr = 0;
20695 else
20696 nr = get_winnr(tp, &argvars[1]);
20697#endif
20698 rettv->vval.v_number = nr;
20699}
20700
20701
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020702/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020703 * "tagfiles()" function
20704 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020706f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020707{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020708 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020709 tagname_T tn;
20710 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020711
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020712 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020713 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020714 fname = alloc(MAXPATHL);
20715 if (fname == NULL)
20716 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020717
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020718 for (first = TRUE; ; first = FALSE)
20719 if (get_tagfname(&tn, first, fname) == FAIL
20720 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020721 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020722 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020723 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020724}
20725
20726/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020727 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020728 */
20729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020730f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020731{
20732 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020733
20734 tag_pattern = get_tv_string(&argvars[0]);
20735
20736 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020737 if (*tag_pattern == NUL)
20738 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020739
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020740 if (rettv_list_alloc(rettv) == OK)
20741 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020742}
20743
20744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 * "tempname()" function
20746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020748f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749{
20750 static int x = 'A';
20751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020752 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020753 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754
20755 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20756 * names. Skip 'I' and 'O', they are used for shell redirection. */
20757 do
20758 {
20759 if (x == 'Z')
20760 x = '0';
20761 else if (x == '9')
20762 x = 'A';
20763 else
20764 {
20765#ifdef EBCDIC
20766 if (x == 'I')
20767 x = 'J';
20768 else if (x == 'R')
20769 x = 'S';
20770 else
20771#endif
20772 ++x;
20773 }
20774 } while (x == 'I' || x == 'O');
20775}
20776
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020777#ifdef FEAT_FLOAT
20778/*
20779 * "tan()" function
20780 */
20781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020782f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020783{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020784 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020785
20786 rettv->v_type = VAR_FLOAT;
20787 if (get_float_arg(argvars, &f) == OK)
20788 rettv->vval.v_float = tan(f);
20789 else
20790 rettv->vval.v_float = 0.0;
20791}
20792
20793/*
20794 * "tanh()" function
20795 */
20796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020797f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020798{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020799 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020800
20801 rettv->v_type = VAR_FLOAT;
20802 if (get_float_arg(argvars, &f) == OK)
20803 rettv->vval.v_float = tanh(f);
20804 else
20805 rettv->vval.v_float = 0.0;
20806}
20807#endif
20808
Bram Moolenaar574860b2016-05-24 17:33:34 +020020809/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020810 * "test_alloc_fail(id, countdown, repeat)" function
20811 */
20812 static void
20813f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20814{
20815 if (argvars[0].v_type != VAR_NUMBER
20816 || argvars[0].vval.v_number <= 0
20817 || argvars[1].v_type != VAR_NUMBER
20818 || argvars[1].vval.v_number < 0
20819 || argvars[2].v_type != VAR_NUMBER)
20820 EMSG(_(e_invarg));
20821 else
20822 {
20823 alloc_fail_id = argvars[0].vval.v_number;
20824 if (alloc_fail_id >= aid_last)
20825 EMSG(_(e_invarg));
20826 alloc_fail_countdown = argvars[1].vval.v_number;
20827 alloc_fail_repeat = argvars[2].vval.v_number;
20828 did_outofmem_msg = FALSE;
20829 }
20830}
20831
20832/*
20833 * "test_disable_char_avail({expr})" function
20834 */
20835 static void
20836f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20837{
20838 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20839}
20840
20841/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020842 * "test_garbagecollect_now()" function
20843 */
20844 static void
20845f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20846{
20847 /* This is dangerous, any Lists and Dicts used internally may be freed
20848 * while still in use. */
20849 garbage_collect(TRUE);
20850}
20851
20852#ifdef FEAT_JOB_CHANNEL
20853 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020854f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020855{
20856 rettv->v_type = VAR_CHANNEL;
20857 rettv->vval.v_channel = NULL;
20858}
20859#endif
20860
20861 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020862f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020863{
20864 rettv->v_type = VAR_DICT;
20865 rettv->vval.v_dict = NULL;
20866}
20867
20868#ifdef FEAT_JOB_CHANNEL
20869 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020870f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020871{
20872 rettv->v_type = VAR_JOB;
20873 rettv->vval.v_job = NULL;
20874}
20875#endif
20876
20877 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020878f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020879{
20880 rettv->v_type = VAR_LIST;
20881 rettv->vval.v_list = NULL;
20882}
20883
20884 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020885f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020886{
20887 rettv->v_type = VAR_PARTIAL;
20888 rettv->vval.v_partial = NULL;
20889}
20890
20891 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020892f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020893{
20894 rettv->v_type = VAR_STRING;
20895 rettv->vval.v_string = NULL;
20896}
20897
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020898 static void
20899f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
20900{
20901 time_for_testing = (time_t)get_tv_number(&argvars[0]);
20902}
20903
Bram Moolenaar975b5272016-03-15 23:10:59 +010020904#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20905/*
20906 * Get a callback from "arg". It can be a Funcref or a function name.
20907 * When "arg" is zero return an empty string.
20908 * Return NULL for an invalid argument.
20909 */
20910 char_u *
20911get_callback(typval_T *arg, partial_T **pp)
20912{
20913 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20914 {
20915 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020916 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020917 return (*pp)->pt_name;
20918 }
20919 *pp = NULL;
20920 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20921 return arg->vval.v_string;
20922 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20923 return (char_u *)"";
20924 EMSG(_("E921: Invalid callback argument"));
20925 return NULL;
20926}
20927#endif
20928
20929#ifdef FEAT_TIMERS
20930/*
20931 * "timer_start(time, callback [, options])" function
20932 */
20933 static void
20934f_timer_start(typval_T *argvars, typval_T *rettv)
20935{
20936 long msec = get_tv_number(&argvars[0]);
20937 timer_T *timer;
20938 int repeat = 0;
20939 char_u *callback;
20940 dict_T *dict;
20941
Bram Moolenaar38499922016-04-22 20:46:52 +020020942 if (check_secure())
20943 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020944 if (argvars[2].v_type != VAR_UNKNOWN)
20945 {
20946 if (argvars[2].v_type != VAR_DICT
20947 || (dict = argvars[2].vval.v_dict) == NULL)
20948 {
20949 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20950 return;
20951 }
20952 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20953 repeat = get_dict_number(dict, (char_u *)"repeat");
20954 }
20955
20956 timer = create_timer(msec, repeat);
20957 callback = get_callback(&argvars[1], &timer->tr_partial);
20958 if (callback == NULL)
20959 {
20960 stop_timer(timer);
20961 rettv->vval.v_number = -1;
20962 }
20963 else
20964 {
20965 timer->tr_callback = vim_strsave(callback);
20966 rettv->vval.v_number = timer->tr_id;
20967 }
20968}
20969
20970/*
20971 * "timer_stop(timer)" function
20972 */
20973 static void
20974f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20975{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020976 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020977
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020978 if (argvars[0].v_type != VAR_NUMBER)
20979 {
20980 EMSG(_(e_number_exp));
20981 return;
20982 }
20983 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020984 if (timer != NULL)
20985 stop_timer(timer);
20986}
20987#endif
20988
Bram Moolenaard52d9742005-08-21 22:20:28 +000020989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 * "tolower(string)" function
20991 */
20992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020993f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994{
20995 char_u *p;
20996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020997 p = vim_strsave(get_tv_string(&argvars[0]));
20998 rettv->v_type = VAR_STRING;
20999 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000
21001 if (p != NULL)
21002 while (*p != NUL)
21003 {
21004#ifdef FEAT_MBYTE
21005 int l;
21006
21007 if (enc_utf8)
21008 {
21009 int c, lc;
21010
21011 c = utf_ptr2char(p);
21012 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021013 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 /* TODO: reallocate string when byte count changes. */
21015 if (utf_char2len(lc) == l)
21016 utf_char2bytes(lc, p);
21017 p += l;
21018 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021019 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 p += l; /* skip multi-byte character */
21021 else
21022#endif
21023 {
21024 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21025 ++p;
21026 }
21027 }
21028}
21029
21030/*
21031 * "toupper(string)" function
21032 */
21033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021034f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021036 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021037 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038}
21039
21040/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021041 * "tr(string, fromstr, tostr)" function
21042 */
21043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021044f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021045{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021046 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021047 char_u *fromstr;
21048 char_u *tostr;
21049 char_u *p;
21050#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021051 int inlen;
21052 int fromlen;
21053 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021054 int idx;
21055 char_u *cpstr;
21056 int cplen;
21057 int first = TRUE;
21058#endif
21059 char_u buf[NUMBUFLEN];
21060 char_u buf2[NUMBUFLEN];
21061 garray_T ga;
21062
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021063 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021064 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21065 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021066
21067 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021068 rettv->v_type = VAR_STRING;
21069 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021070 if (fromstr == NULL || tostr == NULL)
21071 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021072 ga_init2(&ga, (int)sizeof(char), 80);
21073
21074#ifdef FEAT_MBYTE
21075 if (!has_mbyte)
21076#endif
21077 /* not multi-byte: fromstr and tostr must be the same length */
21078 if (STRLEN(fromstr) != STRLEN(tostr))
21079 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021080#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021081error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021082#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021083 EMSG2(_(e_invarg2), fromstr);
21084 ga_clear(&ga);
21085 return;
21086 }
21087
21088 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021089 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021090 {
21091#ifdef FEAT_MBYTE
21092 if (has_mbyte)
21093 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021094 inlen = (*mb_ptr2len)(in_str);
21095 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021096 cplen = inlen;
21097 idx = 0;
21098 for (p = fromstr; *p != NUL; p += fromlen)
21099 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021100 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021101 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021102 {
21103 for (p = tostr; *p != NUL; p += tolen)
21104 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021105 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021106 if (idx-- == 0)
21107 {
21108 cplen = tolen;
21109 cpstr = p;
21110 break;
21111 }
21112 }
21113 if (*p == NUL) /* tostr is shorter than fromstr */
21114 goto error;
21115 break;
21116 }
21117 ++idx;
21118 }
21119
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021120 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021121 {
21122 /* Check that fromstr and tostr have the same number of
21123 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021124 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021125 first = FALSE;
21126 for (p = tostr; *p != NUL; p += tolen)
21127 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021128 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021129 --idx;
21130 }
21131 if (idx != 0)
21132 goto error;
21133 }
21134
Bram Moolenaarcde88542015-08-11 19:14:00 +020021135 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021136 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021137 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021138
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021139 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021140 }
21141 else
21142#endif
21143 {
21144 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021145 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021146 if (p != NULL)
21147 ga_append(&ga, tostr[p - fromstr]);
21148 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021149 ga_append(&ga, *in_str);
21150 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021151 }
21152 }
21153
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021154 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021155 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021156 ga_append(&ga, NUL);
21157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021158 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021159}
21160
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021161#ifdef FEAT_FLOAT
21162/*
21163 * "trunc({float})" function
21164 */
21165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021166f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021167{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021168 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021169
21170 rettv->v_type = VAR_FLOAT;
21171 if (get_float_arg(argvars, &f) == OK)
21172 /* trunc() is not in C90, use floor() or ceil() instead. */
21173 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21174 else
21175 rettv->vval.v_float = 0.0;
21176}
21177#endif
21178
Bram Moolenaar8299df92004-07-10 09:47:34 +000021179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 * "type(expr)" function
21181 */
21182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021183f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021185 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021186
21187 switch (argvars[0].v_type)
21188 {
21189 case VAR_NUMBER: n = 0; break;
21190 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021191 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021192 case VAR_FUNC: n = 2; break;
21193 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021194 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021195 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021196 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021197 if (argvars[0].vval.v_number == VVAL_FALSE
21198 || argvars[0].vval.v_number == VVAL_TRUE)
21199 n = 6;
21200 else
21201 n = 7;
21202 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021203 case VAR_JOB: n = 8; break;
21204 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021205 case VAR_UNKNOWN:
21206 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21207 n = -1;
21208 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021209 }
21210 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211}
21212
21213/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021214 * "undofile(name)" function
21215 */
21216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021217f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021218{
21219 rettv->v_type = VAR_STRING;
21220#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021221 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021222 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021223
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021224 if (*fname == NUL)
21225 {
21226 /* If there is no file name there will be no undo file. */
21227 rettv->vval.v_string = NULL;
21228 }
21229 else
21230 {
21231 char_u *ffname = FullName_save(fname, FALSE);
21232
21233 if (ffname != NULL)
21234 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21235 vim_free(ffname);
21236 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021237 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021238#else
21239 rettv->vval.v_string = NULL;
21240#endif
21241}
21242
21243/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021244 * "undotree()" function
21245 */
21246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021247f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021248{
21249 if (rettv_dict_alloc(rettv) == OK)
21250 {
21251 dict_T *dict = rettv->vval.v_dict;
21252 list_T *list;
21253
Bram Moolenaar730cde92010-06-27 05:18:54 +020021254 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021255 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021256 dict_add_nr_str(dict, "save_last",
21257 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021258 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21259 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021260 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021261
21262 list = list_alloc();
21263 if (list != NULL)
21264 {
21265 u_eval_tree(curbuf->b_u_oldhead, list);
21266 dict_add_list(dict, "entries", list);
21267 }
21268 }
21269}
21270
21271/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021272 * "values(dict)" function
21273 */
21274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021275f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021276{
21277 dict_list(argvars, rettv, 1);
21278}
21279
21280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 * "virtcol(string)" function
21282 */
21283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021284f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285{
21286 colnr_T vcol = 0;
21287 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021288 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021290 fp = var2fpos(&argvars[0], FALSE, &fnum);
21291 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21292 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 {
21294 getvvcol(curwin, fp, NULL, NULL, &vcol);
21295 ++vcol;
21296 }
21297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021298 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299}
21300
21301/*
21302 * "visualmode()" function
21303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021305f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 char_u str[2];
21308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021309 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 str[0] = curbuf->b_visual_mode_eval;
21311 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021312 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313
21314 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021315 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317}
21318
21319/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021320 * "wildmenumode()" function
21321 */
21322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021323f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021324{
21325#ifdef FEAT_WILDMENU
21326 if (wild_menu_showing)
21327 rettv->vval.v_number = 1;
21328#endif
21329}
21330
21331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 * "winbufnr(nr)" function
21333 */
21334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021335f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336{
21337 win_T *wp;
21338
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021339 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021341 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021343 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344}
21345
21346/*
21347 * "wincol()" function
21348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021350f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351{
21352 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021353 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354}
21355
21356/*
21357 * "winheight(nr)" function
21358 */
21359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021360f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361{
21362 win_T *wp;
21363
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021364 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021366 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021368 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369}
21370
21371/*
21372 * "winline()" function
21373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021375f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376{
21377 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021378 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379}
21380
21381/*
21382 * "winnr()" function
21383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021385f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386{
21387 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021388
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021390 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021392 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393}
21394
21395/*
21396 * "winrestcmd()" function
21397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021399f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400{
21401#ifdef FEAT_WINDOWS
21402 win_T *wp;
21403 int winnr = 1;
21404 garray_T ga;
21405 char_u buf[50];
21406
21407 ga_init2(&ga, (int)sizeof(char), 70);
21408 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21409 {
21410 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21411 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21413 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 ++winnr;
21415 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021416 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021418 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021420 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021422 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423}
21424
21425/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021426 * "winrestview()" function
21427 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021429f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021430{
21431 dict_T *dict;
21432
21433 if (argvars[0].v_type != VAR_DICT
21434 || (dict = argvars[0].vval.v_dict) == NULL)
21435 EMSG(_(e_invarg));
21436 else
21437 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021438 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21439 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21440 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21441 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021442#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021443 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21444 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021445#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021446 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21447 {
21448 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21449 curwin->w_set_curswant = FALSE;
21450 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021451
Bram Moolenaar82c25852014-05-28 16:47:16 +020021452 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21453 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021454#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021455 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21456 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021457#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021458 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21459 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21460 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21461 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021462
21463 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021464 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021465# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021466 win_new_width(curwin, W_WIDTH(curwin));
21467# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021468 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021469
Bram Moolenaarb851a962014-10-31 15:45:52 +010021470 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021471 curwin->w_topline = 1;
21472 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21473 curwin->w_topline = curbuf->b_ml.ml_line_count;
21474#ifdef FEAT_DIFF
21475 check_topfill(curwin, TRUE);
21476#endif
21477 }
21478}
21479
21480/*
21481 * "winsaveview()" function
21482 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021484f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021485{
21486 dict_T *dict;
21487
Bram Moolenaara800b422010-06-27 01:15:55 +020021488 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021489 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021490 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021491
21492 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21493 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21494#ifdef FEAT_VIRTUALEDIT
21495 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21496#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021497 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021498 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21499
21500 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21501#ifdef FEAT_DIFF
21502 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21503#endif
21504 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21505 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21506}
21507
21508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 * "winwidth(nr)" function
21510 */
21511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021512f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513{
21514 win_T *wp;
21515
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021516 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021518 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021520#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021521 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021523 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524#endif
21525}
21526
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021528 * "wordcount()" function
21529 */
21530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021531f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021532{
21533 if (rettv_dict_alloc(rettv) == FAIL)
21534 return;
21535 cursor_pos_info(rettv->vval.v_dict);
21536}
21537
21538/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021539 * Write list of strings to file
21540 */
21541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021542write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021543{
21544 listitem_T *li;
21545 int c;
21546 int ret = OK;
21547 char_u *s;
21548
21549 for (li = list->lv_first; li != NULL; li = li->li_next)
21550 {
21551 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21552 {
21553 if (*s == '\n')
21554 c = putc(NUL, fd);
21555 else
21556 c = putc(*s, fd);
21557 if (c == EOF)
21558 {
21559 ret = FAIL;
21560 break;
21561 }
21562 }
21563 if (!binary || li->li_next != NULL)
21564 if (putc('\n', fd) == EOF)
21565 {
21566 ret = FAIL;
21567 break;
21568 }
21569 if (ret == FAIL)
21570 {
21571 EMSG(_(e_write));
21572 break;
21573 }
21574 }
21575 return ret;
21576}
21577
21578/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021579 * "writefile()" function
21580 */
21581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021582f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021583{
21584 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021585 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021586 char_u *fname;
21587 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021588 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021589
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021590 if (check_restricted() || check_secure())
21591 return;
21592
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021593 if (argvars[0].v_type != VAR_LIST)
21594 {
21595 EMSG2(_(e_listarg), "writefile()");
21596 return;
21597 }
21598 if (argvars[0].vval.v_list == NULL)
21599 return;
21600
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021601 if (argvars[2].v_type != VAR_UNKNOWN)
21602 {
21603 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21604 binary = TRUE;
21605 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21606 append = TRUE;
21607 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021608
21609 /* Always open the file in binary mode, library functions have a mind of
21610 * their own about CR-LF conversion. */
21611 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021612 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21613 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021614 {
21615 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21616 ret = -1;
21617 }
21618 else
21619 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021620 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21621 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021622 fclose(fd);
21623 }
21624
21625 rettv->vval.v_number = ret;
21626}
21627
21628/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021629 * "xor(expr, expr)" function
21630 */
21631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021632f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021633{
21634 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21635 ^ get_tv_number_chk(&argvars[1], NULL);
21636}
21637
21638
21639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021641 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 */
21643 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021644var2fpos(
21645 typval_T *varp,
21646 int dollar_lnum, /* TRUE when $ is last line */
21647 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021649 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021651 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652
Bram Moolenaara5525202006-03-02 22:52:09 +000021653 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021654 if (varp->v_type == VAR_LIST)
21655 {
21656 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021657 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021658 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021659 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021660
21661 l = varp->vval.v_list;
21662 if (l == NULL)
21663 return NULL;
21664
21665 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021666 pos.lnum = list_find_nr(l, 0L, &error);
21667 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021668 return NULL; /* invalid line number */
21669
21670 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021671 pos.col = list_find_nr(l, 1L, &error);
21672 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021673 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021674 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021675
21676 /* We accept "$" for the column number: last column. */
21677 li = list_find(l, 1L);
21678 if (li != NULL && li->li_tv.v_type == VAR_STRING
21679 && li->li_tv.vval.v_string != NULL
21680 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21681 pos.col = len + 1;
21682
Bram Moolenaara5525202006-03-02 22:52:09 +000021683 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021684 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021685 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021686 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021687
Bram Moolenaara5525202006-03-02 22:52:09 +000021688#ifdef FEAT_VIRTUALEDIT
21689 /* Get the virtual offset. Defaults to zero. */
21690 pos.coladd = list_find_nr(l, 2L, &error);
21691 if (error)
21692 pos.coladd = 0;
21693#endif
21694
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021695 return &pos;
21696 }
21697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021698 name = get_tv_string_chk(varp);
21699 if (name == NULL)
21700 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021701 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021703 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21704 {
21705 if (VIsual_active)
21706 return &VIsual;
21707 return &curwin->w_cursor;
21708 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021709 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021711 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21713 return NULL;
21714 return pp;
21715 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021716
21717#ifdef FEAT_VIRTUALEDIT
21718 pos.coladd = 0;
21719#endif
21720
Bram Moolenaar477933c2007-07-17 14:32:23 +000021721 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021722 {
21723 pos.col = 0;
21724 if (name[1] == '0') /* "w0": first visible line */
21725 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021726 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021727 pos.lnum = curwin->w_topline;
21728 return &pos;
21729 }
21730 else if (name[1] == '$') /* "w$": last visible line */
21731 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021732 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021733 pos.lnum = curwin->w_botline - 1;
21734 return &pos;
21735 }
21736 }
21737 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021739 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 {
21741 pos.lnum = curbuf->b_ml.ml_line_count;
21742 pos.col = 0;
21743 }
21744 else
21745 {
21746 pos.lnum = curwin->w_cursor.lnum;
21747 pos.col = (colnr_T)STRLEN(ml_get_curline());
21748 }
21749 return &pos;
21750 }
21751 return NULL;
21752}
21753
21754/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021755 * Convert list in "arg" into a position and optional file number.
21756 * When "fnump" is NULL there is no file number, only 3 items.
21757 * Note that the column is passed on as-is, the caller may want to decrement
21758 * it to use 1 for the first column.
21759 * Return FAIL when conversion is not possible, doesn't check the position for
21760 * validity.
21761 */
21762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021763list2fpos(
21764 typval_T *arg,
21765 pos_T *posp,
21766 int *fnump,
21767 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021768{
21769 list_T *l = arg->vval.v_list;
21770 long i = 0;
21771 long n;
21772
Bram Moolenaar493c1782014-05-28 14:34:46 +020021773 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21774 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021775 if (arg->v_type != VAR_LIST
21776 || l == NULL
21777 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021778 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021779 return FAIL;
21780
21781 if (fnump != NULL)
21782 {
21783 n = list_find_nr(l, i++, NULL); /* fnum */
21784 if (n < 0)
21785 return FAIL;
21786 if (n == 0)
21787 n = curbuf->b_fnum; /* current buffer */
21788 *fnump = n;
21789 }
21790
21791 n = list_find_nr(l, i++, NULL); /* lnum */
21792 if (n < 0)
21793 return FAIL;
21794 posp->lnum = n;
21795
21796 n = list_find_nr(l, i++, NULL); /* col */
21797 if (n < 0)
21798 return FAIL;
21799 posp->col = n;
21800
21801#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021802 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021803 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021804 posp->coladd = 0;
21805 else
21806 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021807#endif
21808
Bram Moolenaar493c1782014-05-28 14:34:46 +020021809 if (curswantp != NULL)
21810 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21811
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021812 return OK;
21813}
21814
21815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 * Get the length of an environment variable name.
21817 * Advance "arg" to the first character after the name.
21818 * Return 0 for error.
21819 */
21820 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021821get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822{
21823 char_u *p;
21824 int len;
21825
21826 for (p = *arg; vim_isIDc(*p); ++p)
21827 ;
21828 if (p == *arg) /* no name found */
21829 return 0;
21830
21831 len = (int)(p - *arg);
21832 *arg = p;
21833 return len;
21834}
21835
21836/*
21837 * Get the length of the name of a function or internal variable.
21838 * "arg" is advanced to the first non-white character after the name.
21839 * Return 0 if something is wrong.
21840 */
21841 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021842get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843{
21844 char_u *p;
21845 int len;
21846
21847 /* Find the end of the name. */
21848 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021849 {
21850 if (*p == ':')
21851 {
21852 /* "s:" is start of "s:var", but "n:" is not and can be used in
21853 * slice "[n:]". Also "xx:" is not a namespace. */
21854 len = (int)(p - *arg);
21855 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21856 || len > 1)
21857 break;
21858 }
21859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 if (p == *arg) /* no name found */
21861 return 0;
21862
21863 len = (int)(p - *arg);
21864 *arg = skipwhite(p);
21865
21866 return len;
21867}
21868
21869/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021870 * Get the length of the name of a variable or function.
21871 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021873 * Return -1 if curly braces expansion failed.
21874 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875 * If the name contains 'magic' {}'s, expand them and return the
21876 * expanded name in an allocated string via 'alias' - caller must free.
21877 */
21878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021879get_name_len(
21880 char_u **arg,
21881 char_u **alias,
21882 int evaluate,
21883 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884{
21885 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 char_u *p;
21887 char_u *expr_start;
21888 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889
21890 *alias = NULL; /* default to no alias */
21891
21892 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21893 && (*arg)[2] == (int)KE_SNR)
21894 {
21895 /* hard coded <SNR>, already translated */
21896 *arg += 3;
21897 return get_id_len(arg) + 3;
21898 }
21899 len = eval_fname_script(*arg);
21900 if (len > 0)
21901 {
21902 /* literal "<SID>", "s:" or "<SNR>" */
21903 *arg += len;
21904 }
21905
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021907 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021909 p = find_name_end(*arg, &expr_start, &expr_end,
21910 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911 if (expr_start != NULL)
21912 {
21913 char_u *temp_string;
21914
21915 if (!evaluate)
21916 {
21917 len += (int)(p - *arg);
21918 *arg = skipwhite(p);
21919 return len;
21920 }
21921
21922 /*
21923 * Include any <SID> etc in the expanded string:
21924 * Thus the -len here.
21925 */
21926 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21927 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021928 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 *alias = temp_string;
21930 *arg = skipwhite(p);
21931 return (int)STRLEN(temp_string);
21932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933
21934 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021935 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 EMSG2(_(e_invexpr2), *arg);
21937
21938 return len;
21939}
21940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021941/*
21942 * Find the end of a variable or function name, taking care of magic braces.
21943 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21944 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021945 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021946 * Return a pointer to just after the name. Equal to "arg" if there is no
21947 * valid name.
21948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021950find_name_end(
21951 char_u *arg,
21952 char_u **expr_start,
21953 char_u **expr_end,
21954 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021956 int mb_nest = 0;
21957 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021959 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021961 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021963 *expr_start = NULL;
21964 *expr_end = NULL;
21965 }
21966
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021967 /* Quick check for valid starting character. */
21968 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21969 return arg;
21970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021971 for (p = arg; *p != NUL
21972 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021973 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021974 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021975 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021976 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021977 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021978 if (*p == '\'')
21979 {
21980 /* skip over 'string' to avoid counting [ and ] inside it. */
21981 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21982 ;
21983 if (*p == NUL)
21984 break;
21985 }
21986 else if (*p == '"')
21987 {
21988 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21989 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21990 if (*p == '\\' && p[1] != NUL)
21991 ++p;
21992 if (*p == NUL)
21993 break;
21994 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021995 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21996 {
21997 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021998 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021999 len = (int)(p - arg);
22000 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022001 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022002 break;
22003 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022005 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022007 if (*p == '[')
22008 ++br_nest;
22009 else if (*p == ']')
22010 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022013 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022015 if (*p == '{')
22016 {
22017 mb_nest++;
22018 if (expr_start != NULL && *expr_start == NULL)
22019 *expr_start = p;
22020 }
22021 else if (*p == '}')
22022 {
22023 mb_nest--;
22024 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22025 *expr_end = p;
22026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 }
22029
22030 return p;
22031}
22032
22033/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022034 * Expands out the 'magic' {}'s in a variable/function name.
22035 * Note that this can call itself recursively, to deal with
22036 * constructs like foo{bar}{baz}{bam}
22037 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22038 * "in_start" ^
22039 * "expr_start" ^
22040 * "expr_end" ^
22041 * "in_end" ^
22042 *
22043 * Returns a new allocated string, which the caller must free.
22044 * Returns NULL for failure.
22045 */
22046 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022047make_expanded_name(
22048 char_u *in_start,
22049 char_u *expr_start,
22050 char_u *expr_end,
22051 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022052{
22053 char_u c1;
22054 char_u *retval = NULL;
22055 char_u *temp_result;
22056 char_u *nextcmd = NULL;
22057
22058 if (expr_end == NULL || in_end == NULL)
22059 return NULL;
22060 *expr_start = NUL;
22061 *expr_end = NUL;
22062 c1 = *in_end;
22063 *in_end = NUL;
22064
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022065 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022066 if (temp_result != NULL && nextcmd == NULL)
22067 {
22068 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22069 + (in_end - expr_end) + 1));
22070 if (retval != NULL)
22071 {
22072 STRCPY(retval, in_start);
22073 STRCAT(retval, temp_result);
22074 STRCAT(retval, expr_end + 1);
22075 }
22076 }
22077 vim_free(temp_result);
22078
22079 *in_end = c1; /* put char back for error messages */
22080 *expr_start = '{';
22081 *expr_end = '}';
22082
22083 if (retval != NULL)
22084 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022085 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022086 if (expr_start != NULL)
22087 {
22088 /* Further expansion! */
22089 temp_result = make_expanded_name(retval, expr_start,
22090 expr_end, temp_result);
22091 vim_free(retval);
22092 retval = temp_result;
22093 }
22094 }
22095
22096 return retval;
22097}
22098
22099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022101 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 */
22103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022104eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022106 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22107}
22108
22109/*
22110 * Return TRUE if character "c" can be used as the first character in a
22111 * variable or function name (excluding '{' and '}').
22112 */
22113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022114eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022115{
22116 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117}
22118
22119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 * Set number v: variable to "val".
22121 */
22122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022123set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022125 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126}
22127
22128/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022129 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022130 */
22131 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022132get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022134 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135}
22136
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022137/*
22138 * Get string v: variable value. Uses a static buffer, can only be used once.
22139 */
22140 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022141get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022142{
22143 return get_tv_string(&vimvars[idx].vv_tv);
22144}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022145
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022147 * Get List v: variable value. Caller must take care of reference count when
22148 * needed.
22149 */
22150 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022151get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022152{
22153 return vimvars[idx].vv_list;
22154}
22155
22156/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022157 * Set v:char to character "c".
22158 */
22159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022160set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022161{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022162 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022163
22164#ifdef FEAT_MBYTE
22165 if (has_mbyte)
22166 buf[(*mb_char2bytes)(c, buf)] = NUL;
22167 else
22168#endif
22169 {
22170 buf[0] = c;
22171 buf[1] = NUL;
22172 }
22173 set_vim_var_string(VV_CHAR, buf, -1);
22174}
22175
22176/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022177 * Set v:count to "count" and v:count1 to "count1".
22178 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 */
22180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022181set_vcount(
22182 long count,
22183 long count1,
22184 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022186 if (set_prevcount)
22187 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022188 vimvars[VV_COUNT].vv_nr = count;
22189 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022190}
22191
22192/*
22193 * Set string v: variable to a copy of "val".
22194 */
22195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022196set_vim_var_string(
22197 int idx,
22198 char_u *val,
22199 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200{
Bram Moolenaara542c682016-01-31 16:28:04 +010022201 clear_tv(&vimvars[idx].vv_di.di_tv);
22202 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022204 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022206 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022208 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209}
22210
22211/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022212 * Set List v: variable to "val".
22213 */
22214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022215set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022216{
Bram Moolenaara542c682016-01-31 16:28:04 +010022217 clear_tv(&vimvars[idx].vv_di.di_tv);
22218 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022219 vimvars[idx].vv_list = val;
22220 if (val != NULL)
22221 ++val->lv_refcount;
22222}
22223
22224/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022225 * Set Dictionary v: variable to "val".
22226 */
22227 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022228set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022229{
22230 int todo;
22231 hashitem_T *hi;
22232
Bram Moolenaara542c682016-01-31 16:28:04 +010022233 clear_tv(&vimvars[idx].vv_di.di_tv);
22234 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022235 vimvars[idx].vv_dict = val;
22236 if (val != NULL)
22237 {
22238 ++val->dv_refcount;
22239
22240 /* Set readonly */
22241 todo = (int)val->dv_hashtab.ht_used;
22242 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22243 {
22244 if (HASHITEM_EMPTY(hi))
22245 continue;
22246 --todo;
22247 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22248 }
22249 }
22250}
22251
22252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 * Set v:register if needed.
22254 */
22255 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022256set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257{
22258 char_u regname;
22259
22260 if (c == 0 || c == ' ')
22261 regname = '"';
22262 else
22263 regname = c;
22264 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022265 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266 set_vim_var_string(VV_REG, &regname, 1);
22267}
22268
22269/*
22270 * Get or set v:exception. If "oldval" == NULL, return the current value.
22271 * Otherwise, restore the value to "oldval" and return NULL.
22272 * Must always be called in pairs to save and restore v:exception! Does not
22273 * take care of memory allocations.
22274 */
22275 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022276v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277{
22278 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022279 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280
Bram Moolenaare9a41262005-01-15 22:18:47 +000022281 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 return NULL;
22283}
22284
22285/*
22286 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22287 * Otherwise, restore the value to "oldval" and return NULL.
22288 * Must always be called in pairs to save and restore v:throwpoint! Does not
22289 * take care of memory allocations.
22290 */
22291 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022292v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293{
22294 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022295 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296
Bram Moolenaare9a41262005-01-15 22:18:47 +000022297 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 return NULL;
22299}
22300
22301#if defined(FEAT_AUTOCMD) || defined(PROTO)
22302/*
22303 * Set v:cmdarg.
22304 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22305 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22306 * Must always be called in pairs!
22307 */
22308 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022309set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310{
22311 char_u *oldval;
22312 char_u *newval;
22313 unsigned len;
22314
Bram Moolenaare9a41262005-01-15 22:18:47 +000022315 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022316 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022318 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022319 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022320 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321 }
22322
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022323 if (eap->force_bin == FORCE_BIN)
22324 len = 6;
22325 else if (eap->force_bin == FORCE_NOBIN)
22326 len = 8;
22327 else
22328 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022329
22330 if (eap->read_edit)
22331 len += 7;
22332
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022333 if (eap->force_ff != 0)
22334 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22335# ifdef FEAT_MBYTE
22336 if (eap->force_enc != 0)
22337 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022338 if (eap->bad_char != 0)
22339 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022340# endif
22341
22342 newval = alloc(len + 1);
22343 if (newval == NULL)
22344 return NULL;
22345
22346 if (eap->force_bin == FORCE_BIN)
22347 sprintf((char *)newval, " ++bin");
22348 else if (eap->force_bin == FORCE_NOBIN)
22349 sprintf((char *)newval, " ++nobin");
22350 else
22351 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022352
22353 if (eap->read_edit)
22354 STRCAT(newval, " ++edit");
22355
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022356 if (eap->force_ff != 0)
22357 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22358 eap->cmd + eap->force_ff);
22359# ifdef FEAT_MBYTE
22360 if (eap->force_enc != 0)
22361 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22362 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022363 if (eap->bad_char == BAD_KEEP)
22364 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22365 else if (eap->bad_char == BAD_DROP)
22366 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22367 else if (eap->bad_char != 0)
22368 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022369# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022370 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022371 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372}
22373#endif
22374
22375/*
22376 * Get the value of internal variable "name".
22377 * Return OK or FAIL.
22378 */
22379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022380get_var_tv(
22381 char_u *name,
22382 int len, /* length of "name" */
22383 typval_T *rettv, /* NULL when only checking existence */
22384 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22385 int verbose, /* may give error message */
22386 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387{
22388 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022389 typval_T *tv = NULL;
22390 typval_T atv;
22391 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393
22394 /* truncate the name, so that we can use strcmp() */
22395 cc = name[len];
22396 name[len] = NUL;
22397
22398 /*
22399 * Check for "b:changedtick".
22400 */
22401 if (STRCMP(name, "b:changedtick") == 0)
22402 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022403 atv.v_type = VAR_NUMBER;
22404 atv.vval.v_number = curbuf->b_changedtick;
22405 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 }
22407
22408 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 * Check for user-defined variables.
22410 */
22411 else
22412 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022413 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022415 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022416 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022417 if (dip != NULL)
22418 *dip = v;
22419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 }
22421
Bram Moolenaare9a41262005-01-15 22:18:47 +000022422 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022424 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022425 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 ret = FAIL;
22427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022428 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022429 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430
22431 name[len] = cc;
22432
22433 return ret;
22434}
22435
22436/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022437 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22438 * Also handle function call with Funcref variable: func(expr)
22439 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22440 */
22441 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022442handle_subscript(
22443 char_u **arg,
22444 typval_T *rettv,
22445 int evaluate, /* do more than finding the end */
22446 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022447{
22448 int ret = OK;
22449 dict_T *selfdict = NULL;
22450 char_u *s;
22451 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022452 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022453
22454 while (ret == OK
22455 && (**arg == '['
22456 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022457 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22458 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022459 && !vim_iswhite(*(*arg - 1)))
22460 {
22461 if (**arg == '(')
22462 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022463 partial_T *pt = NULL;
22464
Bram Moolenaard9fba312005-06-26 22:34:35 +000022465 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022466 if (evaluate)
22467 {
22468 functv = *rettv;
22469 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022470
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022471 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022472 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022473 {
22474 pt = functv.vval.v_partial;
22475 s = pt->pt_name;
22476 }
22477 else
22478 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022479 }
22480 else
22481 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022482 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022483 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022484 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022485
22486 /* Clear the funcref afterwards, so that deleting it while
22487 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022488 if (evaluate)
22489 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022490
22491 /* Stop the expression evaluation when immediately aborting on
22492 * error, or when an interrupt occurred or an exception was thrown
22493 * but not caught. */
22494 if (aborting())
22495 {
22496 if (ret == OK)
22497 clear_tv(rettv);
22498 ret = FAIL;
22499 }
22500 dict_unref(selfdict);
22501 selfdict = NULL;
22502 }
22503 else /* **arg == '[' || **arg == '.' */
22504 {
22505 dict_unref(selfdict);
22506 if (rettv->v_type == VAR_DICT)
22507 {
22508 selfdict = rettv->vval.v_dict;
22509 if (selfdict != NULL)
22510 ++selfdict->dv_refcount;
22511 }
22512 else
22513 selfdict = NULL;
22514 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22515 {
22516 clear_tv(rettv);
22517 ret = FAIL;
22518 }
22519 }
22520 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022521
Bram Moolenaar1d429612016-05-24 15:44:17 +020022522 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22523 * Don't do this when "Func" is already a partial that was bound
22524 * explicitly (pt_auto is FALSE). */
22525 if (selfdict != NULL
22526 && (rettv->v_type == VAR_FUNC
22527 || (rettv->v_type == VAR_PARTIAL
22528 && (rettv->vval.v_partial->pt_auto
22529 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022530 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022531 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22532 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022533 char_u *tofree = NULL;
22534 ufunc_T *fp;
22535 char_u fname_buf[FLEN_FIXED + 1];
22536 int error;
22537
22538 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022539 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022540 fp = find_func(fname);
22541 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022542
Bram Moolenaar65639032016-03-16 21:40:30 +010022543 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022544 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022545 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22546
22547 if (pt != NULL)
22548 {
22549 pt->pt_refcount = 1;
22550 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022551 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022552 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022553 if (rettv->v_type == VAR_FUNC)
22554 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022555 /* Just a function: Take over the function name and use
22556 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022557 pt->pt_name = rettv->vval.v_string;
22558 }
22559 else
22560 {
22561 partial_T *ret_pt = rettv->vval.v_partial;
22562 int i;
22563
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022564 /* Partial: copy the function name, use selfdict and copy
22565 * args. Can't take over name or args, the partial might
22566 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022567 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022568 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022569 if (ret_pt->pt_argc > 0)
22570 {
22571 pt->pt_argv = (typval_T *)alloc(
22572 sizeof(typval_T) * ret_pt->pt_argc);
22573 if (pt->pt_argv == NULL)
22574 /* out of memory: drop the arguments */
22575 pt->pt_argc = 0;
22576 else
22577 {
22578 pt->pt_argc = ret_pt->pt_argc;
22579 for (i = 0; i < pt->pt_argc; i++)
22580 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22581 }
22582 }
22583 partial_unref(ret_pt);
22584 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022585 rettv->v_type = VAR_PARTIAL;
22586 rettv->vval.v_partial = pt;
22587 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022588 }
22589 }
22590
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022591 dict_unref(selfdict);
22592 return ret;
22593}
22594
22595/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022596 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022597 * value).
22598 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022599 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022600alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022601{
Bram Moolenaar33570922005-01-25 22:26:29 +000022602 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022603}
22604
22605/*
22606 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 * The string "s" must have been allocated, it is consumed.
22608 * Return NULL for out of memory, the variable otherwise.
22609 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022610 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022611alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612{
Bram Moolenaar33570922005-01-25 22:26:29 +000022613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022615 rettv = alloc_tv();
22616 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022618 rettv->v_type = VAR_STRING;
22619 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 }
22621 else
22622 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022623 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624}
22625
22626/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022627 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022630free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631{
22632 if (varp != NULL)
22633 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022634 switch (varp->v_type)
22635 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022636 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022637 func_unref(varp->vval.v_string);
22638 /*FALLTHROUGH*/
22639 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022640 vim_free(varp->vval.v_string);
22641 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022642 case VAR_PARTIAL:
22643 partial_unref(varp->vval.v_partial);
22644 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022645 case VAR_LIST:
22646 list_unref(varp->vval.v_list);
22647 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022648 case VAR_DICT:
22649 dict_unref(varp->vval.v_dict);
22650 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022651 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022652#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022653 job_unref(varp->vval.v_job);
22654 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022655#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022656 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022657#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022658 channel_unref(varp->vval.v_channel);
22659 break;
22660#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022661 case VAR_NUMBER:
22662 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022663 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022664 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022665 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 vim_free(varp);
22668 }
22669}
22670
22671/*
22672 * Free the memory for a variable value and set the value to NULL or 0.
22673 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022674 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022675clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676{
22677 if (varp != NULL)
22678 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022679 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022681 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022682 func_unref(varp->vval.v_string);
22683 /*FALLTHROUGH*/
22684 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022685 vim_free(varp->vval.v_string);
22686 varp->vval.v_string = NULL;
22687 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022688 case VAR_PARTIAL:
22689 partial_unref(varp->vval.v_partial);
22690 varp->vval.v_partial = NULL;
22691 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022692 case VAR_LIST:
22693 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022694 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022695 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022696 case VAR_DICT:
22697 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022698 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022699 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022700 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022701 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022702 varp->vval.v_number = 0;
22703 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022704 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022705#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022706 varp->vval.v_float = 0.0;
22707 break;
22708#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022709 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022710#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022711 job_unref(varp->vval.v_job);
22712 varp->vval.v_job = NULL;
22713#endif
22714 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022715 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022716#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022717 channel_unref(varp->vval.v_channel);
22718 varp->vval.v_channel = NULL;
22719#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022720 case VAR_UNKNOWN:
22721 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022723 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 }
22725}
22726
22727/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022728 * Set the value of a variable to NULL without freeing items.
22729 */
22730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022731init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022732{
22733 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022734 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022735}
22736
22737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 * Get the number value of a variable.
22739 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022740 * For incompatible types, return 0.
22741 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22742 * caller of incompatible types: it sets *denote to TRUE if "denote"
22743 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022745 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022746get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022748 int error = FALSE;
22749
22750 return get_tv_number_chk(varp, &error); /* return 0L on error */
22751}
22752
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022753 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022754get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022755{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022756 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022758 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022760 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022761 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022762 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022763#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022764 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022765 break;
22766#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022767 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022768 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022769 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022770 break;
22771 case VAR_STRING:
22772 if (varp->vval.v_string != NULL)
22773 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022774 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022775 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022776 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022777 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022778 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022779 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022780 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022781 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022782 case VAR_SPECIAL:
22783 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22784 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022785 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022786#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022787 EMSG(_("E910: Using a Job as a Number"));
22788 break;
22789#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022790 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022791#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022792 EMSG(_("E913: Using a Channel as a Number"));
22793 break;
22794#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022795 case VAR_UNKNOWN:
22796 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022797 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022799 if (denote == NULL) /* useful for values that must be unsigned */
22800 n = -1;
22801 else
22802 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022803 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804}
22805
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022806#ifdef FEAT_FLOAT
22807 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022808get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022809{
22810 switch (varp->v_type)
22811 {
22812 case VAR_NUMBER:
22813 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022814 case VAR_FLOAT:
22815 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022816 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022817 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022818 EMSG(_("E891: Using a Funcref as a Float"));
22819 break;
22820 case VAR_STRING:
22821 EMSG(_("E892: Using a String as a Float"));
22822 break;
22823 case VAR_LIST:
22824 EMSG(_("E893: Using a List as a Float"));
22825 break;
22826 case VAR_DICT:
22827 EMSG(_("E894: Using a Dictionary as a Float"));
22828 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022829 case VAR_SPECIAL:
22830 EMSG(_("E907: Using a special value as a Float"));
22831 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022832 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022833# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022834 EMSG(_("E911: Using a Job as a Float"));
22835 break;
22836# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022837 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022838# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022839 EMSG(_("E914: Using a Channel as a Float"));
22840 break;
22841# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022842 case VAR_UNKNOWN:
22843 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022844 break;
22845 }
22846 return 0;
22847}
22848#endif
22849
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022851 * Get the lnum from the first argument.
22852 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022853 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 */
22855 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022856get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857{
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 linenr_T lnum;
22860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022861 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 if (lnum == 0) /* no valid number, try using line() */
22863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022864 rettv.v_type = VAR_NUMBER;
22865 f_line(argvars, &rettv);
22866 lnum = rettv.vval.v_number;
22867 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 }
22869 return lnum;
22870}
22871
22872/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022873 * Get the lnum from the first argument.
22874 * Also accepts "$", then "buf" is used.
22875 * Returns 0 on error.
22876 */
22877 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022878get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022879{
22880 if (argvars[0].v_type == VAR_STRING
22881 && argvars[0].vval.v_string != NULL
22882 && argvars[0].vval.v_string[0] == '$'
22883 && buf != NULL)
22884 return buf->b_ml.ml_line_count;
22885 return get_tv_number_chk(&argvars[0], NULL);
22886}
22887
22888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 * Get the string value of a variable.
22890 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022891 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22892 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 * If the String variable has never been set, return an empty string.
22894 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022895 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22896 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022898 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022899get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900{
22901 static char_u mybuf[NUMBUFLEN];
22902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022903 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904}
22905
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022906 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022907get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022909 char_u *res = get_tv_string_buf_chk(varp, buf);
22910
22911 return res != NULL ? res : (char_u *)"";
22912}
22913
Bram Moolenaar7d647822014-04-05 21:28:56 +020022914/*
22915 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22916 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022917 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022918get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022919{
22920 static char_u mybuf[NUMBUFLEN];
22921
22922 return get_tv_string_buf_chk(varp, mybuf);
22923}
22924
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022925 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022926get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022927{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022928 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022930 case VAR_NUMBER:
22931 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22932 return buf;
22933 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022934 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022935 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022936 break;
22937 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022938 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022939 break;
22940 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022941 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022942 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022943 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022944#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022945 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022946 break;
22947#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022948 case VAR_STRING:
22949 if (varp->vval.v_string != NULL)
22950 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022951 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022952 case VAR_SPECIAL:
22953 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22954 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022955 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022956#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022957 {
22958 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022959 char *status;
22960
22961 if (job == NULL)
22962 return (char_u *)"no process";
22963 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022964 : job->jv_status == JOB_ENDED ? "dead"
22965 : "run";
22966# ifdef UNIX
22967 vim_snprintf((char *)buf, NUMBUFLEN,
22968 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022969# elif defined(WIN32)
22970 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022971 "process %ld %s",
22972 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022973 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022974# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022975 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022976 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22977# endif
22978 return buf;
22979 }
22980#endif
22981 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022982 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022983#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022984 {
22985 channel_T *channel = varp->vval.v_channel;
22986 char *status = channel_status(channel);
22987
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022988 if (channel == NULL)
22989 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22990 else
22991 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022992 "channel %d %s", channel->ch_id, status);
22993 return buf;
22994 }
22995#endif
22996 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022997 case VAR_UNKNOWN:
22998 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022999 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023001 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002}
23003
23004/*
23005 * Find variable "name" in the list of variables.
23006 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023007 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023008 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023009 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023012find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023015 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016
Bram Moolenaara7043832005-01-21 11:56:39 +000023017 ht = find_var_ht(name, &varname);
23018 if (htp != NULL)
23019 *htp = ht;
23020 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023022 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023}
23024
23025/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023026 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023027 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023029 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023030find_var_in_ht(
23031 hashtab_T *ht,
23032 int htname,
23033 char_u *varname,
23034 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023035{
Bram Moolenaar33570922005-01-25 22:26:29 +000023036 hashitem_T *hi;
23037
23038 if (*varname == NUL)
23039 {
23040 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023041 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023042 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023043 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023044 case 'g': return &globvars_var;
23045 case 'v': return &vimvars_var;
23046 case 'b': return &curbuf->b_bufvar;
23047 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023048#ifdef FEAT_WINDOWS
23049 case 't': return &curtab->tp_winvar;
23050#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023051 case 'l': return current_funccal == NULL
23052 ? NULL : &current_funccal->l_vars_var;
23053 case 'a': return current_funccal == NULL
23054 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023055 }
23056 return NULL;
23057 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023058
23059 hi = hash_find(ht, varname);
23060 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023061 {
23062 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023063 * worked find the variable again. Don't auto-load a script if it was
23064 * loaded already, otherwise it would be loaded every time when
23065 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023066 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023067 {
23068 /* Note: script_autoload() may make "hi" invalid. It must either
23069 * be obtained again or not used. */
23070 if (!script_autoload(varname, FALSE) || aborting())
23071 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023072 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023073 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023074 if (HASHITEM_EMPTY(hi))
23075 return NULL;
23076 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023077 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023078}
23079
23080/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023081 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023082 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023083 * Set "varname" to the start of name without ':'.
23084 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023085 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023086find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023088 hashitem_T *hi;
23089
Bram Moolenaar73627d02015-08-11 15:46:09 +020023090 if (name[0] == NUL)
23091 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 if (name[1] != ':')
23093 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023094 /* The name must not start with a colon or #. */
23095 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 return NULL;
23097 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023098
23099 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023100 hi = hash_find(&compat_hashtab, name);
23101 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023102 return &compat_hashtab;
23103
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023105 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023106 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023107 }
23108 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023109 if (*name == 'g') /* global variable */
23110 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023111 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23112 */
23113 if (vim_strchr(name + 2, ':') != NULL
23114 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023115 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023117 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023119 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023120#ifdef FEAT_WINDOWS
23121 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023122 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023123#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023124 if (*name == 'v') /* v: variable */
23125 return &vimvarht;
23126 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023127 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023128 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023129 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130 if (*name == 's' /* script variable */
23131 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23132 return &SCRIPT_VARS(current_SID);
23133 return NULL;
23134}
23135
23136/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023137 * Get function call environment based on bactrace debug level
23138 */
23139 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023140get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023141{
23142 int i;
23143 funccall_T *funccal;
23144 funccall_T *temp_funccal;
23145
23146 funccal = current_funccal;
23147 if (debug_backtrace_level > 0)
23148 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023149 for (i = 0; i < debug_backtrace_level; i++)
23150 {
23151 temp_funccal = funccal->caller;
23152 if (temp_funccal)
23153 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023154 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023155 /* backtrace level overflow. reset to max */
23156 debug_backtrace_level = i;
23157 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023158 }
23159 return funccal;
23160}
23161
23162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023164 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 * Returns NULL when it doesn't exist.
23166 */
23167 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023168get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169{
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023172 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 if (v == NULL)
23174 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023175 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176}
23177
23178/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023179 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 * sourcing this script and when executing functions defined in the script.
23181 */
23182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023183new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184{
Bram Moolenaara7043832005-01-21 11:56:39 +000023185 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023186 hashtab_T *ht;
23187 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023188
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23190 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023191 /* Re-allocating ga_data means that an ht_array pointing to
23192 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023193 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023194 for (i = 1; i <= ga_scripts.ga_len; ++i)
23195 {
23196 ht = &SCRIPT_VARS(i);
23197 if (ht->ht_mask == HT_INIT_SIZE - 1)
23198 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023199 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023200 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023201 }
23202
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203 while (ga_scripts.ga_len < id)
23204 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023205 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023206 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023207 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 }
23210 }
23211}
23212
23213/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023214 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23215 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 */
23217 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023218init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219{
Bram Moolenaar33570922005-01-25 22:26:29 +000023220 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023221 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023222 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023223 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023224 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023225 dict_var->di_tv.vval.v_dict = dict;
23226 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023227 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023228 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23229 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230}
23231
23232/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023233 * Unreference a dictionary initialized by init_var_dict().
23234 */
23235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023236unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023237{
23238 /* Now the dict needs to be freed if no one else is using it, go back to
23239 * normal reference counting. */
23240 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23241 dict_unref(dict);
23242}
23243
23244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023246 * Frees all allocated variables and the value they contain.
23247 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 */
23249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023250vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023251{
23252 vars_clear_ext(ht, TRUE);
23253}
23254
23255/*
23256 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23257 */
23258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023259vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260{
Bram Moolenaara7043832005-01-21 11:56:39 +000023261 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023262 hashitem_T *hi;
23263 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264
Bram Moolenaar33570922005-01-25 22:26:29 +000023265 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023266 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023267 for (hi = ht->ht_array; todo > 0; ++hi)
23268 {
23269 if (!HASHITEM_EMPTY(hi))
23270 {
23271 --todo;
23272
Bram Moolenaar33570922005-01-25 22:26:29 +000023273 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023274 * ht_array might change then. hash_clear() takes care of it
23275 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023276 v = HI2DI(hi);
23277 if (free_val)
23278 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023279 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023280 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023281 }
23282 }
23283 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023284 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285}
23286
Bram Moolenaara7043832005-01-21 11:56:39 +000023287/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023288 * Delete a variable from hashtab "ht" at item "hi".
23289 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023292delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293{
Bram Moolenaar33570922005-01-25 22:26:29 +000023294 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023295
23296 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023297 clear_tv(&di->di_tv);
23298 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299}
23300
23301/*
23302 * List the value of one internal variable.
23303 */
23304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023305list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023307 char_u *tofree;
23308 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023309 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023310
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023311 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023312 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023313 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023314 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315}
23316
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023318list_one_var_a(
23319 char_u *prefix,
23320 char_u *name,
23321 int type,
23322 char_u *string,
23323 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324{
Bram Moolenaar31859182007-08-14 20:41:13 +000023325 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23326 msg_start();
23327 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328 if (name != NULL) /* "a:" vars don't have a name stored */
23329 msg_puts(name);
23330 msg_putchar(' ');
23331 msg_advance(22);
23332 if (type == VAR_NUMBER)
23333 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023334 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023335 msg_putchar('*');
23336 else if (type == VAR_LIST)
23337 {
23338 msg_putchar('[');
23339 if (*string == '[')
23340 ++string;
23341 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023342 else if (type == VAR_DICT)
23343 {
23344 msg_putchar('{');
23345 if (*string == '{')
23346 ++string;
23347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 else
23349 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023350
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023352
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023353 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023354 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023355 if (*first)
23356 {
23357 msg_clr_eos();
23358 *first = FALSE;
23359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360}
23361
23362/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023363 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364 * If the variable already exists, the value is updated.
23365 * Otherwise the variable is created.
23366 */
23367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023368set_var(
23369 char_u *name,
23370 typval_T *tv,
23371 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372{
Bram Moolenaar33570922005-01-25 22:26:29 +000023373 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023375 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023377 ht = find_var_ht(name, &varname);
23378 if (ht == NULL || *varname == NUL)
23379 {
23380 EMSG2(_(e_illvar), name);
23381 return;
23382 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023383 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023384
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023385 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23386 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023387 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023388
Bram Moolenaar33570922005-01-25 22:26:29 +000023389 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023391 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023392 if (var_check_ro(v->di_flags, name, FALSE)
23393 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023394 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023395
23396 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023397 * Handle setting internal v: variables separately where needed to
23398 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023399 */
23400 if (ht == &vimvarht)
23401 {
23402 if (v->di_tv.v_type == VAR_STRING)
23403 {
23404 vim_free(v->di_tv.vval.v_string);
23405 if (copy || tv->v_type != VAR_STRING)
23406 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23407 else
23408 {
23409 /* Take over the string to avoid an extra alloc/free. */
23410 v->di_tv.vval.v_string = tv->vval.v_string;
23411 tv->vval.v_string = NULL;
23412 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023413 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023414 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023415 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023416 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023417 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023418 if (STRCMP(varname, "searchforward") == 0)
23419 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023420#ifdef FEAT_SEARCH_EXTRA
23421 else if (STRCMP(varname, "hlsearch") == 0)
23422 {
23423 no_hlsearch = !v->di_tv.vval.v_number;
23424 redraw_all_later(SOME_VALID);
23425 }
23426#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023427 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023428 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023429 else if (v->di_tv.v_type != tv->v_type)
23430 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023431 }
23432
23433 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 }
23435 else /* add a new variable */
23436 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023437 /* Can't add "v:" variable. */
23438 if (ht == &vimvarht)
23439 {
23440 EMSG2(_(e_illvar), name);
23441 return;
23442 }
23443
Bram Moolenaar92124a32005-06-17 22:03:40 +000023444 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023445 if (!valid_varname(varname))
23446 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023447
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023448 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23449 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023450 if (v == NULL)
23451 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023452 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023453 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023455 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 return;
23457 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023458 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023460
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023461 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023462 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023463 else
23464 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023465 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023466 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023467 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469}
23470
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023471/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023472 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023473 * Also give an error message.
23474 */
23475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023476var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023477{
23478 if (flags & DI_FLAGS_RO)
23479 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023480 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023481 return TRUE;
23482 }
23483 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23484 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023485 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023486 return TRUE;
23487 }
23488 return FALSE;
23489}
23490
23491/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023492 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23493 * Also give an error message.
23494 */
23495 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023496var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023497{
23498 if (flags & DI_FLAGS_FIX)
23499 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023500 EMSG2(_("E795: Cannot delete variable %s"),
23501 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023502 return TRUE;
23503 }
23504 return FALSE;
23505}
23506
23507/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023508 * Check if a funcref is assigned to a valid variable name.
23509 * Return TRUE and give an error if not.
23510 */
23511 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023512var_check_func_name(
23513 char_u *name, /* points to start of variable name */
23514 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023515{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023516 /* Allow for w: b: s: and t:. */
23517 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023518 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23519 ? name[2] : name[0]))
23520 {
23521 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23522 name);
23523 return TRUE;
23524 }
23525 /* Don't allow hiding a function. When "v" is not NULL we might be
23526 * assigning another function to the same var, the type is checked
23527 * below. */
23528 if (new_var && function_exists(name))
23529 {
23530 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23531 name);
23532 return TRUE;
23533 }
23534 return FALSE;
23535}
23536
23537/*
23538 * Check if a variable name is valid.
23539 * Return FALSE and give an error if not.
23540 */
23541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023542valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023543{
23544 char_u *p;
23545
23546 for (p = varname; *p != NUL; ++p)
23547 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23548 && *p != AUTOLOAD_CHAR)
23549 {
23550 EMSG2(_(e_illvar), varname);
23551 return FALSE;
23552 }
23553 return TRUE;
23554}
23555
23556/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023557 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023558 * Also give an error message, using "name" or _("name") when use_gettext is
23559 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023560 */
23561 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023562tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023563{
23564 if (lock & VAR_LOCKED)
23565 {
23566 EMSG2(_("E741: Value is locked: %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 if (lock & VAR_FIXED)
23573 {
23574 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023575 name == NULL ? (char_u *)_("Unknown")
23576 : use_gettext ? (char_u *)_(name)
23577 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023578 return TRUE;
23579 }
23580 return FALSE;
23581}
23582
23583/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023584 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023585 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023586 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023587 * It is OK for "from" and "to" to point to the same item. This is used to
23588 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023589 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023591copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023593 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023594 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023595 switch (from->v_type)
23596 {
23597 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023598 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023599 to->vval.v_number = from->vval.v_number;
23600 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023601 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023602#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023603 to->vval.v_float = from->vval.v_float;
23604 break;
23605#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023606 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023607#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023608 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023609 if (to->vval.v_job != NULL)
23610 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023611 break;
23612#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023613 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023614#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023615 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023616 if (to->vval.v_channel != NULL)
23617 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023618 break;
23619#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023620 case VAR_STRING:
23621 case VAR_FUNC:
23622 if (from->vval.v_string == NULL)
23623 to->vval.v_string = NULL;
23624 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023626 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023627 if (from->v_type == VAR_FUNC)
23628 func_ref(to->vval.v_string);
23629 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023630 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023631 case VAR_PARTIAL:
23632 if (from->vval.v_partial == NULL)
23633 to->vval.v_partial = NULL;
23634 else
23635 {
23636 to->vval.v_partial = from->vval.v_partial;
23637 ++to->vval.v_partial->pt_refcount;
23638 }
23639 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023640 case VAR_LIST:
23641 if (from->vval.v_list == NULL)
23642 to->vval.v_list = NULL;
23643 else
23644 {
23645 to->vval.v_list = from->vval.v_list;
23646 ++to->vval.v_list->lv_refcount;
23647 }
23648 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023649 case VAR_DICT:
23650 if (from->vval.v_dict == NULL)
23651 to->vval.v_dict = NULL;
23652 else
23653 {
23654 to->vval.v_dict = from->vval.v_dict;
23655 ++to->vval.v_dict->dv_refcount;
23656 }
23657 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023658 case VAR_UNKNOWN:
23659 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023660 break;
23661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662}
23663
23664/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023665 * Make a copy of an item.
23666 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023667 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23668 * reference to an already copied list/dict can be used.
23669 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023670 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023671 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023672item_copy(
23673 typval_T *from,
23674 typval_T *to,
23675 int deep,
23676 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023677{
23678 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023679 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023680
Bram Moolenaar33570922005-01-25 22:26:29 +000023681 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023682 {
23683 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023684 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023685 }
23686 ++recurse;
23687
23688 switch (from->v_type)
23689 {
23690 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023691 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023692 case VAR_STRING:
23693 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023694 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023695 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023696 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023697 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023698 copy_tv(from, to);
23699 break;
23700 case VAR_LIST:
23701 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023702 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023703 if (from->vval.v_list == NULL)
23704 to->vval.v_list = NULL;
23705 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23706 {
23707 /* use the copy made earlier */
23708 to->vval.v_list = from->vval.v_list->lv_copylist;
23709 ++to->vval.v_list->lv_refcount;
23710 }
23711 else
23712 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23713 if (to->vval.v_list == NULL)
23714 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023715 break;
23716 case VAR_DICT:
23717 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023718 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023719 if (from->vval.v_dict == NULL)
23720 to->vval.v_dict = NULL;
23721 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23722 {
23723 /* use the copy made earlier */
23724 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23725 ++to->vval.v_dict->dv_refcount;
23726 }
23727 else
23728 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23729 if (to->vval.v_dict == NULL)
23730 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023731 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023732 case VAR_UNKNOWN:
23733 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023734 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023735 }
23736 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023737 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023738}
23739
23740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741 * ":echo expr1 ..." print each argument separated with a space, add a
23742 * newline at the end.
23743 * ":echon expr1 ..." print each argument plain.
23744 */
23745 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023746ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747{
23748 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023749 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023750 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 char_u *p;
23752 int needclr = TRUE;
23753 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023754 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755
23756 if (eap->skip)
23757 ++emsg_skip;
23758 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23759 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023760 /* If eval1() causes an error message the text from the command may
23761 * still need to be cleared. E.g., "echo 22,44". */
23762 need_clr_eos = needclr;
23763
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023765 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 {
23767 /*
23768 * Report the invalid expression unless the expression evaluation
23769 * has been cancelled due to an aborting error, an interrupt, or an
23770 * exception.
23771 */
23772 if (!aborting())
23773 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023774 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023775 break;
23776 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023777 need_clr_eos = FALSE;
23778
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 if (!eap->skip)
23780 {
23781 if (atstart)
23782 {
23783 atstart = FALSE;
23784 /* Call msg_start() after eval1(), evaluating the expression
23785 * may cause a message to appear. */
23786 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023787 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023788 /* Mark the saved text as finishing the line, so that what
23789 * follows is displayed on a new line when scrolling back
23790 * at the more prompt. */
23791 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 }
23795 else if (eap->cmdidx == CMD_echo)
23796 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023797 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023798 if (p != NULL)
23799 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023801 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023803 if (*p != TAB && needclr)
23804 {
23805 /* remove any text still there from the command */
23806 msg_clr_eos();
23807 needclr = FALSE;
23808 }
23809 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810 }
23811 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023812 {
23813#ifdef FEAT_MBYTE
23814 if (has_mbyte)
23815 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023816 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023817
23818 (void)msg_outtrans_len_attr(p, i, echo_attr);
23819 p += i - 1;
23820 }
23821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023823 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023826 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023828 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 arg = skipwhite(arg);
23830 }
23831 eap->nextcmd = check_nextcmd(arg);
23832
23833 if (eap->skip)
23834 --emsg_skip;
23835 else
23836 {
23837 /* remove text that may still be there from the command */
23838 if (needclr)
23839 msg_clr_eos();
23840 if (eap->cmdidx == CMD_echo)
23841 msg_end();
23842 }
23843}
23844
23845/*
23846 * ":echohl {name}".
23847 */
23848 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023849ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850{
23851 int id;
23852
23853 id = syn_name2id(eap->arg);
23854 if (id == 0)
23855 echo_attr = 0;
23856 else
23857 echo_attr = syn_id2attr(id);
23858}
23859
23860/*
23861 * ":execute expr1 ..." execute the result of an expression.
23862 * ":echomsg expr1 ..." Print a message
23863 * ":echoerr expr1 ..." Print an error
23864 * Each gets spaces around each argument and a newline at the end for
23865 * echo commands
23866 */
23867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023868ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869{
23870 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023871 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 int ret = OK;
23873 char_u *p;
23874 garray_T ga;
23875 int len;
23876 int save_did_emsg;
23877
23878 ga_init2(&ga, 1, 80);
23879
23880 if (eap->skip)
23881 ++emsg_skip;
23882 while (*arg != NUL && *arg != '|' && *arg != '\n')
23883 {
23884 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023885 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 {
23887 /*
23888 * Report the invalid expression unless the expression evaluation
23889 * has been cancelled due to an aborting error, an interrupt, or an
23890 * exception.
23891 */
23892 if (!aborting())
23893 EMSG2(_(e_invexpr2), p);
23894 ret = FAIL;
23895 break;
23896 }
23897
23898 if (!eap->skip)
23899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023900 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 len = (int)STRLEN(p);
23902 if (ga_grow(&ga, len + 2) == FAIL)
23903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023904 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 ret = FAIL;
23906 break;
23907 }
23908 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023909 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023911 ga.ga_len += len;
23912 }
23913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023914 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915 arg = skipwhite(arg);
23916 }
23917
23918 if (ret != FAIL && ga.ga_data != NULL)
23919 {
23920 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023923 out_flush();
23924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 else if (eap->cmdidx == CMD_echoerr)
23926 {
23927 /* We don't want to abort following commands, restore did_emsg. */
23928 save_did_emsg = did_emsg;
23929 EMSG((char_u *)ga.ga_data);
23930 if (!force_abort)
23931 did_emsg = save_did_emsg;
23932 }
23933 else if (eap->cmdidx == CMD_execute)
23934 do_cmdline((char_u *)ga.ga_data,
23935 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23936 }
23937
23938 ga_clear(&ga);
23939
23940 if (eap->skip)
23941 --emsg_skip;
23942
23943 eap->nextcmd = check_nextcmd(arg);
23944}
23945
23946/*
23947 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23948 * "arg" points to the "&" or '+' when called, to "option" when returning.
23949 * Returns NULL when no option name found. Otherwise pointer to the char
23950 * after the option name.
23951 */
23952 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023953find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954{
23955 char_u *p = *arg;
23956
23957 ++p;
23958 if (*p == 'g' && p[1] == ':')
23959 {
23960 *opt_flags = OPT_GLOBAL;
23961 p += 2;
23962 }
23963 else if (*p == 'l' && p[1] == ':')
23964 {
23965 *opt_flags = OPT_LOCAL;
23966 p += 2;
23967 }
23968 else
23969 *opt_flags = 0;
23970
23971 if (!ASCII_ISALPHA(*p))
23972 return NULL;
23973 *arg = p;
23974
23975 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23976 p += 4; /* termcap option */
23977 else
23978 while (ASCII_ISALPHA(*p))
23979 ++p;
23980 return p;
23981}
23982
23983/*
23984 * ":function"
23985 */
23986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023987ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988{
23989 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023990 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 int j;
23992 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023994 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 char_u *name = NULL;
23996 char_u *p;
23997 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023998 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 garray_T newargs;
24000 garray_T newlines;
24001 int varargs = FALSE;
24002 int mustend = FALSE;
24003 int flags = 0;
24004 ufunc_T *fp;
24005 int indent;
24006 int nesting;
24007 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024008 dictitem_T *v;
24009 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024010 static int func_nr = 0; /* number for nameless function */
24011 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024012 hashtab_T *ht;
24013 int todo;
24014 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024015 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016
24017 /*
24018 * ":function" without argument: list functions.
24019 */
24020 if (ends_excmd(*eap->arg))
24021 {
24022 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024023 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024024 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024025 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024026 {
24027 if (!HASHITEM_EMPTY(hi))
24028 {
24029 --todo;
24030 fp = HI2UF(hi);
24031 if (!isdigit(*fp->uf_name))
24032 list_func_head(fp, FALSE);
24033 }
24034 }
24035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036 eap->nextcmd = check_nextcmd(eap->arg);
24037 return;
24038 }
24039
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024040 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024041 * ":function /pat": list functions matching pattern.
24042 */
24043 if (*eap->arg == '/')
24044 {
24045 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24046 if (!eap->skip)
24047 {
24048 regmatch_T regmatch;
24049
24050 c = *p;
24051 *p = NUL;
24052 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24053 *p = c;
24054 if (regmatch.regprog != NULL)
24055 {
24056 regmatch.rm_ic = p_ic;
24057
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024058 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024059 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24060 {
24061 if (!HASHITEM_EMPTY(hi))
24062 {
24063 --todo;
24064 fp = HI2UF(hi);
24065 if (!isdigit(*fp->uf_name)
24066 && vim_regexec(&regmatch, fp->uf_name, 0))
24067 list_func_head(fp, FALSE);
24068 }
24069 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024070 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024071 }
24072 }
24073 if (*p == '/')
24074 ++p;
24075 eap->nextcmd = check_nextcmd(p);
24076 return;
24077 }
24078
24079 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024080 * Get the function name. There are these situations:
24081 * func normal function name
24082 * "name" == func, "fudi.fd_dict" == NULL
24083 * dict.func new dictionary entry
24084 * "name" == NULL, "fudi.fd_dict" set,
24085 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24086 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024087 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024088 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24089 * dict.func existing dict entry that's not a Funcref
24090 * "name" == NULL, "fudi.fd_dict" set,
24091 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024092 * s:func script-local function name
24093 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024096 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024097 paren = (vim_strchr(p, '(') != NULL);
24098 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024099 {
24100 /*
24101 * Return on an invalid expression in braces, unless the expression
24102 * evaluation has been cancelled due to an aborting error, an
24103 * interrupt, or an exception.
24104 */
24105 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024106 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024107 if (!eap->skip && fudi.fd_newkey != NULL)
24108 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024109 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 else
24113 eap->skip = TRUE;
24114 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024115
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 /* An error in a function call during evaluation of an expression in magic
24117 * braces should not cause the function not to be defined. */
24118 saved_did_emsg = did_emsg;
24119 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120
24121 /*
24122 * ":function func" with only function name: list function.
24123 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024124 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 {
24126 if (!ends_excmd(*skipwhite(p)))
24127 {
24128 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024129 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 }
24131 eap->nextcmd = check_nextcmd(p);
24132 if (eap->nextcmd != NULL)
24133 *p = NUL;
24134 if (!eap->skip && !got_int)
24135 {
24136 fp = find_func(name);
24137 if (fp != NULL)
24138 {
24139 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024140 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024142 if (FUNCLINE(fp, j) == NULL)
24143 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 msg_putchar('\n');
24145 msg_outnum((long)(j + 1));
24146 if (j < 9)
24147 msg_putchar(' ');
24148 if (j < 99)
24149 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024150 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151 out_flush(); /* show a line at a time */
24152 ui_breakcheck();
24153 }
24154 if (!got_int)
24155 {
24156 msg_putchar('\n');
24157 msg_puts((char_u *)" endfunction");
24158 }
24159 }
24160 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024161 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024163 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164 }
24165
24166 /*
24167 * ":function name(arg1, arg2)" Define function.
24168 */
24169 p = skipwhite(p);
24170 if (*p != '(')
24171 {
24172 if (!eap->skip)
24173 {
24174 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024175 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 }
24177 /* attempt to continue by skipping some text */
24178 if (vim_strchr(p, '(') != NULL)
24179 p = vim_strchr(p, '(');
24180 }
24181 p = skipwhite(p + 1);
24182
24183 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24184 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24185
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024186 if (!eap->skip)
24187 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024188 /* Check the name of the function. Unless it's a dictionary function
24189 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024190 if (name != NULL)
24191 arg = name;
24192 else
24193 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024194 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024195 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24196 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024197 {
24198 if (*arg == K_SPECIAL)
24199 j = 3;
24200 else
24201 j = 0;
24202 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24203 : eval_isnamec(arg[j])))
24204 ++j;
24205 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024206 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024207 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024208 /* Disallow using the g: dict. */
24209 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24210 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024211 }
24212
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213 /*
24214 * Isolate the arguments: "arg1, arg2, ...)"
24215 */
24216 while (*p != ')')
24217 {
24218 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24219 {
24220 varargs = TRUE;
24221 p += 3;
24222 mustend = TRUE;
24223 }
24224 else
24225 {
24226 arg = p;
24227 while (ASCII_ISALNUM(*p) || *p == '_')
24228 ++p;
24229 if (arg == p || isdigit(*arg)
24230 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24231 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24232 {
24233 if (!eap->skip)
24234 EMSG2(_("E125: Illegal argument: %s"), arg);
24235 break;
24236 }
24237 if (ga_grow(&newargs, 1) == FAIL)
24238 goto erret;
24239 c = *p;
24240 *p = NUL;
24241 arg = vim_strsave(arg);
24242 if (arg == NULL)
24243 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024244
24245 /* Check for duplicate argument name. */
24246 for (i = 0; i < newargs.ga_len; ++i)
24247 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24248 {
24249 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024250 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024251 goto erret;
24252 }
24253
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24255 *p = c;
24256 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257 if (*p == ',')
24258 ++p;
24259 else
24260 mustend = TRUE;
24261 }
24262 p = skipwhite(p);
24263 if (mustend && *p != ')')
24264 {
24265 if (!eap->skip)
24266 EMSG2(_(e_invarg2), eap->arg);
24267 break;
24268 }
24269 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024270 if (*p != ')')
24271 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272 ++p; /* skip the ')' */
24273
Bram Moolenaare9a41262005-01-15 22:18:47 +000024274 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275 for (;;)
24276 {
24277 p = skipwhite(p);
24278 if (STRNCMP(p, "range", 5) == 0)
24279 {
24280 flags |= FC_RANGE;
24281 p += 5;
24282 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024283 else if (STRNCMP(p, "dict", 4) == 0)
24284 {
24285 flags |= FC_DICT;
24286 p += 4;
24287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288 else if (STRNCMP(p, "abort", 5) == 0)
24289 {
24290 flags |= FC_ABORT;
24291 p += 5;
24292 }
24293 else
24294 break;
24295 }
24296
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024297 /* When there is a line break use what follows for the function body.
24298 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24299 if (*p == '\n')
24300 line_arg = p + 1;
24301 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 EMSG(_(e_trailing));
24303
24304 /*
24305 * Read the body of the function, until ":endfunction" is found.
24306 */
24307 if (KeyTyped)
24308 {
24309 /* Check if the function already exists, don't let the user type the
24310 * whole function before telling him it doesn't work! For a script we
24311 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024312 if (!eap->skip && !eap->forceit)
24313 {
24314 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24315 EMSG(_(e_funcdict));
24316 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024317 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024320 if (!eap->skip && did_emsg)
24321 goto erret;
24322
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323 msg_putchar('\n'); /* don't overwrite the function name */
24324 cmdline_row = msg_row;
24325 }
24326
24327 indent = 2;
24328 nesting = 0;
24329 for (;;)
24330 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024331 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024332 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024333 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024334 saved_wait_return = FALSE;
24335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024337 sourcing_lnum_off = sourcing_lnum;
24338
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024339 if (line_arg != NULL)
24340 {
24341 /* Use eap->arg, split up in parts by line breaks. */
24342 theline = line_arg;
24343 p = vim_strchr(theline, '\n');
24344 if (p == NULL)
24345 line_arg += STRLEN(line_arg);
24346 else
24347 {
24348 *p = NUL;
24349 line_arg = p + 1;
24350 }
24351 }
24352 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024353 theline = getcmdline(':', 0L, indent);
24354 else
24355 theline = eap->getline(':', eap->cookie, indent);
24356 if (KeyTyped)
24357 lines_left = Rows - 1;
24358 if (theline == NULL)
24359 {
24360 EMSG(_("E126: Missing :endfunction"));
24361 goto erret;
24362 }
24363
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024364 /* Detect line continuation: sourcing_lnum increased more than one. */
24365 if (sourcing_lnum > sourcing_lnum_off + 1)
24366 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24367 else
24368 sourcing_lnum_off = 0;
24369
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 if (skip_until != NULL)
24371 {
24372 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24373 * don't check for ":endfunc". */
24374 if (STRCMP(theline, skip_until) == 0)
24375 {
24376 vim_free(skip_until);
24377 skip_until = NULL;
24378 }
24379 }
24380 else
24381 {
24382 /* skip ':' and blanks*/
24383 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24384 ;
24385
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024386 /* Check for "endfunction". */
24387 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024389 if (line_arg == NULL)
24390 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391 break;
24392 }
24393
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024394 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 * at "end". */
24396 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24397 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024398 else if (STRNCMP(p, "if", 2) == 0
24399 || STRNCMP(p, "wh", 2) == 0
24400 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 || STRNCMP(p, "try", 3) == 0)
24402 indent += 2;
24403
24404 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024405 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024407 if (*p == '!')
24408 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024410 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024411 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024413 ++nesting;
24414 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024415 }
24416 }
24417
24418 /* Check for ":append" or ":insert". */
24419 p = skip_range(p, NULL);
24420 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24421 || (p[0] == 'i'
24422 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24423 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24424 skip_until = vim_strsave((char_u *)".");
24425
24426 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24427 arg = skipwhite(skiptowhite(p));
24428 if (arg[0] == '<' && arg[1] =='<'
24429 && ((p[0] == 'p' && p[1] == 'y'
24430 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24431 || (p[0] == 'p' && p[1] == 'e'
24432 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24433 || (p[0] == 't' && p[1] == 'c'
24434 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024435 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24436 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024437 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24438 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024439 || (p[0] == 'm' && p[1] == 'z'
24440 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024441 ))
24442 {
24443 /* ":python <<" continues until a dot, like ":append" */
24444 p = skipwhite(arg + 2);
24445 if (*p == NUL)
24446 skip_until = vim_strsave((char_u *)".");
24447 else
24448 skip_until = vim_strsave(p);
24449 }
24450 }
24451
24452 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024453 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024454 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024455 if (line_arg == NULL)
24456 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024458 }
24459
24460 /* Copy the line to newly allocated memory. get_one_sourceline()
24461 * allocates 250 bytes per line, this saves 80% on average. The cost
24462 * is an extra alloc/free. */
24463 p = vim_strsave(theline);
24464 if (p != NULL)
24465 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024466 if (line_arg == NULL)
24467 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024468 theline = p;
24469 }
24470
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024471 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24472
24473 /* Add NULL lines for continuation lines, so that the line count is
24474 * equal to the index in the growarray. */
24475 while (sourcing_lnum_off-- > 0)
24476 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024477
24478 /* Check for end of eap->arg. */
24479 if (line_arg != NULL && *line_arg == NUL)
24480 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 }
24482
24483 /* Don't define the function when skipping commands or when an error was
24484 * detected. */
24485 if (eap->skip || did_emsg)
24486 goto erret;
24487
24488 /*
24489 * If there are no errors, add the function
24490 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024491 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024492 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024493 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024494 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024495 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024496 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024497 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024498 goto erret;
24499 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024500
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024501 fp = find_func(name);
24502 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024504 if (!eap->forceit)
24505 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024506 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024507 goto erret;
24508 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024509 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024510 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024511 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024512 name);
24513 goto erret;
24514 }
24515 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024516 ga_clear_strings(&(fp->uf_args));
24517 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024518 vim_free(name);
24519 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521 }
24522 else
24523 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024524 char numbuf[20];
24525
24526 fp = NULL;
24527 if (fudi.fd_newkey == NULL && !eap->forceit)
24528 {
24529 EMSG(_(e_funcdict));
24530 goto erret;
24531 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024532 if (fudi.fd_di == NULL)
24533 {
24534 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024535 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024536 goto erret;
24537 }
24538 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024539 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024540 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024541
24542 /* Give the function a sequential number. Can only be used with a
24543 * Funcref! */
24544 vim_free(name);
24545 sprintf(numbuf, "%d", ++func_nr);
24546 name = vim_strsave((char_u *)numbuf);
24547 if (name == NULL)
24548 goto erret;
24549 }
24550
24551 if (fp == NULL)
24552 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024553 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024554 {
24555 int slen, plen;
24556 char_u *scriptname;
24557
24558 /* Check that the autoload name matches the script name. */
24559 j = FAIL;
24560 if (sourcing_name != NULL)
24561 {
24562 scriptname = autoload_name(name);
24563 if (scriptname != NULL)
24564 {
24565 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024566 plen = (int)STRLEN(p);
24567 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024568 if (slen > plen && fnamecmp(p,
24569 sourcing_name + slen - plen) == 0)
24570 j = OK;
24571 vim_free(scriptname);
24572 }
24573 }
24574 if (j == FAIL)
24575 {
24576 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24577 goto erret;
24578 }
24579 }
24580
24581 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024582 if (fp == NULL)
24583 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024584
24585 if (fudi.fd_dict != NULL)
24586 {
24587 if (fudi.fd_di == NULL)
24588 {
24589 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024590 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024591 if (fudi.fd_di == NULL)
24592 {
24593 vim_free(fp);
24594 goto erret;
24595 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024596 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24597 {
24598 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024599 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024600 goto erret;
24601 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024602 }
24603 else
24604 /* overwrite existing dict entry */
24605 clear_tv(&fudi.fd_di->di_tv);
24606 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024607 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024608 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024609 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024610
24611 /* behave like "dict" was used */
24612 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024613 }
24614
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024616 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024617 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24618 {
24619 vim_free(fp);
24620 goto erret;
24621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024623 fp->uf_args = newargs;
24624 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024625#ifdef FEAT_PROFILE
24626 fp->uf_tml_count = NULL;
24627 fp->uf_tml_total = NULL;
24628 fp->uf_tml_self = NULL;
24629 fp->uf_profiling = FALSE;
24630 if (prof_def_func())
24631 func_do_profile(fp);
24632#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024633 fp->uf_varargs = varargs;
24634 fp->uf_flags = flags;
24635 fp->uf_calls = 0;
24636 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024637 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638
24639erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024640 ga_clear_strings(&newargs);
24641 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024642ret_free:
24643 vim_free(skip_until);
24644 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024647 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648}
24649
24650/*
24651 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024652 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024654 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024655 * TFN_INT: internal function name OK
24656 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024657 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 * Advances "pp" to just after the function name (if no error).
24659 */
24660 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024661trans_function_name(
24662 char_u **pp,
24663 int skip, /* only find the end, don't evaluate */
24664 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024665 funcdict_T *fdp, /* return: info about dictionary used */
24666 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024668 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669 char_u *start;
24670 char_u *end;
24671 int lead;
24672 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024674 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024675
24676 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024677 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024679
24680 /* Check for hard coded <SNR>: already translated function ID (from a user
24681 * command). */
24682 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24683 && (*pp)[2] == (int)KE_SNR)
24684 {
24685 *pp += 3;
24686 len = get_id_len(pp) + 3;
24687 return vim_strnsave(start, len);
24688 }
24689
24690 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24691 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024693 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024695
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024696 /* Note that TFN_ flags use the same values as GLV_ flags. */
24697 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024698 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024699 if (end == start)
24700 {
24701 if (!skip)
24702 EMSG(_("E129: Function name required"));
24703 goto theend;
24704 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024705 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024706 {
24707 /*
24708 * Report an invalid expression in braces, unless the expression
24709 * evaluation has been cancelled due to an aborting error, an
24710 * interrupt, or an exception.
24711 */
24712 if (!aborting())
24713 {
24714 if (end != NULL)
24715 EMSG2(_(e_invarg2), start);
24716 }
24717 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024718 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024719 goto theend;
24720 }
24721
24722 if (lv.ll_tv != NULL)
24723 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024724 if (fdp != NULL)
24725 {
24726 fdp->fd_dict = lv.ll_dict;
24727 fdp->fd_newkey = lv.ll_newkey;
24728 lv.ll_newkey = NULL;
24729 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024730 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024731 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24732 {
24733 name = vim_strsave(lv.ll_tv->vval.v_string);
24734 *pp = end;
24735 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024736 else if (lv.ll_tv->v_type == VAR_PARTIAL
24737 && lv.ll_tv->vval.v_partial != NULL)
24738 {
24739 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24740 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024741 if (partial != NULL)
24742 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024743 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024744 else
24745 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024746 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24747 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024748 EMSG(_(e_funcref));
24749 else
24750 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024751 name = NULL;
24752 }
24753 goto theend;
24754 }
24755
24756 if (lv.ll_name == NULL)
24757 {
24758 /* Error found, but continue after the function name. */
24759 *pp = end;
24760 goto theend;
24761 }
24762
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024763 /* Check if the name is a Funcref. If so, use the value. */
24764 if (lv.ll_exp_name != NULL)
24765 {
24766 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024767 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024768 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024769 if (name == lv.ll_exp_name)
24770 name = NULL;
24771 }
24772 else
24773 {
24774 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024775 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024776 if (name == *pp)
24777 name = NULL;
24778 }
24779 if (name != NULL)
24780 {
24781 name = vim_strsave(name);
24782 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024783 if (STRNCMP(name, "<SNR>", 5) == 0)
24784 {
24785 /* Change "<SNR>" to the byte sequence. */
24786 name[0] = K_SPECIAL;
24787 name[1] = KS_EXTRA;
24788 name[2] = (int)KE_SNR;
24789 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24790 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024791 goto theend;
24792 }
24793
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024794 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024795 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024796 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024797 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24798 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24799 {
24800 /* When there was "s:" already or the name expanded to get a
24801 * leading "s:" then remove it. */
24802 lv.ll_name += 2;
24803 len -= 2;
24804 lead = 2;
24805 }
24806 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024807 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024808 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024809 /* skip over "s:" and "g:" */
24810 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024811 lv.ll_name += 2;
24812 len = (int)(end - lv.ll_name);
24813 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024814
24815 /*
24816 * Copy the function name to allocated memory.
24817 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24818 * Accept <SNR>123_name() outside a script.
24819 */
24820 if (skip)
24821 lead = 0; /* do nothing */
24822 else if (lead > 0)
24823 {
24824 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024825 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24826 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024827 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024828 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024829 if (current_SID <= 0)
24830 {
24831 EMSG(_(e_usingsid));
24832 goto theend;
24833 }
24834 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24835 lead += (int)STRLEN(sid_buf);
24836 }
24837 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024838 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024839 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024840 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024841 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024842 goto theend;
24843 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024844 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024845 {
24846 char_u *cp = vim_strchr(lv.ll_name, ':');
24847
24848 if (cp != NULL && cp < end)
24849 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024850 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024851 goto theend;
24852 }
24853 }
24854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024855 name = alloc((unsigned)(len + lead + 1));
24856 if (name != NULL)
24857 {
24858 if (lead > 0)
24859 {
24860 name[0] = K_SPECIAL;
24861 name[1] = KS_EXTRA;
24862 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024863 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024864 STRCPY(name + 3, sid_buf);
24865 }
24866 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024867 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024868 }
24869 *pp = end;
24870
24871theend:
24872 clear_lval(&lv);
24873 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024874}
24875
24876/*
24877 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24878 * Return 2 if "p" starts with "s:".
24879 * Return 0 otherwise.
24880 */
24881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024882eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024883{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024884 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24885 * the standard library function. */
24886 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24887 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888 return 5;
24889 if (p[0] == 's' && p[1] == ':')
24890 return 2;
24891 return 0;
24892}
24893
24894/*
24895 * Return TRUE if "p" starts with "<SID>" or "s:".
24896 * Only works if eval_fname_script() returned non-zero for "p"!
24897 */
24898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024899eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900{
24901 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24902}
24903
24904/*
24905 * List the head of the function: "name(arg1, arg2)".
24906 */
24907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024908list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909{
24910 int j;
24911
24912 msg_start();
24913 if (indent)
24914 MSG_PUTS(" ");
24915 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024916 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917 {
24918 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024919 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 }
24921 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024922 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024924 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 {
24926 if (j)
24927 MSG_PUTS(", ");
24928 msg_puts(FUNCARG(fp, j));
24929 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024930 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 {
24932 if (j)
24933 MSG_PUTS(", ");
24934 MSG_PUTS("...");
24935 }
24936 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024937 if (fp->uf_flags & FC_ABORT)
24938 MSG_PUTS(" abort");
24939 if (fp->uf_flags & FC_RANGE)
24940 MSG_PUTS(" range");
24941 if (fp->uf_flags & FC_DICT)
24942 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024943 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024944 if (p_verbose > 0)
24945 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946}
24947
24948/*
24949 * Find a function by name, return pointer to it in ufuncs.
24950 * Return NULL for unknown function.
24951 */
24952 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024953find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024955 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024956
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024957 hi = hash_find(&func_hashtab, name);
24958 if (!HASHITEM_EMPTY(hi))
24959 return HI2UF(hi);
24960 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024961}
24962
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024963#if defined(EXITFREE) || defined(PROTO)
24964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024965free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024966{
24967 hashitem_T *hi;
24968
24969 /* Need to start all over every time, because func_free() may change the
24970 * hash table. */
24971 while (func_hashtab.ht_used > 0)
24972 for (hi = func_hashtab.ht_array; ; ++hi)
24973 if (!HASHITEM_EMPTY(hi))
24974 {
24975 func_free(HI2UF(hi));
24976 break;
24977 }
24978}
24979#endif
24980
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024982translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024983{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024984 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024985 return find_internal_func(name) >= 0;
24986 return find_func(name) != NULL;
24987}
24988
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024989/*
24990 * Return TRUE if a function "name" exists.
24991 */
24992 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024993function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024994{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024995 char_u *nm = name;
24996 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024997 int n = FALSE;
24998
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024999 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025000 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025001 nm = skipwhite(nm);
25002
25003 /* Only accept "funcname", "funcname ", "funcname (..." and
25004 * "funcname(...", not "funcname!...". */
25005 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025006 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025007 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025008 return n;
25009}
25010
Bram Moolenaara1544c02013-05-30 12:35:52 +020025011 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025012get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025013{
25014 char_u *nm = name;
25015 char_u *p;
25016
Bram Moolenaar65639032016-03-16 21:40:30 +010025017 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025018
25019 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025020 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025021 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025022
Bram Moolenaara1544c02013-05-30 12:35:52 +020025023 vim_free(p);
25024 return NULL;
25025}
25026
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025027/*
25028 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025029 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25030 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025031 */
25032 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025033builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025034{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025035 char_u *p;
25036
25037 if (!ASCII_ISLOWER(name[0]))
25038 return FALSE;
25039 p = vim_strchr(name, AUTOLOAD_CHAR);
25040 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025041}
25042
Bram Moolenaar05159a02005-02-26 23:04:13 +000025043#if defined(FEAT_PROFILE) || defined(PROTO)
25044/*
25045 * Start profiling function "fp".
25046 */
25047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025048func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025049{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025050 int len = fp->uf_lines.ga_len;
25051
25052 if (len == 0)
25053 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025054 fp->uf_tm_count = 0;
25055 profile_zero(&fp->uf_tm_self);
25056 profile_zero(&fp->uf_tm_total);
25057 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025058 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025059 if (fp->uf_tml_total == NULL)
25060 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025061 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025062 if (fp->uf_tml_self == NULL)
25063 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025064 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025065 fp->uf_tml_idx = -1;
25066 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25067 || fp->uf_tml_self == NULL)
25068 return; /* out of memory */
25069
25070 fp->uf_profiling = TRUE;
25071}
25072
25073/*
25074 * Dump the profiling results for all functions in file "fd".
25075 */
25076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025077func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025078{
25079 hashitem_T *hi;
25080 int todo;
25081 ufunc_T *fp;
25082 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025083 ufunc_T **sorttab;
25084 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025085
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025086 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025087 if (todo == 0)
25088 return; /* nothing to dump */
25089
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025090 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025091
Bram Moolenaar05159a02005-02-26 23:04:13 +000025092 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25093 {
25094 if (!HASHITEM_EMPTY(hi))
25095 {
25096 --todo;
25097 fp = HI2UF(hi);
25098 if (fp->uf_profiling)
25099 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025100 if (sorttab != NULL)
25101 sorttab[st_len++] = fp;
25102
Bram Moolenaar05159a02005-02-26 23:04:13 +000025103 if (fp->uf_name[0] == K_SPECIAL)
25104 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25105 else
25106 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25107 if (fp->uf_tm_count == 1)
25108 fprintf(fd, "Called 1 time\n");
25109 else
25110 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25111 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25112 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25113 fprintf(fd, "\n");
25114 fprintf(fd, "count total (s) self (s)\n");
25115
25116 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25117 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025118 if (FUNCLINE(fp, i) == NULL)
25119 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025120 prof_func_line(fd, fp->uf_tml_count[i],
25121 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025122 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25123 }
25124 fprintf(fd, "\n");
25125 }
25126 }
25127 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025128
25129 if (sorttab != NULL && st_len > 0)
25130 {
25131 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25132 prof_total_cmp);
25133 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25134 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25135 prof_self_cmp);
25136 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25137 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025138
25139 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025140}
Bram Moolenaar73830342005-02-28 22:48:19 +000025141
25142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025143prof_sort_list(
25144 FILE *fd,
25145 ufunc_T **sorttab,
25146 int st_len,
25147 char *title,
25148 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025149{
25150 int i;
25151 ufunc_T *fp;
25152
25153 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25154 fprintf(fd, "count total (s) self (s) function\n");
25155 for (i = 0; i < 20 && i < st_len; ++i)
25156 {
25157 fp = sorttab[i];
25158 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25159 prefer_self);
25160 if (fp->uf_name[0] == K_SPECIAL)
25161 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25162 else
25163 fprintf(fd, " %s()\n", fp->uf_name);
25164 }
25165 fprintf(fd, "\n");
25166}
25167
25168/*
25169 * Print the count and times for one function or function line.
25170 */
25171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025172prof_func_line(
25173 FILE *fd,
25174 int count,
25175 proftime_T *total,
25176 proftime_T *self,
25177 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025178{
25179 if (count > 0)
25180 {
25181 fprintf(fd, "%5d ", count);
25182 if (prefer_self && profile_equal(total, self))
25183 fprintf(fd, " ");
25184 else
25185 fprintf(fd, "%s ", profile_msg(total));
25186 if (!prefer_self && profile_equal(total, self))
25187 fprintf(fd, " ");
25188 else
25189 fprintf(fd, "%s ", profile_msg(self));
25190 }
25191 else
25192 fprintf(fd, " ");
25193}
25194
25195/*
25196 * Compare function for total time sorting.
25197 */
25198 static int
25199#ifdef __BORLANDC__
25200_RTLENTRYF
25201#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025202prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025203{
25204 ufunc_T *p1, *p2;
25205
25206 p1 = *(ufunc_T **)s1;
25207 p2 = *(ufunc_T **)s2;
25208 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25209}
25210
25211/*
25212 * Compare function for self time sorting.
25213 */
25214 static int
25215#ifdef __BORLANDC__
25216_RTLENTRYF
25217#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025218prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025219{
25220 ufunc_T *p1, *p2;
25221
25222 p1 = *(ufunc_T **)s1;
25223 p2 = *(ufunc_T **)s2;
25224 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25225}
25226
Bram Moolenaar05159a02005-02-26 23:04:13 +000025227#endif
25228
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025229/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025230 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025231 * Return TRUE if a package was loaded.
25232 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025234script_autoload(
25235 char_u *name,
25236 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025237{
25238 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025239 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025240 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025241 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025242
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025243 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025244 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025245 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025246 return FALSE;
25247
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025248 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025249
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025250 /* Find the name in the list of previously loaded package names. Skip
25251 * "autoload/", it's always the same. */
25252 for (i = 0; i < ga_loaded.ga_len; ++i)
25253 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25254 break;
25255 if (!reload && i < ga_loaded.ga_len)
25256 ret = FALSE; /* was loaded already */
25257 else
25258 {
25259 /* Remember the name if it wasn't loaded already. */
25260 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25261 {
25262 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25263 tofree = NULL;
25264 }
25265
25266 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025267 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025268 ret = TRUE;
25269 }
25270
25271 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025272 return ret;
25273}
25274
25275/*
25276 * Return the autoload script name for a function or variable name.
25277 * Returns NULL when out of memory.
25278 */
25279 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025280autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025281{
25282 char_u *p;
25283 char_u *scriptname;
25284
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025285 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025286 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25287 if (scriptname == NULL)
25288 return FALSE;
25289 STRCPY(scriptname, "autoload/");
25290 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025291 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025292 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025293 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025294 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025295 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025296}
25297
Bram Moolenaar071d4272004-06-13 20:20:40 +000025298#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25299
25300/*
25301 * Function given to ExpandGeneric() to obtain the list of user defined
25302 * function names.
25303 */
25304 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025305get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025307 static long_u done;
25308 static hashitem_T *hi;
25309 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310
25311 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025313 done = 0;
25314 hi = func_hashtab.ht_array;
25315 }
25316 if (done < func_hashtab.ht_used)
25317 {
25318 if (done++ > 0)
25319 ++hi;
25320 while (HASHITEM_EMPTY(hi))
25321 ++hi;
25322 fp = HI2UF(hi);
25323
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025324 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025325 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025326
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025327 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25328 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329
25330 cat_func_name(IObuff, fp);
25331 if (xp->xp_context != EXPAND_USER_FUNC)
25332 {
25333 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025334 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335 STRCAT(IObuff, ")");
25336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337 return IObuff;
25338 }
25339 return NULL;
25340}
25341
25342#endif /* FEAT_CMDL_COMPL */
25343
25344/*
25345 * Copy the function name of "fp" to buffer "buf".
25346 * "buf" must be able to hold the function name plus three bytes.
25347 * Takes care of script-local function names.
25348 */
25349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025350cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025352 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353 {
25354 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025355 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025356 }
25357 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025358 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359}
25360
25361/*
25362 * ":delfunction {name}"
25363 */
25364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025365ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025367 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025368 char_u *p;
25369 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025370 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371
25372 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025373 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025374 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025376 {
25377 if (fudi.fd_dict != NULL && !eap->skip)
25378 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025381 if (!ends_excmd(*skipwhite(p)))
25382 {
25383 vim_free(name);
25384 EMSG(_(e_trailing));
25385 return;
25386 }
25387 eap->nextcmd = check_nextcmd(p);
25388 if (eap->nextcmd != NULL)
25389 *p = NUL;
25390
25391 if (!eap->skip)
25392 fp = find_func(name);
25393 vim_free(name);
25394
25395 if (!eap->skip)
25396 {
25397 if (fp == NULL)
25398 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025399 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025400 return;
25401 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025402 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403 {
25404 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25405 return;
25406 }
25407
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025408 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025410 /* Delete the dict item that refers to the function, it will
25411 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025412 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025414 else
25415 func_free(fp);
25416 }
25417}
25418
25419/*
25420 * Free a function and remove it from the list of functions.
25421 */
25422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025423func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025424{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025425 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025426
25427 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025428 ga_clear_strings(&(fp->uf_args));
25429 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025430#ifdef FEAT_PROFILE
25431 vim_free(fp->uf_tml_count);
25432 vim_free(fp->uf_tml_total);
25433 vim_free(fp->uf_tml_self);
25434#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025435
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025436 /* remove the function from the function hashtable */
25437 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25438 if (HASHITEM_EMPTY(hi))
25439 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025440 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025441 hash_remove(&func_hashtab, hi);
25442
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025443 vim_free(fp);
25444}
25445
25446/*
25447 * Unreference a Function: decrement the reference count and free it when it
25448 * becomes zero. Only for numbered functions.
25449 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025451func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025452{
25453 ufunc_T *fp;
25454
25455 if (name != NULL && isdigit(*name))
25456 {
25457 fp = find_func(name);
25458 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025459 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025460#ifdef EXITFREE
25461 if (!entered_free_all_mem)
25462#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025463 EMSG2(_(e_intern2), "func_unref()");
25464 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025465 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025466 {
25467 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025468 * when "uf_calls" becomes zero. */
25469 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025470 func_free(fp);
25471 }
25472 }
25473}
25474
25475/*
25476 * Count a reference to a Function.
25477 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025479func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025480{
25481 ufunc_T *fp;
25482
25483 if (name != NULL && isdigit(*name))
25484 {
25485 fp = find_func(name);
25486 if (fp == NULL)
25487 EMSG2(_(e_intern2), "func_ref()");
25488 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025489 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490 }
25491}
25492
25493/*
25494 * Call a user function.
25495 */
25496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025497call_user_func(
25498 ufunc_T *fp, /* pointer to function */
25499 int argcount, /* nr of args */
25500 typval_T *argvars, /* arguments */
25501 typval_T *rettv, /* return value */
25502 linenr_T firstline, /* first line of range */
25503 linenr_T lastline, /* last line of range */
25504 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505{
Bram Moolenaar33570922005-01-25 22:26:29 +000025506 char_u *save_sourcing_name;
25507 linenr_T save_sourcing_lnum;
25508 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025509 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025510 int save_did_emsg;
25511 static int depth = 0;
25512 dictitem_T *v;
25513 int fixvar_idx = 0; /* index in fixvar[] */
25514 int i;
25515 int ai;
25516 char_u numbuf[NUMBUFLEN];
25517 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025518 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025519#ifdef FEAT_PROFILE
25520 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025521 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523
25524 /* If depth of calling is getting too high, don't execute the function */
25525 if (depth >= p_mfd)
25526 {
25527 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025528 rettv->v_type = VAR_NUMBER;
25529 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 return;
25531 }
25532 ++depth;
25533
25534 line_breakcheck(); /* check for CTRL-C hit */
25535
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025536 fc = (funccall_T *)alloc(sizeof(funccall_T));
25537 fc->caller = current_funccal;
25538 current_funccal = fc;
25539 fc->func = fp;
25540 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025541 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025542 fc->linenr = 0;
25543 fc->returned = FALSE;
25544 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025546 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25547 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548
Bram Moolenaar33570922005-01-25 22:26:29 +000025549 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025550 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025551 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25552 * each argument variable and saves a lot of time.
25553 */
25554 /*
25555 * Init l: variables.
25556 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025557 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025558 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025559 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025560 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25561 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025562 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025563 name = v->di_key;
25564 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025565 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025566 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025567 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025568 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025569 v->di_tv.vval.v_dict = selfdict;
25570 ++selfdict->dv_refcount;
25571 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025572
Bram Moolenaar33570922005-01-25 22:26:29 +000025573 /*
25574 * Init a: variables.
25575 * Set a:0 to "argcount".
25576 * Set a:000 to a list with room for the "..." arguments.
25577 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025578 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025579 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025580 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025581 /* Use "name" to avoid a warning from some compiler that checks the
25582 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025583 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025584 name = v->di_key;
25585 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025586 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025587 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025588 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025589 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025590 v->di_tv.vval.v_list = &fc->l_varlist;
25591 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25592 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25593 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025594
25595 /*
25596 * Set a:firstline to "firstline" and a:lastline to "lastline".
25597 * Set a:name to named arguments.
25598 * Set a:N to the "..." arguments.
25599 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025600 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025601 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025602 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025603 (varnumber_T)lastline);
25604 for (i = 0; i < argcount; ++i)
25605 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025606 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025607 if (ai < 0)
25608 /* named argument a:name */
25609 name = FUNCARG(fp, i);
25610 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025611 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025612 /* "..." argument a:1, a:2, etc. */
25613 sprintf((char *)numbuf, "%d", ai + 1);
25614 name = numbuf;
25615 }
25616 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25617 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025618 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025619 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25620 }
25621 else
25622 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025623 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25624 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025625 if (v == NULL)
25626 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025627 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025628 }
25629 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025630 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025631
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025632 /* Note: the values are copied directly to avoid alloc/free.
25633 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025634 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025635 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025636
25637 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25638 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025639 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25640 fc->l_listitems[ai].li_tv = argvars[i];
25641 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025642 }
25643 }
25644
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645 /* Don't redraw while executing the function. */
25646 ++RedrawingDisabled;
25647 save_sourcing_name = sourcing_name;
25648 save_sourcing_lnum = sourcing_lnum;
25649 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025650 /* need space for function name + ("function " + 3) or "[number]" */
25651 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25652 + STRLEN(fp->uf_name) + 20;
25653 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654 if (sourcing_name != NULL)
25655 {
25656 if (save_sourcing_name != NULL
25657 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025658 sprintf((char *)sourcing_name, "%s[%d]..",
25659 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660 else
25661 STRCPY(sourcing_name, "function ");
25662 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25663
25664 if (p_verbose >= 12)
25665 {
25666 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025667 verbose_enter_scroll();
25668
Bram Moolenaar555b2802005-05-19 21:08:39 +000025669 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670 if (p_verbose >= 14)
25671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025672 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025673 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025674 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025675 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676
25677 msg_puts((char_u *)"(");
25678 for (i = 0; i < argcount; ++i)
25679 {
25680 if (i > 0)
25681 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025682 if (argvars[i].v_type == VAR_NUMBER)
25683 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025684 else
25685 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025686 /* Do not want errors such as E724 here. */
25687 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025688 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025689 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025690 if (s != NULL)
25691 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025692 if (vim_strsize(s) > MSG_BUF_CLEN)
25693 {
25694 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25695 s = buf;
25696 }
25697 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025698 vim_free(tofree);
25699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025700 }
25701 }
25702 msg_puts((char_u *)")");
25703 }
25704 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025705
25706 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025707 --no_wait_return;
25708 }
25709 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025710#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025711 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025712 {
25713 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25714 func_do_profile(fp);
25715 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025716 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025717 {
25718 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025719 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025720 profile_zero(&fp->uf_tm_children);
25721 }
25722 script_prof_save(&wait_start);
25723 }
25724#endif
25725
Bram Moolenaar071d4272004-06-13 20:20:40 +000025726 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025727 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728 save_did_emsg = did_emsg;
25729 did_emsg = FALSE;
25730
25731 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025732 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025733 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25734
25735 --RedrawingDisabled;
25736
25737 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025738 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025740 clear_tv(rettv);
25741 rettv->v_type = VAR_NUMBER;
25742 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025743 }
25744
Bram Moolenaar05159a02005-02-26 23:04:13 +000025745#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025746 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025747 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025748 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025749 profile_end(&call_start);
25750 profile_sub_wait(&wait_start, &call_start);
25751 profile_add(&fp->uf_tm_total, &call_start);
25752 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025753 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025754 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025755 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25756 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025757 }
25758 }
25759#endif
25760
Bram Moolenaar071d4272004-06-13 20:20:40 +000025761 /* when being verbose, mention the return value */
25762 if (p_verbose >= 12)
25763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025764 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025765 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766
Bram Moolenaar071d4272004-06-13 20:20:40 +000025767 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025768 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025769 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025770 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025771 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025772 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025774 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025775 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025776 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025777 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025778
Bram Moolenaar555b2802005-05-19 21:08:39 +000025779 /* The value may be very long. Skip the middle part, so that we
25780 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025781 * truncate it at the end. Don't want errors such as E724 here. */
25782 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025783 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025784 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025785 if (s != NULL)
25786 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025787 if (vim_strsize(s) > MSG_BUF_CLEN)
25788 {
25789 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25790 s = buf;
25791 }
25792 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025793 vim_free(tofree);
25794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795 }
25796 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025797
25798 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025799 --no_wait_return;
25800 }
25801
25802 vim_free(sourcing_name);
25803 sourcing_name = save_sourcing_name;
25804 sourcing_lnum = save_sourcing_lnum;
25805 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025806#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025807 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025808 script_prof_restore(&wait_start);
25809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025810
25811 if (p_verbose >= 12 && sourcing_name != NULL)
25812 {
25813 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025814 verbose_enter_scroll();
25815
Bram Moolenaar555b2802005-05-19 21:08:39 +000025816 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025817 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025818
25819 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820 --no_wait_return;
25821 }
25822
25823 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025824 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025826
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025827 /* If the a:000 list and the l: and a: dicts are not referenced we can
25828 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025829 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25830 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25831 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25832 {
25833 free_funccal(fc, FALSE);
25834 }
25835 else
25836 {
25837 hashitem_T *hi;
25838 listitem_T *li;
25839 int todo;
25840
25841 /* "fc" is still in use. This can happen when returning "a:000" or
25842 * assigning "l:" to a global variable.
25843 * Link "fc" in the list for garbage collection later. */
25844 fc->caller = previous_funccal;
25845 previous_funccal = fc;
25846
25847 /* Make a copy of the a: variables, since we didn't do that above. */
25848 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25849 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25850 {
25851 if (!HASHITEM_EMPTY(hi))
25852 {
25853 --todo;
25854 v = HI2DI(hi);
25855 copy_tv(&v->di_tv, &v->di_tv);
25856 }
25857 }
25858
25859 /* Make a copy of the a:000 items, since we didn't do that above. */
25860 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25861 copy_tv(&li->li_tv, &li->li_tv);
25862 }
25863}
25864
25865/*
25866 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025867 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025868 */
25869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025870can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025871{
25872 return (fc->l_varlist.lv_copyID != copyID
25873 && fc->l_vars.dv_copyID != copyID
25874 && fc->l_avars.dv_copyID != copyID);
25875}
25876
25877/*
25878 * Free "fc" and what it contains.
25879 */
25880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025881free_funccal(
25882 funccall_T *fc,
25883 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025884{
25885 listitem_T *li;
25886
25887 /* The a: variables typevals may not have been allocated, only free the
25888 * allocated variables. */
25889 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25890
25891 /* free all l: variables */
25892 vars_clear(&fc->l_vars.dv_hashtab);
25893
25894 /* Free the a:000 variables if they were allocated. */
25895 if (free_val)
25896 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25897 clear_tv(&li->li_tv);
25898
25899 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900}
25901
25902/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025903 * Add a number variable "name" to dict "dp" with value "nr".
25904 */
25905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025906add_nr_var(
25907 dict_T *dp,
25908 dictitem_T *v,
25909 char *name,
25910 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025911{
25912 STRCPY(v->di_key, name);
25913 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25914 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25915 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025916 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025917 v->di_tv.vval.v_number = nr;
25918}
25919
25920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025921 * ":return [expr]"
25922 */
25923 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025924ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025925{
25926 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025927 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025928 int returning = FALSE;
25929
25930 if (current_funccal == NULL)
25931 {
25932 EMSG(_("E133: :return not inside a function"));
25933 return;
25934 }
25935
25936 if (eap->skip)
25937 ++emsg_skip;
25938
25939 eap->nextcmd = NULL;
25940 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025941 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942 {
25943 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025944 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025945 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025946 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947 }
25948 /* It's safer to return also on error. */
25949 else if (!eap->skip)
25950 {
25951 /*
25952 * Return unless the expression evaluation has been cancelled due to an
25953 * aborting error, an interrupt, or an exception.
25954 */
25955 if (!aborting())
25956 returning = do_return(eap, FALSE, TRUE, NULL);
25957 }
25958
25959 /* When skipping or the return gets pending, advance to the next command
25960 * in this line (!returning). Otherwise, ignore the rest of the line.
25961 * Following lines will be ignored by get_func_line(). */
25962 if (returning)
25963 eap->nextcmd = NULL;
25964 else if (eap->nextcmd == NULL) /* no argument */
25965 eap->nextcmd = check_nextcmd(arg);
25966
25967 if (eap->skip)
25968 --emsg_skip;
25969}
25970
25971/*
25972 * Return from a function. Possibly makes the return pending. Also called
25973 * for a pending return at the ":endtry" or after returning from an extra
25974 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025975 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025976 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977 * FALSE when the return gets pending.
25978 */
25979 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025980do_return(
25981 exarg_T *eap,
25982 int reanimate,
25983 int is_cmd,
25984 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025985{
25986 int idx;
25987 struct condstack *cstack = eap->cstack;
25988
25989 if (reanimate)
25990 /* Undo the return. */
25991 current_funccal->returned = FALSE;
25992
25993 /*
25994 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25995 * not in its finally clause (which then is to be executed next) is found.
25996 * In this case, make the ":return" pending for execution at the ":endtry".
25997 * Otherwise, return normally.
25998 */
25999 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26000 if (idx >= 0)
26001 {
26002 cstack->cs_pending[idx] = CSTP_RETURN;
26003
26004 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026005 /* A pending return again gets pending. "rettv" points to an
26006 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026008 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026009 else
26010 {
26011 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026012 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026014 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026016 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026017 {
26018 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026019 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026020 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026021 else
26022 EMSG(_(e_outofmem));
26023 }
26024 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026025 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026
26027 if (reanimate)
26028 {
26029 /* The pending return value could be overwritten by a ":return"
26030 * without argument in a finally clause; reset the default
26031 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026032 current_funccal->rettv->v_type = VAR_NUMBER;
26033 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026034 }
26035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026036 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026037 }
26038 else
26039 {
26040 current_funccal->returned = TRUE;
26041
26042 /* If the return is carried out now, store the return value. For
26043 * a return immediately after reanimation, the value is already
26044 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026045 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026046 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026047 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026048 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026050 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051 }
26052 }
26053
26054 return idx < 0;
26055}
26056
26057/*
26058 * Free the variable with a pending return value.
26059 */
26060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026061discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026062{
Bram Moolenaar33570922005-01-25 22:26:29 +000026063 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026064}
26065
26066/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026067 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068 * is an allocated string. Used by report_pending() for verbose messages.
26069 */
26070 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026071get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026072{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026073 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026074 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026075 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026076
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026077 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026078 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026079 if (s == NULL)
26080 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026081
26082 STRCPY(IObuff, ":return ");
26083 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26084 if (STRLEN(s) + 8 >= IOSIZE)
26085 STRCPY(IObuff + IOSIZE - 4, "...");
26086 vim_free(tofree);
26087 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026088}
26089
26090/*
26091 * Get next function line.
26092 * Called by do_cmdline() to get the next line.
26093 * Returns allocated string, or NULL for end of function.
26094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026095 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026096get_func_line(
26097 int c UNUSED,
26098 void *cookie,
26099 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026100{
Bram Moolenaar33570922005-01-25 22:26:29 +000026101 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026102 ufunc_T *fp = fcp->func;
26103 char_u *retval;
26104 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026105
26106 /* If breakpoints have been added/deleted need to check for it. */
26107 if (fcp->dbg_tick != debug_tick)
26108 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026109 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026110 sourcing_lnum);
26111 fcp->dbg_tick = debug_tick;
26112 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026113#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026114 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026115 func_line_end(cookie);
26116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026117
Bram Moolenaar05159a02005-02-26 23:04:13 +000026118 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026119 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26120 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026121 retval = NULL;
26122 else
26123 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026124 /* Skip NULL lines (continuation lines). */
26125 while (fcp->linenr < gap->ga_len
26126 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26127 ++fcp->linenr;
26128 if (fcp->linenr >= gap->ga_len)
26129 retval = NULL;
26130 else
26131 {
26132 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26133 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026134#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026135 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026136 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026137#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026139 }
26140
26141 /* Did we encounter a breakpoint? */
26142 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26143 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026144 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026145 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026146 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026147 sourcing_lnum);
26148 fcp->dbg_tick = debug_tick;
26149 }
26150
26151 return retval;
26152}
26153
Bram Moolenaar05159a02005-02-26 23:04:13 +000026154#if defined(FEAT_PROFILE) || defined(PROTO)
26155/*
26156 * Called when starting to read a function line.
26157 * "sourcing_lnum" must be correct!
26158 * When skipping lines it may not actually be executed, but we won't find out
26159 * until later and we need to store the time now.
26160 */
26161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026162func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026163{
26164 funccall_T *fcp = (funccall_T *)cookie;
26165 ufunc_T *fp = fcp->func;
26166
26167 if (fp->uf_profiling && sourcing_lnum >= 1
26168 && sourcing_lnum <= fp->uf_lines.ga_len)
26169 {
26170 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026171 /* Skip continuation lines. */
26172 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26173 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026174 fp->uf_tml_execed = FALSE;
26175 profile_start(&fp->uf_tml_start);
26176 profile_zero(&fp->uf_tml_children);
26177 profile_get_wait(&fp->uf_tml_wait);
26178 }
26179}
26180
26181/*
26182 * Called when actually executing a function line.
26183 */
26184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026185func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026186{
26187 funccall_T *fcp = (funccall_T *)cookie;
26188 ufunc_T *fp = fcp->func;
26189
26190 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26191 fp->uf_tml_execed = TRUE;
26192}
26193
26194/*
26195 * Called when done with a function line.
26196 */
26197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026198func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026199{
26200 funccall_T *fcp = (funccall_T *)cookie;
26201 ufunc_T *fp = fcp->func;
26202
26203 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26204 {
26205 if (fp->uf_tml_execed)
26206 {
26207 ++fp->uf_tml_count[fp->uf_tml_idx];
26208 profile_end(&fp->uf_tml_start);
26209 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026210 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026211 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26212 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026213 }
26214 fp->uf_tml_idx = -1;
26215 }
26216}
26217#endif
26218
Bram Moolenaar071d4272004-06-13 20:20:40 +000026219/*
26220 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026221 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026222 */
26223 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026224func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026225{
Bram Moolenaar33570922005-01-25 22:26:29 +000026226 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026227
26228 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26229 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026230 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026231 || fcp->returned);
26232}
26233
26234/*
26235 * return TRUE if cookie indicates a function which "abort"s on errors.
26236 */
26237 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026238func_has_abort(
26239 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026240{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026241 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026242}
26243
26244#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26245typedef enum
26246{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026247 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26248 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26249 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026250} var_flavour_T;
26251
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026252static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026253
26254 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026255var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026256{
26257 char_u *p = varname;
26258
26259 if (ASCII_ISUPPER(*p))
26260 {
26261 while (*(++p))
26262 if (ASCII_ISLOWER(*p))
26263 return VAR_FLAVOUR_SESSION;
26264 return VAR_FLAVOUR_VIMINFO;
26265 }
26266 else
26267 return VAR_FLAVOUR_DEFAULT;
26268}
26269#endif
26270
26271#if defined(FEAT_VIMINFO) || defined(PROTO)
26272/*
26273 * Restore global vars that start with a capital from the viminfo file
26274 */
26275 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026276read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026277{
26278 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026279 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026280 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026281 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026282
26283 if (!writing && (find_viminfo_parameter('!') != NULL))
26284 {
26285 tab = vim_strchr(virp->vir_line + 1, '\t');
26286 if (tab != NULL)
26287 {
26288 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026289 switch (*tab)
26290 {
26291 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026292#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026293 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026294#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026295 case 'D': type = VAR_DICT; break;
26296 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026297 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026299
26300 tab = vim_strchr(tab, '\t');
26301 if (tab != NULL)
26302 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026303 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026304 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026305 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026306 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026307#ifdef FEAT_FLOAT
26308 else if (type == VAR_FLOAT)
26309 (void)string2float(tab + 1, &tv.vval.v_float);
26310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026311 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026312 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026313 if (type == VAR_DICT || type == VAR_LIST)
26314 {
26315 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26316
26317 if (etv == NULL)
26318 /* Failed to parse back the dict or list, use it as a
26319 * string. */
26320 tv.v_type = VAR_STRING;
26321 else
26322 {
26323 vim_free(tv.vval.v_string);
26324 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026325 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026326 }
26327 }
26328
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026329 /* when in a function use global variables */
26330 save_funccal = current_funccal;
26331 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026332 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026333 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026334
26335 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026336 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026337 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26338 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026339 }
26340 }
26341 }
26342
26343 return viminfo_readline(virp);
26344}
26345
26346/*
26347 * Write global vars that start with a capital to the viminfo file
26348 */
26349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026350write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026351{
Bram Moolenaar33570922005-01-25 22:26:29 +000026352 hashitem_T *hi;
26353 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026354 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026355 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026356 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026357 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026359
26360 if (find_viminfo_parameter('!') == NULL)
26361 return;
26362
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026363 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026364
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026365 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026366 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026367 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026368 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026369 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026370 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026371 this_var = HI2DI(hi);
26372 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026373 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026374 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026375 {
26376 case VAR_STRING: s = "STR"; break;
26377 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026378 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026379 case VAR_DICT: s = "DIC"; break;
26380 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026381 case VAR_SPECIAL: s = "XPL"; break;
26382
26383 case VAR_UNKNOWN:
26384 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026385 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026386 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026387 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026388 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026389 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026390 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026391 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026392 if (p != NULL)
26393 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026394 vim_free(tofree);
26395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026396 }
26397 }
26398}
26399#endif
26400
26401#if defined(FEAT_SESSION) || defined(PROTO)
26402 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026403store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026404{
Bram Moolenaar33570922005-01-25 22:26:29 +000026405 hashitem_T *hi;
26406 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026407 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026408 char_u *p, *t;
26409
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026410 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026411 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026412 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026413 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026414 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026415 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026416 this_var = HI2DI(hi);
26417 if ((this_var->di_tv.v_type == VAR_NUMBER
26418 || this_var->di_tv.v_type == VAR_STRING)
26419 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026420 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026421 /* Escape special characters with a backslash. Turn a LF and
26422 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026423 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026424 (char_u *)"\\\"\n\r");
26425 if (p == NULL) /* out of memory */
26426 break;
26427 for (t = p; *t != NUL; ++t)
26428 if (*t == '\n')
26429 *t = 'n';
26430 else if (*t == '\r')
26431 *t = 'r';
26432 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026433 this_var->di_key,
26434 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26435 : ' ',
26436 p,
26437 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26438 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026439 || put_eol(fd) == FAIL)
26440 {
26441 vim_free(p);
26442 return FAIL;
26443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026444 vim_free(p);
26445 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026446#ifdef FEAT_FLOAT
26447 else if (this_var->di_tv.v_type == VAR_FLOAT
26448 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26449 {
26450 float_T f = this_var->di_tv.vval.v_float;
26451 int sign = ' ';
26452
26453 if (f < 0)
26454 {
26455 f = -f;
26456 sign = '-';
26457 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026458 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026459 this_var->di_key, sign, f) < 0)
26460 || put_eol(fd) == FAIL)
26461 return FAIL;
26462 }
26463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026464 }
26465 }
26466 return OK;
26467}
26468#endif
26469
Bram Moolenaar661b1822005-07-28 22:36:45 +000026470/*
26471 * Display script name where an item was last set.
26472 * Should only be invoked when 'verbose' is non-zero.
26473 */
26474 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026475last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026476{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026477 char_u *p;
26478
Bram Moolenaar661b1822005-07-28 22:36:45 +000026479 if (scriptID != 0)
26480 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026481 p = home_replace_save(NULL, get_scriptname(scriptID));
26482 if (p != NULL)
26483 {
26484 verbose_enter();
26485 MSG_PUTS(_("\n\tLast set from "));
26486 MSG_PUTS(p);
26487 vim_free(p);
26488 verbose_leave();
26489 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026490 }
26491}
26492
Bram Moolenaard812df62008-11-09 12:46:09 +000026493/*
26494 * List v:oldfiles in a nice way.
26495 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026497ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026498{
26499 list_T *l = vimvars[VV_OLDFILES].vv_list;
26500 listitem_T *li;
26501 int nr = 0;
26502
26503 if (l == NULL)
26504 msg((char_u *)_("No old files"));
26505 else
26506 {
26507 msg_start();
26508 msg_scroll = TRUE;
26509 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26510 {
26511 msg_outnum((long)++nr);
26512 MSG_PUTS(": ");
26513 msg_outtrans(get_tv_string(&li->li_tv));
26514 msg_putchar('\n');
26515 out_flush(); /* output one line at a time */
26516 ui_breakcheck();
26517 }
26518 /* Assume "got_int" was set to truncate the listing. */
26519 got_int = FALSE;
26520
26521#ifdef FEAT_BROWSE_CMD
26522 if (cmdmod.browse)
26523 {
26524 quit_more = FALSE;
26525 nr = prompt_for_number(FALSE);
26526 msg_starthere();
26527 if (nr > 0)
26528 {
26529 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26530 (long)nr);
26531
26532 if (p != NULL)
26533 {
26534 p = expand_env_save(p);
26535 eap->arg = p;
26536 eap->cmdidx = CMD_edit;
26537 cmdmod.browse = FALSE;
26538 do_exedit(eap, NULL);
26539 vim_free(p);
26540 }
26541 }
26542 }
26543#endif
26544 }
26545}
26546
Bram Moolenaar53744302015-07-17 17:38:22 +020026547/* reset v:option_new, v:option_old and v:option_type */
26548 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026549reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026550{
26551 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26552 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26553 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26554}
26555
26556
Bram Moolenaar071d4272004-06-13 20:20:40 +000026557#endif /* FEAT_EVAL */
26558
Bram Moolenaar071d4272004-06-13 20:20:40 +000026559
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026560#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026561
26562#ifdef WIN3264
26563/*
26564 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26565 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026566static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26567static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26568static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026569
26570/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026571 * Get the short path (8.3) for the filename in "fnamep".
26572 * Only works for a valid file name.
26573 * When the path gets longer "fnamep" is changed and the allocated buffer
26574 * is put in "bufp".
26575 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26576 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026577 */
26578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026579get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026580{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026581 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026582 char_u *newbuf;
26583
26584 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026585 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026586 if (l > len - 1)
26587 {
26588 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026589 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026590 newbuf = vim_strnsave(*fnamep, l);
26591 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026593
26594 vim_free(*bufp);
26595 *fnamep = *bufp = newbuf;
26596
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026597 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026598 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026599 }
26600
26601 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026602 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026603}
26604
26605/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026606 * Get the short path (8.3) for the filename in "fname". The converted
26607 * path is returned in "bufp".
26608 *
26609 * Some of the directories specified in "fname" may not exist. This function
26610 * will shorten the existing directories at the beginning of the path and then
26611 * append the remaining non-existing path.
26612 *
26613 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026614 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026615 * bufp - Pointer to an allocated buffer for the filename.
26616 * fnamelen - Length of the filename pointed to by fname
26617 *
26618 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026619 */
26620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026621shortpath_for_invalid_fname(
26622 char_u **fname,
26623 char_u **bufp,
26624 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026625{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026626 char_u *short_fname, *save_fname, *pbuf_unused;
26627 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026628 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026629 int old_len, len;
26630 int new_len, sfx_len;
26631 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026632
26633 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026634 old_len = *fnamelen;
26635 save_fname = vim_strnsave(*fname, old_len);
26636 pbuf_unused = NULL;
26637 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026638
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026639 endp = save_fname + old_len - 1; /* Find the end of the copy */
26640 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026641
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026642 /*
26643 * Try shortening the supplied path till it succeeds by removing one
26644 * directory at a time from the tail of the path.
26645 */
26646 len = 0;
26647 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026648 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026649 /* go back one path-separator */
26650 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26651 --endp;
26652 if (endp <= save_fname)
26653 break; /* processed the complete path */
26654
26655 /*
26656 * Replace the path separator with a NUL and try to shorten the
26657 * resulting path.
26658 */
26659 ch = *endp;
26660 *endp = 0;
26661 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026662 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026663 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26664 {
26665 retval = FAIL;
26666 goto theend;
26667 }
26668 *endp = ch; /* preserve the string */
26669
26670 if (len > 0)
26671 break; /* successfully shortened the path */
26672
26673 /* failed to shorten the path. Skip the path separator */
26674 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026675 }
26676
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026677 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026678 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026679 /*
26680 * Succeeded in shortening the path. Now concatenate the shortened
26681 * path with the remaining path at the tail.
26682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026683
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026684 /* Compute the length of the new path. */
26685 sfx_len = (int)(save_endp - endp) + 1;
26686 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026687
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026688 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026689 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026690 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026691 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026692 /* There is not enough space in the currently allocated string,
26693 * copy it to a buffer big enough. */
26694 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026695 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026696 {
26697 retval = FAIL;
26698 goto theend;
26699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026700 }
26701 else
26702 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026703 /* Transfer short_fname to the main buffer (it's big enough),
26704 * unless get_short_pathname() did its work in-place. */
26705 *fname = *bufp = save_fname;
26706 if (short_fname != save_fname)
26707 vim_strncpy(save_fname, short_fname, len);
26708 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026709 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026710
26711 /* concat the not-shortened part of the path */
26712 vim_strncpy(*fname + len, endp, sfx_len);
26713 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026714 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026715
26716theend:
26717 vim_free(pbuf_unused);
26718 vim_free(save_fname);
26719
26720 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026721}
26722
26723/*
26724 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026725 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026726 */
26727 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026728shortpath_for_partial(
26729 char_u **fnamep,
26730 char_u **bufp,
26731 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026732{
26733 int sepcount, len, tflen;
26734 char_u *p;
26735 char_u *pbuf, *tfname;
26736 int hasTilde;
26737
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026738 /* Count up the path separators from the RHS.. so we know which part
26739 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026740 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026741 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026742 if (vim_ispathsep(*p))
26743 ++sepcount;
26744
26745 /* Need full path first (use expand_env() to remove a "~/") */
26746 hasTilde = (**fnamep == '~');
26747 if (hasTilde)
26748 pbuf = tfname = expand_env_save(*fnamep);
26749 else
26750 pbuf = tfname = FullName_save(*fnamep, FALSE);
26751
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026752 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026753
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026754 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26755 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026756
26757 if (len == 0)
26758 {
26759 /* Don't have a valid filename, so shorten the rest of the
26760 * path if we can. This CAN give us invalid 8.3 filenames, but
26761 * there's not a lot of point in guessing what it might be.
26762 */
26763 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026764 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26765 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026766 }
26767
26768 /* Count the paths backward to find the beginning of the desired string. */
26769 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026770 {
26771#ifdef FEAT_MBYTE
26772 if (has_mbyte)
26773 p -= mb_head_off(tfname, p);
26774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026775 if (vim_ispathsep(*p))
26776 {
26777 if (sepcount == 0 || (hasTilde && sepcount == 1))
26778 break;
26779 else
26780 sepcount --;
26781 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026783 if (hasTilde)
26784 {
26785 --p;
26786 if (p >= tfname)
26787 *p = '~';
26788 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026789 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026790 }
26791 else
26792 ++p;
26793
26794 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26795 vim_free(*bufp);
26796 *fnamelen = (int)STRLEN(p);
26797 *bufp = pbuf;
26798 *fnamep = p;
26799
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026800 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026801}
26802#endif /* WIN3264 */
26803
26804/*
26805 * Adjust a filename, according to a string of modifiers.
26806 * *fnamep must be NUL terminated when called. When returning, the length is
26807 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026808 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026809 * When there is an error, *fnamep is set to NULL.
26810 */
26811 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026812modify_fname(
26813 char_u *src, /* string with modifiers */
26814 int *usedlen, /* characters after src that are used */
26815 char_u **fnamep, /* file name so far */
26816 char_u **bufp, /* buffer for allocated file name or NULL */
26817 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026818{
26819 int valid = 0;
26820 char_u *tail;
26821 char_u *s, *p, *pbuf;
26822 char_u dirname[MAXPATHL];
26823 int c;
26824 int has_fullname = 0;
26825#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026826 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026827 int has_shortname = 0;
26828#endif
26829
26830repeat:
26831 /* ":p" - full path/file_name */
26832 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26833 {
26834 has_fullname = 1;
26835
26836 valid |= VALID_PATH;
26837 *usedlen += 2;
26838
26839 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26840 if ((*fnamep)[0] == '~'
26841#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26842 && ((*fnamep)[1] == '/'
26843# ifdef BACKSLASH_IN_FILENAME
26844 || (*fnamep)[1] == '\\'
26845# endif
26846 || (*fnamep)[1] == NUL)
26847
26848#endif
26849 )
26850 {
26851 *fnamep = expand_env_save(*fnamep);
26852 vim_free(*bufp); /* free any allocated file name */
26853 *bufp = *fnamep;
26854 if (*fnamep == NULL)
26855 return -1;
26856 }
26857
26858 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026859 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026860 {
26861 if (vim_ispathsep(*p)
26862 && p[1] == '.'
26863 && (p[2] == NUL
26864 || vim_ispathsep(p[2])
26865 || (p[2] == '.'
26866 && (p[3] == NUL || vim_ispathsep(p[3])))))
26867 break;
26868 }
26869
26870 /* FullName_save() is slow, don't use it when not needed. */
26871 if (*p != NUL || !vim_isAbsName(*fnamep))
26872 {
26873 *fnamep = FullName_save(*fnamep, *p != NUL);
26874 vim_free(*bufp); /* free any allocated file name */
26875 *bufp = *fnamep;
26876 if (*fnamep == NULL)
26877 return -1;
26878 }
26879
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026880#ifdef WIN3264
26881# if _WIN32_WINNT >= 0x0500
26882 if (vim_strchr(*fnamep, '~') != NULL)
26883 {
26884 /* Expand 8.3 filename to full path. Needed to make sure the same
26885 * file does not have two different names.
26886 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26887 p = alloc(_MAX_PATH + 1);
26888 if (p != NULL)
26889 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026890 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026891 {
26892 vim_free(*bufp);
26893 *bufp = *fnamep = p;
26894 }
26895 else
26896 vim_free(p);
26897 }
26898 }
26899# endif
26900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026901 /* Append a path separator to a directory. */
26902 if (mch_isdir(*fnamep))
26903 {
26904 /* Make room for one or two extra characters. */
26905 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26906 vim_free(*bufp); /* free any allocated file name */
26907 *bufp = *fnamep;
26908 if (*fnamep == NULL)
26909 return -1;
26910 add_pathsep(*fnamep);
26911 }
26912 }
26913
26914 /* ":." - path relative to the current directory */
26915 /* ":~" - path relative to the home directory */
26916 /* ":8" - shortname path - postponed till after */
26917 while (src[*usedlen] == ':'
26918 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26919 {
26920 *usedlen += 2;
26921 if (c == '8')
26922 {
26923#ifdef WIN3264
26924 has_shortname = 1; /* Postpone this. */
26925#endif
26926 continue;
26927 }
26928 pbuf = NULL;
26929 /* Need full path first (use expand_env() to remove a "~/") */
26930 if (!has_fullname)
26931 {
26932 if (c == '.' && **fnamep == '~')
26933 p = pbuf = expand_env_save(*fnamep);
26934 else
26935 p = pbuf = FullName_save(*fnamep, FALSE);
26936 }
26937 else
26938 p = *fnamep;
26939
26940 has_fullname = 0;
26941
26942 if (p != NULL)
26943 {
26944 if (c == '.')
26945 {
26946 mch_dirname(dirname, MAXPATHL);
26947 s = shorten_fname(p, dirname);
26948 if (s != NULL)
26949 {
26950 *fnamep = s;
26951 if (pbuf != NULL)
26952 {
26953 vim_free(*bufp); /* free any allocated file name */
26954 *bufp = pbuf;
26955 pbuf = NULL;
26956 }
26957 }
26958 }
26959 else
26960 {
26961 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26962 /* Only replace it when it starts with '~' */
26963 if (*dirname == '~')
26964 {
26965 s = vim_strsave(dirname);
26966 if (s != NULL)
26967 {
26968 *fnamep = s;
26969 vim_free(*bufp);
26970 *bufp = s;
26971 }
26972 }
26973 }
26974 vim_free(pbuf);
26975 }
26976 }
26977
26978 tail = gettail(*fnamep);
26979 *fnamelen = (int)STRLEN(*fnamep);
26980
26981 /* ":h" - head, remove "/file_name", can be repeated */
26982 /* Don't remove the first "/" or "c:\" */
26983 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26984 {
26985 valid |= VALID_HEAD;
26986 *usedlen += 2;
26987 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026988 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026989 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026990 *fnamelen = (int)(tail - *fnamep);
26991#ifdef VMS
26992 if (*fnamelen > 0)
26993 *fnamelen += 1; /* the path separator is part of the path */
26994#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026995 if (*fnamelen == 0)
26996 {
26997 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26998 p = vim_strsave((char_u *)".");
26999 if (p == NULL)
27000 return -1;
27001 vim_free(*bufp);
27002 *bufp = *fnamep = tail = p;
27003 *fnamelen = 1;
27004 }
27005 else
27006 {
27007 while (tail > s && !after_pathsep(s, tail))
27008 mb_ptr_back(*fnamep, tail);
27009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027010 }
27011
27012 /* ":8" - shortname */
27013 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27014 {
27015 *usedlen += 2;
27016#ifdef WIN3264
27017 has_shortname = 1;
27018#endif
27019 }
27020
27021#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027022 /*
27023 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027024 */
27025 if (has_shortname)
27026 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027027 /* Copy the string if it is shortened by :h and when it wasn't copied
27028 * yet, because we are going to change it in place. Avoids changing
27029 * the buffer name for "%:8". */
27030 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027031 {
27032 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027033 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027034 return -1;
27035 vim_free(*bufp);
27036 *bufp = *fnamep = p;
27037 }
27038
27039 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027040 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027041 if (!has_fullname && !vim_isAbsName(*fnamep))
27042 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027043 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027044 return -1;
27045 }
27046 else
27047 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027048 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027049
Bram Moolenaardc935552011-08-17 15:23:23 +020027050 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027051 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027052 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027053 return -1;
27054
27055 if (l == 0)
27056 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027057 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027058 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027059 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027060 return -1;
27061 }
27062 *fnamelen = l;
27063 }
27064 }
27065#endif /* WIN3264 */
27066
27067 /* ":t" - tail, just the basename */
27068 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27069 {
27070 *usedlen += 2;
27071 *fnamelen -= (int)(tail - *fnamep);
27072 *fnamep = tail;
27073 }
27074
27075 /* ":e" - extension, can be repeated */
27076 /* ":r" - root, without extension, can be repeated */
27077 while (src[*usedlen] == ':'
27078 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27079 {
27080 /* find a '.' in the tail:
27081 * - for second :e: before the current fname
27082 * - otherwise: The last '.'
27083 */
27084 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27085 s = *fnamep - 2;
27086 else
27087 s = *fnamep + *fnamelen - 1;
27088 for ( ; s > tail; --s)
27089 if (s[0] == '.')
27090 break;
27091 if (src[*usedlen + 1] == 'e') /* :e */
27092 {
27093 if (s > tail)
27094 {
27095 *fnamelen += (int)(*fnamep - (s + 1));
27096 *fnamep = s + 1;
27097#ifdef VMS
27098 /* cut version from the extension */
27099 s = *fnamep + *fnamelen - 1;
27100 for ( ; s > *fnamep; --s)
27101 if (s[0] == ';')
27102 break;
27103 if (s > *fnamep)
27104 *fnamelen = s - *fnamep;
27105#endif
27106 }
27107 else if (*fnamep <= tail)
27108 *fnamelen = 0;
27109 }
27110 else /* :r */
27111 {
27112 if (s > tail) /* remove one extension */
27113 *fnamelen = (int)(s - *fnamep);
27114 }
27115 *usedlen += 2;
27116 }
27117
27118 /* ":s?pat?foo?" - substitute */
27119 /* ":gs?pat?foo?" - global substitute */
27120 if (src[*usedlen] == ':'
27121 && (src[*usedlen + 1] == 's'
27122 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27123 {
27124 char_u *str;
27125 char_u *pat;
27126 char_u *sub;
27127 int sep;
27128 char_u *flags;
27129 int didit = FALSE;
27130
27131 flags = (char_u *)"";
27132 s = src + *usedlen + 2;
27133 if (src[*usedlen + 1] == 'g')
27134 {
27135 flags = (char_u *)"g";
27136 ++s;
27137 }
27138
27139 sep = *s++;
27140 if (sep)
27141 {
27142 /* find end of pattern */
27143 p = vim_strchr(s, sep);
27144 if (p != NULL)
27145 {
27146 pat = vim_strnsave(s, (int)(p - s));
27147 if (pat != NULL)
27148 {
27149 s = p + 1;
27150 /* find end of substitution */
27151 p = vim_strchr(s, sep);
27152 if (p != NULL)
27153 {
27154 sub = vim_strnsave(s, (int)(p - s));
27155 str = vim_strnsave(*fnamep, *fnamelen);
27156 if (sub != NULL && str != NULL)
27157 {
27158 *usedlen = (int)(p + 1 - src);
27159 s = do_string_sub(str, pat, sub, flags);
27160 if (s != NULL)
27161 {
27162 *fnamep = s;
27163 *fnamelen = (int)STRLEN(s);
27164 vim_free(*bufp);
27165 *bufp = s;
27166 didit = TRUE;
27167 }
27168 }
27169 vim_free(sub);
27170 vim_free(str);
27171 }
27172 vim_free(pat);
27173 }
27174 }
27175 /* after using ":s", repeat all the modifiers */
27176 if (didit)
27177 goto repeat;
27178 }
27179 }
27180
Bram Moolenaar26df0922014-02-23 23:39:13 +010027181 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27182 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027183 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027184 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027185 if (c != NUL)
27186 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027187 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027188 if (c != NUL)
27189 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027190 if (p == NULL)
27191 return -1;
27192 vim_free(*bufp);
27193 *bufp = *fnamep = p;
27194 *fnamelen = (int)STRLEN(p);
27195 *usedlen += 2;
27196 }
27197
Bram Moolenaar071d4272004-06-13 20:20:40 +000027198 return valid;
27199}
27200
27201/*
27202 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27203 * "flags" can be "g" to do a global substitute.
27204 * Returns an allocated string, NULL for error.
27205 */
27206 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027207do_string_sub(
27208 char_u *str,
27209 char_u *pat,
27210 char_u *sub,
27211 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027212{
27213 int sublen;
27214 regmatch_T regmatch;
27215 int i;
27216 int do_all;
27217 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027218 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027219 garray_T ga;
27220 char_u *ret;
27221 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027222 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027223
27224 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27225 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027226 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027227
27228 ga_init2(&ga, 1, 200);
27229
27230 do_all = (flags[0] == 'g');
27231
27232 regmatch.rm_ic = p_ic;
27233 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27234 if (regmatch.regprog != NULL)
27235 {
27236 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027237 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027238 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27239 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027240 /* Skip empty match except for first match. */
27241 if (regmatch.startp[0] == regmatch.endp[0])
27242 {
27243 if (zero_width == regmatch.startp[0])
27244 {
27245 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027246 i = MB_PTR2LEN(tail);
27247 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27248 (size_t)i);
27249 ga.ga_len += i;
27250 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027251 continue;
27252 }
27253 zero_width = regmatch.startp[0];
27254 }
27255
Bram Moolenaar071d4272004-06-13 20:20:40 +000027256 /*
27257 * Get some space for a temporary buffer to do the substitution
27258 * into. It will contain:
27259 * - The text up to where the match is.
27260 * - The substituted text.
27261 * - The text after the match.
27262 */
27263 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027264 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027265 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27266 {
27267 ga_clear(&ga);
27268 break;
27269 }
27270
27271 /* copy the text up to where the match is */
27272 i = (int)(regmatch.startp[0] - tail);
27273 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27274 /* add the substituted text */
27275 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27276 + ga.ga_len + i, TRUE, TRUE, FALSE);
27277 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027278 tail = regmatch.endp[0];
27279 if (*tail == NUL)
27280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027281 if (!do_all)
27282 break;
27283 }
27284
27285 if (ga.ga_data != NULL)
27286 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27287
Bram Moolenaar473de612013-06-08 18:19:48 +020027288 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027289 }
27290
27291 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27292 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027293 if (p_cpo == empty_option)
27294 p_cpo = save_cpo;
27295 else
27296 /* Darn, evaluating {sub} expression changed the value. */
27297 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027298
27299 return ret;
27300}
27301
27302#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */