blob: 779886431ea1eb4c8fff11aba0d3364f109bba52 [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);
Bram Moolenaar79815f12016-07-09 17:07:29 +0200560static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_exepath(typval_T *argvars, typval_T *rettv);
562static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200563#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200565#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100566static void f_expand(typval_T *argvars, typval_T *rettv);
567static void f_extend(typval_T *argvars, typval_T *rettv);
568static void f_feedkeys(typval_T *argvars, typval_T *rettv);
569static void f_filereadable(typval_T *argvars, typval_T *rettv);
570static void f_filewritable(typval_T *argvars, typval_T *rettv);
571static void f_filter(typval_T *argvars, typval_T *rettv);
572static void f_finddir(typval_T *argvars, typval_T *rettv);
573static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000574#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100575static void f_float2nr(typval_T *argvars, typval_T *rettv);
576static void f_floor(typval_T *argvars, typval_T *rettv);
577static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000578#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100579static void f_fnameescape(typval_T *argvars, typval_T *rettv);
580static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
581static void f_foldclosed(typval_T *argvars, typval_T *rettv);
582static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
583static void f_foldlevel(typval_T *argvars, typval_T *rettv);
584static void f_foldtext(typval_T *argvars, typval_T *rettv);
585static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
586static void f_foreground(typval_T *argvars, typval_T *rettv);
587static void f_function(typval_T *argvars, typval_T *rettv);
588static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
589static void f_get(typval_T *argvars, typval_T *rettv);
590static void f_getbufline(typval_T *argvars, typval_T *rettv);
591static void f_getbufvar(typval_T *argvars, typval_T *rettv);
592static void f_getchar(typval_T *argvars, typval_T *rettv);
593static void f_getcharmod(typval_T *argvars, typval_T *rettv);
594static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
595static void f_getcmdline(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200596#if defined(FEAT_CMDL_COMPL)
597static void f_getcompletion(typval_T *argvars, typval_T *rettv);
598#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100599static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
600static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
601static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
602static void f_getcwd(typval_T *argvars, typval_T *rettv);
603static void f_getfontname(typval_T *argvars, typval_T *rettv);
604static void f_getfperm(typval_T *argvars, typval_T *rettv);
605static void f_getfsize(typval_T *argvars, typval_T *rettv);
606static void f_getftime(typval_T *argvars, typval_T *rettv);
607static void f_getftype(typval_T *argvars, typval_T *rettv);
608static void f_getline(typval_T *argvars, typval_T *rettv);
609static void f_getmatches(typval_T *argvars, typval_T *rettv);
610static void f_getpid(typval_T *argvars, typval_T *rettv);
611static void f_getcurpos(typval_T *argvars, typval_T *rettv);
612static void f_getpos(typval_T *argvars, typval_T *rettv);
613static void f_getqflist(typval_T *argvars, typval_T *rettv);
614static void f_getreg(typval_T *argvars, typval_T *rettv);
615static void f_getregtype(typval_T *argvars, typval_T *rettv);
616static void f_gettabvar(typval_T *argvars, typval_T *rettv);
617static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
618static void f_getwinposx(typval_T *argvars, typval_T *rettv);
619static void f_getwinposy(typval_T *argvars, typval_T *rettv);
620static void f_getwinvar(typval_T *argvars, typval_T *rettv);
621static void f_glob(typval_T *argvars, typval_T *rettv);
622static void f_globpath(typval_T *argvars, typval_T *rettv);
623static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
624static void f_has(typval_T *argvars, typval_T *rettv);
625static void f_has_key(typval_T *argvars, typval_T *rettv);
626static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
627static void f_hasmapto(typval_T *argvars, typval_T *rettv);
628static void f_histadd(typval_T *argvars, typval_T *rettv);
629static void f_histdel(typval_T *argvars, typval_T *rettv);
630static void f_histget(typval_T *argvars, typval_T *rettv);
631static void f_histnr(typval_T *argvars, typval_T *rettv);
632static void f_hlID(typval_T *argvars, typval_T *rettv);
633static void f_hlexists(typval_T *argvars, typval_T *rettv);
634static void f_hostname(typval_T *argvars, typval_T *rettv);
635static void f_iconv(typval_T *argvars, typval_T *rettv);
636static void f_indent(typval_T *argvars, typval_T *rettv);
637static void f_index(typval_T *argvars, typval_T *rettv);
638static void f_input(typval_T *argvars, typval_T *rettv);
639static void f_inputdialog(typval_T *argvars, typval_T *rettv);
640static void f_inputlist(typval_T *argvars, typval_T *rettv);
641static void f_inputrestore(typval_T *argvars, typval_T *rettv);
642static void f_inputsave(typval_T *argvars, typval_T *rettv);
643static void f_inputsecret(typval_T *argvars, typval_T *rettv);
644static void f_insert(typval_T *argvars, typval_T *rettv);
645static void f_invert(typval_T *argvars, typval_T *rettv);
646static void f_isdirectory(typval_T *argvars, typval_T *rettv);
647static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100648#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
649static void f_isnan(typval_T *argvars, typval_T *rettv);
650#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100651static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100652#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100653static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100654static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100655static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100656static void f_job_start(typval_T *argvars, typval_T *rettv);
657static void f_job_stop(typval_T *argvars, typval_T *rettv);
658static void f_job_status(typval_T *argvars, typval_T *rettv);
659#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100661static void f_js_decode(typval_T *argvars, typval_T *rettv);
662static void f_js_encode(typval_T *argvars, typval_T *rettv);
663static void f_json_decode(typval_T *argvars, typval_T *rettv);
664static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_keys(typval_T *argvars, typval_T *rettv);
666static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
667static void f_len(typval_T *argvars, typval_T *rettv);
668static void f_libcall(typval_T *argvars, typval_T *rettv);
669static void f_libcallnr(typval_T *argvars, typval_T *rettv);
670static void f_line(typval_T *argvars, typval_T *rettv);
671static void f_line2byte(typval_T *argvars, typval_T *rettv);
672static void f_lispindent(typval_T *argvars, typval_T *rettv);
673static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000674#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_log(typval_T *argvars, typval_T *rettv);
676static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000677#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200678#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_map(typval_T *argvars, typval_T *rettv);
682static void f_maparg(typval_T *argvars, typval_T *rettv);
683static void f_mapcheck(typval_T *argvars, typval_T *rettv);
684static void f_match(typval_T *argvars, typval_T *rettv);
685static void f_matchadd(typval_T *argvars, typval_T *rettv);
686static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
687static void f_matcharg(typval_T *argvars, typval_T *rettv);
688static void f_matchdelete(typval_T *argvars, typval_T *rettv);
689static void f_matchend(typval_T *argvars, typval_T *rettv);
690static void f_matchlist(typval_T *argvars, typval_T *rettv);
691static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200692static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_max(typval_T *argvars, typval_T *rettv);
694static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000695#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000697#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100699#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100701#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
703static void f_nr2char(typval_T *argvars, typval_T *rettv);
704static void f_or(typval_T *argvars, typval_T *rettv);
705static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100706#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100708#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
713static void f_printf(typval_T *argvars, typval_T *rettv);
714static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200715#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200717#endif
718#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_range(typval_T *argvars, typval_T *rettv);
722static void f_readfile(typval_T *argvars, typval_T *rettv);
723static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100724#ifdef FEAT_FLOAT
725static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
726#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_reltimestr(typval_T *argvars, typval_T *rettv);
728static void f_remote_expr(typval_T *argvars, typval_T *rettv);
729static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
730static void f_remote_peek(typval_T *argvars, typval_T *rettv);
731static void f_remote_read(typval_T *argvars, typval_T *rettv);
732static void f_remote_send(typval_T *argvars, typval_T *rettv);
733static void f_remove(typval_T *argvars, typval_T *rettv);
734static void f_rename(typval_T *argvars, typval_T *rettv);
735static void f_repeat(typval_T *argvars, typval_T *rettv);
736static void f_resolve(typval_T *argvars, typval_T *rettv);
737static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000738#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000740#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100741static void f_screenattr(typval_T *argvars, typval_T *rettv);
742static void f_screenchar(typval_T *argvars, typval_T *rettv);
743static void f_screencol(typval_T *argvars, typval_T *rettv);
744static void f_screenrow(typval_T *argvars, typval_T *rettv);
745static void f_search(typval_T *argvars, typval_T *rettv);
746static void f_searchdecl(typval_T *argvars, typval_T *rettv);
747static void f_searchpair(typval_T *argvars, typval_T *rettv);
748static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
749static void f_searchpos(typval_T *argvars, typval_T *rettv);
750static void f_server2client(typval_T *argvars, typval_T *rettv);
751static void f_serverlist(typval_T *argvars, typval_T *rettv);
752static void f_setbufvar(typval_T *argvars, typval_T *rettv);
753static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
754static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100755static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_setline(typval_T *argvars, typval_T *rettv);
757static void f_setloclist(typval_T *argvars, typval_T *rettv);
758static void f_setmatches(typval_T *argvars, typval_T *rettv);
759static void f_setpos(typval_T *argvars, typval_T *rettv);
760static void f_setqflist(typval_T *argvars, typval_T *rettv);
761static void f_setreg(typval_T *argvars, typval_T *rettv);
762static void f_settabvar(typval_T *argvars, typval_T *rettv);
763static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
764static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100765#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100767#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_shellescape(typval_T *argvars, typval_T *rettv);
769static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
770static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000771#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_sin(typval_T *argvars, typval_T *rettv);
773static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000774#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_sort(typval_T *argvars, typval_T *rettv);
776static void f_soundfold(typval_T *argvars, typval_T *rettv);
777static void f_spellbadword(typval_T *argvars, typval_T *rettv);
778static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
779static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000780#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100781static void f_sqrt(typval_T *argvars, typval_T *rettv);
782static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000783#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_str2nr(typval_T *argvars, typval_T *rettv);
785static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000788#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200789static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100790static void f_stridx(typval_T *argvars, typval_T *rettv);
791static void f_string(typval_T *argvars, typval_T *rettv);
792static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200793static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_strpart(typval_T *argvars, typval_T *rettv);
795static void f_strridx(typval_T *argvars, typval_T *rettv);
796static void f_strtrans(typval_T *argvars, typval_T *rettv);
797static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
798static void f_strwidth(typval_T *argvars, typval_T *rettv);
799static void f_submatch(typval_T *argvars, typval_T *rettv);
800static void f_substitute(typval_T *argvars, typval_T *rettv);
801static void f_synID(typval_T *argvars, typval_T *rettv);
802static void f_synIDattr(typval_T *argvars, typval_T *rettv);
803static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
804static void f_synstack(typval_T *argvars, typval_T *rettv);
805static void f_synconcealed(typval_T *argvars, typval_T *rettv);
806static void f_system(typval_T *argvars, typval_T *rettv);
807static void f_systemlist(typval_T *argvars, typval_T *rettv);
808static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
809static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
810static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
811static void f_taglist(typval_T *argvars, typval_T *rettv);
812static void f_tagfiles(typval_T *argvars, typval_T *rettv);
813static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200814static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5c719942016-07-09 23:40:45 +0200815static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200816static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200817static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
818#ifdef FEAT_JOB_CHANNEL
819static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
820#endif
821static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
822#ifdef FEAT_JOB_CHANNEL
823static void f_test_null_job(typval_T *argvars, typval_T *rettv);
824#endif
825static void f_test_null_list(typval_T *argvars, typval_T *rettv);
826static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
827static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200828static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200829#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100830static void f_tan(typval_T *argvars, typval_T *rettv);
831static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200832#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100833#ifdef FEAT_TIMERS
834static void f_timer_start(typval_T *argvars, typval_T *rettv);
835static void f_timer_stop(typval_T *argvars, typval_T *rettv);
836#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static void f_tolower(typval_T *argvars, typval_T *rettv);
838static void f_toupper(typval_T *argvars, typval_T *rettv);
839static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000840#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000842#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100843static void f_type(typval_T *argvars, typval_T *rettv);
844static void f_undofile(typval_T *argvars, typval_T *rettv);
845static void f_undotree(typval_T *argvars, typval_T *rettv);
846static void f_uniq(typval_T *argvars, typval_T *rettv);
847static void f_values(typval_T *argvars, typval_T *rettv);
848static void f_virtcol(typval_T *argvars, typval_T *rettv);
849static void f_visualmode(typval_T *argvars, typval_T *rettv);
850static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100851static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100852static void f_win_getid(typval_T *argvars, typval_T *rettv);
853static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
854static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
855static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100856static void f_winbufnr(typval_T *argvars, typval_T *rettv);
857static void f_wincol(typval_T *argvars, typval_T *rettv);
858static void f_winheight(typval_T *argvars, typval_T *rettv);
859static void f_winline(typval_T *argvars, typval_T *rettv);
860static void f_winnr(typval_T *argvars, typval_T *rettv);
861static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
862static void f_winrestview(typval_T *argvars, typval_T *rettv);
863static void f_winsaveview(typval_T *argvars, typval_T *rettv);
864static void f_winwidth(typval_T *argvars, typval_T *rettv);
865static void f_writefile(typval_T *argvars, typval_T *rettv);
866static void f_wordcount(typval_T *argvars, typval_T *rettv);
867static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000868
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
870static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
871static int get_env_len(char_u **arg);
872static int get_id_len(char_u **arg);
873static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
874static 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 +0000875#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
876#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
877 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
879static int eval_isnamec(int c);
880static int eval_isnamec1(int c);
881static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
882static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883static typval_T *alloc_string_tv(char_u *string);
884static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100885#ifdef FEAT_FLOAT
886static float_T get_tv_float(typval_T *varp);
887#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static linenr_T get_tv_lnum(typval_T *argvars);
889static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100890static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
891static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
892static hashtab_T *find_var_ht(char_u *name, char_u **varname);
893static funccall_T *get_funccal(void);
894static void vars_clear_ext(hashtab_T *ht, int free_val);
895static void delete_var(hashtab_T *ht, hashitem_T *hi);
896static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
897static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
898static void set_var(char_u *name, typval_T *varp, int copy);
899static int var_check_ro(int flags, char_u *name, int use_gettext);
900static int var_check_fixed(int flags, char_u *name, int use_gettext);
901static int var_check_func_name(char_u *name, int new_var);
902static int valid_varname(char_u *varname);
903static int tv_check_lock(int lock, char_u *name, int use_gettext);
904static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
905static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100906static 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 +0100907static int eval_fname_script(char_u *p);
908static int eval_fname_sid(char_u *p);
909static void list_func_head(ufunc_T *fp, int indent);
910static ufunc_T *find_func(char_u *name);
911static int function_exists(char_u *name);
912static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000913#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100914static void func_do_profile(ufunc_T *fp);
915static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
916static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
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_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000922static int
923# ifdef __BORLANDC__
924 _RTLENTRYF
925# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100926 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100928static int script_autoload(char_u *name, int reload);
929static char_u *autoload_name(char_u *name);
930static void cat_func_name(char_u *buf, ufunc_T *fp);
931static void func_free(ufunc_T *fp);
932static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
933static int can_free_funccal(funccall_T *fc, int copyID) ;
934static void free_funccal(funccall_T *fc, int free_val);
935static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
936static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
937static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
938static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
939static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
940static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
941static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
942static int write_list(FILE *fd, list_T *list, int binary);
943static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000944
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200945
946#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100947static int compare_func_name(const void *s1, const void *s2);
948static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200949#endif
950
Bram Moolenaar33570922005-01-25 22:26:29 +0000951/*
952 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000953 */
954 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100955eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 int i;
958 struct vimvar *p;
959
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200960 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
961 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200962 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000963 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000964 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000965
966 for (i = 0; i < VV_LEN; ++i)
967 {
968 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200969 if (STRLEN(p->vv_name) > 16)
970 {
971 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
972 getout(1);
973 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000974 STRCPY(p->vv_di.di_key, p->vv_name);
975 if (p->vv_flags & VV_RO)
976 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
977 else if (p->vv_flags & VV_RO_SBX)
978 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
979 else
980 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000981
982 /* add to v: scope dict, unless the value is not always available */
983 if (p->vv_type != VAR_UNKNOWN)
984 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000986 /* add to compat scope dict */
987 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000988 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100989 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
990
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000991 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100992 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200993 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100994 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100995
996 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
997 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
998 set_vim_var_nr(VV_NONE, VVAL_NONE);
999 set_vim_var_nr(VV_NULL, VVAL_NULL);
1000
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001001 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001002
1003#ifdef EBCDIC
1004 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +01001005 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001006 */
1007 sortFunctions();
1008#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001009}
1010
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001011#if defined(EXITFREE) || defined(PROTO)
1012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001013eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001014{
1015 int i;
1016 struct vimvar *p;
1017
1018 for (i = 0; i < VV_LEN; ++i)
1019 {
1020 p = &vimvars[i];
1021 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001022 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001023 vim_free(p->vv_str);
1024 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001025 }
1026 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1027 {
1028 list_unref(p->vv_list);
1029 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001030 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001031 }
1032 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001033 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001034 hash_clear(&compat_hashtab);
1035
Bram Moolenaard9fba312005-06-26 22:34:35 +00001036 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001037# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001038 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001039# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001040
1041 /* global variables */
1042 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001043
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001044 /* autoloaded script names */
1045 ga_clear_strings(&ga_loaded);
1046
Bram Moolenaarcca74132013-09-25 21:00:28 +02001047 /* Script-local variables. First clear all the variables and in a second
1048 * loop free the scriptvar_T, because a variable in one script might hold
1049 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001050 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001051 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001052 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001053 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001054 ga_clear(&ga_scripts);
1055
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001056 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001057 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001058
1059 /* functions */
1060 free_all_functions();
1061 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001062}
1063#endif
1064
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 * Return the name of the executed function.
1067 */
1068 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001069func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072}
1073
1074/*
1075 * Return the address holding the next breakpoint line for a funccall cookie.
1076 */
1077 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001078func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar33570922005-01-25 22:26:29 +00001080 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081}
1082
1083/*
1084 * Return the address holding the debug tick for a funccall cookie.
1085 */
1086 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001087func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
Bram Moolenaar33570922005-01-25 22:26:29 +00001089 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090}
1091
1092/*
1093 * Return the nesting level for a funccall cookie.
1094 */
1095 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001096func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
Bram Moolenaar33570922005-01-25 22:26:29 +00001098 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099}
1100
1101/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001102funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001104/* pointer to list of previously used funccal, still around because some
1105 * item in it is still being used. */
1106funccall_T *previous_funccal = NULL;
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108/*
1109 * Return TRUE when a function was ended by a ":return" command.
1110 */
1111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001112current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 return current_funccal->returned;
1115}
1116
1117
1118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 * Set an internal variable to a string value. Creates the variable if it does
1120 * not already exist.
1121 */
1122 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001123set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001125 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001126 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
1128 val = vim_strsave(value);
1129 if (val != NULL)
1130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001132 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001134 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137 }
1138}
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001141#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143static char_u *redir_endp = NULL;
1144static char_u *redir_varname = NULL;
1145
1146/*
1147 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001148 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 * Returns OK if successfully completed the setup. FAIL otherwise.
1150 */
1151 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001152var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153{
1154 int save_emsg;
1155 int err;
1156 typval_T tv;
1157
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001159 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 {
1161 EMSG(_(e_invarg));
1162 return FAIL;
1163 }
1164
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 redir_varname = vim_strsave(name);
1167 if (redir_varname == NULL)
1168 return FAIL;
1169
1170 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1171 if (redir_lval == NULL)
1172 {
1173 var_redir_stop();
1174 return FAIL;
1175 }
1176
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177 /* The output is stored in growarray "redir_ga" until redirection ends. */
1178 ga_init2(&redir_ga, (int)sizeof(char), 500);
1179
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001181 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001182 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1184 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001185 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 if (redir_endp != NULL && *redir_endp != NUL)
1187 /* Trailing characters are present after the variable name */
1188 EMSG(_(e_trailing));
1189 else
1190 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001191 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 var_redir_stop();
1193 return FAIL;
1194 }
1195
1196 /* check if we can write to the variable: set it to or append an empty
1197 * string */
1198 save_emsg = did_emsg;
1199 did_emsg = FALSE;
1200 tv.v_type = VAR_STRING;
1201 tv.vval.v_string = (char_u *)"";
1202 if (append)
1203 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1204 else
1205 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001206 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001208 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209 if (err)
1210 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001211 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212 var_redir_stop();
1213 return FAIL;
1214 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001215
1216 return OK;
1217}
1218
1219/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001220 * Append "value[value_len]" to the variable set by var_redir_start().
1221 * The actual appending is postponed until redirection ends, because the value
1222 * appended may in fact be the string we write to, changing it may cause freed
1223 * memory to be used:
1224 * :redir => foo
1225 * :let foo
1226 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 */
1228 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001229var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001230{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001231 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001232
1233 if (redir_lval == NULL)
1234 return;
1235
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001236 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001237 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001238 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001239 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001240
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001241 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001242 {
1243 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001244 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001245 }
1246 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001247 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001248}
1249
1250/*
1251 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001252 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001253 */
1254 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001255var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001256{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001257 typval_T tv;
1258
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001259 if (EVALCMD_BUSY)
1260 {
1261 redir_lval = NULL;
1262 return;
1263 }
1264
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001265 if (redir_lval != NULL)
1266 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001267 /* If there was no error: assign the text to the variable. */
1268 if (redir_endp != NULL)
1269 {
1270 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1271 tv.v_type = VAR_STRING;
1272 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001273 /* Call get_lval() again, if it's inside a Dict or List it may
1274 * have changed. */
1275 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001276 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001277 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1278 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1279 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001280 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001281
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001282 /* free the collected output */
1283 vim_free(redir_ga.ga_data);
1284 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001285
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001286 vim_free(redir_lval);
1287 redir_lval = NULL;
1288 }
1289 vim_free(redir_varname);
1290 redir_varname = NULL;
1291}
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293# if defined(FEAT_MBYTE) || defined(PROTO)
1294 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001295eval_charconvert(
1296 char_u *enc_from,
1297 char_u *enc_to,
1298 char_u *fname_from,
1299 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int err = FALSE;
1302
1303 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1304 set_vim_var_string(VV_CC_TO, enc_to, -1);
1305 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1306 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1307 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1308 err = TRUE;
1309 set_vim_var_string(VV_CC_FROM, NULL, -1);
1310 set_vim_var_string(VV_CC_TO, NULL, -1);
1311 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1312 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1313
1314 if (err)
1315 return FAIL;
1316 return OK;
1317}
1318# endif
1319
1320# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1321 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001322eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
1324 int err = FALSE;
1325
1326 set_vim_var_string(VV_FNAME_IN, fname, -1);
1327 set_vim_var_string(VV_CMDARG, args, -1);
1328 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1329 err = TRUE;
1330 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1331 set_vim_var_string(VV_CMDARG, NULL, -1);
1332
1333 if (err)
1334 {
1335 mch_remove(fname);
1336 return FAIL;
1337 }
1338 return OK;
1339}
1340# endif
1341
1342# if defined(FEAT_DIFF) || defined(PROTO)
1343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001344eval_diff(
1345 char_u *origfile,
1346 char_u *newfile,
1347 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int err = FALSE;
1350
1351 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1352 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1353 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1354 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1355 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1356 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1357 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1358}
1359
1360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001361eval_patch(
1362 char_u *origfile,
1363 char_u *difffile,
1364 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 int err;
1367
1368 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1369 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1370 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1371 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1372 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1373 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1374 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1375}
1376# endif
1377
1378/*
1379 * Top level evaluation function, returning a boolean.
1380 * Sets "error" to TRUE if there was an error.
1381 * Return TRUE or FALSE.
1382 */
1383 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001384eval_to_bool(
1385 char_u *arg,
1386 int *error,
1387 char_u **nextcmd,
1388 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
Bram Moolenaar33570922005-01-25 22:26:29 +00001390 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001391 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
1393 if (skip)
1394 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 else
1398 {
1399 *error = FALSE;
1400 if (!skip)
1401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001402 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405 }
1406 if (skip)
1407 --emsg_skip;
1408
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001409 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410}
1411
1412/*
1413 * Top level evaluation function, returning a string. If "skip" is TRUE,
1414 * only parsing to "nextcmd" is done, without reporting errors. Return
1415 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1416 */
1417 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001418eval_to_string_skip(
1419 char_u *arg,
1420 char_u **nextcmd,
1421 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
Bram Moolenaar33570922005-01-25 22:26:29 +00001423 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 char_u *retval;
1425
1426 if (skip)
1427 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 retval = NULL;
1430 else
1431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 retval = vim_strsave(get_tv_string(&tv));
1433 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435 if (skip)
1436 --emsg_skip;
1437
1438 return retval;
1439}
1440
1441/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001442 * Skip over an expression at "*pp".
1443 * Return FAIL for an error, OK otherwise.
1444 */
1445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001446skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001447{
Bram Moolenaar33570922005-01-25 22:26:29 +00001448 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001449
1450 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001451 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001452}
1453
1454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001456 * When "convert" is TRUE convert a List into a sequence of lines and convert
1457 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 * Return pointer to allocated memory, or NULL for failure.
1459 */
1460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001461eval_to_string(
1462 char_u *arg,
1463 char_u **nextcmd,
1464 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
Bram Moolenaar33570922005-01-25 22:26:29 +00001466 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001468 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001469#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001470 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001473 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 retval = NULL;
1475 else
1476 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001477 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001478 {
1479 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001480 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001481 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001482 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001483 if (tv.vval.v_list->lv_len > 0)
1484 ga_append(&ga, NL);
1485 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001486 ga_append(&ga, NUL);
1487 retval = (char_u *)ga.ga_data;
1488 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001489#ifdef FEAT_FLOAT
1490 else if (convert && tv.v_type == VAR_FLOAT)
1491 {
1492 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1493 retval = vim_strsave(numbuf);
1494 }
1495#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001496 else
1497 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001498 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500
1501 return retval;
1502}
1503
1504/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001505 * Call eval_to_string() without using current local variables and using
1506 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 */
1508 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509eval_to_string_safe(
1510 char_u *arg,
1511 char_u **nextcmd,
1512 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 char_u *retval;
1515 void *save_funccalp;
1516
1517 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001518 if (use_sandbox)
1519 ++sandbox;
1520 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001521 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001522 if (use_sandbox)
1523 --sandbox;
1524 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 restore_funccal(save_funccalp);
1526 return retval;
1527}
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529/*
1530 * Top level evaluation function, returning a number.
1531 * Evaluates "expr" silently.
1532 * Returns -1 for an error.
1533 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001534 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
Bram Moolenaar33570922005-01-25 22:26:29 +00001537 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001538 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001539 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541 ++emsg_off;
1542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001543 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 retval = -1;
1545 else
1546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001547 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550 --emsg_off;
1551
1552 return retval;
1553}
1554
Bram Moolenaara40058a2005-07-11 22:42:07 +00001555/*
1556 * Prepare v: variable "idx" to be used.
1557 * Save the current typeval in "save_tv".
1558 * When not used yet add the variable to the v: hashtable.
1559 */
1560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001562{
1563 *save_tv = vimvars[idx].vv_tv;
1564 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1565 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1566}
1567
1568/*
1569 * Restore v: variable "idx" to typeval "save_tv".
1570 * When no longer defined, remove the variable from the v: hashtable.
1571 */
1572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001573restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001574{
1575 hashitem_T *hi;
1576
Bram Moolenaara40058a2005-07-11 22:42:07 +00001577 vimvars[idx].vv_tv = *save_tv;
1578 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1579 {
1580 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1581 if (HASHITEM_EMPTY(hi))
1582 EMSG2(_(e_intern2), "restore_vimvar()");
1583 else
1584 hash_remove(&vimvarht, hi);
1585 }
1586}
1587
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001588#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001589/*
1590 * Evaluate an expression to a list with suggestions.
1591 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001592 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001593 */
1594 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001595eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001596{
1597 typval_T save_val;
1598 typval_T rettv;
1599 list_T *list = NULL;
1600 char_u *p = skipwhite(expr);
1601
1602 /* Set "v:val" to the bad word. */
1603 prepare_vimvar(VV_VAL, &save_val);
1604 vimvars[VV_VAL].vv_type = VAR_STRING;
1605 vimvars[VV_VAL].vv_str = badword;
1606 if (p_verbose == 0)
1607 ++emsg_off;
1608
1609 if (eval1(&p, &rettv, TRUE) == OK)
1610 {
1611 if (rettv.v_type != VAR_LIST)
1612 clear_tv(&rettv);
1613 else
1614 list = rettv.vval.v_list;
1615 }
1616
1617 if (p_verbose == 0)
1618 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001619 restore_vimvar(VV_VAL, &save_val);
1620
1621 return list;
1622}
1623
1624/*
1625 * "list" is supposed to contain two items: a word and a number. Return the
1626 * word in "pp" and the number as the return value.
1627 * Return -1 if anything isn't right.
1628 * Used to get the good word and score from the eval_spell_expr() result.
1629 */
1630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001631get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001632{
1633 listitem_T *li;
1634
1635 li = list->lv_first;
1636 if (li == NULL)
1637 return -1;
1638 *pp = get_tv_string(&li->li_tv);
1639
1640 li = li->li_next;
1641 if (li == NULL)
1642 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001643 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001644}
1645#endif
1646
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001647/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001648 * Top level evaluation function.
1649 * Returns an allocated typval_T with the result.
1650 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001651 */
1652 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001653eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001654{
1655 typval_T *tv;
1656
1657 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001658 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001659 {
1660 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001661 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001662 }
1663
1664 return tv;
1665}
1666
1667
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001670 * Uses argv[argc] for the function arguments. Only Number and String
1671 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001675call_vim_function(
1676 char_u *func,
1677 int argc,
1678 char_u **argv,
1679 int safe, /* use the sandbox */
1680 int str_arg_only, /* all arguments are strings */
1681 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
Bram Moolenaar33570922005-01-25 22:26:29 +00001683 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001684 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 int len;
1686 int i;
1687 int doesrange;
1688 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001691 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
1695 for (i = 0; i < argc; i++)
1696 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001697 /* Pass a NULL or empty argument as an empty string */
1698 if (argv[i] == NULL || *argv[i] == NUL)
1699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001700 argvars[i].v_type = VAR_STRING;
1701 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001702 continue;
1703 }
1704
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001705 if (str_arg_only)
1706 len = 0;
1707 else
1708 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001709 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 if (len != 0 && len == (int)STRLEN(argv[i]))
1711 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001712 argvars[i].v_type = VAR_NUMBER;
1713 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 else
1716 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001717 argvars[i].v_type = VAR_STRING;
1718 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 }
1721
1722 if (safe)
1723 {
1724 save_funccalp = save_funccal();
1725 ++sandbox;
1726 }
1727
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1729 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001731 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (safe)
1733 {
1734 --sandbox;
1735 restore_funccal(save_funccalp);
1736 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737 vim_free(argvars);
1738
1739 if (ret == FAIL)
1740 clear_tv(rettv);
1741
1742 return ret;
1743}
1744
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001745/*
1746 * Call vimL function "func" and return the result as a number.
1747 * Returns -1 when calling the function fails.
1748 * Uses argv[argc] for the function arguments.
1749 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001750 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001751call_func_retnr(
1752 char_u *func,
1753 int argc,
1754 char_u **argv,
1755 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001756{
1757 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001758 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001759
1760 /* All arguments are passed as strings, no conversion to number. */
1761 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1762 return -1;
1763
1764 retval = get_tv_number_chk(&rettv, NULL);
1765 clear_tv(&rettv);
1766 return retval;
1767}
1768
1769#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1770 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1771
Bram Moolenaar4f688582007-07-24 12:34:30 +00001772# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001774 * Call vimL function "func" and return the result as a string.
1775 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 * Uses argv[argc] for the function arguments.
1777 */
1778 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779call_func_retstr(
1780 char_u *func,
1781 int argc,
1782 char_u **argv,
1783 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001784{
1785 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001786 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001787
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001788 /* All arguments are passed as strings, no conversion to number. */
1789 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001790 return NULL;
1791
1792 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001793 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 return retval;
1795}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001796# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001797
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001798/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001799 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001800 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001801 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001802 */
1803 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001804call_func_retlist(
1805 char_u *func,
1806 int argc,
1807 char_u **argv,
1808 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001809{
1810 typval_T rettv;
1811
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001812 /* All arguments are passed as strings, no conversion to number. */
1813 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001814 return NULL;
1815
1816 if (rettv.v_type != VAR_LIST)
1817 {
1818 clear_tv(&rettv);
1819 return NULL;
1820 }
1821
1822 return rettv.vval.v_list;
1823}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#endif
1825
1826/*
1827 * Save the current function call pointer, and set it to NULL.
1828 * Used when executing autocommands and for ":source".
1829 */
1830 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001831save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001833 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 current_funccal = NULL;
1836 return (void *)fc;
1837}
1838
1839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001842 funccall_T *fc = (funccall_T *)vfc;
1843
1844 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845}
1846
Bram Moolenaar05159a02005-02-26 23:04:13 +00001847#if defined(FEAT_PROFILE) || defined(PROTO)
1848/*
1849 * Prepare profiling for entering a child or something else that is not
1850 * counted for the script/function itself.
1851 * Should always be called in pair with prof_child_exit().
1852 */
1853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001854prof_child_enter(
1855 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001856{
1857 funccall_T *fc = current_funccal;
1858
1859 if (fc != NULL && fc->func->uf_profiling)
1860 profile_start(&fc->prof_child);
1861 script_prof_save(tm);
1862}
1863
1864/*
1865 * Take care of time spent in a child.
1866 * Should always be called after prof_child_enter().
1867 */
1868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001869prof_child_exit(
1870 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001871{
1872 funccall_T *fc = current_funccal;
1873
1874 if (fc != NULL && fc->func->uf_profiling)
1875 {
1876 profile_end(&fc->prof_child);
1877 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1878 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1879 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1880 }
1881 script_prof_restore(tm);
1882}
1883#endif
1884
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886#ifdef FEAT_FOLDING
1887/*
1888 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1889 * it in "*cp". Doesn't give error messages.
1890 */
1891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001895 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001897 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1898 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001901 if (use_sandbox)
1902 ++sandbox;
1903 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 retval = 0;
1907 else
1908 {
1909 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 if (tv.v_type == VAR_NUMBER)
1911 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001912 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 retval = 0;
1914 else
1915 {
1916 /* If the result is a string, check if there is a non-digit before
1917 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (!VIM_ISDIGIT(*s) && *s != '-')
1920 *cp = *s++;
1921 retval = atol((char *)s);
1922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001926 if (use_sandbox)
1927 --sandbox;
1928 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001930 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931}
1932#endif
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 * ":let" list all variable values
1936 * ":let var1 var2" list variable values
1937 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 * ":let var += expr" assignment command.
1939 * ":let var -= expr" assignment command.
1940 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 */
1943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001944ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
1946 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 int var_count = 0;
1951 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001953 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001954 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
Bram Moolenaardb552d602006-03-23 22:59:57 +00001956 argend = skip_var_list(arg, &var_count, &semicolon);
1957 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001959 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1960 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001961 expr = skipwhite(argend);
1962 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1963 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001965 /*
1966 * ":let" without "=": list variables
1967 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 if (*arg == '[')
1969 EMSG(_(e_invarg));
1970 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001972 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001976 list_glob_vars(&first);
1977 list_buf_vars(&first);
1978 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001979#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001980 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001981#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001982 list_script_vars(&first);
1983 list_func_vars(&first);
1984 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 eap->nextcmd = check_nextcmd(arg);
1987 }
1988 else
1989 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 op[0] = '=';
1991 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001992 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001994 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1995 op[0] = *expr; /* +=, -= or .= */
1996 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001998 else
1999 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (eap->skip)
2002 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (eap->skip)
2005 {
2006 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 --emsg_skip;
2009 }
2010 else if (i != FAIL)
2011 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002013 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 }
2017}
2018
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019/*
2020 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2021 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 * When "nextchars" is not NULL it points to a string with characters that
2023 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2024 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 * Returns OK or FAIL;
2026 */
2027 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002028ex_let_vars(
2029 char_u *arg_start,
2030 typval_T *tv,
2031 int copy, /* copy values from "tv", don't move */
2032 int semicolon, /* from skip_var_list() */
2033 int var_count, /* from skip_var_list() */
2034 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035{
2036 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002037 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002039 listitem_T *item;
2040 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041
2042 if (*arg != '[')
2043 {
2044 /*
2045 * ":let var = expr" or ":for var in list"
2046 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 return FAIL;
2049 return OK;
2050 }
2051
2052 /*
2053 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2054 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002055 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 {
2057 EMSG(_(e_listreq));
2058 return FAIL;
2059 }
2060
2061 i = list_len(l);
2062 if (semicolon == 0 && var_count < i)
2063 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002064 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065 return FAIL;
2066 }
2067 if (var_count - semicolon > i)
2068 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002069 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 return FAIL;
2071 }
2072
2073 item = l->lv_first;
2074 while (*arg != ']')
2075 {
2076 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002077 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078 item = item->li_next;
2079 if (arg == NULL)
2080 return FAIL;
2081
2082 arg = skipwhite(arg);
2083 if (*arg == ';')
2084 {
2085 /* Put the rest of the list (may be empty) in the var after ';'.
2086 * Create a new list for this. */
2087 l = list_alloc();
2088 if (l == NULL)
2089 return FAIL;
2090 while (item != NULL)
2091 {
2092 list_append_tv(l, &item->li_tv);
2093 item = item->li_next;
2094 }
2095
2096 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002097 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002098 ltv.vval.v_list = l;
2099 l->lv_refcount = 1;
2100
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2102 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002103 clear_tv(&ltv);
2104 if (arg == NULL)
2105 return FAIL;
2106 break;
2107 }
2108 else if (*arg != ',' && *arg != ']')
2109 {
2110 EMSG2(_(e_intern2), "ex_let_vars()");
2111 return FAIL;
2112 }
2113 }
2114
2115 return OK;
2116}
2117
2118/*
2119 * Skip over assignable variable "var" or list of variables "[var, var]".
2120 * Used for ":let varvar = expr" and ":for varvar in expr".
2121 * For "[var, var]" increment "*var_count" for each variable.
2122 * for "[var, var; var]" set "semicolon".
2123 * Return NULL for an error.
2124 */
2125 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002126skip_var_list(
2127 char_u *arg,
2128 int *var_count,
2129 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130{
2131 char_u *p, *s;
2132
2133 if (*arg == '[')
2134 {
2135 /* "[var, var]": find the matching ']'. */
2136 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002137 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002138 {
2139 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2140 s = skip_var_one(p);
2141 if (s == p)
2142 {
2143 EMSG2(_(e_invarg2), p);
2144 return NULL;
2145 }
2146 ++*var_count;
2147
2148 p = skipwhite(s);
2149 if (*p == ']')
2150 break;
2151 else if (*p == ';')
2152 {
2153 if (*semicolon == 1)
2154 {
2155 EMSG(_("Double ; in list of variables"));
2156 return NULL;
2157 }
2158 *semicolon = 1;
2159 }
2160 else if (*p != ',')
2161 {
2162 EMSG2(_(e_invarg2), p);
2163 return NULL;
2164 }
2165 }
2166 return p + 1;
2167 }
2168 else
2169 return skip_var_one(arg);
2170}
2171
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002173 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002174 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002176 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002178{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002179 if (*arg == '@' && arg[1] != NUL)
2180 return arg + 2;
2181 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2182 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002183}
2184
Bram Moolenaara7043832005-01-21 11:56:39 +00002185/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002186 * List variables for hashtab "ht" with prefix "prefix".
2187 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002188 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190list_hashtable_vars(
2191 hashtab_T *ht,
2192 char_u *prefix,
2193 int empty,
2194 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002195{
Bram Moolenaar33570922005-01-25 22:26:29 +00002196 hashitem_T *hi;
2197 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002198 int todo;
2199
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002200 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002201 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2202 {
2203 if (!HASHITEM_EMPTY(hi))
2204 {
2205 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002206 di = HI2DI(hi);
2207 if (empty || di->di_tv.v_type != VAR_STRING
2208 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002210 }
2211 }
2212}
2213
2214/*
2215 * List global variables.
2216 */
2217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002219{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002221}
2222
2223/*
2224 * List buffer variables.
2225 */
2226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002228{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 char_u numbuf[NUMBUFLEN];
2230
Bram Moolenaar429fa852013-04-15 12:27:36 +02002231 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002232 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233
2234 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2236 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002237}
2238
2239/*
2240 * List window variables.
2241 */
2242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002243list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002245 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002247}
2248
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#ifdef FEAT_WINDOWS
2250/*
2251 * List tab page variables.
2252 */
2253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002254list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002256 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002258}
2259#endif
2260
Bram Moolenaara7043832005-01-21 11:56:39 +00002261/*
2262 * List Vim variables.
2263 */
2264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002265list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268}
2269
2270/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002271 * List script-local variables, if there is a script.
2272 */
2273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002274list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002275{
2276 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2278 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002279}
2280
2281/*
2282 * List function variables, if there is a function.
2283 */
2284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002285list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002286{
2287 if (current_funccal != NULL)
2288 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002290}
2291
2292/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 * List variables in "arg".
2294 */
2295 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002296list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297{
2298 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 char_u *name_start;
2302 char_u *arg_subsc;
2303 char_u *tofree;
2304 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305
2306 while (!ends_excmd(*arg) && !got_int)
2307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002310 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2312 {
2313 emsg_severe = TRUE;
2314 EMSG(_(e_trailing));
2315 break;
2316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320 /* get_name_len() takes care of expanding curly braces */
2321 name_start = name = arg;
2322 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2323 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 /* This is mainly to keep test 49 working: when expanding
2326 * curly braces fails overrule the exception error message. */
2327 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329 emsg_severe = TRUE;
2330 EMSG2(_(e_invarg2), arg);
2331 break;
2332 }
2333 error = TRUE;
2334 }
2335 else
2336 {
2337 if (tofree != NULL)
2338 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002339 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 else
2342 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002343 /* handle d.key, l[idx], f(expr) */
2344 arg_subsc = arg;
2345 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002346 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002349 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002350 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002351 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002352 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002353 case 'g': list_glob_vars(first); break;
2354 case 'b': list_buf_vars(first); break;
2355 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002356#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002357 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002358#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002359 case 'v': list_vim_vars(first); break;
2360 case 's': list_script_vars(first); break;
2361 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002362 default:
2363 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002364 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002365 }
2366 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002367 {
2368 char_u numbuf[NUMBUFLEN];
2369 char_u *tf;
2370 int c;
2371 char_u *s;
2372
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002373 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002374 c = *arg;
2375 *arg = NUL;
2376 list_one_var_a((char_u *)"",
2377 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002378 tv.v_type,
2379 s == NULL ? (char_u *)"" : s,
2380 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002381 *arg = c;
2382 vim_free(tf);
2383 }
2384 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 }
2387 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002388
2389 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002391
2392 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 }
2394
2395 return arg;
2396}
2397
2398/*
2399 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2400 * Returns a pointer to the char just after the var name.
2401 * Returns NULL if there is an error.
2402 */
2403 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002404ex_let_one(
2405 char_u *arg, /* points to variable name */
2406 typval_T *tv, /* value to assign to variable */
2407 int copy, /* copy value from "tv" */
2408 char_u *endchars, /* valid chars after variable name or NULL */
2409 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410{
2411 int c1;
2412 char_u *name;
2413 char_u *p;
2414 char_u *arg_end = NULL;
2415 int len;
2416 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418
2419 /*
2420 * ":let $VAR = expr": Set environment variable.
2421 */
2422 if (*arg == '$')
2423 {
2424 /* Find the end of the name. */
2425 ++arg;
2426 name = arg;
2427 len = get_env_len(&arg);
2428 if (len == 0)
2429 EMSG2(_(e_invarg2), name - 1);
2430 else
2431 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 if (op != NULL && (*op == '+' || *op == '-'))
2433 EMSG2(_(e_letwrong), op);
2434 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002437 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 {
2439 c1 = name[len];
2440 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 p = get_tv_string_chk(tv);
2442 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 int mustfree = FALSE;
2445 char_u *s = vim_getenv(name, &mustfree);
2446
2447 if (s != NULL)
2448 {
2449 p = tofree = concat_str(s, p);
2450 if (mustfree)
2451 vim_free(s);
2452 }
2453 }
2454 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 if (STRICMP(name, "HOME") == 0)
2458 init_homedir();
2459 else if (didset_vim && STRICMP(name, "VIM") == 0)
2460 didset_vim = FALSE;
2461 else if (didset_vimruntime
2462 && STRICMP(name, "VIMRUNTIME") == 0)
2463 didset_vimruntime = FALSE;
2464 arg_end = arg;
2465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469 }
2470 }
2471
2472 /*
2473 * ":let &option = expr": Set option value.
2474 * ":let &l:option = expr": Set local option value.
2475 * ":let &g:option = expr": Set global option value.
2476 */
2477 else if (*arg == '&')
2478 {
2479 /* Find the end of the name. */
2480 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002481 if (p == NULL || (endchars != NULL
2482 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 EMSG(_(e_letunexp));
2484 else
2485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 long n;
2487 int opt_type;
2488 long numval;
2489 char_u *stringval = NULL;
2490 char_u *s;
2491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 c1 = *p;
2493 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002495 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 s = get_tv_string_chk(tv); /* != NULL if number or string */
2497 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 {
2499 opt_type = get_option_value(arg, &numval,
2500 &stringval, opt_flags);
2501 if ((opt_type == 1 && *op == '.')
2502 || (opt_type == 0 && *op != '.'))
2503 EMSG2(_(e_letwrong), op);
2504 else
2505 {
2506 if (opt_type == 1) /* number */
2507 {
2508 if (*op == '+')
2509 n = numval + n;
2510 else
2511 n = numval - n;
2512 }
2513 else if (opt_type == 0 && stringval != NULL) /* string */
2514 {
2515 s = concat_str(stringval, s);
2516 vim_free(stringval);
2517 stringval = s;
2518 }
2519 }
2520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 if (s != NULL)
2522 {
2523 set_option_value(arg, n, s, opt_flags);
2524 arg_end = p;
2525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002527 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 }
2529 }
2530
2531 /*
2532 * ":let @r = expr": Set register contents.
2533 */
2534 else if (*arg == '@')
2535 {
2536 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 if (op != NULL && (*op == '+' || *op == '-'))
2538 EMSG2(_(e_letwrong), op);
2539 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002540 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 EMSG(_(e_letunexp));
2542 else
2543 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002544 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 char_u *s;
2546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002547 p = get_tv_string_chk(tv);
2548 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002549 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002550 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002551 if (s != NULL)
2552 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002553 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002554 vim_free(s);
2555 }
2556 }
2557 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002558 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002559 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002560 arg_end = arg + 1;
2561 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002562 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 }
2564 }
2565
2566 /*
2567 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002570 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002574 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2578 EMSG(_(e_letunexp));
2579 else
2580 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 arg_end = p;
2583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 }
2587
2588 else
2589 EMSG2(_(e_invarg2), arg);
2590
2591 return arg_end;
2592}
2593
2594/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2596 */
2597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002598check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599{
2600 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2601 {
2602 EMSG2(_(e_readonlyvar), arg);
2603 return TRUE;
2604 }
2605 return FALSE;
2606}
2607
2608/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 * Get an lval: variable, Dict item or List item that can be assigned a value
2610 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2611 * "name.key", "name.key[expr]" etc.
2612 * Indexing only works if "name" is an existing List or Dictionary.
2613 * "name" points to the start of the name.
2614 * If "rettv" is not NULL it points to the value to be assigned.
2615 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2616 * wrong; must end in space or cmd separator.
2617 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002618 * flags:
2619 * GLV_QUIET: do not give error messages
2620 * GLV_NO_AUTOLOAD: do not use script autoloading
2621 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002623 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002625 */
2626 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002627get_lval(
2628 char_u *name,
2629 typval_T *rettv,
2630 lval_T *lp,
2631 int unlet,
2632 int skip,
2633 int flags, /* GLV_ values */
2634 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 char_u *expr_start, *expr_end;
2638 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 dictitem_T *v;
2640 typval_T var1;
2641 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002647 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002650 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651
2652 if (skip)
2653 {
2654 /* When skipping just find the end of the name. */
2655 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002656 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 }
2658
2659 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002660 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (expr_start != NULL)
2662 {
2663 /* Don't expand the name when we already know there is an error. */
2664 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2665 && *p != '[' && *p != '.')
2666 {
2667 EMSG(_(e_trailing));
2668 return NULL;
2669 }
2670
2671 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2672 if (lp->ll_exp_name == NULL)
2673 {
2674 /* Report an invalid expression in braces, unless the
2675 * expression evaluation has been cancelled due to an
2676 * aborting error, an interrupt, or an exception. */
2677 if (!aborting() && !quiet)
2678 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002679 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 EMSG2(_(e_invarg2), name);
2681 return NULL;
2682 }
2683 }
2684 lp->ll_name = lp->ll_exp_name;
2685 }
2686 else
2687 lp->ll_name = name;
2688
2689 /* Without [idx] or .key we are done. */
2690 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2691 return p;
2692
2693 cc = *p;
2694 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002695 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (v == NULL && !quiet)
2697 EMSG2(_(e_undefvar), lp->ll_name);
2698 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 if (v == NULL)
2700 return NULL;
2701
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 /*
2703 * Loop until no more [idx] or .key is following.
2704 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002705 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2709 && !(lp->ll_tv->v_type == VAR_DICT
2710 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
2713 EMSG(_("E689: Can only index a List or Dictionary"));
2714 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002715 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_("E708: [:] must come last"));
2720 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002721 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 len = -1;
2724 if (*p == '.')
2725 {
2726 key = p + 1;
2727 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2728 ;
2729 if (len == 0)
2730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
2732 EMSG(_(e_emptykey));
2733 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
2735 p = key + len;
2736 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 else
2738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002740 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 if (*p == ':')
2742 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002743 else
2744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 empty1 = FALSE;
2746 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002748 if (get_tv_string_chk(&var1) == NULL)
2749 {
2750 /* not a number or string */
2751 clear_tv(&var1);
2752 return NULL;
2753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 }
2755
2756 /* Optionally get the second index [ :expr]. */
2757 if (*p == ':')
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002762 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002763 if (!empty1)
2764 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002766 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (rettv != NULL && (rettv->v_type != VAR_LIST
2768 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 if (!quiet)
2771 EMSG(_("E709: [:] requires a List value"));
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 }
2776 p = skipwhite(p + 1);
2777 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 else
2780 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 if (!empty1)
2785 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002788 if (get_tv_string_chk(&var2) == NULL)
2789 {
2790 /* not a number or string */
2791 if (!empty1)
2792 clear_tv(&var1);
2793 clear_tv(&var2);
2794 return NULL;
2795 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002801
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (!quiet)
2805 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 if (!empty1)
2807 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812
2813 /* Skip to past ']'. */
2814 ++p;
2815 }
2816
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
2819 if (len == -1)
2820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002822 key = get_tv_string_chk(&var1); /* is number or string */
2823 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 }
2828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 lp->ll_list = NULL;
2830 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002831 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002832
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002833 /* When assigning to a scope dictionary check that a function and
2834 * variable name is valid (only variable name unless it is l: or
2835 * g: dictionary). Disallow overwriting a builtin function. */
2836 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002837 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002838 int prevval;
2839 int wrong;
2840
2841 if (len != -1)
2842 {
2843 prevval = key[len];
2844 key[len] = NUL;
2845 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002846 else
2847 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002848 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2849 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002850 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002851 || !valid_varname(key);
2852 if (len != -1)
2853 key[len] = prevval;
2854 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002855 return NULL;
2856 }
2857
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002860 /* Can't add "v:" variable. */
2861 if (lp->ll_dict == &vimvardict)
2862 {
2863 EMSG2(_(e_illvar), name);
2864 return NULL;
2865 }
2866
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002867 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 if (len == -1)
2873 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 }
2876 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 if (len == -1)
2881 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 p = NULL;
2884 break;
2885 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002886 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002887 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002888 return NULL;
2889
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 if (len == -1)
2891 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 }
2894 else
2895 {
2896 /*
2897 * Get the number and item for the only or first index of the List.
2898 */
2899 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 else
2902 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002903 lp->ll_n1 = (long)get_tv_number(&var1);
2904 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 clear_tv(&var1);
2906 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 lp->ll_list = lp->ll_tv->vval.v_list;
2909 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2910 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002911 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002912 if (lp->ll_n1 < 0)
2913 {
2914 lp->ll_n1 = 0;
2915 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2916 }
2917 }
2918 if (lp->ll_li == NULL)
2919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002922 if (!quiet)
2923 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002925 }
2926
2927 /*
2928 * May need to find the item or absolute index for the second
2929 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 * When no index given: "lp->ll_empty2" is TRUE.
2931 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002932 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002934 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002935 lp->ll_n2 = (long)get_tv_number(&var2);
2936 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002937 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002941 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002942 {
2943 if (!quiet)
2944 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002946 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002948 }
2949
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2951 if (lp->ll_n1 < 0)
2952 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2953 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002954 {
2955 if (!quiet)
2956 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002958 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002959 }
2960
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002962 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002963 }
2964
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 return p;
2966}
2967
2968/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002969 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 */
2971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002972clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973{
2974 vim_free(lp->ll_exp_name);
2975 vim_free(lp->ll_newkey);
2976}
2977
2978/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002979 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 */
2983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002984set_var_lval(
2985 lval_T *lp,
2986 char_u *endp,
2987 typval_T *rettv,
2988 int copy,
2989 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990{
2991 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002992 listitem_T *ri;
2993 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994
2995 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 cc = *endp;
3000 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 if (op != NULL && *op != '=')
3002 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003004
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003005 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003006 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003007 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003008 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003010 if ((di == NULL
3011 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
3012 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
3013 FALSE)))
3014 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 set_var(lp->ll_name, &tv, FALSE);
3016 clear_tv(&tv);
3017 }
3018 }
3019 else
3020 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003022 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003024 else if (tv_check_lock(lp->ll_newkey == NULL
3025 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003026 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003027 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 else if (lp->ll_range)
3029 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003030 listitem_T *ll_li = lp->ll_li;
3031 int ll_n1 = lp->ll_n1;
3032
3033 /*
3034 * Check whether any of the list items is locked
3035 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003036 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003037 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003038 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003039 return;
3040 ri = ri->li_next;
3041 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3042 break;
3043 ll_li = ll_li->li_next;
3044 ++ll_n1;
3045 }
3046
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 /*
3048 * Assign the List values to the list items.
3049 */
3050 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 if (op != NULL && *op != '=')
3053 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3054 else
3055 {
3056 clear_tv(&lp->ll_li->li_tv);
3057 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3058 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 ri = ri->li_next;
3060 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3061 break;
3062 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003063 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003065 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003066 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 ri = NULL;
3068 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003069 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003070 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003071 lp->ll_li = lp->ll_li->li_next;
3072 ++lp->ll_n1;
3073 }
3074 if (ri != NULL)
3075 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 else if (lp->ll_empty2
3077 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003078 : lp->ll_n1 != lp->ll_n2)
3079 EMSG(_("E711: List value has not enough items"));
3080 }
3081 else
3082 {
3083 /*
3084 * Assign to a List or Dictionary item.
3085 */
3086 if (lp->ll_newkey != NULL)
3087 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 if (op != NULL && *op != '=')
3089 {
3090 EMSG2(_(e_letwrong), op);
3091 return;
3092 }
3093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003095 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003096 if (di == NULL)
3097 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003098 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3099 {
3100 vim_free(di);
3101 return;
3102 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003103 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003104 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 else if (op != NULL && *op != '=')
3106 {
3107 tv_op(lp->ll_tv, rettv, op);
3108 return;
3109 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003110 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003111 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003112
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003113 /*
3114 * Assign the value to the variable or list item.
3115 */
3116 if (copy)
3117 copy_tv(rettv, lp->ll_tv);
3118 else
3119 {
3120 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003121 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003122 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003123 }
3124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003125}
3126
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3129 * Returns OK or FAIL.
3130 */
3131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003132tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003134 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 char_u numbuf[NUMBUFLEN];
3136 char_u *s;
3137
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003138 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3139 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3140 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 {
3142 switch (tv1->v_type)
3143 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003144 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 case VAR_DICT:
3146 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003147 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003148 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003149 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003150 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003151 break;
3152
3153 case VAR_LIST:
3154 if (*op != '+' || tv2->v_type != VAR_LIST)
3155 break;
3156 /* List += List */
3157 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3158 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3159 return OK;
3160
3161 case VAR_NUMBER:
3162 case VAR_STRING:
3163 if (tv2->v_type == VAR_LIST)
3164 break;
3165 if (*op == '+' || *op == '-')
3166 {
3167 /* nr += nr or nr -= nr*/
3168 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169#ifdef FEAT_FLOAT
3170 if (tv2->v_type == VAR_FLOAT)
3171 {
3172 float_T f = n;
3173
3174 if (*op == '+')
3175 f += tv2->vval.v_float;
3176 else
3177 f -= tv2->vval.v_float;
3178 clear_tv(tv1);
3179 tv1->v_type = VAR_FLOAT;
3180 tv1->vval.v_float = f;
3181 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003182 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003183#endif
3184 {
3185 if (*op == '+')
3186 n += get_tv_number(tv2);
3187 else
3188 n -= get_tv_number(tv2);
3189 clear_tv(tv1);
3190 tv1->v_type = VAR_NUMBER;
3191 tv1->vval.v_number = n;
3192 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003193 }
3194 else
3195 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196 if (tv2->v_type == VAR_FLOAT)
3197 break;
3198
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003199 /* str .= str */
3200 s = get_tv_string(tv1);
3201 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3202 clear_tv(tv1);
3203 tv1->v_type = VAR_STRING;
3204 tv1->vval.v_string = s;
3205 }
3206 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003208 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003209#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210 {
3211 float_T f;
3212
3213 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3214 && tv2->v_type != VAR_NUMBER
3215 && tv2->v_type != VAR_STRING))
3216 break;
3217 if (tv2->v_type == VAR_FLOAT)
3218 f = tv2->vval.v_float;
3219 else
3220 f = get_tv_number(tv2);
3221 if (*op == '+')
3222 tv1->vval.v_float += f;
3223 else
3224 tv1->vval.v_float -= f;
3225 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003226#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003227 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003228 }
3229 }
3230
3231 EMSG2(_(e_letwrong), op);
3232 return FAIL;
3233}
3234
3235/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 * Add a watcher to a list.
3237 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003238 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003239list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240{
3241 lw->lw_next = l->lv_watch;
3242 l->lv_watch = lw;
3243}
3244
3245/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003246 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 * No warning when it isn't found...
3248 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003250list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251{
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253
3254 lwp = &l->lv_watch;
3255 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3256 {
3257 if (lw == lwrem)
3258 {
3259 *lwp = lw->lw_next;
3260 break;
3261 }
3262 lwp = &lw->lw_next;
3263 }
3264}
3265
3266/*
3267 * Just before removing an item from a list: advance watchers to the next
3268 * item.
3269 */
3270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272{
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274
3275 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3276 if (lw->lw_item == item)
3277 lw->lw_item = item->li_next;
3278}
3279
3280/*
3281 * Evaluate the expression used in a ":for var in expr" command.
3282 * "arg" points to "var".
3283 * Set "*errp" to TRUE for an error, FALSE otherwise;
3284 * Return a pointer that holds the info. Null when there is an error.
3285 */
3286 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003287eval_for_line(
3288 char_u *arg,
3289 int *errp,
3290 char_u **nextcmdp,
3291 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292{
Bram Moolenaar33570922005-01-25 22:26:29 +00003293 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 typval_T tv;
3296 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297
3298 *errp = TRUE; /* default: there is an error */
3299
Bram Moolenaar33570922005-01-25 22:26:29 +00003300 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 if (fi == NULL)
3302 return NULL;
3303
3304 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3305 if (expr == NULL)
3306 return fi;
3307
3308 expr = skipwhite(expr);
3309 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3310 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003311 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003312 return fi;
3313 }
3314
3315 if (skip)
3316 ++emsg_skip;
3317 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3318 {
3319 *errp = FALSE;
3320 if (!skip)
3321 {
3322 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003323 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 clear_tv(&tv);
3327 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003328 else if (l == NULL)
3329 {
3330 /* a null list is like an empty list: do nothing */
3331 clear_tv(&tv);
3332 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 else
3334 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003335 /* No need to increment the refcount, it's already set for the
3336 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 fi->fi_list = l;
3338 list_add_watch(l, &fi->fi_lw);
3339 fi->fi_lw.lw_item = l->lv_first;
3340 }
3341 }
3342 }
3343 if (skip)
3344 --emsg_skip;
3345
3346 return fi;
3347}
3348
3349/*
3350 * Use the first item in a ":for" list. Advance to the next.
3351 * Assign the values to the variable (list). "arg" points to the first one.
3352 * Return TRUE when a valid item was found, FALSE when at end of list or
3353 * something wrong.
3354 */
3355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003356next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003357{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003358 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003360 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361
3362 item = fi->fi_lw.lw_item;
3363 if (item == NULL)
3364 result = FALSE;
3365 else
3366 {
3367 fi->fi_lw.lw_item = item->li_next;
3368 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3369 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3370 }
3371 return result;
3372}
3373
3374/*
3375 * Free the structure used to store info used by ":for".
3376 */
3377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003378free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003379{
Bram Moolenaar33570922005-01-25 22:26:29 +00003380 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003382 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003383 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003385 list_unref(fi->fi_list);
3386 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003387 vim_free(fi);
3388}
3389
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3391
3392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003393set_context_for_expression(
3394 expand_T *xp,
3395 char_u *arg,
3396 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
3398 int got_eq = FALSE;
3399 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003400 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003402 if (cmdidx == CMD_let)
3403 {
3404 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003405 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003406 {
3407 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003408 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003409 {
3410 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003411 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003412 if (vim_iswhite(*p))
3413 break;
3414 }
3415 return;
3416 }
3417 }
3418 else
3419 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3420 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 while ((xp->xp_pattern = vim_strpbrk(arg,
3422 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3423 {
3424 c = *xp->xp_pattern;
3425 if (c == '&')
3426 {
3427 c = xp->xp_pattern[1];
3428 if (c == '&')
3429 {
3430 ++xp->xp_pattern;
3431 xp->xp_context = cmdidx != CMD_let || got_eq
3432 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3433 }
3434 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003437 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3438 xp->xp_pattern += 2;
3439
3440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
3442 else if (c == '$')
3443 {
3444 /* environment variable */
3445 xp->xp_context = EXPAND_ENV_VARS;
3446 }
3447 else if (c == '=')
3448 {
3449 got_eq = TRUE;
3450 xp->xp_context = EXPAND_EXPRESSION;
3451 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003452 else if (c == '#'
3453 && xp->xp_context == EXPAND_EXPRESSION)
3454 {
3455 /* Autoload function/variable contains '#'. */
3456 break;
3457 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003458 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 && xp->xp_context == EXPAND_FUNCTIONS
3460 && vim_strchr(xp->xp_pattern, '(') == NULL)
3461 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003462 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 break;
3464 }
3465 else if (cmdidx != CMD_let || got_eq)
3466 {
3467 if (c == '"') /* string */
3468 {
3469 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3470 if (c == '\\' && xp->xp_pattern[1] != NUL)
3471 ++xp->xp_pattern;
3472 xp->xp_context = EXPAND_NOTHING;
3473 }
3474 else if (c == '\'') /* literal string */
3475 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003476 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3478 /* skip */ ;
3479 xp->xp_context = EXPAND_NOTHING;
3480 }
3481 else if (c == '|')
3482 {
3483 if (xp->xp_pattern[1] == '|')
3484 {
3485 ++xp->xp_pattern;
3486 xp->xp_context = EXPAND_EXPRESSION;
3487 }
3488 else
3489 xp->xp_context = EXPAND_COMMANDS;
3490 }
3491 else
3492 xp->xp_context = EXPAND_EXPRESSION;
3493 }
3494 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003495 /* Doesn't look like something valid, expand as an expression
3496 * anyway. */
3497 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 arg = xp->xp_pattern;
3499 if (*arg != NUL)
3500 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3501 /* skip */ ;
3502 }
3503 xp->xp_pattern = arg;
3504}
3505
3506#endif /* FEAT_CMDL_COMPL */
3507
3508/*
3509 * ":1,25call func(arg1, arg2)" function call.
3510 */
3511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003512ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
3514 char_u *arg = eap->arg;
3515 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003517 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 linenr_T lnum;
3521 int doesrange;
3522 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003523 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003524 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003526 if (eap->skip)
3527 {
3528 /* trans_function_name() doesn't work well when skipping, use eval0()
3529 * instead to skip to any following command, e.g. for:
3530 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003531 ++emsg_skip;
3532 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3533 clear_tv(&rettv);
3534 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003535 return;
3536 }
3537
Bram Moolenaar65639032016-03-16 21:40:30 +01003538 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003539 if (fudi.fd_newkey != NULL)
3540 {
3541 /* Still need to give an error message for missing key. */
3542 EMSG2(_(e_dictkey), fudi.fd_newkey);
3543 vim_free(fudi.fd_newkey);
3544 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003545 if (tofree == NULL)
3546 return;
3547
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003548 /* Increase refcount on dictionary, it could get deleted when evaluating
3549 * the arguments. */
3550 if (fudi.fd_dict != NULL)
3551 ++fudi.fd_dict->dv_refcount;
3552
Bram Moolenaar65639032016-03-16 21:40:30 +01003553 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3554 * contents. For VAR_PARTIAL get its partial, unless we already have one
3555 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003556 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003557 name = deref_func_name(tofree, &len,
3558 partial != NULL ? NULL : &partial, FALSE);
3559
Bram Moolenaar532c7802005-01-27 14:44:31 +00003560 /* Skip white space to allow ":call func ()". Not good, but required for
3561 * backward compatibility. */
3562 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003563 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565 if (*startarg != '(')
3566 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003567 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 goto end;
3569 }
3570
3571 /*
3572 * When skipping, evaluate the function once, to find the end of the
3573 * arguments.
3574 * When the function takes a range, this is discovered after the first
3575 * call, and the loop is broken.
3576 */
3577 if (eap->skip)
3578 {
3579 ++emsg_skip;
3580 lnum = eap->line2; /* do it once, also with an invalid range */
3581 }
3582 else
3583 lnum = eap->line1;
3584 for ( ; lnum <= eap->line2; ++lnum)
3585 {
3586 if (!eap->skip && eap->addr_count > 0)
3587 {
3588 curwin->w_cursor.lnum = lnum;
3589 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003590#ifdef FEAT_VIRTUALEDIT
3591 curwin->w_cursor.coladd = 0;
3592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
3594 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003595 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003596 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003597 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 failed = TRUE;
3600 break;
3601 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003602
3603 /* Handle a function returning a Funcref, Dictionary or List. */
3604 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3605 {
3606 failed = TRUE;
3607 break;
3608 }
3609
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (doesrange || eap->skip)
3612 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003615 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003617 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (aborting())
3619 break;
3620 }
3621 if (eap->skip)
3622 --emsg_skip;
3623
3624 if (!failed)
3625 {
3626 /* Check for trailing illegal characters and a following command. */
3627 if (!ends_excmd(*arg))
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 else
3633 eap->nextcmd = check_nextcmd(arg);
3634 }
3635
3636end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003637 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003638 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639}
3640
3641/*
3642 * ":unlet[!] var1 ... " command.
3643 */
3644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003645ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 ex_unletlock(eap, eap->arg, 0);
3648}
3649
3650/*
3651 * ":lockvar" and ":unlockvar" commands
3652 */
3653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003655{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 int deep = 2;
3658
3659 if (eap->forceit)
3660 deep = -1;
3661 else if (vim_isdigit(*arg))
3662 {
3663 deep = getdigits(&arg);
3664 arg = skipwhite(arg);
3665 }
3666
3667 ex_unletlock(eap, arg, deep);
3668}
3669
3670/*
3671 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3672 */
3673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003674ex_unletlock(
3675 exarg_T *eap,
3676 char_u *argstart,
3677 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678{
3679 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003682 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
3684 do
3685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003687 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003688 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 if (lv.ll_name == NULL)
3690 error = TRUE; /* error but continue parsing */
3691 if (name_end == NULL || (!vim_iswhite(*name_end)
3692 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694 if (name_end != NULL)
3695 {
3696 emsg_severe = TRUE;
3697 EMSG(_(e_trailing));
3698 }
3699 if (!(eap->skip || error))
3700 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 break;
3702 }
3703
3704 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003705 {
3706 if (eap->cmdidx == CMD_unlet)
3707 {
3708 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3709 error = TRUE;
3710 }
3711 else
3712 {
3713 if (do_lock_var(&lv, name_end, deep,
3714 eap->cmdidx == CMD_lockvar) == FAIL)
3715 error = TRUE;
3716 }
3717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 if (!eap->skip)
3720 clear_lval(&lv);
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 arg = skipwhite(name_end);
3723 } while (!ends_excmd(*arg));
3724
3725 eap->nextcmd = check_nextcmd(arg);
3726}
3727
Bram Moolenaar8c711452005-01-14 21:53:12 +00003728 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003729do_unlet_var(
3730 lval_T *lp,
3731 char_u *name_end,
3732 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003733{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003734 int ret = OK;
3735 int cc;
3736
3737 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003738 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003739 cc = *name_end;
3740 *name_end = NUL;
3741
3742 /* Normal name or expanded name. */
3743 if (check_changedtick(lp->ll_name))
3744 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003745 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003746 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003747 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003748 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003749 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003750 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003751 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003752 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003753 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003754 else if (lp->ll_range)
3755 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003757 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003758 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003759
3760 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3761 {
3762 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003763 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003764 return FAIL;
3765 ll_li = li;
3766 ++ll_n1;
3767 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003768
3769 /* Delete a range of List items. */
3770 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3771 {
3772 li = lp->ll_li->li_next;
3773 listitem_remove(lp->ll_list, lp->ll_li);
3774 lp->ll_li = li;
3775 ++lp->ll_n1;
3776 }
3777 }
3778 else
3779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003780 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003781 /* unlet a List item. */
3782 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003783 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003784 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003785 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003786 }
3787
3788 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003789}
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791/*
3792 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003793 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
3795 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003796do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
Bram Moolenaar33570922005-01-25 22:26:29 +00003798 hashtab_T *ht;
3799 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003800 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003801 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003802 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Bram Moolenaar33570922005-01-25 22:26:29 +00003804 ht = find_var_ht(name, &varname);
3805 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003807 if (ht == &globvarht)
3808 d = &globvardict;
3809 else if (current_funccal != NULL
3810 && ht == &current_funccal->l_vars.dv_hashtab)
3811 d = &current_funccal->l_vars;
3812 else if (ht == &compat_hashtab)
3813 d = &vimvardict;
3814 else
3815 {
3816 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3817 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3818 }
3819 if (d == NULL)
3820 {
3821 EMSG2(_(e_intern2), "do_unlet()");
3822 return FAIL;
3823 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 hi = hash_find(ht, varname);
3825 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003826 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003827 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003828 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003829 || var_check_ro(di->di_flags, name, FALSE)
3830 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003831 return FAIL;
3832
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003833 delete_var(ht, hi);
3834 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003837 if (forceit)
3838 return OK;
3839 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 return FAIL;
3841}
3842
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003843/*
3844 * Lock or unlock variable indicated by "lp".
3845 * "deep" is the levels to go (-1 for unlimited);
3846 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3847 */
3848 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003849do_lock_var(
3850 lval_T *lp,
3851 char_u *name_end,
3852 int deep,
3853 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003854{
3855 int ret = OK;
3856 int cc;
3857 dictitem_T *di;
3858
3859 if (deep == 0) /* nothing to do */
3860 return OK;
3861
3862 if (lp->ll_tv == NULL)
3863 {
3864 cc = *name_end;
3865 *name_end = NUL;
3866
3867 /* Normal name or expanded name. */
3868 if (check_changedtick(lp->ll_name))
3869 ret = FAIL;
3870 else
3871 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003872 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003873 if (di == NULL)
3874 ret = FAIL;
3875 else
3876 {
3877 if (lock)
3878 di->di_flags |= DI_FLAGS_LOCK;
3879 else
3880 di->di_flags &= ~DI_FLAGS_LOCK;
3881 item_lock(&di->di_tv, deep, lock);
3882 }
3883 }
3884 *name_end = cc;
3885 }
3886 else if (lp->ll_range)
3887 {
3888 listitem_T *li = lp->ll_li;
3889
3890 /* (un)lock a range of List items. */
3891 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3892 {
3893 item_lock(&li->li_tv, deep, lock);
3894 li = li->li_next;
3895 ++lp->ll_n1;
3896 }
3897 }
3898 else if (lp->ll_list != NULL)
3899 /* (un)lock a List item. */
3900 item_lock(&lp->ll_li->li_tv, deep, lock);
3901 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003902 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003903 item_lock(&lp->ll_di->di_tv, deep, lock);
3904
3905 return ret;
3906}
3907
3908/*
3909 * Lock or unlock an item. "deep" is nr of levels to go.
3910 */
3911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003912item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003913{
3914 static int recurse = 0;
3915 list_T *l;
3916 listitem_T *li;
3917 dict_T *d;
3918 hashitem_T *hi;
3919 int todo;
3920
3921 if (recurse >= DICT_MAXNEST)
3922 {
3923 EMSG(_("E743: variable nested too deep for (un)lock"));
3924 return;
3925 }
3926 if (deep == 0)
3927 return;
3928 ++recurse;
3929
3930 /* lock/unlock the item itself */
3931 if (lock)
3932 tv->v_lock |= VAR_LOCKED;
3933 else
3934 tv->v_lock &= ~VAR_LOCKED;
3935
3936 switch (tv->v_type)
3937 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003938 case VAR_UNKNOWN:
3939 case VAR_NUMBER:
3940 case VAR_STRING:
3941 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003942 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003943 case VAR_FLOAT:
3944 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003945 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003946 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003947 break;
3948
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003949 case VAR_LIST:
3950 if ((l = tv->vval.v_list) != NULL)
3951 {
3952 if (lock)
3953 l->lv_lock |= VAR_LOCKED;
3954 else
3955 l->lv_lock &= ~VAR_LOCKED;
3956 if (deep < 0 || deep > 1)
3957 /* recursive: lock/unlock the items the List contains */
3958 for (li = l->lv_first; li != NULL; li = li->li_next)
3959 item_lock(&li->li_tv, deep - 1, lock);
3960 }
3961 break;
3962 case VAR_DICT:
3963 if ((d = tv->vval.v_dict) != NULL)
3964 {
3965 if (lock)
3966 d->dv_lock |= VAR_LOCKED;
3967 else
3968 d->dv_lock &= ~VAR_LOCKED;
3969 if (deep < 0 || deep > 1)
3970 {
3971 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003972 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003973 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3974 {
3975 if (!HASHITEM_EMPTY(hi))
3976 {
3977 --todo;
3978 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3979 }
3980 }
3981 }
3982 }
3983 }
3984 --recurse;
3985}
3986
Bram Moolenaara40058a2005-07-11 22:42:07 +00003987/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003988 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3989 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003990 */
3991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003992tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003993{
3994 return (tv->v_lock & VAR_LOCKED)
3995 || (tv->v_type == VAR_LIST
3996 && tv->vval.v_list != NULL
3997 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3998 || (tv->v_type == VAR_DICT
3999 && tv->vval.v_dict != NULL
4000 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
4001}
4002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
4004/*
4005 * Delete all "menutrans_" variables.
4006 */
4007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004008del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004014 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 {
4017 if (!HASHITEM_EMPTY(hi))
4018 {
4019 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4021 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 }
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025}
4026#endif
4027
4028#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4029
4030/*
4031 * Local string buffer for the next two functions to store a variable name
4032 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4033 * get_user_var_name().
4034 */
4035
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004036static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037
4038static char_u *varnamebuf = NULL;
4039static int varnamebuflen = 0;
4040
4041/*
4042 * Function to concatenate a prefix and a variable name.
4043 */
4044 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004045cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
4047 int len;
4048
4049 len = (int)STRLEN(name) + 3;
4050 if (len > varnamebuflen)
4051 {
4052 vim_free(varnamebuf);
4053 len += 10; /* some additional space */
4054 varnamebuf = alloc(len);
4055 if (varnamebuf == NULL)
4056 {
4057 varnamebuflen = 0;
4058 return NULL;
4059 }
4060 varnamebuflen = len;
4061 }
4062 *varnamebuf = prefix;
4063 varnamebuf[1] = ':';
4064 STRCPY(varnamebuf + 2, name);
4065 return varnamebuf;
4066}
4067
4068/*
4069 * Function given to ExpandGeneric() to obtain the list of user defined
4070 * (global/buffer/window/built-in) variable names.
4071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004073get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004075 static long_u gdone;
4076 static long_u bdone;
4077 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004078#ifdef FEAT_WINDOWS
4079 static long_u tdone;
4080#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004081 static int vidx;
4082 static hashitem_T *hi;
4083 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004086 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004087 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004088#ifdef FEAT_WINDOWS
4089 tdone = 0;
4090#endif
4091 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004092
4093 /* Global variables */
4094 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004097 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004098 else
4099 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004100 while (HASHITEM_EMPTY(hi))
4101 ++hi;
4102 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4103 return cat_prefix_varname('g', hi->hi_key);
4104 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004106
4107 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004108 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004109 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004111 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004112 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004113 else
4114 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004115 while (HASHITEM_EMPTY(hi))
4116 ++hi;
4117 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004119 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004121 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 return (char_u *)"b:changedtick";
4123 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004124
4125 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004126 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004127 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004129 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004130 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004131 else
4132 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004133 while (HASHITEM_EMPTY(hi))
4134 ++hi;
4135 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004137
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004138#ifdef FEAT_WINDOWS
4139 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004140 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004141 if (tdone < ht->ht_used)
4142 {
4143 if (tdone++ == 0)
4144 hi = ht->ht_array;
4145 else
4146 ++hi;
4147 while (HASHITEM_EMPTY(hi))
4148 ++hi;
4149 return cat_prefix_varname('t', hi->hi_key);
4150 }
4151#endif
4152
Bram Moolenaar33570922005-01-25 22:26:29 +00004153 /* v: variables */
4154 if (vidx < VV_LEN)
4155 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 vim_free(varnamebuf);
4158 varnamebuf = NULL;
4159 varnamebuflen = 0;
4160 return NULL;
4161}
4162
4163#endif /* FEAT_CMDL_COMPL */
4164
4165/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004166 * Return TRUE if "pat" matches "text".
4167 * Does not use 'cpo' and always uses 'magic'.
4168 */
4169 static int
4170pattern_match(char_u *pat, char_u *text, int ic)
4171{
4172 int matches = FALSE;
4173 char_u *save_cpo;
4174 regmatch_T regmatch;
4175
4176 /* avoid 'l' flag in 'cpoptions' */
4177 save_cpo = p_cpo;
4178 p_cpo = (char_u *)"";
4179 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4180 if (regmatch.regprog != NULL)
4181 {
4182 regmatch.rm_ic = ic;
4183 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4184 vim_regfree(regmatch.regprog);
4185 }
4186 p_cpo = save_cpo;
4187 return matches;
4188}
4189
4190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 * types for expressions.
4192 */
4193typedef enum
4194{
4195 TYPE_UNKNOWN = 0
4196 , TYPE_EQUAL /* == */
4197 , TYPE_NEQUAL /* != */
4198 , TYPE_GREATER /* > */
4199 , TYPE_GEQUAL /* >= */
4200 , TYPE_SMALLER /* < */
4201 , TYPE_SEQUAL /* <= */
4202 , TYPE_MATCH /* =~ */
4203 , TYPE_NOMATCH /* !~ */
4204} exptype_T;
4205
4206/*
4207 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4210 */
4211
4212/*
4213 * Handle zero level expression.
4214 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004216 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 * Return OK or FAIL.
4218 */
4219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004220eval0(
4221 char_u *arg,
4222 typval_T *rettv,
4223 char_u **nextcmd,
4224 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 int ret;
4227 char_u *p;
4228
4229 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 if (ret == FAIL || !ends_excmd(*p))
4232 {
4233 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 /*
4236 * Report the invalid expression unless the expression evaluation has
4237 * been cancelled due to an aborting error, an interrupt, or an
4238 * exception.
4239 */
4240 if (!aborting())
4241 EMSG2(_(e_invexpr2), arg);
4242 ret = FAIL;
4243 }
4244 if (nextcmd != NULL)
4245 *nextcmd = check_nextcmd(p);
4246
4247 return ret;
4248}
4249
4250/*
4251 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004252 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 *
4254 * "arg" must point to the first non-white of the expression.
4255 * "arg" is advanced to the next non-white after the recognized expression.
4256 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004257 * Note: "rettv.v_lock" is not set.
4258 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 * Return OK or FAIL.
4260 */
4261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004262eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
4264 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004265 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
4267 /*
4268 * Get the first variable.
4269 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 return FAIL;
4272
4273 if ((*arg)[0] == '?')
4274 {
4275 result = FALSE;
4276 if (evaluate)
4277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 int error = FALSE;
4279
4280 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (error)
4284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286
4287 /*
4288 * Get the second variable.
4289 */
4290 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 return FAIL;
4293
4294 /*
4295 * Check for the ":".
4296 */
4297 if ((*arg)[0] != ':')
4298 {
4299 EMSG(_("E109: Missing ':' after '?'"));
4300 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 return FAIL;
4303 }
4304
4305 /*
4306 * Get the third variable.
4307 */
4308 *arg = skipwhite(*arg + 1);
4309 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4310 {
4311 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FAIL;
4314 }
4315 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318
4319 return OK;
4320}
4321
4322/*
4323 * Handle first level expression:
4324 * expr2 || expr2 || expr2 logical OR
4325 *
4326 * "arg" must point to the first non-white of the expression.
4327 * "arg" is advanced to the next non-white after the recognized expression.
4328 *
4329 * Return OK or FAIL.
4330 */
4331 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004332eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
Bram Moolenaar33570922005-01-25 22:26:29 +00004334 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 long result;
4336 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339 /*
4340 * Get the first variable.
4341 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 return FAIL;
4344
4345 /*
4346 * Repeat until there is no following "||".
4347 */
4348 first = TRUE;
4349 result = FALSE;
4350 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4351 {
4352 if (evaluate && first)
4353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (error)
4358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 first = FALSE;
4360 }
4361
4362 /*
4363 * Get the second variable.
4364 */
4365 *arg = skipwhite(*arg + 2);
4366 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4367 return FAIL;
4368
4369 /*
4370 * Compute the result.
4371 */
4372 if (evaluate && !result)
4373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004374 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004377 if (error)
4378 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380 if (evaluate)
4381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382 rettv->v_type = VAR_NUMBER;
4383 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 }
4385 }
4386
4387 return OK;
4388}
4389
4390/*
4391 * Handle second level expression:
4392 * expr3 && expr3 && expr3 logical AND
4393 *
4394 * "arg" must point to the first non-white of the expression.
4395 * "arg" is advanced to the next non-white after the recognized expression.
4396 *
4397 * Return OK or FAIL.
4398 */
4399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004400eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
Bram Moolenaar33570922005-01-25 22:26:29 +00004402 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 long result;
4404 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004405 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
4407 /*
4408 * Get the first variable.
4409 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004410 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return FAIL;
4412
4413 /*
4414 * Repeat until there is no following "&&".
4415 */
4416 first = TRUE;
4417 result = TRUE;
4418 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4419 {
4420 if (evaluate && first)
4421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004422 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004425 if (error)
4426 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 first = FALSE;
4428 }
4429
4430 /*
4431 * Get the second variable.
4432 */
4433 *arg = skipwhite(*arg + 2);
4434 if (eval4(arg, &var2, evaluate && result) == FAIL)
4435 return FAIL;
4436
4437 /*
4438 * Compute the result.
4439 */
4440 if (evaluate && result)
4441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004442 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004444 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 if (error)
4446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 if (evaluate)
4449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 rettv->v_type = VAR_NUMBER;
4451 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 }
4454
4455 return OK;
4456}
4457
4458/*
4459 * Handle third level expression:
4460 * var1 == var2
4461 * var1 =~ var2
4462 * var1 != var2
4463 * var1 !~ var2
4464 * var1 > var2
4465 * var1 >= var2
4466 * var1 < var2
4467 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 * var1 is var2
4469 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 *
4471 * "arg" must point to the first non-white of the expression.
4472 * "arg" is advanced to the next non-white after the recognized expression.
4473 *
4474 * Return OK or FAIL.
4475 */
4476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004477eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
Bram Moolenaar33570922005-01-25 22:26:29 +00004479 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 char_u *p;
4481 int i;
4482 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004485 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 char_u *s1, *s2;
4487 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 /*
4491 * Get the first variable.
4492 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004493 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 return FAIL;
4495
4496 p = *arg;
4497 switch (p[0])
4498 {
4499 case '=': if (p[1] == '=')
4500 type = TYPE_EQUAL;
4501 else if (p[1] == '~')
4502 type = TYPE_MATCH;
4503 break;
4504 case '!': if (p[1] == '=')
4505 type = TYPE_NEQUAL;
4506 else if (p[1] == '~')
4507 type = TYPE_NOMATCH;
4508 break;
4509 case '>': if (p[1] != '=')
4510 {
4511 type = TYPE_GREATER;
4512 len = 1;
4513 }
4514 else
4515 type = TYPE_GEQUAL;
4516 break;
4517 case '<': if (p[1] != '=')
4518 {
4519 type = TYPE_SMALLER;
4520 len = 1;
4521 }
4522 else
4523 type = TYPE_SEQUAL;
4524 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004525 case 'i': if (p[1] == 's')
4526 {
4527 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4528 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004529 i = p[len];
4530 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004531 {
4532 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4533 type_is = TRUE;
4534 }
4535 }
4536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538
4539 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004540 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 */
4542 if (type != TYPE_UNKNOWN)
4543 {
4544 /* extra question mark appended: ignore case */
4545 if (p[len] == '?')
4546 {
4547 ic = TRUE;
4548 ++len;
4549 }
4550 /* extra '#' appended: match case */
4551 else if (p[len] == '#')
4552 {
4553 ic = FALSE;
4554 ++len;
4555 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004556 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else
4558 ic = p_ic;
4559
4560 /*
4561 * Get the second variable.
4562 */
4563 *arg = skipwhite(p + len);
4564 if (eval5(arg, &var2, evaluate) == FAIL)
4565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004566 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 return FAIL;
4568 }
4569
4570 if (evaluate)
4571 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004572 if (type_is && rettv->v_type != var2.v_type)
4573 {
4574 /* For "is" a different type always means FALSE, for "notis"
4575 * it means TRUE. */
4576 n1 = (type == TYPE_NEQUAL);
4577 }
4578 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4579 {
4580 if (type_is)
4581 {
4582 n1 = (rettv->v_type == var2.v_type
4583 && rettv->vval.v_list == var2.vval.v_list);
4584 if (type == TYPE_NEQUAL)
4585 n1 = !n1;
4586 }
4587 else if (rettv->v_type != var2.v_type
4588 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4589 {
4590 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004591 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004592 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004593 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004594 clear_tv(rettv);
4595 clear_tv(&var2);
4596 return FAIL;
4597 }
4598 else
4599 {
4600 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004601 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4602 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004603 if (type == TYPE_NEQUAL)
4604 n1 = !n1;
4605 }
4606 }
4607
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004608 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4609 {
4610 if (type_is)
4611 {
4612 n1 = (rettv->v_type == var2.v_type
4613 && rettv->vval.v_dict == var2.vval.v_dict);
4614 if (type == TYPE_NEQUAL)
4615 n1 = !n1;
4616 }
4617 else if (rettv->v_type != var2.v_type
4618 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4619 {
4620 if (rettv->v_type != var2.v_type)
4621 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4622 else
4623 EMSG(_("E736: Invalid operation for Dictionary"));
4624 clear_tv(rettv);
4625 clear_tv(&var2);
4626 return FAIL;
4627 }
4628 else
4629 {
4630 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004631 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4632 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004633 if (type == TYPE_NEQUAL)
4634 n1 = !n1;
4635 }
4636 }
4637
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004638 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4639 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004640 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004641 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004642 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004643 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004644 clear_tv(rettv);
4645 clear_tv(&var2);
4646 return FAIL;
4647 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004648 if ((rettv->v_type == VAR_PARTIAL
4649 && rettv->vval.v_partial == NULL)
4650 || (var2.v_type == VAR_PARTIAL
4651 && var2.vval.v_partial == NULL))
4652 /* when a partial is NULL assume not equal */
4653 n1 = FALSE;
4654 else if (type_is)
4655 {
4656 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4657 /* strings are considered the same if their value is
4658 * the same */
4659 n1 = tv_equal(rettv, &var2, ic, FALSE);
4660 else if (rettv->v_type == VAR_PARTIAL
4661 && var2.v_type == VAR_PARTIAL)
4662 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4663 else
4664 n1 = FALSE;
4665 }
4666 else
4667 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004668 if (type == TYPE_NEQUAL)
4669 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004670 }
4671
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004672#ifdef FEAT_FLOAT
4673 /*
4674 * If one of the two variables is a float, compare as a float.
4675 * When using "=~" or "!~", always compare as string.
4676 */
4677 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4678 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4679 {
4680 float_T f1, f2;
4681
4682 if (rettv->v_type == VAR_FLOAT)
4683 f1 = rettv->vval.v_float;
4684 else
4685 f1 = get_tv_number(rettv);
4686 if (var2.v_type == VAR_FLOAT)
4687 f2 = var2.vval.v_float;
4688 else
4689 f2 = get_tv_number(&var2);
4690 n1 = FALSE;
4691 switch (type)
4692 {
4693 case TYPE_EQUAL: n1 = (f1 == f2); break;
4694 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4695 case TYPE_GREATER: n1 = (f1 > f2); break;
4696 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4697 case TYPE_SMALLER: n1 = (f1 < f2); break;
4698 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4699 case TYPE_UNKNOWN:
4700 case TYPE_MATCH:
4701 case TYPE_NOMATCH: break; /* avoid gcc warning */
4702 }
4703 }
4704#endif
4705
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 /*
4707 * If one of the two variables is a number, compare as a number.
4708 * When using "=~" or "!~", always compare as string.
4709 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004710 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713 n1 = get_tv_number(rettv);
4714 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 switch (type)
4716 {
4717 case TYPE_EQUAL: n1 = (n1 == n2); break;
4718 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4719 case TYPE_GREATER: n1 = (n1 > n2); break;
4720 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4721 case TYPE_SMALLER: n1 = (n1 < n2); break;
4722 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4723 case TYPE_UNKNOWN:
4724 case TYPE_MATCH:
4725 case TYPE_NOMATCH: break; /* avoid gcc warning */
4726 }
4727 }
4728 else
4729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 s1 = get_tv_string_buf(rettv, buf1);
4731 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4733 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4734 else
4735 i = 0;
4736 n1 = FALSE;
4737 switch (type)
4738 {
4739 case TYPE_EQUAL: n1 = (i == 0); break;
4740 case TYPE_NEQUAL: n1 = (i != 0); break;
4741 case TYPE_GREATER: n1 = (i > 0); break;
4742 case TYPE_GEQUAL: n1 = (i >= 0); break;
4743 case TYPE_SMALLER: n1 = (i < 0); break;
4744 case TYPE_SEQUAL: n1 = (i <= 0); break;
4745
4746 case TYPE_MATCH:
4747 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004748 n1 = pattern_match(s2, s1, ic);
4749 if (type == TYPE_NOMATCH)
4750 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 break;
4752
4753 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4754 }
4755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 clear_tv(rettv);
4757 clear_tv(&var2);
4758 rettv->v_type = VAR_NUMBER;
4759 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 }
4762
4763 return OK;
4764}
4765
4766/*
4767 * Handle fourth level expression:
4768 * + number addition
4769 * - number subtraction
4770 * . string concatenation
4771 *
4772 * "arg" must point to the first non-white of the expression.
4773 * "arg" is advanced to the next non-white after the recognized expression.
4774 *
4775 * Return OK or FAIL.
4776 */
4777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004778eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
Bram Moolenaar33570922005-01-25 22:26:29 +00004780 typval_T var2;
4781 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004783 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004784#ifdef FEAT_FLOAT
4785 float_T f1 = 0, f2 = 0;
4786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 char_u *s1, *s2;
4788 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4789 char_u *p;
4790
4791 /*
4792 * Get the first variable.
4793 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004794 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796
4797 /*
4798 * Repeat computing, until no '+', '-' or '.' is following.
4799 */
4800 for (;;)
4801 {
4802 op = **arg;
4803 if (op != '+' && op != '-' && op != '.')
4804 break;
4805
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004806 if ((op != '+' || rettv->v_type != VAR_LIST)
4807#ifdef FEAT_FLOAT
4808 && (op == '.' || rettv->v_type != VAR_FLOAT)
4809#endif
4810 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 {
4812 /* For "list + ...", an illegal use of the first operand as
4813 * a number cannot be determined before evaluating the 2nd
4814 * operand: if this is also a list, all is ok.
4815 * For "something . ...", "something - ..." or "non-list + ...",
4816 * we know that the first operand needs to be a string or number
4817 * without evaluating the 2nd operand. So check before to avoid
4818 * side effects after an error. */
4819 if (evaluate && get_tv_string_chk(rettv) == NULL)
4820 {
4821 clear_tv(rettv);
4822 return FAIL;
4823 }
4824 }
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 /*
4827 * Get the second variable.
4828 */
4829 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004830 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004832 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return FAIL;
4834 }
4835
4836 if (evaluate)
4837 {
4838 /*
4839 * Compute the result.
4840 */
4841 if (op == '.')
4842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004843 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4844 s2 = get_tv_string_buf_chk(&var2, buf2);
4845 if (s2 == NULL) /* type error ? */
4846 {
4847 clear_tv(rettv);
4848 clear_tv(&var2);
4849 return FAIL;
4850 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004851 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 clear_tv(rettv);
4853 rettv->v_type = VAR_STRING;
4854 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004856 else if (op == '+' && rettv->v_type == VAR_LIST
4857 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004858 {
4859 /* concatenate Lists */
4860 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4861 &var3) == FAIL)
4862 {
4863 clear_tv(rettv);
4864 clear_tv(&var2);
4865 return FAIL;
4866 }
4867 clear_tv(rettv);
4868 *rettv = var3;
4869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 else
4871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872 int error = FALSE;
4873
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#ifdef FEAT_FLOAT
4875 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004876 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004877 f1 = rettv->vval.v_float;
4878 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880 else
4881#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 n1 = get_tv_number_chk(rettv, &error);
4884 if (error)
4885 {
4886 /* This can only happen for "list + non-list". For
4887 * "non-list + ..." or "something - ...", we returned
4888 * before evaluating the 2nd operand. */
4889 clear_tv(rettv);
4890 return FAIL;
4891 }
4892#ifdef FEAT_FLOAT
4893 if (var2.v_type == VAR_FLOAT)
4894 f1 = n1;
4895#endif
4896 }
4897#ifdef FEAT_FLOAT
4898 if (var2.v_type == VAR_FLOAT)
4899 {
4900 f2 = var2.vval.v_float;
4901 n2 = 0;
4902 }
4903 else
4904#endif
4905 {
4906 n2 = get_tv_number_chk(&var2, &error);
4907 if (error)
4908 {
4909 clear_tv(rettv);
4910 clear_tv(&var2);
4911 return FAIL;
4912 }
4913#ifdef FEAT_FLOAT
4914 if (rettv->v_type == VAR_FLOAT)
4915 f2 = n2;
4916#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919
4920#ifdef FEAT_FLOAT
4921 /* If there is a float on either side the result is a float. */
4922 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4923 {
4924 if (op == '+')
4925 f1 = f1 + f2;
4926 else
4927 f1 = f1 - f2;
4928 rettv->v_type = VAR_FLOAT;
4929 rettv->vval.v_float = f1;
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932#endif
4933 {
4934 if (op == '+')
4935 n1 = n1 + n2;
4936 else
4937 n1 = n1 - n2;
4938 rettv->v_type = VAR_NUMBER;
4939 rettv->vval.v_number = n1;
4940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004942 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944 }
4945 return OK;
4946}
4947
4948/*
4949 * Handle fifth level expression:
4950 * * number multiplication
4951 * / number division
4952 * % number modulo
4953 *
4954 * "arg" must point to the first non-white of the expression.
4955 * "arg" is advanced to the next non-white after the recognized expression.
4956 *
4957 * Return OK or FAIL.
4958 */
4959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004960eval6(
4961 char_u **arg,
4962 typval_T *rettv,
4963 int evaluate,
4964 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
Bram Moolenaar33570922005-01-25 22:26:29 +00004966 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004968 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004969#ifdef FEAT_FLOAT
4970 int use_float = FALSE;
4971 float_T f1 = 0, f2;
4972#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004973 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974
4975 /*
4976 * Get the first variable.
4977 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004978 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 return FAIL;
4980
4981 /*
4982 * Repeat computing, until no '*', '/' or '%' is following.
4983 */
4984 for (;;)
4985 {
4986 op = **arg;
4987 if (op != '*' && op != '/' && op != '%')
4988 break;
4989
4990 if (evaluate)
4991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992#ifdef FEAT_FLOAT
4993 if (rettv->v_type == VAR_FLOAT)
4994 {
4995 f1 = rettv->vval.v_float;
4996 use_float = TRUE;
4997 n1 = 0;
4998 }
4999 else
5000#endif
5001 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005002 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005003 if (error)
5004 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 else
5007 n1 = 0;
5008
5009 /*
5010 * Get the second variable.
5011 */
5012 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005013 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 return FAIL;
5015
5016 if (evaluate)
5017 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018#ifdef FEAT_FLOAT
5019 if (var2.v_type == VAR_FLOAT)
5020 {
5021 if (!use_float)
5022 {
5023 f1 = n1;
5024 use_float = TRUE;
5025 }
5026 f2 = var2.vval.v_float;
5027 n2 = 0;
5028 }
5029 else
5030#endif
5031 {
5032 n2 = get_tv_number_chk(&var2, &error);
5033 clear_tv(&var2);
5034 if (error)
5035 return FAIL;
5036#ifdef FEAT_FLOAT
5037 if (use_float)
5038 f2 = n2;
5039#endif
5040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042 /*
5043 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005046#ifdef FEAT_FLOAT
5047 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005049 if (op == '*')
5050 f1 = f1 * f2;
5051 else if (op == '/')
5052 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005053# ifdef VMS
5054 /* VMS crashes on divide by zero, work around it */
5055 if (f2 == 0.0)
5056 {
5057 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005058 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005059 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005060 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005061 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005062 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005063 }
5064 else
5065 f1 = f1 / f2;
5066# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067 /* We rely on the floating point library to handle divide
5068 * by zero to result in "inf" and not a crash. */
5069 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005070# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005074 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005075 return FAIL;
5076 }
5077 rettv->v_type = VAR_FLOAT;
5078 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005083 if (op == '*')
5084 n1 = n1 * n2;
5085 else if (op == '/')
5086 {
5087 if (n2 == 0) /* give an error message? */
5088 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005089#ifdef FEAT_NUM64
5090 if (n1 == 0)
5091 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
5092 else if (n1 < 0)
5093 n1 = -0x7fffffffffffffff;
5094 else
5095 n1 = 0x7fffffffffffffff;
5096#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005097 if (n1 == 0)
5098 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5099 else if (n1 < 0)
5100 n1 = -0x7fffffffL;
5101 else
5102 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005103#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005104 }
5105 else
5106 n1 = n1 / n2;
5107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 {
5110 if (n2 == 0) /* give an error message? */
5111 n1 = 0;
5112 else
5113 n1 = n1 % n2;
5114 }
5115 rettv->v_type = VAR_NUMBER;
5116 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 }
5120
5121 return OK;
5122}
5123
5124/*
5125 * Handle sixth level expression:
5126 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005127 * "string" string constant
5128 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 * &option-name option value
5130 * @r register contents
5131 * identifier variable value
5132 * function() function call
5133 * $VAR environment variable
5134 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005135 * [expr, expr] List
5136 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 *
5138 * Also handle:
5139 * ! in front logical NOT
5140 * - in front unary minus
5141 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 * trailing [] subscript in String or List
5143 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 *
5145 * "arg" must point to the first non-white of the expression.
5146 * "arg" is advanced to the next non-white after the recognized expression.
5147 *
5148 * Return OK or FAIL.
5149 */
5150 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005151eval7(
5152 char_u **arg,
5153 typval_T *rettv,
5154 int evaluate,
5155 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005157 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 int len;
5159 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 char_u *start_leader, *end_leader;
5161 int ret = OK;
5162 char_u *alias;
5163
5164 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170 /*
5171 * Skip '!' and '-' characters. They are handled later.
5172 */
5173 start_leader = *arg;
5174 while (**arg == '!' || **arg == '-' || **arg == '+')
5175 *arg = skipwhite(*arg + 1);
5176 end_leader = *arg;
5177
5178 switch (**arg)
5179 {
5180 /*
5181 * Number constant.
5182 */
5183 case '0':
5184 case '1':
5185 case '2':
5186 case '3':
5187 case '4':
5188 case '5':
5189 case '6':
5190 case '7':
5191 case '8':
5192 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005193 {
5194#ifdef FEAT_FLOAT
5195 char_u *p = skipdigits(*arg + 1);
5196 int get_float = FALSE;
5197
5198 /* We accept a float when the format matches
5199 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005200 * strict to avoid backwards compatibility problems.
5201 * Don't look for a float after the "." operator, so that
5202 * ":let vers = 1.2.3" doesn't fail. */
5203 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005205 get_float = TRUE;
5206 p = skipdigits(p + 2);
5207 if (*p == 'e' || *p == 'E')
5208 {
5209 ++p;
5210 if (*p == '-' || *p == '+')
5211 ++p;
5212 if (!vim_isdigit(*p))
5213 get_float = FALSE;
5214 else
5215 p = skipdigits(p + 1);
5216 }
5217 if (ASCII_ISALPHA(*p) || *p == '.')
5218 get_float = FALSE;
5219 }
5220 if (get_float)
5221 {
5222 float_T f;
5223
5224 *arg += string2float(*arg, &f);
5225 if (evaluate)
5226 {
5227 rettv->v_type = VAR_FLOAT;
5228 rettv->vval.v_float = f;
5229 }
5230 }
5231 else
5232#endif
5233 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005234 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235 *arg += len;
5236 if (evaluate)
5237 {
5238 rettv->v_type = VAR_NUMBER;
5239 rettv->vval.v_number = n;
5240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 /*
5246 * String constant: "string".
5247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005248 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 break;
5250
5251 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005254 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 break;
5256
5257 /*
5258 * List: [expr, expr]
5259 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005260 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 break;
5262
5263 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 * Dictionary: {key: val, key: val}
5265 */
5266 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5267 break;
5268
5269 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005270 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005272 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 break;
5274
5275 /*
5276 * Environment variable: $VAR.
5277 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005278 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 break;
5280
5281 /*
5282 * Register contents: @r.
5283 */
5284 case '@': ++*arg;
5285 if (evaluate)
5286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005287 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005288 rettv->vval.v_string = get_reg_contents(**arg,
5289 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291 if (**arg != NUL)
5292 ++*arg;
5293 break;
5294
5295 /*
5296 * nested expression: (expression).
5297 */
5298 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005299 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (**arg == ')')
5301 ++*arg;
5302 else if (ret == OK)
5303 {
5304 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 ret = FAIL;
5307 }
5308 break;
5309
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 break;
5312 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313
5314 if (ret == NOTDONE)
5315 {
5316 /*
5317 * Must be a variable or function name.
5318 * Can also be a curly-braces kind of name: {expr}.
5319 */
5320 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005321 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 if (alias != NULL)
5323 s = alias;
5324
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005325 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 ret = FAIL;
5327 else
5328 {
5329 if (**arg == '(') /* recursive! */
5330 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005331 partial_T *partial;
5332
Bram Moolenaar8c711452005-01-14 21:53:12 +00005333 /* If "s" is the name of a variable of type VAR_FUNC
5334 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005335 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005336
5337 /* Invoke the function. */
5338 ret = get_func_tv(s, len, rettv, arg,
5339 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005340 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005341
5342 /* If evaluate is FALSE rettv->v_type was not set in
5343 * get_func_tv, but it's needed in handle_subscript() to parse
5344 * what follows. So set it here. */
5345 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5346 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005347 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005348 rettv->v_type = VAR_FUNC;
5349 }
5350
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 /* Stop the expression evaluation when immediately
5352 * aborting on error, or when an interrupt occurred or
5353 * an exception was thrown but not caught. */
5354 if (aborting())
5355 {
5356 if (ret == OK)
5357 clear_tv(rettv);
5358 ret = FAIL;
5359 }
5360 }
5361 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005362 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005363 else
5364 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005365 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005366 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 }
5368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 *arg = skipwhite(*arg);
5370
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005371 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5372 * expr(expr). */
5373 if (ret == OK)
5374 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 /*
5377 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5378 */
5379 if (ret == OK && evaluate && end_leader > start_leader)
5380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005381 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005382 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005383#ifdef FEAT_FLOAT
5384 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005386 if (rettv->v_type == VAR_FLOAT)
5387 f = rettv->vval.v_float;
5388 else
5389#endif
5390 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005391 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005393 clear_tv(rettv);
5394 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005396 else
5397 {
5398 while (end_leader > start_leader)
5399 {
5400 --end_leader;
5401 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005402 {
5403#ifdef FEAT_FLOAT
5404 if (rettv->v_type == VAR_FLOAT)
5405 f = !f;
5406 else
5407#endif
5408 val = !val;
5409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005410 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005411 {
5412#ifdef FEAT_FLOAT
5413 if (rettv->v_type == VAR_FLOAT)
5414 f = -f;
5415 else
5416#endif
5417 val = -val;
5418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005419 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005420#ifdef FEAT_FLOAT
5421 if (rettv->v_type == VAR_FLOAT)
5422 {
5423 clear_tv(rettv);
5424 rettv->vval.v_float = f;
5425 }
5426 else
5427#endif
5428 {
5429 clear_tv(rettv);
5430 rettv->v_type = VAR_NUMBER;
5431 rettv->vval.v_number = val;
5432 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435
5436 return ret;
5437}
5438
5439/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005440 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5441 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5443 */
5444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005445eval_index(
5446 char_u **arg,
5447 typval_T *rettv,
5448 int evaluate,
5449 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450{
5451 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 long len = -1;
5455 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458
Bram Moolenaara03f2332016-02-06 18:09:59 +01005459 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005461 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005462 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005463 if (verbose)
5464 EMSG(_("E695: Cannot index a Funcref"));
5465 return FAIL;
5466 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005467#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005468 if (verbose)
5469 EMSG(_(e_float_as_string));
5470 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005471#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005472 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005473 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005474 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005475 if (verbose)
5476 EMSG(_("E909: Cannot index a special variable"));
5477 return FAIL;
5478 case VAR_UNKNOWN:
5479 if (evaluate)
5480 return FAIL;
5481 /* FALLTHROUGH */
5482
5483 case VAR_STRING:
5484 case VAR_NUMBER:
5485 case VAR_LIST:
5486 case VAR_DICT:
5487 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005488 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005490 init_tv(&var1);
5491 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494 /*
5495 * dict.name
5496 */
5497 key = *arg + 1;
5498 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5499 ;
5500 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 }
5504 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 /*
5507 * something[idx]
5508 *
5509 * Get the (first) variable from inside the [].
5510 */
5511 *arg = skipwhite(*arg + 1);
5512 if (**arg == ':')
5513 empty1 = TRUE;
5514 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5515 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005516 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5517 {
5518 /* not a number or string */
5519 clear_tv(&var1);
5520 return FAIL;
5521 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005522
5523 /*
5524 * Get the second variable from inside the [:].
5525 */
5526 if (**arg == ':')
5527 {
5528 range = TRUE;
5529 *arg = skipwhite(*arg + 1);
5530 if (**arg == ']')
5531 empty2 = TRUE;
5532 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005534 if (!empty1)
5535 clear_tv(&var1);
5536 return FAIL;
5537 }
5538 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5539 {
5540 /* not a number or string */
5541 if (!empty1)
5542 clear_tv(&var1);
5543 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005544 return FAIL;
5545 }
5546 }
5547
5548 /* Check for the ']'. */
5549 if (**arg != ']')
5550 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005551 if (verbose)
5552 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005553 clear_tv(&var1);
5554 if (range)
5555 clear_tv(&var2);
5556 return FAIL;
5557 }
5558 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 }
5560
5561 if (evaluate)
5562 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005563 n1 = 0;
5564 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 n1 = get_tv_number(&var1);
5567 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 }
5569 if (range)
5570 {
5571 if (empty2)
5572 n2 = -1;
5573 else
5574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 n2 = get_tv_number(&var2);
5576 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 }
5578 }
5579
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005582 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005583 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005584 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005585 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005586 case VAR_SPECIAL:
5587 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005588 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005589 break; /* not evaluating, skipping over subscript */
5590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 case VAR_NUMBER:
5592 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 len = (long)STRLEN(s);
5595 if (range)
5596 {
5597 /* The resulting variable is a substring. If the indexes
5598 * are out of range the result is empty. */
5599 if (n1 < 0)
5600 {
5601 n1 = len + n1;
5602 if (n1 < 0)
5603 n1 = 0;
5604 }
5605 if (n2 < 0)
5606 n2 = len + n2;
5607 else if (n2 >= len)
5608 n2 = len;
5609 if (n1 >= len || n2 < 0 || n1 > n2)
5610 s = NULL;
5611 else
5612 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5613 }
5614 else
5615 {
5616 /* The resulting variable is a string of a single
5617 * character. If the index is too big or negative the
5618 * result is empty. */
5619 if (n1 >= len || n1 < 0)
5620 s = NULL;
5621 else
5622 s = vim_strnsave(s + n1, 1);
5623 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 clear_tv(rettv);
5625 rettv->v_type = VAR_STRING;
5626 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 break;
5628
5629 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005631 if (n1 < 0)
5632 n1 = len + n1;
5633 if (!empty1 && (n1 < 0 || n1 >= len))
5634 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005635 /* For a range we allow invalid values and return an empty
5636 * list. A list index out of range is an error. */
5637 if (!range)
5638 {
5639 if (verbose)
5640 EMSGN(_(e_listidx), n1);
5641 return FAIL;
5642 }
5643 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 }
5645 if (range)
5646 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005647 list_T *l;
5648 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649
5650 if (n2 < 0)
5651 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005652 else if (n2 >= len)
5653 n2 = len - 1;
5654 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005655 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 l = list_alloc();
5657 if (l == NULL)
5658 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005660 n1 <= n2; ++n1)
5661 {
5662 if (list_append_tv(l, &item->li_tv) == FAIL)
5663 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005664 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 return FAIL;
5666 }
5667 item = item->li_next;
5668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 clear_tv(rettv);
5670 rettv->v_type = VAR_LIST;
5671 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005672 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673 }
5674 else
5675 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005676 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 clear_tv(rettv);
5678 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 }
5680 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681
5682 case VAR_DICT:
5683 if (range)
5684 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005685 if (verbose)
5686 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 if (len == -1)
5688 clear_tv(&var1);
5689 return FAIL;
5690 }
5691 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005692 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693
5694 if (len == -1)
5695 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005696 key = get_tv_string_chk(&var1);
5697 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 clear_tv(&var1);
5700 return FAIL;
5701 }
5702 }
5703
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005704 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005706 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005707 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 if (len == -1)
5709 clear_tv(&var1);
5710 if (item == NULL)
5711 return FAIL;
5712
5713 copy_tv(&item->di_tv, &var1);
5714 clear_tv(rettv);
5715 *rettv = var1;
5716 }
5717 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 }
5719 }
5720
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 return OK;
5722}
5723
5724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * Get an option value.
5726 * "arg" points to the '&' or '+' before the option name.
5727 * "arg" is advanced to character after the option name.
5728 * Return OK or FAIL.
5729 */
5730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005731get_option_tv(
5732 char_u **arg,
5733 typval_T *rettv, /* when NULL, only check if option exists */
5734 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 char_u *option_end;
5737 long numval;
5738 char_u *stringval;
5739 int opt_type;
5740 int c;
5741 int working = (**arg == '+'); /* has("+option") */
5742 int ret = OK;
5743 int opt_flags;
5744
5745 /*
5746 * Isolate the option name and find its value.
5747 */
5748 option_end = find_option_end(arg, &opt_flags);
5749 if (option_end == NULL)
5750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005751 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 EMSG2(_("E112: Option name missing: %s"), *arg);
5753 return FAIL;
5754 }
5755
5756 if (!evaluate)
5757 {
5758 *arg = option_end;
5759 return OK;
5760 }
5761
5762 c = *option_end;
5763 *option_end = NUL;
5764 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 if (opt_type == -3) /* invalid name */
5768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005769 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 EMSG2(_("E113: Unknown option: %s"), *arg);
5771 ret = FAIL;
5772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005773 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 if (opt_type == -2) /* hidden string option */
5776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005777 rettv->v_type = VAR_STRING;
5778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 }
5780 else if (opt_type == -1) /* hidden number option */
5781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005782 rettv->v_type = VAR_NUMBER;
5783 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
5785 else if (opt_type == 1) /* number option */
5786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005787 rettv->v_type = VAR_NUMBER;
5788 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
5790 else /* string option */
5791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005792 rettv->v_type = VAR_STRING;
5793 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
5795 }
5796 else if (working && (opt_type == -2 || opt_type == -1))
5797 ret = FAIL;
5798
5799 *option_end = c; /* put back for error messages */
5800 *arg = option_end;
5801
5802 return ret;
5803}
5804
5805/*
5806 * Allocate a variable for a string constant.
5807 * Return OK or FAIL.
5808 */
5809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005810get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 char_u *p;
5813 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 int extra = 0;
5815
5816 /*
5817 * Find the end of the string, skipping backslashed characters.
5818 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005819 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 {
5821 if (*p == '\\' && p[1] != NUL)
5822 {
5823 ++p;
5824 /* A "\<x>" form occupies at least 4 characters, and produces up
5825 * to 6 characters: reserve space for 2 extra */
5826 if (*p == '<')
5827 extra += 2;
5828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830
5831 if (*p != '"')
5832 {
5833 EMSG2(_("E114: Missing quote: %s"), *arg);
5834 return FAIL;
5835 }
5836
5837 /* If only parsing, set *arg and return here */
5838 if (!evaluate)
5839 {
5840 *arg = p + 1;
5841 return OK;
5842 }
5843
5844 /*
5845 * Copy the string into allocated memory, handling backslashed
5846 * characters.
5847 */
5848 name = alloc((unsigned)(p - *arg + extra));
5849 if (name == NULL)
5850 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 rettv->v_type = VAR_STRING;
5852 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 {
5856 if (*p == '\\')
5857 {
5858 switch (*++p)
5859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 case 'b': *name++ = BS; ++p; break;
5861 case 'e': *name++ = ESC; ++p; break;
5862 case 'f': *name++ = FF; ++p; break;
5863 case 'n': *name++ = NL; ++p; break;
5864 case 'r': *name++ = CAR; ++p; break;
5865 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
5867 case 'X': /* hex: "\x1", "\x12" */
5868 case 'x':
5869 case 'u': /* Unicode: "\u0023" */
5870 case 'U':
5871 if (vim_isxdigit(p[1]))
5872 {
5873 int n, nr;
5874 int c = toupper(*p);
5875
5876 if (c == 'X')
5877 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005878 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005880 else
5881 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 nr = 0;
5883 while (--n >= 0 && vim_isxdigit(p[1]))
5884 {
5885 ++p;
5886 nr = (nr << 4) + hex2nr(*p);
5887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#ifdef FEAT_MBYTE
5890 /* For "\u" store the number according to
5891 * 'encoding'. */
5892 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 else
5895#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 break;
5899
5900 /* octal: "\1", "\12", "\123" */
5901 case '0':
5902 case '1':
5903 case '2':
5904 case '3':
5905 case '4':
5906 case '5':
5907 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 case '7': *name = *p++ - '0';
5909 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 *name = (*name << 3) + *p++ - '0';
5912 if (*p >= '0' && *p <= '7')
5913 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 break;
5917
5918 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 if (extra != 0)
5921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 break;
5924 }
5925 /* FALLTHROUGH */
5926
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 break;
5929 }
5930 }
5931 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 *arg = p + 1;
5937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 return OK;
5939}
5940
5941/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 * Return OK or FAIL.
5944 */
5945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005946get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947{
5948 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005949 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 int reduce = 0;
5951
5952 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 */
5955 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005957 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005959 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005960 break;
5961 ++reduce;
5962 ++p;
5963 }
5964 }
5965
Bram Moolenaar8c711452005-01-14 21:53:12 +00005966 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969 return FAIL;
5970 }
5971
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005973 if (!evaluate)
5974 {
5975 *arg = p + 1;
5976 return OK;
5977 }
5978
5979 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005981 */
5982 str = alloc((unsigned)((p - *arg) - reduce));
5983 if (str == NULL)
5984 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005985 rettv->v_type = VAR_STRING;
5986 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005987
Bram Moolenaar8c711452005-01-14 21:53:12 +00005988 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005990 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005992 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 break;
5994 ++p;
5995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005996 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005998 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005999 *arg = p + 1;
6000
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006001 return OK;
6002}
6003
Bram Moolenaarddecc252016-04-06 22:59:37 +02006004 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006005partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02006006{
6007 int i;
6008
6009 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006010 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006011 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006012 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006013 func_unref(pt->pt_name);
6014 vim_free(pt->pt_name);
6015 vim_free(pt);
6016}
6017
6018/*
6019 * Unreference a closure: decrement the reference count and free it when it
6020 * becomes zero.
6021 */
6022 void
6023partial_unref(partial_T *pt)
6024{
6025 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006026 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006027}
6028
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006029/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 * Allocate a variable for a List and fill it from "*arg".
6031 * Return OK or FAIL.
6032 */
6033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006034get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035{
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 list_T *l = NULL;
6037 typval_T tv;
6038 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039
6040 if (evaluate)
6041 {
6042 l = list_alloc();
6043 if (l == NULL)
6044 return FAIL;
6045 }
6046
6047 *arg = skipwhite(*arg + 1);
6048 while (**arg != ']' && **arg != NUL)
6049 {
6050 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6051 goto failret;
6052 if (evaluate)
6053 {
6054 item = listitem_alloc();
6055 if (item != NULL)
6056 {
6057 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006058 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 list_append(l, item);
6060 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006061 else
6062 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 }
6064
6065 if (**arg == ']')
6066 break;
6067 if (**arg != ',')
6068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006069 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 goto failret;
6071 }
6072 *arg = skipwhite(*arg + 1);
6073 }
6074
6075 if (**arg != ']')
6076 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006077 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078failret:
6079 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006080 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 return FAIL;
6082 }
6083
6084 *arg = skipwhite(*arg + 1);
6085 if (evaluate)
6086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006087 rettv->v_type = VAR_LIST;
6088 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089 ++l->lv_refcount;
6090 }
6091
6092 return OK;
6093}
6094
6095/*
6096 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006097 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006099 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006101{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006102 list_T *l;
6103
6104 l = (list_T *)alloc_clear(sizeof(list_T));
6105 if (l != NULL)
6106 {
6107 /* Prepend the list to the list of lists for garbage collection. */
6108 if (first_list != NULL)
6109 first_list->lv_used_prev = l;
6110 l->lv_used_prev = NULL;
6111 l->lv_used_next = first_list;
6112 first_list = l;
6113 }
6114 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115}
6116
6117/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006118 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006119 * Returns OK or FAIL.
6120 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006121 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006122rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006123{
6124 list_T *l = list_alloc();
6125
6126 if (l == NULL)
6127 return FAIL;
6128
6129 rettv->vval.v_list = l;
6130 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006131 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006132 ++l->lv_refcount;
6133 return OK;
6134}
6135
6136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006137 * Unreference a list: decrement the reference count and free it when it
6138 * becomes zero.
6139 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006141list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006143 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006144 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145}
6146
6147/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006148 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 * Ignores the reference count.
6150 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006151 static void
6152list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153{
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006156 for (item = l->lv_first; item != NULL; item = l->lv_first)
6157 {
6158 /* Remove the item before deleting it. */
6159 l->lv_first = item->li_next;
6160 clear_tv(&item->li_tv);
6161 vim_free(item);
6162 }
6163}
6164
6165 static void
6166list_free_list(list_T *l)
6167{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006168 /* Remove the list from the list of lists for garbage collection. */
6169 if (l->lv_used_prev == NULL)
6170 first_list = l->lv_used_next;
6171 else
6172 l->lv_used_prev->lv_used_next = l->lv_used_next;
6173 if (l->lv_used_next != NULL)
6174 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6175
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 vim_free(l);
6177}
6178
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006179 void
6180list_free(list_T *l)
6181{
6182 if (!in_free_unref_items)
6183 {
6184 list_free_contents(l);
6185 list_free_list(l);
6186 }
6187}
6188
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189/*
6190 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006191 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006193 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006194listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195{
Bram Moolenaar33570922005-01-25 22:26:29 +00006196 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197}
6198
6199/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006200 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006203listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006205 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006206 vim_free(item);
6207}
6208
6209/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006210 * Remove a list item from a List and free it. Also clears the value.
6211 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006213listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006214{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006215 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006216 listitem_free(item);
6217}
6218
6219/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220 * Get the number of items in a list.
6221 */
6222 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006223list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006225 if (l == NULL)
6226 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006227 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228}
6229
6230/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006231 * Return TRUE when two lists have exactly the same values.
6232 */
6233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006234list_equal(
6235 list_T *l1,
6236 list_T *l2,
6237 int ic, /* ignore case for strings */
6238 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239{
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006241
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006242 if (l1 == NULL || l2 == NULL)
6243 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006244 if (l1 == l2)
6245 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006246 if (list_len(l1) != list_len(l2))
6247 return FALSE;
6248
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 for (item1 = l1->lv_first, item2 = l2->lv_first;
6250 item1 != NULL && item2 != NULL;
6251 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006252 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 return FALSE;
6254 return item1 == NULL && item2 == NULL;
6255}
6256
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006257/*
6258 * Return the dictitem that an entry in a hashtable points to.
6259 */
6260 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006261dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006262{
6263 return HI2DI(hi);
6264}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006265
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006266/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006267 * Return TRUE when two dictionaries have exactly the same key/values.
6268 */
6269 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006270dict_equal(
6271 dict_T *d1,
6272 dict_T *d2,
6273 int ic, /* ignore case for strings */
6274 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006275{
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 hashitem_T *hi;
6277 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006278 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006279
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006280 if (d1 == NULL && d2 == NULL)
6281 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006282 if (d1 == NULL || d2 == NULL)
6283 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006284 if (d1 == d2)
6285 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006286 if (dict_len(d1) != dict_len(d2))
6287 return FALSE;
6288
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006289 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006291 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006292 if (!HASHITEM_EMPTY(hi))
6293 {
6294 item2 = dict_find(d2, hi->hi_key, -1);
6295 if (item2 == NULL)
6296 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006297 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006298 return FALSE;
6299 --todo;
6300 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006301 }
6302 return TRUE;
6303}
6304
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006305static int tv_equal_recurse_limit;
6306
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006307 static int
6308func_equal(
6309 typval_T *tv1,
6310 typval_T *tv2,
6311 int ic) /* ignore case */
6312{
6313 char_u *s1, *s2;
6314 dict_T *d1, *d2;
6315 int a1, a2;
6316 int i;
6317
6318 /* empty and NULL function name considered the same */
6319 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6320 : tv1->vval.v_partial->pt_name;
6321 if (s1 != NULL && *s1 == NUL)
6322 s1 = NULL;
6323 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6324 : tv2->vval.v_partial->pt_name;
6325 if (s2 != NULL && *s2 == NUL)
6326 s2 = NULL;
6327 if (s1 == NULL || s2 == NULL)
6328 {
6329 if (s1 != s2)
6330 return FALSE;
6331 }
6332 else if (STRCMP(s1, s2) != 0)
6333 return FALSE;
6334
6335 /* empty dict and NULL dict is different */
6336 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6337 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6338 if (d1 == NULL || d2 == NULL)
6339 {
6340 if (d1 != d2)
6341 return FALSE;
6342 }
6343 else if (!dict_equal(d1, d2, ic, TRUE))
6344 return FALSE;
6345
6346 /* empty list and no list considered the same */
6347 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6348 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6349 if (a1 != a2)
6350 return FALSE;
6351 for (i = 0; i < a1; ++i)
6352 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6353 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6354 return FALSE;
6355
6356 return TRUE;
6357}
6358
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006359/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006360 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006361 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006362 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006363 */
6364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006365tv_equal(
6366 typval_T *tv1,
6367 typval_T *tv2,
6368 int ic, /* ignore case */
6369 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006370{
6371 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006372 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006373 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006374 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006376 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006377 * recursiveness to a limit. We guess they are equal then.
6378 * A fixed limit has the problem of still taking an awful long time.
6379 * Reduce the limit every time running into it. That should work fine for
6380 * deeply linked structures that are not recursively linked and catch
6381 * recursiveness quickly. */
6382 if (!recursive)
6383 tv_equal_recurse_limit = 1000;
6384 if (recursive_cnt >= tv_equal_recurse_limit)
6385 {
6386 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006387 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006388 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006389
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02006390 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
6391 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006392 if ((tv1->v_type == VAR_FUNC
6393 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6394 && (tv2->v_type == VAR_FUNC
6395 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6396 {
6397 ++recursive_cnt;
6398 r = func_equal(tv1, tv2, ic);
6399 --recursive_cnt;
6400 return r;
6401 }
6402
6403 if (tv1->v_type != tv2->v_type)
6404 return FALSE;
6405
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006406 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006408 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006409 ++recursive_cnt;
6410 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6411 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006412 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006413
6414 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006415 ++recursive_cnt;
6416 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6417 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006418 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006419
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006420 case VAR_NUMBER:
6421 return tv1->vval.v_number == tv2->vval.v_number;
6422
6423 case VAR_STRING:
6424 s1 = get_tv_string_buf(tv1, buf1);
6425 s2 = get_tv_string_buf(tv2, buf2);
6426 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006427
6428 case VAR_SPECIAL:
6429 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006430
6431 case VAR_FLOAT:
6432#ifdef FEAT_FLOAT
6433 return tv1->vval.v_float == tv2->vval.v_float;
6434#endif
6435 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006436#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006437 return tv1->vval.v_job == tv2->vval.v_job;
6438#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006439 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006440#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006441 return tv1->vval.v_channel == tv2->vval.v_channel;
6442#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006443 case VAR_FUNC:
6444 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006445 case VAR_UNKNOWN:
6446 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006448
Bram Moolenaara03f2332016-02-06 18:09:59 +01006449 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6450 * does not equal anything, not even itself. */
6451 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452}
6453
6454/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 * Locate item with index "n" in list "l" and return it.
6456 * A negative index is counted from the end; -1 is the last item.
6457 * Returns NULL when "n" is out of range.
6458 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006459 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461{
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463 long idx;
6464
6465 if (l == NULL)
6466 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006467
6468 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006470 n = l->lv_len + n;
6471
6472 /* Check for index out of range. */
6473 if (n < 0 || n >= l->lv_len)
6474 return NULL;
6475
6476 /* When there is a cached index may start search from there. */
6477 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006479 if (n < l->lv_idx / 2)
6480 {
6481 /* closest to the start of the list */
6482 item = l->lv_first;
6483 idx = 0;
6484 }
6485 else if (n > (l->lv_idx + l->lv_len) / 2)
6486 {
6487 /* closest to the end of the list */
6488 item = l->lv_last;
6489 idx = l->lv_len - 1;
6490 }
6491 else
6492 {
6493 /* closest to the cached index */
6494 item = l->lv_idx_item;
6495 idx = l->lv_idx;
6496 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 }
6498 else
6499 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006500 if (n < l->lv_len / 2)
6501 {
6502 /* closest to the start of the list */
6503 item = l->lv_first;
6504 idx = 0;
6505 }
6506 else
6507 {
6508 /* closest to the end of the list */
6509 item = l->lv_last;
6510 idx = l->lv_len - 1;
6511 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006513
6514 while (n > idx)
6515 {
6516 /* search forward */
6517 item = item->li_next;
6518 ++idx;
6519 }
6520 while (n < idx)
6521 {
6522 /* search backward */
6523 item = item->li_prev;
6524 --idx;
6525 }
6526
6527 /* cache the used index */
6528 l->lv_idx = idx;
6529 l->lv_idx_item = item;
6530
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 return item;
6532}
6533
6534/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006535 * Get list item "l[idx]" as a number.
6536 */
6537 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_find_nr(
6539 list_T *l,
6540 long idx,
6541 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006542{
6543 listitem_T *li;
6544
6545 li = list_find(l, idx);
6546 if (li == NULL)
6547 {
6548 if (errorp != NULL)
6549 *errorp = TRUE;
6550 return -1L;
6551 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006552 return (long)get_tv_number_chk(&li->li_tv, errorp);
Bram Moolenaara5525202006-03-02 22:52:09 +00006553}
6554
6555/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006556 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6557 */
6558 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006560{
6561 listitem_T *li;
6562
6563 li = list_find(l, idx - 1);
6564 if (li == NULL)
6565 {
6566 EMSGN(_(e_listidx), idx);
6567 return NULL;
6568 }
6569 return get_tv_string(&li->li_tv);
6570}
6571
6572/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006573 * Locate "item" list "l" and return its index.
6574 * Returns -1 when "item" is not in the list.
6575 */
6576 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006578{
6579 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006581
6582 if (l == NULL)
6583 return -1;
6584 idx = 0;
6585 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6586 ++idx;
6587 if (li == NULL)
6588 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006589 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006590}
6591
6592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 * Append item "item" to the end of list "l".
6594 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006596list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597{
6598 if (l->lv_last == NULL)
6599 {
6600 /* empty list */
6601 l->lv_first = item;
6602 l->lv_last = item;
6603 item->li_prev = NULL;
6604 }
6605 else
6606 {
6607 l->lv_last->li_next = item;
6608 item->li_prev = l->lv_last;
6609 l->lv_last = item;
6610 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006611 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 item->li_next = NULL;
6613}
6614
6615/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006617 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006620list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006622 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623
Bram Moolenaar05159a02005-02-26 23:04:13 +00006624 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006626 copy_tv(tv, &li->li_tv);
6627 list_append(l, li);
6628 return OK;
6629}
6630
6631/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006632 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006633 * Return FAIL when out of memory.
6634 */
6635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006637{
6638 listitem_T *li = listitem_alloc();
6639
6640 if (li == NULL)
6641 return FAIL;
6642 li->li_tv.v_type = VAR_DICT;
6643 li->li_tv.v_lock = 0;
6644 li->li_tv.vval.v_dict = dict;
6645 list_append(list, li);
6646 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 return OK;
6648}
6649
6650/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006651 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006652 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006653 * Returns FAIL when out of memory.
6654 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006656list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006657{
6658 listitem_T *li = listitem_alloc();
6659
6660 if (li == NULL)
6661 return FAIL;
6662 list_append(l, li);
6663 li->li_tv.v_type = VAR_STRING;
6664 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006665 if (str == NULL)
6666 li->li_tv.vval.v_string = NULL;
6667 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006668 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006669 return FAIL;
6670 return OK;
6671}
6672
6673/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006674 * Append "n" to list "l".
6675 * Returns FAIL when out of memory.
6676 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006678list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006679{
6680 listitem_T *li;
6681
6682 li = listitem_alloc();
6683 if (li == NULL)
6684 return FAIL;
6685 li->li_tv.v_type = VAR_NUMBER;
6686 li->li_tv.v_lock = 0;
6687 li->li_tv.vval.v_number = n;
6688 list_append(l, li);
6689 return OK;
6690}
6691
6692/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006693 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006694 * If "item" is NULL append at the end.
6695 * Return FAIL when out of memory.
6696 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006699{
Bram Moolenaar33570922005-01-25 22:26:29 +00006700 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006701
6702 if (ni == NULL)
6703 return FAIL;
6704 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006705 list_insert(l, ni, item);
6706 return OK;
6707}
6708
6709 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006710list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006711{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006712 if (item == NULL)
6713 /* Append new item at end of list. */
6714 list_append(l, ni);
6715 else
6716 {
6717 /* Insert new item before existing item. */
6718 ni->li_prev = item->li_prev;
6719 ni->li_next = item;
6720 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006721 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006722 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006723 ++l->lv_idx;
6724 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006725 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006726 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006727 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006728 l->lv_idx_item = NULL;
6729 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006730 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006731 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006733}
6734
6735/*
6736 * Extend "l1" with "l2".
6737 * If "bef" is NULL append at the end, otherwise insert before this item.
6738 * Returns FAIL when out of memory.
6739 */
6740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006741list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742{
Bram Moolenaar33570922005-01-25 22:26:29 +00006743 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006744 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006745
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006746 /* We also quit the loop when we have inserted the original item count of
6747 * the list, avoid a hang when we extend a list with itself. */
6748 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006749 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6750 return FAIL;
6751 return OK;
6752}
6753
6754/*
6755 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6756 * Return FAIL when out of memory.
6757 */
6758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006759list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006760{
Bram Moolenaar33570922005-01-25 22:26:29 +00006761 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006762
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006763 if (l1 == NULL || l2 == NULL)
6764 return FAIL;
6765
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006766 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006768 if (l == NULL)
6769 return FAIL;
6770 tv->v_type = VAR_LIST;
6771 tv->vval.v_list = l;
6772
6773 /* append all items from the second list */
6774 return list_extend(l, l2, NULL);
6775}
6776
6777/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006778 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006779 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006780 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781 * Returns NULL when out of memory.
6782 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006784list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006785{
Bram Moolenaar33570922005-01-25 22:26:29 +00006786 list_T *copy;
6787 listitem_T *item;
6788 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006789
6790 if (orig == NULL)
6791 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792
6793 copy = list_alloc();
6794 if (copy != NULL)
6795 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006796 if (copyID != 0)
6797 {
6798 /* Do this before adding the items, because one of the items may
6799 * refer back to this list. */
6800 orig->lv_copyID = copyID;
6801 orig->lv_copylist = copy;
6802 }
6803 for (item = orig->lv_first; item != NULL && !got_int;
6804 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006805 {
6806 ni = listitem_alloc();
6807 if (ni == NULL)
6808 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006810 {
6811 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6812 {
6813 vim_free(ni);
6814 break;
6815 }
6816 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006817 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006818 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006819 list_append(copy, ni);
6820 }
6821 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006822 if (item != NULL)
6823 {
6824 list_unref(copy);
6825 copy = NULL;
6826 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006827 }
6828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006829 return copy;
6830}
6831
6832/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006833 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006834 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006835 * This used to be called list_remove, but that conflicts with a Sun header
6836 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006837 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006839vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006840{
Bram Moolenaar33570922005-01-25 22:26:29 +00006841 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006842
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006843 /* notify watchers */
6844 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006846 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006847 list_fix_watch(l, ip);
6848 if (ip == item2)
6849 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006850 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006851
6852 if (item2->li_next == NULL)
6853 l->lv_last = item->li_prev;
6854 else
6855 item2->li_next->li_prev = item->li_prev;
6856 if (item->li_prev == NULL)
6857 l->lv_first = item2->li_next;
6858 else
6859 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006860 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006861}
6862
6863/*
6864 * Return an allocated string with the string representation of a list.
6865 * May return NULL.
6866 */
6867 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006868list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006869{
6870 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006871
6872 if (tv->vval.v_list == NULL)
6873 return NULL;
6874 ga_init2(&ga, (int)sizeof(char), 80);
6875 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006876 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6877 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006878 {
6879 vim_free(ga.ga_data);
6880 return NULL;
6881 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006882 ga_append(&ga, ']');
6883 ga_append(&ga, NUL);
6884 return (char_u *)ga.ga_data;
6885}
6886
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006887typedef struct join_S {
6888 char_u *s;
6889 char_u *tofree;
6890} join_T;
6891
6892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006893list_join_inner(
6894 garray_T *gap, /* to store the result in */
6895 list_T *l,
6896 char_u *sep,
6897 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006898 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006899 int copyID,
6900 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006901{
6902 int i;
6903 join_T *p;
6904 int len;
6905 int sumlen = 0;
6906 int first = TRUE;
6907 char_u *tofree;
6908 char_u numbuf[NUMBUFLEN];
6909 listitem_T *item;
6910 char_u *s;
6911
6912 /* Stringify each item in the list. */
6913 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6914 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006915 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6916 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006917 if (s == NULL)
6918 return FAIL;
6919
6920 len = (int)STRLEN(s);
6921 sumlen += len;
6922
Bram Moolenaarcde88542015-08-11 19:14:00 +02006923 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006924 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6925 if (tofree != NULL || s != numbuf)
6926 {
6927 p->s = s;
6928 p->tofree = tofree;
6929 }
6930 else
6931 {
6932 p->s = vim_strnsave(s, len);
6933 p->tofree = p->s;
6934 }
6935
6936 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006937 if (did_echo_string_emsg) /* recursion error, bail out */
6938 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006939 }
6940
6941 /* Allocate result buffer with its total size, avoid re-allocation and
6942 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6943 if (join_gap->ga_len >= 2)
6944 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6945 if (ga_grow(gap, sumlen + 2) == FAIL)
6946 return FAIL;
6947
6948 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6949 {
6950 if (first)
6951 first = FALSE;
6952 else
6953 ga_concat(gap, sep);
6954 p = ((join_T *)join_gap->ga_data) + i;
6955
6956 if (p->s != NULL)
6957 ga_concat(gap, p->s);
6958 line_breakcheck();
6959 }
6960
6961 return OK;
6962}
6963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006964/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006965 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006966 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006967 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006968 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006970list_join(
6971 garray_T *gap,
6972 list_T *l,
6973 char_u *sep,
6974 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006975 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006976 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006977{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006978 garray_T join_ga;
6979 int retval;
6980 join_T *p;
6981 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006982
Bram Moolenaard39a7512015-04-16 22:51:22 +02006983 if (l->lv_len < 1)
6984 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006985 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006986 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6987 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006988
6989 /* Dispose each item in join_ga. */
6990 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006991 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006992 p = (join_T *)join_ga.ga_data;
6993 for (i = 0; i < join_ga.ga_len; ++i)
6994 {
6995 vim_free(p->tofree);
6996 ++p;
6997 }
6998 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006999 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01007000
7001 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007002}
7003
7004/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007005 * Return the next (unique) copy ID.
7006 * Used for serializing nested structures.
7007 */
7008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007009get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007010{
7011 current_copyID += COPYID_INC;
7012 return current_copyID;
7013}
7014
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007015/* Used by get_func_tv() */
7016static garray_T funcargs = GA_EMPTY;
7017
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007018/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 * Garbage collection for lists and dictionaries.
7020 *
7021 * We use reference counts to be able to free most items right away when they
7022 * are no longer used. But for composite items it's possible that it becomes
7023 * unused while the reference count is > 0: When there is a recursive
7024 * reference. Example:
7025 * :let l = [1, 2, 3]
7026 * :let d = {9: l}
7027 * :let l[1] = d
7028 *
7029 * Since this is quite unusual we handle this with garbage collection: every
7030 * once in a while find out which lists and dicts are not referenced from any
7031 * variable.
7032 *
7033 * Here is a good reference text about garbage collection (refers to Python
7034 * but it applies to all reference-counting mechanisms):
7035 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007036 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007037
7038/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007040 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007042 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007044garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007045{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007046 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 buf_T *buf;
7049 win_T *wp;
7050 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007051 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007052 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007053 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007054#ifdef FEAT_WINDOWS
7055 tabpage_T *tp;
7056#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007058 if (!testing)
7059 {
7060 /* Only do this once. */
7061 want_garbage_collect = FALSE;
7062 may_garbage_collect = FALSE;
7063 garbage_collect_at_exit = FALSE;
7064 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007065
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007066 /* We advance by two because we add one for items referenced through
7067 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007068 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007069
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 /*
7071 * 1. Go through all accessible variables and mark all lists and dicts
7072 * with copyID.
7073 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074
7075 /* Don't free variables in the previous_funccal list unless they are only
7076 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007077 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007078 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7079 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7081 NULL);
7082 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7083 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007084 }
7085
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086 /* script-local variables */
7087 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007089
7090 /* buffer-local variables */
7091 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7093 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094
7095 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007096 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7098 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007099#ifdef FEAT_AUTOCMD
7100 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7102 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007103#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007105#ifdef FEAT_WINDOWS
7106 /* tabpage-local variables */
7107 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7109 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007110#endif
7111
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114
7115 /* function-local variables */
7116 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7117 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7119 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 }
7121
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007122 /* function call arguments, if v:testing is set. */
7123 for (i = 0; i < funcargs.ga_len; ++i)
7124 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7125 copyID, NULL, NULL);
7126
Bram Moolenaard812df62008-11-09 12:46:09 +00007127 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007128 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007129
Bram Moolenaar1dced572012-04-05 16:54:08 +02007130#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007132#endif
7133
Bram Moolenaardb913952012-06-29 12:54:53 +02007134#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007136#endif
7137
7138#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007140#endif
7141
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007143 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007144 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007145#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007146#ifdef FEAT_NETBEANS_INTG
7147 abort = abort || set_ref_in_nb_channel(copyID);
7148#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007149
Bram Moolenaare3188e22016-05-31 21:13:04 +02007150#ifdef FEAT_TIMERS
7151 abort = abort || set_ref_in_timer(copyID);
7152#endif
7153
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007154 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007155 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 /*
7157 * 2. Free lists and dictionaries that are not referenced.
7158 */
7159 did_free = free_unref_items(copyID);
7160
7161 /*
7162 * 3. Check if any funccal can be freed now.
7163 */
7164 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007165 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 if (can_free_funccal(*pfc, copyID))
7167 {
7168 fc = *pfc;
7169 *pfc = fc->caller;
7170 free_funccal(fc, TRUE);
7171 did_free = TRUE;
7172 did_free_funccal = TRUE;
7173 }
7174 else
7175 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007176 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007177 if (did_free_funccal)
7178 /* When a funccal was freed some more items might be garbage
7179 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007180 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007181 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 else if (p_verbose > 0)
7183 {
7184 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7185 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007186
7187 return did_free;
7188}
7189
7190/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007191 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007192 */
7193 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007194free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007195{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007196 dict_T *dd, *dd_next;
7197 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007198 int did_free = FALSE;
7199
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007200 /* Let all "free" functions know that we are here. This means no
7201 * dictionaries, lists, channels or jobs are to be freed, because we will
7202 * do that here. */
7203 in_free_unref_items = TRUE;
7204
7205 /*
7206 * PASS 1: free the contents of the items. We don't free the items
7207 * themselves yet, so that it is possible to decrement refcount counters
7208 */
7209
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007211 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007212 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007213 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007214 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007215 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007216 /* Free the Dictionary and ordinary items it contains, but don't
7217 * recurse into Lists and Dictionaries, they will be in the list
7218 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007219 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007220 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007221 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007222
7223 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007224 * Go through the list of lists and free items without the copyID.
7225 * But don't free a list that has a watcher (used in a for loop), these
7226 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007227 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007228 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7229 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7230 && ll->lv_watch == NULL)
7231 {
7232 /* Free the List and ordinary items it contains, but don't recurse
7233 * into Lists and Dictionaries, they will be in the list of dicts
7234 * or list of lists. */
7235 list_free_contents(ll);
7236 did_free = TRUE;
7237 }
7238
7239#ifdef FEAT_JOB_CHANNEL
7240 /* Go through the list of jobs and free items without the copyID. This
7241 * must happen before doing channels, because jobs refer to channels, but
7242 * the reference from the channel to the job isn't tracked. */
7243 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7244
7245 /* Go through the list of channels and free items without the copyID. */
7246 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7247#endif
7248
7249 /*
7250 * PASS 2: free the items themselves.
7251 */
7252 for (dd = first_dict; dd != NULL; dd = dd_next)
7253 {
7254 dd_next = dd->dv_used_next;
7255 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7256 dict_free_dict(dd);
7257 }
7258
7259 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007260 {
7261 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007262 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7263 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007264 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007265 /* Free the List and ordinary items it contains, but don't recurse
7266 * into Lists and Dictionaries, they will be in the list of dicts
7267 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007268 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007269 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007270 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007271
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007272#ifdef FEAT_JOB_CHANNEL
7273 /* Go through the list of jobs and free items without the copyID. This
7274 * must happen before doing channels, because jobs refer to channels, but
7275 * the reference from the channel to the job isn't tracked. */
7276 free_unused_jobs(copyID, COPYID_MASK);
7277
7278 /* Go through the list of channels and free items without the copyID. */
7279 free_unused_channels(copyID, COPYID_MASK);
7280#endif
7281
7282 in_free_unref_items = FALSE;
7283
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007284 return did_free;
7285}
7286
7287/*
7288 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007289 * "list_stack" is used to add lists to be marked. Can be NULL.
7290 *
7291 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007292 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007294set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007295{
7296 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007297 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007298 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007299 hashtab_T *cur_ht;
7300 ht_stack_T *ht_stack = NULL;
7301 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007302
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007303 cur_ht = ht;
7304 for (;;)
7305 {
7306 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007307 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007308 /* Mark each item in the hashtab. If the item contains a hashtab
7309 * it is added to ht_stack, if it contains a list it is added to
7310 * list_stack. */
7311 todo = (int)cur_ht->ht_used;
7312 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7313 if (!HASHITEM_EMPTY(hi))
7314 {
7315 --todo;
7316 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7317 &ht_stack, list_stack);
7318 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007319 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007320
7321 if (ht_stack == NULL)
7322 break;
7323
7324 /* take an item from the stack */
7325 cur_ht = ht_stack->ht;
7326 tempitem = ht_stack;
7327 ht_stack = ht_stack->prev;
7328 free(tempitem);
7329 }
7330
7331 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007332}
7333
7334/*
7335 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007336 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7337 *
7338 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007339 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007341set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007342{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007343 listitem_T *li;
7344 int abort = FALSE;
7345 list_T *cur_l;
7346 list_stack_T *list_stack = NULL;
7347 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007348
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007349 cur_l = l;
7350 for (;;)
7351 {
7352 if (!abort)
7353 /* Mark each item in the list. If the item contains a hashtab
7354 * it is added to ht_stack, if it contains a list it is added to
7355 * list_stack. */
7356 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7357 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7358 ht_stack, &list_stack);
7359 if (list_stack == NULL)
7360 break;
7361
7362 /* take an item from the stack */
7363 cur_l = list_stack->list;
7364 tempitem = list_stack;
7365 list_stack = list_stack->prev;
7366 free(tempitem);
7367 }
7368
7369 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007370}
7371
7372/*
7373 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007374 * "list_stack" is used to add lists to be marked. Can be NULL.
7375 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7376 *
7377 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007378 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007380set_ref_in_item(
7381 typval_T *tv,
7382 int copyID,
7383 ht_stack_T **ht_stack,
7384 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007385{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007386 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007387
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007388 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007389 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007390 dict_T *dd = tv->vval.v_dict;
7391
Bram Moolenaara03f2332016-02-06 18:09:59 +01007392 if (dd != NULL && dd->dv_copyID != copyID)
7393 {
7394 /* Didn't see this dict yet. */
7395 dd->dv_copyID = copyID;
7396 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007397 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007398 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7399 }
7400 else
7401 {
7402 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7403 if (newitem == NULL)
7404 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007405 else
7406 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007407 newitem->ht = &dd->dv_hashtab;
7408 newitem->prev = *ht_stack;
7409 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007410 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007411 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007412 }
7413 }
7414 else if (tv->v_type == VAR_LIST)
7415 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007416 list_T *ll = tv->vval.v_list;
7417
Bram Moolenaara03f2332016-02-06 18:09:59 +01007418 if (ll != NULL && ll->lv_copyID != copyID)
7419 {
7420 /* Didn't see this list yet. */
7421 ll->lv_copyID = copyID;
7422 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007423 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007424 abort = set_ref_in_list(ll, copyID, ht_stack);
7425 }
7426 else
7427 {
7428 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007429 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007430 if (newitem == NULL)
7431 abort = TRUE;
7432 else
7433 {
7434 newitem->list = ll;
7435 newitem->prev = *list_stack;
7436 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007437 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007438 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007439 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007440 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007441 else if (tv->v_type == VAR_PARTIAL)
7442 {
7443 partial_T *pt = tv->vval.v_partial;
7444 int i;
7445
7446 /* A partial does not have a copyID, because it cannot contain itself.
7447 */
7448 if (pt != NULL)
7449 {
7450 if (pt->pt_dict != NULL)
7451 {
7452 typval_T dtv;
7453
7454 dtv.v_type = VAR_DICT;
7455 dtv.vval.v_dict = pt->pt_dict;
7456 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7457 }
7458
7459 for (i = 0; i < pt->pt_argc; ++i)
7460 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7461 ht_stack, list_stack);
7462 }
7463 }
7464#ifdef FEAT_JOB_CHANNEL
7465 else if (tv->v_type == VAR_JOB)
7466 {
7467 job_T *job = tv->vval.v_job;
7468 typval_T dtv;
7469
7470 if (job != NULL && job->jv_copyID != copyID)
7471 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007472 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007473 if (job->jv_channel != NULL)
7474 {
7475 dtv.v_type = VAR_CHANNEL;
7476 dtv.vval.v_channel = job->jv_channel;
7477 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7478 }
7479 if (job->jv_exit_partial != NULL)
7480 {
7481 dtv.v_type = VAR_PARTIAL;
7482 dtv.vval.v_partial = job->jv_exit_partial;
7483 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7484 }
7485 }
7486 }
7487 else if (tv->v_type == VAR_CHANNEL)
7488 {
7489 channel_T *ch =tv->vval.v_channel;
7490 int part;
7491 typval_T dtv;
7492 jsonq_T *jq;
7493 cbq_T *cq;
7494
7495 if (ch != NULL && ch->ch_copyID != copyID)
7496 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007497 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007498 for (part = PART_SOCK; part <= PART_IN; ++part)
7499 {
7500 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7501 jq = jq->jq_next)
7502 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7503 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7504 cq = cq->cq_next)
7505 if (cq->cq_partial != NULL)
7506 {
7507 dtv.v_type = VAR_PARTIAL;
7508 dtv.vval.v_partial = cq->cq_partial;
7509 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7510 }
7511 if (ch->ch_part[part].ch_partial != NULL)
7512 {
7513 dtv.v_type = VAR_PARTIAL;
7514 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7515 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7516 }
7517 }
7518 if (ch->ch_partial != NULL)
7519 {
7520 dtv.v_type = VAR_PARTIAL;
7521 dtv.vval.v_partial = ch->ch_partial;
7522 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7523 }
7524 if (ch->ch_close_partial != NULL)
7525 {
7526 dtv.v_type = VAR_PARTIAL;
7527 dtv.vval.v_partial = ch->ch_close_partial;
7528 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7529 }
7530 }
7531 }
7532#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007533 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007534}
7535
7536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 * Allocate an empty header for a dictionary.
7538 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007539 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007540dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541{
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007546 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007547 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007548 if (first_dict != NULL)
7549 first_dict->dv_used_prev = d;
7550 d->dv_used_next = first_dict;
7551 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007552 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007553
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007555 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007556 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007557 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007558 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007559 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561}
7562
7563/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007564 * Allocate an empty dict for a return value.
7565 * Returns OK or FAIL.
7566 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007568rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007569{
7570 dict_T *d = dict_alloc();
7571
7572 if (d == NULL)
7573 return FAIL;
7574
7575 rettv->vval.v_dict = d;
7576 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007577 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007578 ++d->dv_refcount;
7579 return OK;
7580}
7581
7582
7583/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 * Unreference a Dictionary: decrement the reference count and free it when it
7585 * becomes zero.
7586 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007588dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007590 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007591 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592}
7593
7594/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007595 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 * Ignores the reference count.
7597 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007598 static void
7599dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007601 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007603 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007605 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007606 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007607 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007610 if (!HASHITEM_EMPTY(hi))
7611 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007612 /* Remove the item before deleting it, just in case there is
7613 * something recursive causing trouble. */
7614 di = HI2DI(hi);
7615 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007616 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007617 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007618 --todo;
7619 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007622}
7623
7624 static void
7625dict_free_dict(dict_T *d)
7626{
7627 /* Remove the dict from the list of dicts for garbage collection. */
7628 if (d->dv_used_prev == NULL)
7629 first_dict = d->dv_used_next;
7630 else
7631 d->dv_used_prev->dv_used_next = d->dv_used_next;
7632 if (d->dv_used_next != NULL)
7633 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 vim_free(d);
7635}
7636
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007637 void
7638dict_free(dict_T *d)
7639{
7640 if (!in_free_unref_items)
7641 {
7642 dict_free_contents(d);
7643 dict_free_dict(d);
7644 }
7645}
7646
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647/*
7648 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007649 * The "key" is copied to the new item.
7650 * Note that the value of the item "di_tv" still needs to be initialized!
7651 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007653 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655{
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007658 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007662 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007663 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007664 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665}
7666
7667/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668 * Make a copy of a Dictionary item.
7669 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007671dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007672{
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007674
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007675 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7676 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677 if (di != NULL)
7678 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007679 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007680 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 copy_tv(&org->di_tv, &di->di_tv);
7682 }
7683 return di;
7684}
7685
7686/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687 * Remove item "item" from Dictionary "dict" and free it.
7688 */
7689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007690dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691{
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007693
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 if (HASHITEM_EMPTY(hi))
7696 EMSG2(_(e_intern2), "dictitem_remove()");
7697 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 dictitem_free(item);
7700}
7701
7702/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 * Free a dict item. Also clears the value.
7704 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007706dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007709 if (item->di_flags & DI_FLAGS_ALLOC)
7710 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711}
7712
7713/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7715 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007716 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007717 * Returns NULL when out of memory.
7718 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007720dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007721{
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 dict_T *copy;
7723 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007724 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007726
7727 if (orig == NULL)
7728 return NULL;
7729
7730 copy = dict_alloc();
7731 if (copy != NULL)
7732 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007733 if (copyID != 0)
7734 {
7735 orig->dv_copyID = copyID;
7736 orig->dv_copydict = copy;
7737 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007738 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007739 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007741 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007743 --todo;
7744
7745 di = dictitem_alloc(hi->hi_key);
7746 if (di == NULL)
7747 break;
7748 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007749 {
7750 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7751 copyID) == FAIL)
7752 {
7753 vim_free(di);
7754 break;
7755 }
7756 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007757 else
7758 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7759 if (dict_add(copy, di) == FAIL)
7760 {
7761 dictitem_free(di);
7762 break;
7763 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007765 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007766
Bram Moolenaare9a41262005-01-15 22:18:47 +00007767 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007768 if (todo > 0)
7769 {
7770 dict_unref(copy);
7771 copy = NULL;
7772 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007773 }
7774
7775 return copy;
7776}
7777
7778/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007780 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007783dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007784{
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786}
7787
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007789 * Add a number or string entry to dictionary "d".
7790 * When "str" is NULL use number "nr", otherwise use "str".
7791 * Returns FAIL when out of memory and when key already exists.
7792 */
7793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794dict_add_nr_str(
7795 dict_T *d,
7796 char *key,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007797 varnumber_T nr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007798 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007799{
7800 dictitem_T *item;
7801
7802 item = dictitem_alloc((char_u *)key);
7803 if (item == NULL)
7804 return FAIL;
7805 item->di_tv.v_lock = 0;
7806 if (str == NULL)
7807 {
7808 item->di_tv.v_type = VAR_NUMBER;
7809 item->di_tv.vval.v_number = nr;
7810 }
7811 else
7812 {
7813 item->di_tv.v_type = VAR_STRING;
7814 item->di_tv.vval.v_string = vim_strsave(str);
7815 }
7816 if (dict_add(d, item) == FAIL)
7817 {
7818 dictitem_free(item);
7819 return FAIL;
7820 }
7821 return OK;
7822}
7823
7824/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007825 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007826 * Returns FAIL when out of memory and when key already exists.
7827 */
7828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007829dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007830{
7831 dictitem_T *item;
7832
7833 item = dictitem_alloc((char_u *)key);
7834 if (item == NULL)
7835 return FAIL;
7836 item->di_tv.v_lock = 0;
7837 item->di_tv.v_type = VAR_LIST;
7838 item->di_tv.vval.v_list = list;
7839 if (dict_add(d, item) == FAIL)
7840 {
7841 dictitem_free(item);
7842 return FAIL;
7843 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007844 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007845 return OK;
7846}
7847
7848/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849 * Get the number of items in a Dictionary.
7850 */
7851 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007852dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 if (d == NULL)
7855 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007856 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007857}
7858
7859/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007860 * Find item "key[len]" in Dictionary "d".
7861 * If "len" is negative use strlen(key).
7862 * Returns NULL when not found.
7863 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007864 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007865dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007866{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007867#define AKEYLEN 200
7868 char_u buf[AKEYLEN];
7869 char_u *akey;
7870 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007871 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007872
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007873 if (d == NULL)
7874 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007875 if (len < 0)
7876 akey = key;
7877 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007878 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007879 tofree = akey = vim_strnsave(key, len);
7880 if (akey == NULL)
7881 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007882 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007883 else
7884 {
7885 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007886 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007887 akey = buf;
7888 }
7889
Bram Moolenaar33570922005-01-25 22:26:29 +00007890 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007891 vim_free(tofree);
7892 if (HASHITEM_EMPTY(hi))
7893 return NULL;
7894 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007895}
7896
7897/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007898 * Get a string item from a dictionary.
7899 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007900 * Returns NULL if the entry doesn't exist or out of memory.
7901 */
7902 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007903get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007904{
7905 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007906 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007907
7908 di = dict_find(d, key, -1);
7909 if (di == NULL)
7910 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007911 s = get_tv_string(&di->di_tv);
7912 if (save && s != NULL)
7913 s = vim_strsave(s);
7914 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007915}
7916
7917/*
7918 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007919 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007920 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007921 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007922get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007923{
7924 dictitem_T *di;
7925
7926 di = dict_find(d, key, -1);
7927 if (di == NULL)
7928 return 0;
7929 return get_tv_number(&di->di_tv);
7930}
7931
7932/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 * Return an allocated string with the string representation of a Dictionary.
7934 * May return NULL.
7935 */
7936 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007937dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938{
7939 garray_T ga;
7940 int first = TRUE;
7941 char_u *tofree;
7942 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007946 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007948 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007949 return NULL;
7950 ga_init2(&ga, (int)sizeof(char), 80);
7951 ga_append(&ga, '{');
7952
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007953 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007954 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007956 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007957 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007958 --todo;
7959
7960 if (first)
7961 first = FALSE;
7962 else
7963 ga_concat(&ga, (char_u *)", ");
7964
7965 tofree = string_quote(hi->hi_key, FALSE);
7966 if (tofree != NULL)
7967 {
7968 ga_concat(&ga, tofree);
7969 vim_free(tofree);
7970 }
7971 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007972 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7973 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007974 if (s != NULL)
7975 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007976 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007977 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007978 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007979 line_breakcheck();
7980
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007983 if (todo > 0)
7984 {
7985 vim_free(ga.ga_data);
7986 return NULL;
7987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007988
7989 ga_append(&ga, '}');
7990 ga_append(&ga, NUL);
7991 return (char_u *)ga.ga_data;
7992}
7993
7994/*
7995 * Allocate a variable for a Dictionary and fill it from "*arg".
7996 * Return OK or FAIL. Returns NOTDONE for {expr}.
7997 */
7998 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007999get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008000{
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 dict_T *d = NULL;
8002 typval_T tvkey;
8003 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00008004 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00008005 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008006 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008007 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00008008
8009 /*
8010 * First check if it's not a curly-braces thing: {expr}.
8011 * Must do this without evaluating, otherwise a function may be called
8012 * twice. Unfortunately this means we need to call eval1() twice for the
8013 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008015 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 if (*start != '}')
8017 {
8018 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
8019 return FAIL;
8020 if (*start == '}')
8021 return NOTDONE;
8022 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008023
8024 if (evaluate)
8025 {
8026 d = dict_alloc();
8027 if (d == NULL)
8028 return FAIL;
8029 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008030 tvkey.v_type = VAR_UNKNOWN;
8031 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008032
8033 *arg = skipwhite(*arg + 1);
8034 while (**arg != '}' && **arg != NUL)
8035 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008036 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008037 goto failret;
8038 if (**arg != ':')
8039 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008040 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008041 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008042 goto failret;
8043 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008044 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008045 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008046 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008047 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008048 {
8049 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008050 clear_tv(&tvkey);
8051 goto failret;
8052 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008053 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054
8055 *arg = skipwhite(*arg + 1);
8056 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8057 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008058 if (evaluate)
8059 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008060 goto failret;
8061 }
8062 if (evaluate)
8063 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008064 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 if (item != NULL)
8066 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008067 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008068 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 clear_tv(&tv);
8070 goto failret;
8071 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008072 item = dictitem_alloc(key);
8073 clear_tv(&tvkey);
8074 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008075 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008076 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008077 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008078 if (dict_add(d, item) == FAIL)
8079 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008080 }
8081 }
8082
8083 if (**arg == '}')
8084 break;
8085 if (**arg != ',')
8086 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008087 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008088 goto failret;
8089 }
8090 *arg = skipwhite(*arg + 1);
8091 }
8092
8093 if (**arg != '}')
8094 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008095 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008096failret:
8097 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008098 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008099 return FAIL;
8100 }
8101
8102 *arg = skipwhite(*arg + 1);
8103 if (evaluate)
8104 {
8105 rettv->v_type = VAR_DICT;
8106 rettv->vval.v_dict = d;
8107 ++d->dv_refcount;
8108 }
8109
8110 return OK;
8111}
8112
Bram Moolenaar17a13432016-01-24 14:22:10 +01008113 static char *
8114get_var_special_name(int nr)
8115{
8116 switch (nr)
8117 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008118 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008119 case VVAL_TRUE: return "v:true";
8120 case VVAL_NONE: return "v:none";
8121 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008122 }
8123 EMSG2(_(e_intern2), "get_var_special_name()");
8124 return "42";
8125}
8126
Bram Moolenaar8c711452005-01-14 21:53:12 +00008127/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128 * Return a string with the string representation of a variable.
8129 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008130 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008131 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008132 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8133 * "string()", otherwise does not put quotes around strings, as ":echo"
8134 * displays values.
8135 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8136 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008137 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 */
8139 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008140echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008141 typval_T *tv,
8142 char_u **tofree,
8143 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008144 int copyID,
8145 int echo_style,
8146 int restore_copyID,
8147 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008149 static int recurse = 0;
8150 char_u *r = NULL;
8151
Bram Moolenaar33570922005-01-25 22:26:29 +00008152 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008153 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008154 if (!did_echo_string_emsg)
8155 {
8156 /* Only give this message once for a recursive call to avoid
8157 * flooding the user with errors. And stop iterating over lists
8158 * and dicts. */
8159 did_echo_string_emsg = TRUE;
8160 EMSG(_("E724: variable nested too deep for displaying"));
8161 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008162 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008163 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008164 }
8165 ++recurse;
8166
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008167 switch (tv->v_type)
8168 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008169 case VAR_STRING:
8170 if (echo_style && !dict_val)
8171 {
8172 *tofree = NULL;
8173 r = get_tv_string_buf(tv, numbuf);
8174 }
8175 else
8176 {
8177 *tofree = string_quote(tv->vval.v_string, FALSE);
8178 r = *tofree;
8179 }
8180 break;
8181
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008182 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008183 if (echo_style)
8184 {
8185 *tofree = NULL;
8186 r = tv->vval.v_string;
8187 }
8188 else
8189 {
8190 *tofree = string_quote(tv->vval.v_string, TRUE);
8191 r = *tofree;
8192 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008193 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008194
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008195 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008196 {
8197 partial_T *pt = tv->vval.v_partial;
8198 char_u *fname = string_quote(pt == NULL ? NULL
8199 : pt->pt_name, FALSE);
8200 garray_T ga;
8201 int i;
8202 char_u *tf;
8203
8204 ga_init2(&ga, 1, 100);
8205 ga_concat(&ga, (char_u *)"function(");
8206 if (fname != NULL)
8207 {
8208 ga_concat(&ga, fname);
8209 vim_free(fname);
8210 }
8211 if (pt != NULL && pt->pt_argc > 0)
8212 {
8213 ga_concat(&ga, (char_u *)", [");
8214 for (i = 0; i < pt->pt_argc; ++i)
8215 {
8216 if (i > 0)
8217 ga_concat(&ga, (char_u *)", ");
8218 ga_concat(&ga,
8219 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8220 vim_free(tf);
8221 }
8222 ga_concat(&ga, (char_u *)"]");
8223 }
8224 if (pt != NULL && pt->pt_dict != NULL)
8225 {
8226 typval_T dtv;
8227
8228 ga_concat(&ga, (char_u *)", ");
8229 dtv.v_type = VAR_DICT;
8230 dtv.vval.v_dict = pt->pt_dict;
8231 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8232 vim_free(tf);
8233 }
8234 ga_concat(&ga, (char_u *)")");
8235
8236 *tofree = ga.ga_data;
8237 r = *tofree;
8238 break;
8239 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008240
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008241 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008242 if (tv->vval.v_list == NULL)
8243 {
8244 *tofree = NULL;
8245 r = NULL;
8246 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008247 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8248 && tv->vval.v_list->lv_len > 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_list->lv_copyID;
8256
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008257 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008258 *tofree = list2string(tv, copyID, restore_copyID);
8259 if (restore_copyID)
8260 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008261 r = *tofree;
8262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008263 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008264
Bram Moolenaar8c711452005-01-14 21:53:12 +00008265 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008266 if (tv->vval.v_dict == NULL)
8267 {
8268 *tofree = NULL;
8269 r = NULL;
8270 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008271 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8272 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008273 {
8274 *tofree = NULL;
8275 r = (char_u *)"{...}";
8276 }
8277 else
8278 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008279 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008280 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008281 *tofree = dict2string(tv, copyID, restore_copyID);
8282 if (restore_copyID)
8283 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008284 r = *tofree;
8285 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008286 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008287
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008288 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008289 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008290 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008291 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008292 *tofree = NULL;
8293 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008294 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008296 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008297#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298 *tofree = NULL;
8299 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8300 r = numbuf;
8301 break;
8302#endif
8303
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008304 case VAR_SPECIAL:
8305 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008306 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008307 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008308 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008309
Bram Moolenaar8502c702014-06-17 12:51:16 +02008310 if (--recurse == 0)
8311 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008312 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008313}
8314
8315/*
8316 * Return a string with the string representation of a variable.
8317 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8318 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008319 * Does not put quotes around strings, as ":echo" displays values.
8320 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8321 * May return NULL.
8322 */
8323 static char_u *
8324echo_string(
8325 typval_T *tv,
8326 char_u **tofree,
8327 char_u *numbuf,
8328 int copyID)
8329{
8330 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8331}
8332
8333/*
8334 * Return a string with the string representation of a variable.
8335 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8336 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008337 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008338 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008339 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008340 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008341tv2string(
8342 typval_T *tv,
8343 char_u **tofree,
8344 char_u *numbuf,
8345 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008346{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008347 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008348}
8349
8350/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 * Return string "str" in ' quotes, doubling ' characters.
8352 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008353 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008354 */
8355 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008356string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008357{
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008359 char_u *p, *r, *s;
8360
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 len = (function ? 13 : 3);
8362 if (str != NULL)
8363 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008364 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008365 for (p = str; *p != NUL; mb_ptr_adv(p))
8366 if (*p == '\'')
8367 ++len;
8368 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008369 s = r = alloc(len);
8370 if (r != NULL)
8371 {
8372 if (function)
8373 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008374 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008375 r += 10;
8376 }
8377 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008378 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 if (str != NULL)
8380 for (p = str; *p != NUL; )
8381 {
8382 if (*p == '\'')
8383 *r++ = '\'';
8384 MB_COPY_CHAR(p, r);
8385 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008386 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008387 if (function)
8388 *r++ = ')';
8389 *r++ = NUL;
8390 }
8391 return s;
8392}
8393
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008394#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008395/*
8396 * Convert the string "text" to a floating point number.
8397 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8398 * this always uses a decimal point.
8399 * Returns the length of the text that was consumed.
8400 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008402string2float(
8403 char_u *text,
8404 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008405{
8406 char *s = (char *)text;
8407 float_T f;
8408
8409 f = strtod(s, &s);
8410 *value = f;
8411 return (int)((char_u *)s - text);
8412}
8413#endif
8414
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 * Get the value of an environment variable.
8417 * "arg" is pointing to the '$'. It is advanced to after the name.
8418 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008419 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 */
8421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008422get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
8424 char_u *string = NULL;
8425 int len;
8426 int cc;
8427 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008428 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429
8430 ++*arg;
8431 name = *arg;
8432 len = get_env_len(arg);
8433 if (evaluate)
8434 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008435 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008436 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008437
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008438 cc = name[len];
8439 name[len] = NUL;
8440 /* first try vim_getenv(), fast for normal environment vars */
8441 string = vim_getenv(name, &mustfree);
8442 if (string != NULL && *string != NUL)
8443 {
8444 if (!mustfree)
8445 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008447 else
8448 {
8449 if (mustfree)
8450 vim_free(string);
8451
8452 /* next try expanding things like $VIM and ${HOME} */
8453 string = expand_env_save(name - 1);
8454 if (string != NULL && *string == '$')
8455 {
8456 vim_free(string);
8457 string = NULL;
8458 }
8459 }
8460 name[len] = cc;
8461
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008462 rettv->v_type = VAR_STRING;
8463 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 }
8465
8466 return OK;
8467}
8468
8469/*
8470 * Array with names and number of arguments of all internal functions
8471 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8472 */
8473static struct fst
8474{
8475 char *f_name; /* function name */
8476 char f_min_argc; /* minimal number of arguments */
8477 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008478 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008479 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480} functions[] =
8481{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008482#ifdef FEAT_FLOAT
8483 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008484 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008485#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008486 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008487 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {"append", 2, 2, f_append},
8489 {"argc", 0, 0, f_argc},
8490 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008491 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008492 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008493#ifdef FEAT_FLOAT
8494 {"asin", 1, 1, f_asin}, /* WJMc */
8495#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008496 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008497 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008498 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008499 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008500 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008501 {"assert_notequal", 2, 3, f_assert_notequal},
8502 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008503 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008504#ifdef FEAT_FLOAT
8505 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008506 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008509 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {"bufexists", 1, 1, f_bufexists},
8511 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8512 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8513 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8514 {"buflisted", 1, 1, f_buflisted},
8515 {"bufloaded", 1, 1, f_bufloaded},
8516 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008517 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008518 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {"bufwinnr", 1, 1, f_bufwinnr},
8520 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008521 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008522 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008523 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008524#ifdef FEAT_FLOAT
8525 {"ceil", 1, 1, f_ceil},
8526#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008527#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008528 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008529 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8530 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008531 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008532 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008533 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008534 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008535 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008536 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008537 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008538 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008539 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8540 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008541 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008542 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008543#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008544 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008545 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008547 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008549#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008550 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008551 {"complete_add", 1, 1, f_complete_add},
8552 {"complete_check", 0, 0, f_complete_check},
8553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008555 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008556#ifdef FEAT_FLOAT
8557 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008558 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008559#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008560 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008562 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008563 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008564 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008566 {"diff_filler", 1, 1, f_diff_filler},
8567 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008568 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008570 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {"eventhandler", 0, 0, f_eventhandler},
8572 {"executable", 1, 1, f_executable},
Bram Moolenaar79815f12016-07-09 17:07:29 +02008573 {"execute", 1, 2, f_execute},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008574 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008576#ifdef FEAT_FLOAT
8577 {"exp", 1, 1, f_exp},
8578#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008579 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008580 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008581 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8583 {"filereadable", 1, 1, f_filereadable},
8584 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008585 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008586 {"finddir", 1, 3, f_finddir},
8587 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008588#ifdef FEAT_FLOAT
8589 {"float2nr", 1, 1, f_float2nr},
8590 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008591 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008592#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008593 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {"fnamemodify", 2, 2, f_fnamemodify},
8595 {"foldclosed", 1, 1, f_foldclosed},
8596 {"foldclosedend", 1, 1, f_foldclosedend},
8597 {"foldlevel", 1, 1, f_foldlevel},
8598 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008599 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008601 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008602 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008603 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008604 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008605 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 {"getchar", 0, 1, f_getchar},
8607 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008608 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {"getcmdline", 0, 0, f_getcmdline},
8610 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008611 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008612 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02008613#if defined(FEAT_CMDL_COMPL)
8614 {"getcompletion", 2, 2, f_getcompletion},
8615#endif
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008616 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008617 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008618 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008619 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 {"getfsize", 1, 1, f_getfsize},
8621 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008622 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008623 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008624 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008625 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008626 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008627 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008628 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008629 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008631 {"gettabvar", 2, 3, f_gettabvar},
8632 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {"getwinposx", 0, 0, f_getwinposx},
8634 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008635 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008636 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008637 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008638 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008641 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008642 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8644 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8645 {"histadd", 2, 2, f_histadd},
8646 {"histdel", 1, 2, f_histdel},
8647 {"histget", 1, 2, f_histget},
8648 {"histnr", 1, 1, f_histnr},
8649 {"hlID", 1, 1, f_hlID},
8650 {"hlexists", 1, 1, f_hlexists},
8651 {"hostname", 0, 0, f_hostname},
8652 {"iconv", 3, 3, f_iconv},
8653 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008654 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008655 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008657 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 {"inputrestore", 0, 0, f_inputrestore},
8659 {"inputsave", 0, 0, f_inputsave},
8660 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008662 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008664 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008665#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8666 {"isnan", 1, 1, f_isnan},
8667#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008668 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008669#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008670 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008671 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008672 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008673 {"job_start", 1, 2, f_job_start},
8674 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008675 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008676#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008677 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008678 {"js_decode", 1, 1, f_js_decode},
8679 {"js_encode", 1, 1, f_js_encode},
8680 {"json_decode", 1, 1, f_json_decode},
8681 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008682 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008684 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {"libcall", 3, 3, f_libcall},
8686 {"libcallnr", 3, 3, f_libcallnr},
8687 {"line", 1, 1, f_line},
8688 {"line2byte", 1, 1, f_line2byte},
8689 {"lispindent", 1, 1, f_lispindent},
8690 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008691#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008692 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008693 {"log10", 1, 1, f_log10},
8694#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008695#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008696 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008697#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008698 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008699 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008700 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008701 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008702 {"matchadd", 2, 5, f_matchadd},
8703 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008704 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008705 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008706 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008707 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008708 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008709 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008710 {"max", 1, 1, f_max},
8711 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008712#ifdef vim_mkdir
8713 {"mkdir", 1, 3, f_mkdir},
8714#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008715 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008716#ifdef FEAT_MZSCHEME
8717 {"mzeval", 1, 1, f_mzeval},
8718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008720 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008721 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008722 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008723#ifdef FEAT_PERL
8724 {"perleval", 1, 1, f_perleval},
8725#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008726#ifdef FEAT_FLOAT
8727 {"pow", 2, 2, f_pow},
8728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008730 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008731 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008732#ifdef FEAT_PYTHON3
8733 {"py3eval", 1, 1, f_py3eval},
8734#endif
8735#ifdef FEAT_PYTHON
8736 {"pyeval", 1, 1, f_pyeval},
8737#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008738 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008739 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008740 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008741#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008742 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008743#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008744 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 {"remote_expr", 2, 3, f_remote_expr},
8746 {"remote_foreground", 1, 1, f_remote_foreground},
8747 {"remote_peek", 1, 2, f_remote_peek},
8748 {"remote_read", 1, 1, f_remote_read},
8749 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008750 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008752 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008754 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008755#ifdef FEAT_FLOAT
8756 {"round", 1, 1, f_round},
8757#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008758 {"screenattr", 2, 2, f_screenattr},
8759 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008760 {"screencol", 0, 0, f_screencol},
8761 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008762 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008763 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008764 {"searchpair", 3, 7, f_searchpair},
8765 {"searchpairpos", 3, 7, f_searchpairpos},
8766 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 {"server2client", 2, 2, f_server2client},
8768 {"serverlist", 0, 0, f_serverlist},
8769 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008770 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008772 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008774 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008775 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008776 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008777 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008779 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008780 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008782#ifdef FEAT_CRYPT
8783 {"sha256", 1, 1, f_sha256},
8784#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008785 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008786 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008788#ifdef FEAT_FLOAT
8789 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008790 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008791#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008792 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008793 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008794 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008795 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008796 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008797#ifdef FEAT_FLOAT
8798 {"sqrt", 1, 1, f_sqrt},
8799 {"str2float", 1, 1, f_str2float},
8800#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008801 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008802 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008803 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008804 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805#ifdef HAVE_STRFTIME
8806 {"strftime", 1, 2, f_strftime},
8807#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008808 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008810 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 {"strlen", 1, 1, f_strlen},
8812 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008813 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008815 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008816 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {"substitute", 4, 4, f_substitute},
8818 {"synID", 3, 3, f_synID},
8819 {"synIDattr", 2, 3, f_synIDattr},
8820 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008821 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008822 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008823 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008824 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008825 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008826 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008827 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008828 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008829 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008830#ifdef FEAT_FLOAT
8831 {"tan", 1, 1, f_tan},
8832 {"tanh", 1, 1, f_tanh},
8833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008835 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
Bram Moolenaar5c719942016-07-09 23:40:45 +02008836 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008837 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008838 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8839#ifdef FEAT_JOB_CHANNEL
8840 {"test_null_channel", 0, 0, f_test_null_channel},
8841#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008842 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008843#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008844 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008845#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008846 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008847 {"test_null_partial", 0, 0, f_test_null_partial},
8848 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008849 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008850#ifdef FEAT_TIMERS
8851 {"timer_start", 2, 3, f_timer_start},
8852 {"timer_stop", 1, 1, f_timer_stop},
8853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 {"tolower", 1, 1, f_tolower},
8855 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008856 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008857#ifdef FEAT_FLOAT
8858 {"trunc", 1, 1, f_trunc},
8859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008861 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008862 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008863 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008864 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {"virtcol", 1, 1, f_virtcol},
8866 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008867 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008868 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008869 {"win_getid", 0, 2, f_win_getid},
8870 {"win_gotoid", 1, 1, f_win_gotoid},
8871 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8872 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 {"winbufnr", 1, 1, f_winbufnr},
8874 {"wincol", 0, 0, f_wincol},
8875 {"winheight", 1, 1, f_winheight},
8876 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008877 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008879 {"winrestview", 1, 1, f_winrestview},
8880 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008882 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008883 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008884 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885};
8886
8887#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8888
8889/*
8890 * Function given to ExpandGeneric() to obtain the list of internal
8891 * or user defined function names.
8892 */
8893 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008894get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896 static int intidx = -1;
8897 char_u *name;
8898
8899 if (idx == 0)
8900 intidx = -1;
8901 if (intidx < 0)
8902 {
8903 name = get_user_func_name(xp, idx);
8904 if (name != NULL)
8905 return name;
8906 }
8907 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8908 {
8909 STRCPY(IObuff, functions[intidx].f_name);
8910 STRCAT(IObuff, "(");
8911 if (functions[intidx].f_max_argc == 0)
8912 STRCAT(IObuff, ")");
8913 return IObuff;
8914 }
8915
8916 return NULL;
8917}
8918
8919/*
8920 * Function given to ExpandGeneric() to obtain the list of internal or
8921 * user defined variable or function names.
8922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008924get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926 static int intidx = -1;
8927 char_u *name;
8928
8929 if (idx == 0)
8930 intidx = -1;
8931 if (intidx < 0)
8932 {
8933 name = get_function_name(xp, idx);
8934 if (name != NULL)
8935 return name;
8936 }
8937 return get_user_var_name(xp, ++intidx);
8938}
8939
8940#endif /* FEAT_CMDL_COMPL */
8941
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008942#if defined(EBCDIC) || defined(PROTO)
8943/*
8944 * Compare struct fst by function name.
8945 */
8946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008947compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008948{
8949 struct fst *p1 = (struct fst *)s1;
8950 struct fst *p2 = (struct fst *)s2;
8951
8952 return STRCMP(p1->f_name, p2->f_name);
8953}
8954
8955/*
8956 * Sort the function table by function name.
8957 * The sorting of the table above is ASCII dependant.
8958 * On machines using EBCDIC we have to sort it.
8959 */
8960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008961sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008962{
8963 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8964
8965 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8966}
8967#endif
8968
8969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970/*
8971 * Find internal function in table above.
8972 * Return index, or -1 if not found
8973 */
8974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008975find_internal_func(
8976 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978 int first = 0;
8979 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8980 int cmp;
8981 int x;
8982
8983 /*
8984 * Find the function name in the table. Binary search.
8985 */
8986 while (first <= last)
8987 {
8988 x = first + ((unsigned)(last - first) >> 1);
8989 cmp = STRCMP(name, functions[x].f_name);
8990 if (cmp < 0)
8991 last = x - 1;
8992 else if (cmp > 0)
8993 first = x + 1;
8994 else
8995 return x;
8996 }
8997 return -1;
8998}
8999
9000/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009001 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
9002 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01009003 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
9004 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 */
9006 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01009007deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009008{
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 int cc;
9011
Bram Moolenaar65639032016-03-16 21:40:30 +01009012 if (partialp != NULL)
9013 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009014
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009015 cc = name[*lenp];
9016 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01009017 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009018 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009022 {
9023 *lenp = 0;
9024 return (char_u *)""; /* just in case */
9025 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009026 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009028 }
9029
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009030 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
9031 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009032 partial_T *pt = v->di_tv.vval.v_partial;
9033
9034 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009035 {
9036 *lenp = 0;
9037 return (char_u *)""; /* just in case */
9038 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009039 if (partialp != NULL)
9040 *partialp = pt;
9041 *lenp = (int)STRLEN(pt->pt_name);
9042 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009043 }
9044
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009045 return name;
9046}
9047
9048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 * Allocate a variable for the result of a function.
9050 * Return OK or FAIL.
9051 */
9052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009053get_func_tv(
9054 char_u *name, /* name of the function */
9055 int len, /* length of "name" */
9056 typval_T *rettv,
9057 char_u **arg, /* argument, pointing to the '(' */
9058 linenr_T firstline, /* first line of range */
9059 linenr_T lastline, /* last line of range */
9060 int *doesrange, /* return: function handled range */
9061 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009062 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009063 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 char_u *argp;
9066 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009067 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 int argcount = 0; /* number of arguments found */
9069
9070 /*
9071 * Get the arguments.
9072 */
9073 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009074 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 {
9076 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9077 if (*argp == ')' || *argp == ',' || *argp == NUL)
9078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9080 {
9081 ret = FAIL;
9082 break;
9083 }
9084 ++argcount;
9085 if (*argp != ',')
9086 break;
9087 }
9088 if (*argp == ')')
9089 ++argp;
9090 else
9091 ret = FAIL;
9092
9093 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009094 {
9095 int i = 0;
9096
9097 if (get_vim_var_nr(VV_TESTING))
9098 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009099 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009100 * what variables are used on the call stack. */
9101 if (funcargs.ga_itemsize == 0)
9102 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9103 for (i = 0; i < argcount; ++i)
9104 if (ga_grow(&funcargs, 1) == OK)
9105 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9106 &argvars[i];
9107 }
9108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009110 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009111
9112 funcargs.ga_len -= i;
9113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009115 {
9116 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009117 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009118 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009119 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
9122 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124
9125 *arg = skipwhite(argp);
9126 return ret;
9127}
9128
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009129#define ERROR_UNKNOWN 0
9130#define ERROR_TOOMANY 1
9131#define ERROR_TOOFEW 2
9132#define ERROR_SCRIPT 3
9133#define ERROR_DICT 4
9134#define ERROR_NONE 5
9135#define ERROR_OTHER 6
9136#define FLEN_FIXED 40
9137
9138/*
9139 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9140 * Change <SNR>123_name() to K_SNR 123_name().
9141 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9142 * (slow).
9143 */
9144 static char_u *
9145fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9146{
9147 int llen;
9148 char_u *fname;
9149 int i;
9150
9151 llen = eval_fname_script(name);
9152 if (llen > 0)
9153 {
9154 fname_buf[0] = K_SPECIAL;
9155 fname_buf[1] = KS_EXTRA;
9156 fname_buf[2] = (int)KE_SNR;
9157 i = 3;
9158 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9159 {
9160 if (current_SID <= 0)
9161 *error = ERROR_SCRIPT;
9162 else
9163 {
9164 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9165 i = (int)STRLEN(fname_buf);
9166 }
9167 }
9168 if (i + STRLEN(name + llen) < FLEN_FIXED)
9169 {
9170 STRCPY(fname_buf + i, name + llen);
9171 fname = fname_buf;
9172 }
9173 else
9174 {
9175 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9176 if (fname == NULL)
9177 *error = ERROR_OTHER;
9178 else
9179 {
9180 *tofree = fname;
9181 mch_memmove(fname, fname_buf, (size_t)i);
9182 STRCPY(fname + i, name + llen);
9183 }
9184 }
9185 }
9186 else
9187 fname = name;
9188 return fname;
9189}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190
9191/*
9192 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009193 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009194 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009196 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009197call_func(
9198 char_u *funcname, /* name of the function */
9199 int len, /* length of "name" */
9200 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009201 int argcount_in, /* number of "argvars" */
9202 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009203 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009204 linenr_T firstline, /* first line of range */
9205 linenr_T lastline, /* last line of range */
9206 int *doesrange, /* return: function handled range */
9207 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009208 partial_T *partial, /* optional, can be NULL */
9209 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
9211 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 int error = ERROR_NONE;
9213 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009216 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009218 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009219 int argcount = argcount_in;
9220 typval_T *argvars = argvars_in;
9221 dict_T *selfdict = selfdict_in;
9222 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9223 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009224
9225 /* Make a copy of the name, if it comes from a funcref variable it could
9226 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009227 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009228 if (name == NULL)
9229 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009231 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
9233 *doesrange = FALSE;
9234
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009235 if (partial != NULL)
9236 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009237 /* When the function has a partial with a dict and there is a dict
9238 * argument, use the dict argument. That is backwards compatible.
9239 * When the dict was bound explicitly use the one from the partial. */
9240 if (partial->pt_dict != NULL
9241 && (selfdict_in == NULL || !partial->pt_auto))
9242 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009243 if (error == ERROR_NONE && partial->pt_argc > 0)
9244 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009245 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9246 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9247 for (i = 0; i < argcount_in; ++i)
9248 argv[i + argv_clear] = argvars_in[i];
9249 argvars = argv;
9250 argcount = partial->pt_argc + argcount_in;
9251 }
9252 }
9253
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254
9255 /* execute the function if no errors detected and executing */
9256 if (evaluate && error == ERROR_NONE)
9257 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009258 char_u *rfname = fname;
9259
9260 /* Ignore "g:" before a function name. */
9261 if (fname[0] == 'g' && fname[1] == ':')
9262 rfname = fname + 2;
9263
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009264 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9265 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 error = ERROR_UNKNOWN;
9267
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009268 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 {
9270 /*
9271 * User defined function.
9272 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009273 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009274
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009276 /* Trigger FuncUndefined event, may load the function. */
9277 if (fp == NULL
9278 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009279 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009280 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009282 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009283 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 }
9285#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009286 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009287 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009288 {
9289 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009290 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009291 }
9292
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 if (fp != NULL)
9294 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009295 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009297 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009299 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009301 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009302 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 else
9304 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009305 int did_save_redo = FALSE;
9306
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 /*
9308 * Call the user function.
9309 * Save and restore search patterns, script variables and
9310 * redo buffer.
9311 */
9312 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009313#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009314 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009315#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009316 {
9317 saveRedobuff();
9318 did_save_redo = TRUE;
9319 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009320 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009321 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009322 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009323 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9324 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9325 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009326 /* Function was unreferenced while being used, free it
9327 * now. */
9328 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009329 if (did_save_redo)
9330 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 restore_search_patterns();
9332 error = ERROR_NONE;
9333 }
9334 }
9335 }
9336 else
9337 {
9338 /*
9339 * Find the function name in the table, call its implementation.
9340 */
9341 i = find_internal_func(fname);
9342 if (i >= 0)
9343 {
9344 if (argcount < functions[i].f_min_argc)
9345 error = ERROR_TOOFEW;
9346 else if (argcount > functions[i].f_max_argc)
9347 error = ERROR_TOOMANY;
9348 else
9349 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009350 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 error = ERROR_NONE;
9353 }
9354 }
9355 }
9356 /*
9357 * The function call (or "FuncUndefined" autocommand sequence) might
9358 * have been aborted by an error, an interrupt, or an explicitly thrown
9359 * exception that has not been caught so far. This situation can be
9360 * tested for by calling aborting(). For an error in an internal
9361 * function or for the "E132" error in call_user_func(), however, the
9362 * throw point at which the "force_abort" flag (temporarily reset by
9363 * emsg()) is normally updated has not been reached yet. We need to
9364 * update that flag first to make aborting() reliable.
9365 */
9366 update_force_abort();
9367 }
9368 if (error == ERROR_NONE)
9369 ret = OK;
9370
9371 /*
9372 * Report an error unless the argument evaluation or function call has been
9373 * cancelled due to an aborting error, an interrupt, or an exception.
9374 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009375 if (!aborting())
9376 {
9377 switch (error)
9378 {
9379 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009380 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009381 break;
9382 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009383 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009384 break;
9385 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009386 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009387 name);
9388 break;
9389 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009390 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009391 name);
9392 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009393 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009394 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009395 name);
9396 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009397 }
9398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009400 while (argv_clear > 0)
9401 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009402 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009403 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404
9405 return ret;
9406}
9407
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009408/*
9409 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009410 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009411 */
9412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009413emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009414{
9415 char_u *p;
9416
9417 if (*name == K_SPECIAL)
9418 p = concat_str((char_u *)"<SNR>", name + 3);
9419 else
9420 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009421 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009422 if (p != name)
9423 vim_free(p);
9424}
9425
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009426/*
9427 * Return TRUE for a non-zero Number and a non-empty String.
9428 */
9429 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009430non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009431{
9432 return ((argvars[0].v_type == VAR_NUMBER
9433 && argvars[0].vval.v_number != 0)
Bram Moolenaare381d3d2016-07-07 14:50:41 +02009434 || (argvars[0].v_type == VAR_SPECIAL
9435 && argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009436 || (argvars[0].v_type == VAR_STRING
9437 && argvars[0].vval.v_string != NULL
9438 && *argvars[0].vval.v_string != NUL));
9439}
9440
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441/*********************************************
9442 * Implementation of the built-in functions
9443 */
9444
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009445#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009446static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009447
9448/*
9449 * Get the float value of "argvars[0]" into "f".
9450 * Returns FAIL when the argument is not a Number or Float.
9451 */
9452 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009453get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009454{
9455 if (argvars[0].v_type == VAR_FLOAT)
9456 {
9457 *f = argvars[0].vval.v_float;
9458 return OK;
9459 }
9460 if (argvars[0].v_type == VAR_NUMBER)
9461 {
9462 *f = (float_T)argvars[0].vval.v_number;
9463 return OK;
9464 }
9465 EMSG(_("E808: Number or Float required"));
9466 return FAIL;
9467}
9468
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009469/*
9470 * "abs(expr)" function
9471 */
9472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009473f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009474{
9475 if (argvars[0].v_type == VAR_FLOAT)
9476 {
9477 rettv->v_type = VAR_FLOAT;
9478 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9479 }
9480 else
9481 {
9482 varnumber_T n;
9483 int error = FALSE;
9484
9485 n = get_tv_number_chk(&argvars[0], &error);
9486 if (error)
9487 rettv->vval.v_number = -1;
9488 else if (n > 0)
9489 rettv->vval.v_number = n;
9490 else
9491 rettv->vval.v_number = -n;
9492 }
9493}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009494
9495/*
9496 * "acos()" function
9497 */
9498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009500{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009501 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009502
9503 rettv->v_type = VAR_FLOAT;
9504 if (get_float_arg(argvars, &f) == OK)
9505 rettv->vval.v_float = acos(f);
9506 else
9507 rettv->vval.v_float = 0.0;
9508}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009509#endif
9510
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009512 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 */
9514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009515f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
Bram Moolenaar33570922005-01-25 22:26:29 +00009517 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009522 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009523 && !tv_check_lock(l->lv_lock,
9524 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009525 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009527 }
9528 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009529 EMSG(_(e_listreq));
9530}
9531
9532/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009533 * "and(expr, expr)" function
9534 */
9535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009536f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009537{
9538 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9539 & get_tv_number_chk(&argvars[1], NULL);
9540}
9541
9542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009543 * "append(lnum, string/list)" function
9544 */
9545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009546f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009547{
9548 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009550 list_T *l = NULL;
9551 listitem_T *li = NULL;
9552 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009553 long added = 0;
9554
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009555 /* When coming here from Insert mode, sync undo, so that this can be
9556 * undone separately from what was previously inserted. */
9557 if (u_sync_once == 2)
9558 {
9559 u_sync_once = 1; /* notify that u_sync() was called */
9560 u_sync(TRUE);
9561 }
9562
Bram Moolenaar0d660222005-01-07 21:51:51 +00009563 lnum = get_tv_lnum(argvars);
9564 if (lnum >= 0
9565 && lnum <= curbuf->b_ml.ml_line_count
9566 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009567 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009568 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009570 l = argvars[1].vval.v_list;
9571 if (l == NULL)
9572 return;
9573 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009574 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009575 for (;;)
9576 {
9577 if (l == NULL)
9578 tv = &argvars[1]; /* append a string */
9579 else if (li == NULL)
9580 break; /* end of list */
9581 else
9582 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 line = get_tv_string_chk(tv);
9584 if (line == NULL) /* type error */
9585 {
9586 rettv->vval.v_number = 1; /* Failed */
9587 break;
9588 }
9589 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009590 ++added;
9591 if (l == NULL)
9592 break;
9593 li = li->li_next;
9594 }
9595
9596 appended_lines_mark(lnum, added);
9597 if (curwin->w_cursor.lnum > lnum)
9598 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009600 else
9601 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602}
9603
9604/*
9605 * "argc()" function
9606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009608f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611}
9612
9613/*
9614 * "argidx()" function
9615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009617f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620}
9621
9622/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009623 * "arglistid()" function
9624 */
9625 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009626f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009627{
9628 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009629
9630 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009631 wp = find_tabwin(&argvars[0], &argvars[1]);
9632 if (wp != NULL)
9633 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009634}
9635
9636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 * "argv(nr)" function
9638 */
9639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009640f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642 int idx;
9643
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009644 if (argvars[0].v_type != VAR_UNKNOWN)
9645 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009646 idx = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009647 if (idx >= 0 && idx < ARGCOUNT)
9648 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9649 else
9650 rettv->vval.v_string = NULL;
9651 rettv->v_type = VAR_STRING;
9652 }
9653 else if (rettv_list_alloc(rettv) == OK)
9654 for (idx = 0; idx < ARGCOUNT; ++idx)
9655 list_append_string(rettv->vval.v_list,
9656 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657}
9658
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009659typedef enum
9660{
9661 ASSERT_EQUAL,
9662 ASSERT_NOTEQUAL,
9663 ASSERT_MATCH,
9664 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009665 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009666} assert_type_T;
9667
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009668static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009669static 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 +01009670static void assert_error(garray_T *gap);
9671static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009672
9673/*
9674 * Prepare "gap" for an assert error and add the sourcing position.
9675 */
9676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009677prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009678{
9679 char buf[NUMBUFLEN];
9680
9681 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009682 if (sourcing_name != NULL)
9683 {
9684 ga_concat(gap, sourcing_name);
9685 if (sourcing_lnum > 0)
9686 ga_concat(gap, (char_u *)" ");
9687 }
9688 if (sourcing_lnum > 0)
9689 {
9690 sprintf(buf, "line %ld", (long)sourcing_lnum);
9691 ga_concat(gap, (char_u *)buf);
9692 }
9693 if (sourcing_name != NULL || sourcing_lnum > 0)
9694 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009695}
9696
9697/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009698 * Append "str" to "gap", escaping unprintable characters.
9699 * Changes NL to \n, CR to \r, etc.
9700 */
9701 static void
9702ga_concat_esc(garray_T *gap, char_u *str)
9703{
9704 char_u *p;
9705 char_u buf[NUMBUFLEN];
9706
Bram Moolenaarf1551962016-03-15 12:55:58 +01009707 if (str == NULL)
9708 {
9709 ga_concat(gap, (char_u *)"NULL");
9710 return;
9711 }
9712
Bram Moolenaar23689172016-02-15 22:37:37 +01009713 for (p = str; *p != NUL; ++p)
9714 switch (*p)
9715 {
9716 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9717 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9718 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9719 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9720 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9721 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9722 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9723 default:
9724 if (*p < ' ')
9725 {
9726 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9727 ga_concat(gap, buf);
9728 }
9729 else
9730 ga_append(gap, *p);
9731 break;
9732 }
9733}
9734
9735/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009736 * Fill "gap" with information about an assert error.
9737 */
9738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009739fill_assert_error(
9740 garray_T *gap,
9741 typval_T *opt_msg_tv,
9742 char_u *exp_str,
9743 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009744 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009745 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009746{
9747 char_u numbuf[NUMBUFLEN];
9748 char_u *tofree;
9749
9750 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9751 {
9752 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9753 vim_free(tofree);
9754 }
9755 else
9756 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009757 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009758 ga_concat(gap, (char_u *)"Pattern ");
9759 else
9760 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009761 if (exp_str == NULL)
9762 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009763 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009764 vim_free(tofree);
9765 }
9766 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009767 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009768 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009769 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009770 else if (atype == ASSERT_NOTMATCH)
9771 ga_concat(gap, (char_u *)" does match ");
9772 else if (atype == ASSERT_NOTEQUAL)
9773 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009774 else
9775 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009776 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009777 vim_free(tofree);
9778 }
9779}
Bram Moolenaar43345542015-11-29 17:35:35 +01009780
9781/*
9782 * Add an assert error to v:errors.
9783 */
9784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009785assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009786{
9787 struct vimvar *vp = &vimvars[VV_ERRORS];
9788
9789 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9790 /* Make sure v:errors is a list. */
9791 set_vim_var_list(VV_ERRORS, list_alloc());
9792 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9793}
9794
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009795 static void
9796assert_equal_common(typval_T *argvars, assert_type_T atype)
9797{
9798 garray_T ga;
9799
9800 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9801 != (atype == ASSERT_EQUAL))
9802 {
9803 prepare_assert_error(&ga);
9804 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9805 atype);
9806 assert_error(&ga);
9807 ga_clear(&ga);
9808 }
9809}
9810
Bram Moolenaar43345542015-11-29 17:35:35 +01009811/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009812 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009813 */
9814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009815f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009816{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009817 assert_equal_common(argvars, ASSERT_EQUAL);
9818}
Bram Moolenaar43345542015-11-29 17:35:35 +01009819
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009820/*
9821 * "assert_notequal(expected, actual[, msg])" function
9822 */
9823 static void
9824f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9825{
9826 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009827}
9828
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009829/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009830 * "assert_exception(string[, msg])" function
9831 */
9832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009833f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009834{
9835 garray_T ga;
9836 char *error;
9837
9838 error = (char *)get_tv_string_chk(&argvars[0]);
9839 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9840 {
9841 prepare_assert_error(&ga);
9842 ga_concat(&ga, (char_u *)"v:exception is not set");
9843 assert_error(&ga);
9844 ga_clear(&ga);
9845 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009846 else if (error != NULL
9847 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009848 {
9849 prepare_assert_error(&ga);
9850 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009851 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009852 assert_error(&ga);
9853 ga_clear(&ga);
9854 }
9855}
9856
9857/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009858 * "assert_fails(cmd [, error])" function
9859 */
9860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009861f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009862{
9863 char_u *cmd = get_tv_string_chk(&argvars[0]);
9864 garray_T ga;
9865
9866 called_emsg = FALSE;
9867 suppress_errthrow = TRUE;
9868 emsg_silent = TRUE;
9869 do_cmdline_cmd(cmd);
9870 if (!called_emsg)
9871 {
9872 prepare_assert_error(&ga);
9873 ga_concat(&ga, (char_u *)"command did not fail: ");
9874 ga_concat(&ga, cmd);
9875 assert_error(&ga);
9876 ga_clear(&ga);
9877 }
9878 else if (argvars[1].v_type != VAR_UNKNOWN)
9879 {
9880 char_u buf[NUMBUFLEN];
9881 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9882
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009883 if (error == NULL
9884 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009885 {
9886 prepare_assert_error(&ga);
9887 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009888 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009889 assert_error(&ga);
9890 ga_clear(&ga);
9891 }
9892 }
9893
9894 called_emsg = FALSE;
9895 suppress_errthrow = FALSE;
9896 emsg_silent = FALSE;
9897 emsg_on_display = FALSE;
9898 set_vim_var_string(VV_ERRMSG, NULL, 0);
9899}
9900
9901/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009902 * Common for assert_true() and assert_false().
9903 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009905assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009906{
9907 int error = FALSE;
9908 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009909
Bram Moolenaar37127922016-02-06 20:29:28 +01009910 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009911 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009912 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009913 if (argvars[0].v_type != VAR_NUMBER
9914 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9915 || error)
9916 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009917 prepare_assert_error(&ga);
9918 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009919 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009920 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009921 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009922 ga_clear(&ga);
9923 }
9924}
9925
9926/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009927 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009928 */
9929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009930f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009931{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009932 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009933}
9934
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009935 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009936assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009937{
9938 garray_T ga;
9939 char_u buf1[NUMBUFLEN];
9940 char_u buf2[NUMBUFLEN];
9941 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9942 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9943
Bram Moolenaar72188e92016-03-28 22:48:29 +02009944 if (pat == NULL || text == NULL)
9945 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009946 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009947 {
9948 prepare_assert_error(&ga);
9949 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009950 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009951 assert_error(&ga);
9952 ga_clear(&ga);
9953 }
9954}
9955
9956/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009957 * "assert_match(pattern, actual[, msg])" function
9958 */
9959 static void
9960f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9961{
9962 assert_match_common(argvars, ASSERT_MATCH);
9963}
9964
9965/*
9966 * "assert_notmatch(pattern, actual[, msg])" function
9967 */
9968 static void
9969f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9970{
9971 assert_match_common(argvars, ASSERT_NOTMATCH);
9972}
9973
9974/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009975 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009976 */
9977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009978f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009979{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009980 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009981}
9982
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009983#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009984/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009985 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009986 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009988f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009989{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009990 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009991
9992 rettv->v_type = VAR_FLOAT;
9993 if (get_float_arg(argvars, &f) == OK)
9994 rettv->vval.v_float = asin(f);
9995 else
9996 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009997}
9998
9999/*
10000 * "atan()" function
10001 */
10002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010003f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010004{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +010010005 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010006
10007 rettv->v_type = VAR_FLOAT;
10008 if (get_float_arg(argvars, &f) == OK)
10009 rettv->vval.v_float = atan(f);
10010 else
10011 rettv->vval.v_float = 0.0;
10012}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010013
10014/*
10015 * "atan2()" function
10016 */
10017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010018f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010019{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010020 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010021
10022 rettv->v_type = VAR_FLOAT;
10023 if (get_float_arg(argvars, &fx) == OK
10024 && get_float_arg(&argvars[1], &fy) == OK)
10025 rettv->vval.v_float = atan2(fx, fy);
10026 else
10027 rettv->vval.v_float = 0.0;
10028}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010029#endif
10030
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031/*
10032 * "browse(save, title, initdir, default)" function
10033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010035f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037#ifdef FEAT_BROWSE
10038 int save;
10039 char_u *title;
10040 char_u *initdir;
10041 char_u *defname;
10042 char_u buf[NUMBUFLEN];
10043 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010044 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010046 save = (int)get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010047 title = get_tv_string_chk(&argvars[1]);
10048 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10049 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010051 if (error || title == NULL || initdir == NULL || defname == NULL)
10052 rettv->vval.v_string = NULL;
10053 else
10054 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010055 do_browse(save ? BROWSE_SAVE : 0,
10056 title, defname, NULL, initdir, NULL, curbuf);
10057#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010059#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010061}
10062
10063/*
10064 * "browsedir(title, initdir)" function
10065 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010067f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010068{
10069#ifdef FEAT_BROWSE
10070 char_u *title;
10071 char_u *initdir;
10072 char_u buf[NUMBUFLEN];
10073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010074 title = get_tv_string_chk(&argvars[0]);
10075 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010077 if (title == NULL || initdir == NULL)
10078 rettv->vval.v_string = NULL;
10079 else
10080 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010081 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010085 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086}
10087
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010088static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010089
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090/*
10091 * Find a buffer by number or exact name.
10092 */
10093 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010094find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095{
10096 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010098 if (avar->v_type == VAR_NUMBER)
10099 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010100 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010102 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010103 if (buf == NULL)
10104 {
10105 /* No full path name match, try a match with a URL or a "nofile"
10106 * buffer, these don't use the full path. */
10107 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10108 if (buf->b_fname != NULL
10109 && (path_with_url(buf->b_fname)
10110#ifdef FEAT_QUICKFIX
10111 || bt_nofile(buf)
10112#endif
10113 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010114 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010115 break;
10116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 }
10118 return buf;
10119}
10120
10121/*
10122 * "bufexists(expr)" function
10123 */
10124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010125f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128}
10129
10130/*
10131 * "buflisted(expr)" function
10132 */
10133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010134f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135{
10136 buf_T *buf;
10137
10138 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140}
10141
10142/*
10143 * "bufloaded(expr)" function
10144 */
10145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010146f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147{
10148 buf_T *buf;
10149
10150 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152}
10153
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010154 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010155buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 int save_magic;
10158 char_u *save_cpo;
10159 buf_T *buf;
10160
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10162 save_magic = p_magic;
10163 p_magic = TRUE;
10164 save_cpo = p_cpo;
10165 p_cpo = (char_u *)"";
10166
10167 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010168 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169
10170 p_magic = save_magic;
10171 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010172 return buf;
10173}
10174
10175/*
10176 * Get buffer by number or pattern.
10177 */
10178 static buf_T *
10179get_buf_tv(typval_T *tv, int curtab_only)
10180{
10181 char_u *name = tv->vval.v_string;
10182 buf_T *buf;
10183
10184 if (tv->v_type == VAR_NUMBER)
10185 return buflist_findnr((int)tv->vval.v_number);
10186 if (tv->v_type != VAR_STRING)
10187 return NULL;
10188 if (name == NULL || *name == NUL)
10189 return curbuf;
10190 if (name[0] == '$' && name[1] == NUL)
10191 return lastbuf;
10192
10193 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
10195 /* If not found, try expanding the name, like done for bufexists(). */
10196 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010197 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198
10199 return buf;
10200}
10201
10202/*
10203 * "bufname(expr)" function
10204 */
10205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010206f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207{
10208 buf_T *buf;
10209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010210 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010212 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010215 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 --emsg_off;
10219}
10220
10221/*
10222 * "bufnr(expr)" function
10223 */
10224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010225f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010228 int error = FALSE;
10229 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010233 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010234 --emsg_off;
10235
10236 /* If the buffer isn't found and the second argument is not zero create a
10237 * new buffer. */
10238 if (buf == NULL
10239 && argvars[1].v_type != VAR_UNKNOWN
10240 && get_tv_number_chk(&argvars[1], &error) != 0
10241 && !error
10242 && (name = get_tv_string_chk(&argvars[0])) != NULL
10243 && !error)
10244 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10245
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250}
10251
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010253buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254{
10255#ifdef FEAT_WINDOWS
10256 win_T *wp;
10257 int winnr = 0;
10258#endif
10259 buf_T *buf;
10260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010263 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264#ifdef FEAT_WINDOWS
10265 for (wp = firstwin; wp; wp = wp->w_next)
10266 {
10267 ++winnr;
10268 if (wp->w_buffer == buf)
10269 break;
10270 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010271 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010273 rettv->vval.v_number = (curwin->w_buffer == buf
10274 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275#endif
10276 --emsg_off;
10277}
10278
10279/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010280 * "bufwinid(nr)" function
10281 */
10282 static void
10283f_bufwinid(typval_T *argvars, typval_T *rettv)
10284{
10285 buf_win_common(argvars, rettv, FALSE);
10286}
10287
10288/*
10289 * "bufwinnr(nr)" function
10290 */
10291 static void
10292f_bufwinnr(typval_T *argvars, typval_T *rettv)
10293{
10294 buf_win_common(argvars, rettv, TRUE);
10295}
10296
10297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 * "byte2line(byte)" function
10299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010301f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302{
10303#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010304 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305#else
10306 long boff = 0;
10307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 (linenr_T)0, &boff);
10314#endif
10315}
10316
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010318byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010319{
10320#ifdef FEAT_MBYTE
10321 char_u *t;
10322#endif
10323 char_u *str;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010324 varnumber_T idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 str = get_tv_string_chk(&argvars[0]);
10327 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010330 return;
10331
10332#ifdef FEAT_MBYTE
10333 t = str;
10334 for ( ; idx > 0; idx--)
10335 {
10336 if (*t == NUL) /* EOL reached */
10337 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010338 if (enc_utf8 && comp)
10339 t += utf_ptr2len(t);
10340 else
10341 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010342 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010343 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010344#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010345 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010347#endif
10348}
10349
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010350/*
10351 * "byteidx()" function
10352 */
10353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010354f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010355{
10356 byteidx(argvars, rettv, FALSE);
10357}
10358
10359/*
10360 * "byteidxcomp()" function
10361 */
10362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010363f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010364{
10365 byteidx(argvars, rettv, TRUE);
10366}
10367
Bram Moolenaardb913952012-06-29 12:54:53 +020010368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010369func_call(
10370 char_u *name,
10371 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010372 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010373 dict_T *selfdict,
10374 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010375{
10376 listitem_T *item;
10377 typval_T argv[MAX_FUNC_ARGS + 1];
10378 int argc = 0;
10379 int dummy;
10380 int r = 0;
10381
10382 for (item = args->vval.v_list->lv_first; item != NULL;
10383 item = item->li_next)
10384 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010385 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010386 {
10387 EMSG(_("E699: Too many arguments"));
10388 break;
10389 }
10390 /* Make a copy of each argument. This is needed to be able to set
10391 * v_lock to VAR_FIXED in the copy without changing the original list.
10392 */
10393 copy_tv(&item->li_tv, &argv[argc++]);
10394 }
10395
10396 if (item == NULL)
10397 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10398 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010399 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010400
10401 /* Free the arguments. */
10402 while (argc > 0)
10403 clear_tv(&argv[--argc]);
10404
10405 return r;
10406}
10407
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010408/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010409 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010410 */
10411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010412f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010413{
10414 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010415 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010416 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010417
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010418 if (argvars[1].v_type != VAR_LIST)
10419 {
10420 EMSG(_(e_listreq));
10421 return;
10422 }
10423 if (argvars[1].vval.v_list == NULL)
10424 return;
10425
10426 if (argvars[0].v_type == VAR_FUNC)
10427 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010428 else if (argvars[0].v_type == VAR_PARTIAL)
10429 {
10430 partial = argvars[0].vval.v_partial;
10431 func = partial->pt_name;
10432 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010433 else
10434 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 if (*func == NUL)
10436 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010437
Bram Moolenaare9a41262005-01-15 22:18:47 +000010438 if (argvars[2].v_type != VAR_UNKNOWN)
10439 {
10440 if (argvars[2].v_type != VAR_DICT)
10441 {
10442 EMSG(_(e_dictreq));
10443 return;
10444 }
10445 selfdict = argvars[2].vval.v_dict;
10446 }
10447
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010448 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010449}
10450
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010451#ifdef FEAT_FLOAT
10452/*
10453 * "ceil({float})" function
10454 */
10455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010456f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010457{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010458 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010459
10460 rettv->v_type = VAR_FLOAT;
10461 if (get_float_arg(argvars, &f) == OK)
10462 rettv->vval.v_float = ceil(f);
10463 else
10464 rettv->vval.v_float = 0.0;
10465}
10466#endif
10467
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010468#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010469/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010470 * "ch_close()" function
10471 */
10472 static void
10473f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10474{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010475 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010476
10477 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010478 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010479 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010480 channel_clear(channel);
10481 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010482}
10483
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010484/*
10485 * "ch_getbufnr()" function
10486 */
10487 static void
10488f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10489{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010490 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010491
10492 rettv->vval.v_number = -1;
10493 if (channel != NULL)
10494 {
10495 char_u *what = get_tv_string(&argvars[1]);
10496 int part;
10497
10498 if (STRCMP(what, "err") == 0)
10499 part = PART_ERR;
10500 else if (STRCMP(what, "out") == 0)
10501 part = PART_OUT;
10502 else if (STRCMP(what, "in") == 0)
10503 part = PART_IN;
10504 else
10505 part = PART_SOCK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020010506 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
10507 rettv->vval.v_number =
10508 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010509 }
10510}
10511
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010512/*
10513 * "ch_getjob()" function
10514 */
10515 static void
10516f_ch_getjob(typval_T *argvars, typval_T *rettv)
10517{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010518 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010519
10520 if (channel != NULL)
10521 {
10522 rettv->v_type = VAR_JOB;
10523 rettv->vval.v_job = channel->ch_job;
10524 if (channel->ch_job != NULL)
10525 ++channel->ch_job->jv_refcount;
10526 }
10527}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010528
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010529/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010530 * "ch_info()" function
10531 */
10532 static void
10533f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10534{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010535 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010536
10537 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10538 channel_info(channel, rettv->vval.v_dict);
10539}
10540
10541/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010542 * "ch_log()" function
10543 */
10544 static void
10545f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10546{
10547 char_u *msg = get_tv_string(&argvars[0]);
10548 channel_T *channel = NULL;
10549
10550 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010551 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010552
10553 ch_log(channel, (char *)msg);
10554}
10555
10556/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010557 * "ch_logfile()" function
10558 */
10559 static void
10560f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10561{
10562 char_u *fname;
10563 char_u *opt = (char_u *)"";
10564 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010565
10566 fname = get_tv_string(&argvars[0]);
10567 if (argvars[1].v_type == VAR_STRING)
10568 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010569 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010570}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010571
10572/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010573 * "ch_open()" function
10574 */
10575 static void
10576f_ch_open(typval_T *argvars, typval_T *rettv)
10577{
Bram Moolenaar77073442016-02-13 23:23:53 +010010578 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010579 if (check_restricted() || check_secure())
10580 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010581 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010582}
10583
10584/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010585 * "ch_read()" function
10586 */
10587 static void
10588f_ch_read(typval_T *argvars, typval_T *rettv)
10589{
10590 common_channel_read(argvars, rettv, FALSE);
10591}
10592
10593/*
10594 * "ch_readraw()" function
10595 */
10596 static void
10597f_ch_readraw(typval_T *argvars, typval_T *rettv)
10598{
10599 common_channel_read(argvars, rettv, TRUE);
10600}
10601
10602/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010603 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010604 */
10605 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010606f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10607{
10608 ch_expr_common(argvars, rettv, TRUE);
10609}
10610
10611/*
10612 * "ch_sendexpr()" function
10613 */
10614 static void
10615f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10616{
10617 ch_expr_common(argvars, rettv, FALSE);
10618}
10619
10620/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010621 * "ch_evalraw()" function
10622 */
10623 static void
10624f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10625{
10626 ch_raw_common(argvars, rettv, TRUE);
10627}
10628
10629/*
10630 * "ch_sendraw()" function
10631 */
10632 static void
10633f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10634{
10635 ch_raw_common(argvars, rettv, FALSE);
10636}
10637
10638/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010639 * "ch_setoptions()" function
10640 */
10641 static void
10642f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10643{
10644 channel_T *channel;
10645 jobopt_T opt;
10646
Bram Moolenaar437905c2016-04-26 19:01:05 +020010647 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010648 if (channel == NULL)
10649 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010650 clear_job_options(&opt);
10651 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010652 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10653 channel_set_options(channel, &opt);
10654 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010655}
10656
10657/*
10658 * "ch_status()" function
10659 */
10660 static void
10661f_ch_status(typval_T *argvars, typval_T *rettv)
10662{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010663 channel_T *channel;
10664
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010665 /* return an empty string by default */
10666 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010667 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010668
Bram Moolenaar437905c2016-04-26 19:01:05 +020010669 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010670 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010671}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010672#endif
10673
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010674/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010675 * "changenr()" function
10676 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010678f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010679{
10680 rettv->vval.v_number = curbuf->b_u_seq_cur;
10681}
10682
10683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 * "char2nr(string)" function
10685 */
10686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010687f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
10689#ifdef FEAT_MBYTE
10690 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010691 {
10692 int utf8 = 0;
10693
10694 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010695 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010010696
10697 if (utf8)
10698 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10699 else
10700 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 else
10703#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705}
10706
10707/*
10708 * "cindent(lnum)" function
10709 */
10710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010711f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712{
10713#ifdef FEAT_CINDENT
10714 pos_T pos;
10715 linenr_T lnum;
10716
10717 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10720 {
10721 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010722 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 curwin->w_cursor = pos;
10724 }
10725 else
10726#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728}
10729
10730/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010731 * "clearmatches()" function
10732 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010734f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010735{
10736#ifdef FEAT_SEARCH_EXTRA
10737 clear_matches(curwin);
10738#endif
10739}
10740
10741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 * "col(string)" function
10743 */
10744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010745f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746{
10747 colnr_T col = 0;
10748 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010749 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010751 fp = var2fpos(&argvars[0], FALSE, &fnum);
10752 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 {
10754 if (fp->col == MAXCOL)
10755 {
10756 /* '> can be MAXCOL, get the length of the line then */
10757 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010758 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 else
10760 col = MAXCOL;
10761 }
10762 else
10763 {
10764 col = fp->col + 1;
10765#ifdef FEAT_VIRTUALEDIT
10766 /* col(".") when the cursor is on the NUL at the end of the line
10767 * because of "coladd" can be seen as an extra column. */
10768 if (virtual_active() && fp == &curwin->w_cursor)
10769 {
10770 char_u *p = ml_get_cursor();
10771
10772 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10773 curwin->w_virtcol - curwin->w_cursor.coladd))
10774 {
10775# ifdef FEAT_MBYTE
10776 int l;
10777
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010778 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 col += l;
10780# else
10781 if (*p != NUL && p[1] == NUL)
10782 ++col;
10783# endif
10784 }
10785 }
10786#endif
10787 }
10788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790}
10791
Bram Moolenaar572cb562005-08-05 21:35:02 +000010792#if defined(FEAT_INS_EXPAND)
10793/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010794 * "complete()" function
10795 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010797f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010798{
10799 int startcol;
10800
10801 if ((State & INSERT) == 0)
10802 {
10803 EMSG(_("E785: complete() can only be used in Insert mode"));
10804 return;
10805 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010806
10807 /* Check for undo allowed here, because if something was already inserted
10808 * the line was already saved for undo and this check isn't done. */
10809 if (!undo_allowed())
10810 return;
10811
Bram Moolenaarade00832006-03-10 21:46:58 +000010812 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10813 {
10814 EMSG(_(e_invarg));
10815 return;
10816 }
10817
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010818 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaarade00832006-03-10 21:46:58 +000010819 if (startcol <= 0)
10820 return;
10821
10822 set_completion(startcol - 1, argvars[1].vval.v_list);
10823}
10824
10825/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010826 * "complete_add()" function
10827 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010829f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010830{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010831 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010832}
10833
10834/*
10835 * "complete_check()" function
10836 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010838f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010839{
10840 int saved = RedrawingDisabled;
10841
10842 RedrawingDisabled = 0;
10843 ins_compl_check_keys(0);
10844 rettv->vval.v_number = compl_interrupted;
10845 RedrawingDisabled = saved;
10846}
10847#endif
10848
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849/*
10850 * "confirm(message, buttons[, default [, type]])" function
10851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010853f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854{
10855#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10856 char_u *message;
10857 char_u *buttons = NULL;
10858 char_u buf[NUMBUFLEN];
10859 char_u buf2[NUMBUFLEN];
10860 int def = 1;
10861 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010862 char_u *typestr;
10863 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 message = get_tv_string_chk(&argvars[0]);
10866 if (message == NULL)
10867 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010868 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010870 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10871 if (buttons == NULL)
10872 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010873 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010875 def = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010876 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10879 if (typestr == NULL)
10880 error = TRUE;
10881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 switch (TOUPPER_ASC(*typestr))
10884 {
10885 case 'E': type = VIM_ERROR; break;
10886 case 'Q': type = VIM_QUESTION; break;
10887 case 'I': type = VIM_INFO; break;
10888 case 'W': type = VIM_WARNING; break;
10889 case 'G': type = VIM_GENERIC; break;
10890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 }
10892 }
10893 }
10894 }
10895
10896 if (buttons == NULL || *buttons == NUL)
10897 buttons = (char_u *)_("&Ok");
10898
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010899 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010901 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902#endif
10903}
10904
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010905/*
10906 * "copy()" function
10907 */
10908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010909f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010910{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010911 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010912}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010914#ifdef FEAT_FLOAT
10915/*
10916 * "cos()" function
10917 */
10918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010919f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010920{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010921 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010922
10923 rettv->v_type = VAR_FLOAT;
10924 if (get_float_arg(argvars, &f) == OK)
10925 rettv->vval.v_float = cos(f);
10926 else
10927 rettv->vval.v_float = 0.0;
10928}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010929
10930/*
10931 * "cosh()" function
10932 */
10933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010934f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010935{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010936 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010937
10938 rettv->v_type = VAR_FLOAT;
10939 if (get_float_arg(argvars, &f) == OK)
10940 rettv->vval.v_float = cosh(f);
10941 else
10942 rettv->vval.v_float = 0.0;
10943}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010944#endif
10945
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010947 * "count()" function
10948 */
10949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010950f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010951{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010952 long n = 0;
10953 int ic = FALSE;
10954
Bram Moolenaare9a41262005-01-15 22:18:47 +000010955 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010956 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010957 listitem_T *li;
10958 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010959 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010960
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 if ((l = argvars[0].vval.v_list) != NULL)
10962 {
10963 li = l->lv_first;
10964 if (argvars[2].v_type != VAR_UNKNOWN)
10965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010966 int error = FALSE;
10967
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010968 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010969 if (argvars[3].v_type != VAR_UNKNOWN)
10970 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010971 idx = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 if (!error)
10973 {
10974 li = list_find(l, idx);
10975 if (li == NULL)
10976 EMSGN(_(e_listidx), idx);
10977 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 if (error)
10980 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010981 }
10982
10983 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010984 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010985 ++n;
10986 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010987 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010988 else if (argvars[0].v_type == VAR_DICT)
10989 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010990 int todo;
10991 dict_T *d;
10992 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010993
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010994 if ((d = argvars[0].vval.v_dict) != NULL)
10995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996 int error = FALSE;
10997
Bram Moolenaare9a41262005-01-15 22:18:47 +000010998 if (argvars[2].v_type != VAR_UNKNOWN)
10999 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011000 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 if (argvars[3].v_type != VAR_UNKNOWN)
11002 EMSG(_(e_invarg));
11003 }
11004
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011005 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011006 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011007 {
11008 if (!HASHITEM_EMPTY(hi))
11009 {
11010 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011011 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011012 ++n;
11013 }
11014 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011015 }
11016 }
11017 else
11018 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011019 rettv->vval.v_number = n;
11020}
11021
11022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11024 *
11025 * Checks the existence of a cscope connection.
11026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011028f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029{
11030#ifdef FEAT_CSCOPE
11031 int num = 0;
11032 char_u *dbpath = NULL;
11033 char_u *prepend = NULL;
11034 char_u buf[NUMBUFLEN];
11035
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011036 if (argvars[0].v_type != VAR_UNKNOWN
11037 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 num = (int)get_tv_number(&argvars[0]);
11040 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011041 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 }
11044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046#endif
11047}
11048
11049/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011050 * "cursor(lnum, col)" function, or
11051 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011053 * Moves the cursor to the specified line and column.
11054 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011057f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058{
11059 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011060#ifdef FEAT_VIRTUALEDIT
11061 long coladd = 0;
11062#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011063 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011065 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011066 if (argvars[1].v_type == VAR_UNKNOWN)
11067 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011068 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011069 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011070
Bram Moolenaar493c1782014-05-28 14:34:46 +020011071 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011072 {
11073 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011074 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011075 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011076 line = pos.lnum;
11077 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011078#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011079 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011080#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011081 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011082 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011083 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011084 set_curswant = FALSE;
11085 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011086 }
11087 else
11088 {
11089 line = get_tv_lnum(argvars);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011090 col = (long)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011091#ifdef FEAT_VIRTUALEDIT
11092 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011093 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011094#endif
11095 }
11096 if (line < 0 || col < 0
11097#ifdef FEAT_VIRTUALEDIT
11098 || coladd < 0
11099#endif
11100 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011101 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 if (line > 0)
11103 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 if (col > 0)
11105 curwin->w_cursor.col = col - 1;
11106#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011107 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108#endif
11109
11110 /* Make sure the cursor is in a valid position. */
11111 check_cursor();
11112#ifdef FEAT_MBYTE
11113 /* Correct cursor for multi-byte character. */
11114 if (has_mbyte)
11115 mb_adjust_cursor();
11116#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011117
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011118 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011119 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120}
11121
11122/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011123 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 */
11125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011126f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011128 int noref = 0;
11129
11130 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011131 noref = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011132 if (noref < 0 || noref > 1)
11133 EMSG(_(e_invarg));
11134 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011135 {
11136 current_copyID += COPYID_INC;
11137 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139}
11140
11141/*
11142 * "delete()" function
11143 */
11144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011145f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011147 char_u nbuf[NUMBUFLEN];
11148 char_u *name;
11149 char_u *flags;
11150
11151 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011153 return;
11154
11155 name = get_tv_string(&argvars[0]);
11156 if (name == NULL || *name == NUL)
11157 {
11158 EMSG(_(e_invarg));
11159 return;
11160 }
11161
11162 if (argvars[1].v_type != VAR_UNKNOWN)
11163 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011165 flags = (char_u *)"";
11166
11167 if (*flags == NUL)
11168 /* delete a file */
11169 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11170 else if (STRCMP(flags, "d") == 0)
11171 /* delete an empty directory */
11172 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11173 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011174 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011175 rettv->vval.v_number = delete_recursive(name);
11176 else
11177 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178}
11179
11180/*
11181 * "did_filetype()" function
11182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011184f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185{
11186#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188#endif
11189}
11190
11191/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011192 * "diff_filler()" function
11193 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011195f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011196{
11197#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011198 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011199#endif
11200}
11201
11202/*
11203 * "diff_hlID()" function
11204 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011206f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011207{
11208#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011210 static linenr_T prev_lnum = 0;
11211 static int changedtick = 0;
11212 static int fnum = 0;
11213 static int change_start = 0;
11214 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011215 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011216 int filler_lines;
11217 int col;
11218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011219 if (lnum < 0) /* ignore type error in {lnum} arg */
11220 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011221 if (lnum != prev_lnum
11222 || changedtick != curbuf->b_changedtick
11223 || fnum != curbuf->b_fnum)
11224 {
11225 /* New line, buffer, change: need to get the values. */
11226 filler_lines = diff_check(curwin, lnum);
11227 if (filler_lines < 0)
11228 {
11229 if (filler_lines == -1)
11230 {
11231 change_start = MAXCOL;
11232 change_end = -1;
11233 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11234 hlID = HLF_ADD; /* added line */
11235 else
11236 hlID = HLF_CHD; /* changed line */
11237 }
11238 else
11239 hlID = HLF_ADD; /* added line */
11240 }
11241 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011242 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011243 prev_lnum = lnum;
11244 changedtick = curbuf->b_changedtick;
11245 fnum = curbuf->b_fnum;
11246 }
11247
11248 if (hlID == HLF_CHD || hlID == HLF_TXD)
11249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011250 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011251 if (col >= change_start && col <= change_end)
11252 hlID = HLF_TXD; /* changed text */
11253 else
11254 hlID = HLF_CHD; /* changed line */
11255 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011256 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011257#endif
11258}
11259
11260/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011261 * "empty({expr})" function
11262 */
11263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011264f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011265{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011266 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011267
11268 switch (argvars[0].v_type)
11269 {
11270 case VAR_STRING:
11271 case VAR_FUNC:
11272 n = argvars[0].vval.v_string == NULL
11273 || *argvars[0].vval.v_string == NUL;
11274 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011275 case VAR_PARTIAL:
11276 n = FALSE;
11277 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011278 case VAR_NUMBER:
11279 n = argvars[0].vval.v_number == 0;
11280 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011281 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011282#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011283 n = argvars[0].vval.v_float == 0.0;
11284 break;
11285#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011286 case VAR_LIST:
11287 n = argvars[0].vval.v_list == NULL
11288 || argvars[0].vval.v_list->lv_first == NULL;
11289 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011290 case VAR_DICT:
11291 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011293 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011294 case VAR_SPECIAL:
11295 n = argvars[0].vval.v_number != VVAL_TRUE;
11296 break;
11297
Bram Moolenaar835dc632016-02-07 14:27:38 +010011298 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011299#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011300 n = argvars[0].vval.v_job == NULL
11301 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11302 break;
11303#endif
11304 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011305#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011306 n = argvars[0].vval.v_channel == NULL
11307 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011308 break;
11309#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011310 case VAR_UNKNOWN:
11311 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11312 n = TRUE;
11313 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011314 }
11315
11316 rettv->vval.v_number = n;
11317}
11318
11319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 * "escape({string}, {chars})" function
11321 */
11322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011323f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324{
11325 char_u buf[NUMBUFLEN];
11326
Bram Moolenaar758711c2005-02-02 23:11:38 +000011327 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11328 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330}
11331
11332/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011333 * "eval()" function
11334 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011336f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011337{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011338 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011339
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 s = get_tv_string_chk(&argvars[0]);
11341 if (s != NULL)
11342 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011343
Bram Moolenaar615b9972015-01-14 17:15:05 +010011344 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011345 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11346 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011347 if (p != NULL && !aborting())
11348 EMSG2(_(e_invexpr2), p);
11349 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011350 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011351 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011352 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011353 else if (*s != NUL)
11354 EMSG(_(e_trailing));
11355}
11356
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 * "eventhandler()" function
11359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011361f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364}
11365
11366/*
11367 * "executable()" function
11368 */
11369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011370f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011372 char_u *name = get_tv_string(&argvars[0]);
11373
11374 /* Check in $PATH and also check directly if there is a directory name. */
11375 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11376 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011377}
11378
Bram Moolenaar79815f12016-07-09 17:07:29 +020011379static garray_T redir_execute_ga;
11380
11381/*
11382 * Append "value[value_len]" to the execute() output.
11383 */
11384 void
11385execute_redir_str(char_u *value, int value_len)
11386{
11387 int len;
11388
11389 if (value_len == -1)
11390 len = (int)STRLEN(value); /* Append the entire string */
11391 else
11392 len = value_len; /* Append only "value_len" characters */
11393 if (ga_grow(&redir_execute_ga, len) == OK)
11394 {
11395 mch_memmove((char *)redir_execute_ga.ga_data
11396 + redir_execute_ga.ga_len, value, len);
11397 redir_execute_ga.ga_len += len;
11398 }
11399}
11400
11401/*
11402 * Get next line from a list.
11403 * Called by do_cmdline() to get the next line.
11404 * Returns allocated string, or NULL for end of function.
11405 */
11406
11407 static char_u *
11408get_list_line(
11409 int c UNUSED,
11410 void *cookie,
11411 int indent UNUSED)
11412{
11413 listitem_T **p = (listitem_T **)cookie;
11414 listitem_T *item = *p;
11415 char_u buf[NUMBUFLEN];
11416 char_u *s;
11417
11418 if (item == NULL)
11419 return NULL;
11420 s = get_tv_string_buf_chk(&item->li_tv, buf);
11421 *p = item->li_next;
11422 return s == NULL ? NULL : vim_strsave(s);
11423}
11424
11425/*
11426 * "execute()" function
11427 */
11428 static void
11429f_execute(typval_T *argvars, typval_T *rettv)
11430{
11431 char_u *cmd = NULL;
11432 list_T *list = NULL;
11433 int save_msg_silent = msg_silent;
11434 int save_emsg_silent = emsg_silent;
11435 int save_emsg_noredir = emsg_noredir;
11436 int save_redir_execute = redir_execute;
11437 garray_T save_ga;
11438
11439 rettv->vval.v_string = NULL;
11440 rettv->v_type = VAR_STRING;
11441
11442 if (argvars[0].v_type == VAR_LIST)
11443 {
11444 list = argvars[0].vval.v_list;
11445 if (list == NULL || list->lv_first == NULL)
11446 /* empty list, no commands, empty output */
11447 return;
11448 ++list->lv_refcount;
11449 }
11450 else
11451 {
11452 cmd = get_tv_string_chk(&argvars[0]);
11453 if (cmd == NULL)
11454 return;
11455 }
11456
Bram Moolenaar79815f12016-07-09 17:07:29 +020011457 if (argvars[1].v_type != VAR_UNKNOWN)
11458 {
11459 char_u buf[NUMBUFLEN];
11460 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
11461
11462 if (s == NULL)
11463 return;
11464 if (STRNCMP(s, "silent", 6) == 0)
11465 ++msg_silent;
11466 if (STRCMP(s, "silent!") == 0)
11467 {
11468 emsg_silent = TRUE;
11469 emsg_noredir = TRUE;
11470 }
11471 }
11472 else
11473 ++msg_silent;
11474
Bram Moolenaared59aa62016-07-09 17:41:12 +020011475 if (redir_execute)
11476 save_ga = redir_execute_ga;
11477 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
11478 redir_execute = TRUE;
11479
Bram Moolenaar79815f12016-07-09 17:07:29 +020011480 if (cmd != NULL)
11481 do_cmdline_cmd(cmd);
11482 else
11483 {
11484 listitem_T *item = list->lv_first;
11485
11486 do_cmdline(NULL, get_list_line, (void *)&item,
11487 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
11488 --list->lv_refcount;
11489 }
11490
11491 rettv->vval.v_string = redir_execute_ga.ga_data;
11492 msg_silent = save_msg_silent;
11493 emsg_silent = save_emsg_silent;
11494 emsg_noredir = save_emsg_noredir;
11495
11496 redir_execute = save_redir_execute;
11497 if (redir_execute)
11498 redir_execute_ga = save_ga;
11499
11500 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
11501 * line. Put it back in the first column. */
11502 msg_col = 0;
11503}
11504
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011505/*
11506 * "exepath()" function
11507 */
11508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011509f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011510{
11511 char_u *p = NULL;
11512
Bram Moolenaarb5971142015-03-21 17:32:19 +010011513 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011514 rettv->v_type = VAR_STRING;
11515 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516}
11517
11518/*
11519 * "exists()" function
11520 */
11521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011522f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523{
11524 char_u *p;
11525 char_u *name;
11526 int n = FALSE;
11527 int len = 0;
11528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 if (*p == '$') /* environment variable */
11531 {
11532 /* first try "normal" environment variables (fast) */
11533 if (mch_getenv(p + 1) != NULL)
11534 n = TRUE;
11535 else
11536 {
11537 /* try expanding things like $VIM and ${HOME} */
11538 p = expand_env_save(p);
11539 if (p != NULL && *p != '$')
11540 n = TRUE;
11541 vim_free(p);
11542 }
11543 }
11544 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011547 if (*skipwhite(p) != NUL)
11548 n = FALSE; /* trailing garbage */
11549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 else if (*p == '*') /* internal or user defined function */
11551 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011552 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 }
11554 else if (*p == ':')
11555 {
11556 n = cmd_exists(p + 1);
11557 }
11558 else if (*p == '#')
11559 {
11560#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011561 if (p[1] == '#')
11562 n = autocmd_supported(p + 2);
11563 else
11564 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565#endif
11566 }
11567 else /* internal variable */
11568 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011569 char_u *tofree;
11570 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011572 /* get_name_len() takes care of expanding curly braces */
11573 name = p;
11574 len = get_name_len(&p, &tofree, TRUE, FALSE);
11575 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011577 if (tofree != NULL)
11578 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011579 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011580 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011582 /* handle d.key, l[idx], f(expr) */
11583 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11584 if (n)
11585 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 }
11587 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011588 if (*p != NUL)
11589 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011591 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 }
11593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011594 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595}
11596
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011597#ifdef FEAT_FLOAT
11598/*
11599 * "exp()" function
11600 */
11601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011602f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011603{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011604 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011605
11606 rettv->v_type = VAR_FLOAT;
11607 if (get_float_arg(argvars, &f) == OK)
11608 rettv->vval.v_float = exp(f);
11609 else
11610 rettv->vval.v_float = 0.0;
11611}
11612#endif
11613
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614/*
11615 * "expand()" function
11616 */
11617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011618f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619{
11620 char_u *s;
11621 int len;
11622 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011623 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011625 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011626 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011629 if (argvars[1].v_type != VAR_UNKNOWN
11630 && argvars[2].v_type != VAR_UNKNOWN
11631 && get_tv_number_chk(&argvars[2], &error)
11632 && !error)
11633 {
11634 rettv->v_type = VAR_LIST;
11635 rettv->vval.v_list = NULL;
11636 }
11637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 if (*s == '%' || *s == '#' || *s == '<')
11640 {
11641 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011642 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011644 if (rettv->v_type == VAR_LIST)
11645 {
11646 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11647 list_append_string(rettv->vval.v_list, result, -1);
11648 else
11649 vim_free(result);
11650 }
11651 else
11652 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 }
11654 else
11655 {
11656 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011657 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011658 if (argvars[1].v_type != VAR_UNKNOWN
11659 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011660 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011661 if (!error)
11662 {
11663 ExpandInit(&xpc);
11664 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011665 if (p_wic)
11666 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011667 if (rettv->v_type == VAR_STRING)
11668 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11669 options, WILD_ALL);
11670 else if (rettv_list_alloc(rettv) != FAIL)
11671 {
11672 int i;
11673
11674 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11675 for (i = 0; i < xpc.xp_numfiles; i++)
11676 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11677 ExpandCleanup(&xpc);
11678 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 }
11680 else
11681 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 }
11683}
11684
11685/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011686 * Go over all entries in "d2" and add them to "d1".
11687 * When "action" is "error" then a duplicate key is an error.
11688 * When "action" is "force" then a duplicate key is overwritten.
11689 * Otherwise duplicate keys are ignored ("action" is "keep").
11690 */
11691 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011692dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011693{
11694 dictitem_T *di1;
11695 hashitem_T *hi2;
11696 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011697 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011698
11699 todo = (int)d2->dv_hashtab.ht_used;
11700 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11701 {
11702 if (!HASHITEM_EMPTY(hi2))
11703 {
11704 --todo;
11705 di1 = dict_find(d1, hi2->hi_key, -1);
11706 if (d1->dv_scope != 0)
11707 {
11708 /* Disallow replacing a builtin function in l: and g:.
11709 * Check the key to be valid when adding to any
11710 * scope. */
11711 if (d1->dv_scope == VAR_DEF_SCOPE
11712 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11713 && var_check_func_name(hi2->hi_key,
11714 di1 == NULL))
11715 break;
11716 if (!valid_varname(hi2->hi_key))
11717 break;
11718 }
11719 if (di1 == NULL)
11720 {
11721 di1 = dictitem_copy(HI2DI(hi2));
11722 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11723 dictitem_free(di1);
11724 }
11725 else if (*action == 'e')
11726 {
11727 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11728 break;
11729 }
11730 else if (*action == 'f' && HI2DI(hi2) != di1)
11731 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011732 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11733 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011734 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011735 clear_tv(&di1->di_tv);
11736 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11737 }
11738 }
11739 }
11740}
11741
11742/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011743 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011745 */
11746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011747f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011748{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011749 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011750
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011752 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011753 list_T *l1, *l2;
11754 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011755 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011756 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011757
Bram Moolenaare9a41262005-01-15 22:18:47 +000011758 l1 = argvars[0].vval.v_list;
11759 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011760 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011761 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011762 {
11763 if (argvars[2].v_type != VAR_UNKNOWN)
11764 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011765 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011766 if (error)
11767 return; /* type error; errmsg already given */
11768
Bram Moolenaar758711c2005-02-02 23:11:38 +000011769 if (before == l1->lv_len)
11770 item = NULL;
11771 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011772 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011773 item = list_find(l1, before);
11774 if (item == NULL)
11775 {
11776 EMSGN(_(e_listidx), before);
11777 return;
11778 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011779 }
11780 }
11781 else
11782 item = NULL;
11783 list_extend(l1, l2, item);
11784
Bram Moolenaare9a41262005-01-15 22:18:47 +000011785 copy_tv(&argvars[0], rettv);
11786 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011787 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011788 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11789 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011790 dict_T *d1, *d2;
11791 char_u *action;
11792 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011793
11794 d1 = argvars[0].vval.v_dict;
11795 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011796 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011797 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011798 {
11799 /* Check the third argument. */
11800 if (argvars[2].v_type != VAR_UNKNOWN)
11801 {
11802 static char *(av[]) = {"keep", "force", "error"};
11803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011804 action = get_tv_string_chk(&argvars[2]);
11805 if (action == NULL)
11806 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011807 for (i = 0; i < 3; ++i)
11808 if (STRCMP(action, av[i]) == 0)
11809 break;
11810 if (i == 3)
11811 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011812 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011813 return;
11814 }
11815 }
11816 else
11817 action = (char_u *)"force";
11818
Bram Moolenaara9922d62013-05-30 13:01:18 +020011819 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011820
Bram Moolenaare9a41262005-01-15 22:18:47 +000011821 copy_tv(&argvars[0], rettv);
11822 }
11823 }
11824 else
11825 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011826}
11827
11828/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011829 * "feedkeys()" function
11830 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011832f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011833{
11834 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011835 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011836 char_u *keys, *flags;
11837 char_u nbuf[NUMBUFLEN];
11838 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011839 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011840 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011841 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011842
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011843 /* This is not allowed in the sandbox. If the commands would still be
11844 * executed in the sandbox it would be OK, but it probably happens later,
11845 * when "sandbox" is no longer set. */
11846 if (check_secure())
11847 return;
11848
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011849 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011850
11851 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011852 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011853 flags = get_tv_string_buf(&argvars[1], nbuf);
11854 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011855 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011856 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011857 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011858 case 'n': remap = FALSE; break;
11859 case 'm': remap = TRUE; break;
11860 case 't': typed = TRUE; break;
11861 case 'i': insert = TRUE; break;
11862 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011863 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011864 }
11865 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011866 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011867
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011868 if (*keys != NUL || execute)
11869 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011870 /* Need to escape K_SPECIAL and CSI before putting the string in the
11871 * typeahead buffer. */
11872 keys_esc = vim_strsave_escape_csi(keys);
11873 if (keys_esc != NULL)
11874 {
11875 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011876 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011877 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011878 if (vgetc_busy)
11879 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011880 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011881 {
11882 int save_msg_scroll = msg_scroll;
11883
11884 /* Avoid a 1 second delay when the keys start Insert mode. */
11885 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011886
Bram Moolenaar245c4102016-04-20 17:37:41 +020011887 if (!dangerous)
11888 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011889 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011890 if (!dangerous)
11891 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011892 msg_scroll |= save_msg_scroll;
11893 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011894 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011895 }
11896}
11897
11898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 * "filereadable()" function
11900 */
11901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011902f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011904 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 char_u *p;
11906 int n;
11907
Bram Moolenaarc236c162008-07-13 17:41:49 +000011908#ifndef O_NONBLOCK
11909# define O_NONBLOCK 0
11910#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011912 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11913 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 {
11915 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011916 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 }
11918 else
11919 n = FALSE;
11920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011921 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922}
11923
11924/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011925 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 * rights to write into.
11927 */
11928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011929f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011931 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932}
11933
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011934 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011935findfilendir(
11936 typval_T *argvars UNUSED,
11937 typval_T *rettv,
11938 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011939{
11940#ifdef FEAT_SEARCHPATH
11941 char_u *fname;
11942 char_u *fresult = NULL;
11943 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11944 char_u *p;
11945 char_u pathbuf[NUMBUFLEN];
11946 int count = 1;
11947 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011948 int error = FALSE;
11949#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011950
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011951 rettv->vval.v_string = NULL;
11952 rettv->v_type = VAR_STRING;
11953
11954#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011957 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11960 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011961 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011962 else
11963 {
11964 if (*p != NUL)
11965 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011967 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011968 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011969 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011970 }
11971
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011972 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11973 error = TRUE;
11974
11975 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011977 do
11978 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011979 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011980 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 fresult = find_file_in_path_option(first ? fname : NULL,
11982 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011983 0, first, path,
11984 find_what,
11985 curbuf->b_ffname,
11986 find_what == FINDFILE_DIR
11987 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011989
11990 if (fresult != NULL && rettv->v_type == VAR_LIST)
11991 list_append_string(rettv->vval.v_list, fresult, -1);
11992
11993 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011994 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011995
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011996 if (rettv->v_type == VAR_STRING)
11997 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011998#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011999}
12000
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012001static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012002static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012003
12004/*
12005 * Implementation of map() and filter().
12006 */
12007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012008filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012009{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012010 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000012011 listitem_T *li, *nli;
12012 list_T *l = NULL;
12013 dictitem_T *di;
12014 hashtab_T *ht;
12015 hashitem_T *hi;
12016 dict_T *d = NULL;
12017 typval_T save_val;
12018 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012019 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012020 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012021 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020012022 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012023 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012024 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012025 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012026
Bram Moolenaare9a41262005-01-15 22:18:47 +000012027 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012028 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012029 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012030 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012031 return;
12032 }
12033 else if (argvars[0].v_type == VAR_DICT)
12034 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012035 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012036 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012037 return;
12038 }
12039 else
12040 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000012041 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012042 return;
12043 }
12044
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012045 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012046 /* On type errors, the preceding call has already displayed an error
12047 * message. Avoid a misleading error message for an empty string that
12048 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012049 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012051 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012052
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012053 /* We reset "did_emsg" to be able to detect whether an error
12054 * occurred during evaluation of the expression. */
12055 save_did_emsg = did_emsg;
12056 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012057
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012058 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012059 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012061 vimvars[VV_KEY].vv_type = VAR_STRING;
12062
12063 ht = &d->dv_hashtab;
12064 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012065 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012066 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012068 if (!HASHITEM_EMPTY(hi))
12069 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012070 int r;
12071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012072 --todo;
12073 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012074 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020012075 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12076 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077 break;
12078 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012079 r = filter_map_one(&di->di_tv, expr, map, &rem);
12080 clear_tv(&vimvars[VV_KEY].vv_tv);
12081 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012082 break;
12083 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012084 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012085 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12086 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012087 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012088 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012089 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012090 }
12091 }
12092 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012093 }
12094 else
12095 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012096 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012098 for (li = l->lv_first; li != NULL; li = nli)
12099 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012100 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012101 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012102 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012103 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012104 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012105 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012106 break;
12107 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012108 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012109 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012110 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012111 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012112
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012113 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012114 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012115
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012116 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012117 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012118
12119 copy_tv(&argvars[0], rettv);
12120}
12121
12122 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012123filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012124{
Bram Moolenaar33570922005-01-25 22:26:29 +000012125 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012126 typval_T argv[3];
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012127 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000012128 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012129 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012130 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012131
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012133 argv[0] = vimvars[VV_KEY].vv_tv;
12134 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012135 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012136 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012137 s = expr->vval.v_string;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012138 if (call_func(s, (int)STRLEN(s),
12139 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
12140 goto theend;
12141 }
12142 else if (expr->v_type == VAR_PARTIAL)
12143 {
12144 partial_T *partial = expr->vval.v_partial;
12145
12146 s = partial->pt_name;
12147 if (call_func(s, (int)STRLEN(s),
12148 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
12149 == FAIL)
12150 goto theend;
12151 }
12152 else
12153 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012154 s = get_tv_string_buf_chk(expr, buf);
12155 if (s == NULL)
12156 goto theend;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012157 s = skipwhite(s);
12158 if (eval1(&s, &rettv, TRUE) == FAIL)
12159 goto theend;
12160 if (*s != NUL) /* check for trailing chars after expr */
12161 {
12162 EMSG2(_(e_invexpr2), s);
12163 goto theend;
12164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012165 }
12166 if (map)
12167 {
12168 /* map(): replace the list item value */
12169 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012170 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012171 *tv = rettv;
12172 }
12173 else
12174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175 int error = FALSE;
12176
Bram Moolenaare9a41262005-01-15 22:18:47 +000012177 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012178 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012179 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012180 /* On type error, nothing has been removed; return FAIL to stop the
12181 * loop. The error message was given by get_tv_number_chk(). */
12182 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012183 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012184 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012185 retval = OK;
12186theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012187 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012188 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012189}
12190
12191/*
12192 * "filter()" function
12193 */
12194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012195f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012196{
12197 filter_map(argvars, rettv, FALSE);
12198}
12199
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012200/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012201 * "finddir({fname}[, {path}[, {count}]])" function
12202 */
12203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012204f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012205{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012206 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012207}
12208
12209/*
12210 * "findfile({fname}[, {path}[, {count}]])" function
12211 */
12212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012213f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012214{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012215 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012216}
12217
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012218#ifdef FEAT_FLOAT
12219/*
12220 * "float2nr({float})" function
12221 */
12222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012223f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012224{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012225 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012226
12227 if (get_float_arg(argvars, &f) == OK)
12228 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012229# ifdef FEAT_NUM64
12230 if (f < -0x7fffffffffffffff)
12231 rettv->vval.v_number = -0x7fffffffffffffff;
12232 else if (f > 0x7fffffffffffffff)
12233 rettv->vval.v_number = 0x7fffffffffffffff;
12234 else
12235 rettv->vval.v_number = (varnumber_T)f;
12236# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012237 if (f < -0x7fffffff)
12238 rettv->vval.v_number = -0x7fffffff;
12239 else if (f > 0x7fffffff)
12240 rettv->vval.v_number = 0x7fffffff;
12241 else
12242 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012243# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012244 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012245}
12246
12247/*
12248 * "floor({float})" function
12249 */
12250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012251f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012252{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012253 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012254
12255 rettv->v_type = VAR_FLOAT;
12256 if (get_float_arg(argvars, &f) == OK)
12257 rettv->vval.v_float = floor(f);
12258 else
12259 rettv->vval.v_float = 0.0;
12260}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012261
12262/*
12263 * "fmod()" function
12264 */
12265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012266f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012267{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012268 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012269
12270 rettv->v_type = VAR_FLOAT;
12271 if (get_float_arg(argvars, &fx) == OK
12272 && get_float_arg(&argvars[1], &fy) == OK)
12273 rettv->vval.v_float = fmod(fx, fy);
12274 else
12275 rettv->vval.v_float = 0.0;
12276}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012277#endif
12278
Bram Moolenaar0d660222005-01-07 21:51:51 +000012279/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012280 * "fnameescape({string})" function
12281 */
12282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012283f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012284{
12285 rettv->vval.v_string = vim_strsave_fnameescape(
12286 get_tv_string(&argvars[0]), FALSE);
12287 rettv->v_type = VAR_STRING;
12288}
12289
12290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 * "fnamemodify({fname}, {mods})" function
12292 */
12293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012294f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295{
12296 char_u *fname;
12297 char_u *mods;
12298 int usedlen = 0;
12299 int len;
12300 char_u *fbuf = NULL;
12301 char_u buf[NUMBUFLEN];
12302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012303 fname = get_tv_string_chk(&argvars[0]);
12304 mods = get_tv_string_buf_chk(&argvars[1], buf);
12305 if (fname == NULL || mods == NULL)
12306 fname = NULL;
12307 else
12308 {
12309 len = (int)STRLEN(fname);
12310 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012313 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 vim_free(fbuf);
12319}
12320
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012321static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322
12323/*
12324 * "foldclosed()" function
12325 */
12326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012327foldclosed_both(
12328 typval_T *argvars UNUSED,
12329 typval_T *rettv,
12330 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331{
12332#ifdef FEAT_FOLDING
12333 linenr_T lnum;
12334 linenr_T first, last;
12335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012336 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12338 {
12339 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12340 {
12341 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012342 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 return;
12346 }
12347 }
12348#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350}
12351
12352/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012353 * "foldclosed()" function
12354 */
12355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012356f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012357{
12358 foldclosed_both(argvars, rettv, FALSE);
12359}
12360
12361/*
12362 * "foldclosedend()" function
12363 */
12364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012365f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012366{
12367 foldclosed_both(argvars, rettv, TRUE);
12368}
12369
12370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 * "foldlevel()" function
12372 */
12373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012374f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375{
12376#ifdef FEAT_FOLDING
12377 linenr_T lnum;
12378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012381 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383}
12384
12385/*
12386 * "foldtext()" function
12387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012389f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390{
12391#ifdef FEAT_FOLDING
12392 linenr_T lnum;
12393 char_u *s;
12394 char_u *r;
12395 int len;
12396 char *txt;
12397#endif
12398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012399 rettv->v_type = VAR_STRING;
12400 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012402 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12403 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12404 <= curbuf->b_ml.ml_line_count
12405 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 {
12407 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012408 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12409 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 {
12411 if (!linewhite(lnum))
12412 break;
12413 ++lnum;
12414 }
12415
12416 /* Find interesting text in this line. */
12417 s = skipwhite(ml_get(lnum));
12418 /* skip C comment-start */
12419 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012422 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012423 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012424 {
12425 s = skipwhite(ml_get(lnum + 1));
12426 if (*s == '*')
12427 s = skipwhite(s + 1);
12428 }
12429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 txt = _("+-%s%3ld lines: ");
12431 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012432 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 + 20 /* for %3ld */
12434 + STRLEN(s))); /* concatenated */
12435 if (r != NULL)
12436 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012437 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12438 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12439 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440 len = (int)STRLEN(r);
12441 STRCAT(r, s);
12442 /* remove 'foldmarker' and 'commentstring' */
12443 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 }
12446 }
12447#endif
12448}
12449
12450/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012451 * "foldtextresult(lnum)" function
12452 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012454f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012455{
12456#ifdef FEAT_FOLDING
12457 linenr_T lnum;
12458 char_u *text;
12459 char_u buf[51];
12460 foldinfo_T foldinfo;
12461 int fold_count;
12462#endif
12463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012464 rettv->v_type = VAR_STRING;
12465 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012466#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012467 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 /* treat illegal types and illegal string values for {lnum} the same */
12469 if (lnum < 0)
12470 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012471 fold_count = foldedCount(curwin, lnum, &foldinfo);
12472 if (fold_count > 0)
12473 {
12474 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12475 &foldinfo, buf);
12476 if (text == buf)
12477 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012479 }
12480#endif
12481}
12482
12483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 * "foreground()" function
12485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012487f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef FEAT_GUI
12490 if (gui.in_use)
12491 gui_mch_set_foreground();
12492#else
12493# ifdef WIN32
12494 win32_set_foreground();
12495# endif
12496#endif
12497}
12498
12499/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012500 * "function()" function
12501 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012503f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012504{
12505 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012506 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012507 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012508 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012509
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012510 if (argvars[0].v_type == VAR_FUNC)
12511 {
12512 /* function(MyFunc, [arg], dict) */
12513 s = argvars[0].vval.v_string;
12514 }
12515 else if (argvars[0].v_type == VAR_PARTIAL
12516 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012517 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012518 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012519 arg_pt = argvars[0].vval.v_partial;
12520 s = arg_pt->pt_name;
12521 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012522 else
12523 {
12524 /* function('MyFunc', [arg], dict) */
12525 s = get_tv_string(&argvars[0]);
12526 use_string = TRUE;
12527 }
12528
12529 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012530 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012531 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012532 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12533 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012534 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012535 else
12536 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012537 int dict_idx = 0;
12538 int arg_idx = 0;
12539 list_T *list = NULL;
12540
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012541 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012542 {
12543 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012544 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012545
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012546 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12547 * also be called from another script. Using trans_function_name()
12548 * would also work, but some plugins depend on the name being
12549 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012550 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012551 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12552 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012553 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012554 STRCPY(name, sid_buf);
12555 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012556 }
12557 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012558 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012559 name = vim_strsave(s);
12560
12561 if (argvars[1].v_type != VAR_UNKNOWN)
12562 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012563 if (argvars[2].v_type != VAR_UNKNOWN)
12564 {
12565 /* function(name, [args], dict) */
12566 arg_idx = 1;
12567 dict_idx = 2;
12568 }
12569 else if (argvars[1].v_type == VAR_DICT)
12570 /* function(name, dict) */
12571 dict_idx = 1;
12572 else
12573 /* function(name, [args]) */
12574 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012575 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012576 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012577 if (argvars[dict_idx].v_type != VAR_DICT)
12578 {
12579 EMSG(_("E922: expected a dict"));
12580 vim_free(name);
12581 return;
12582 }
12583 if (argvars[dict_idx].vval.v_dict == NULL)
12584 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012585 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012586 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012587 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012588 if (argvars[arg_idx].v_type != VAR_LIST)
12589 {
12590 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12591 vim_free(name);
12592 return;
12593 }
12594 list = argvars[arg_idx].vval.v_list;
12595 if (list == NULL || list->lv_len == 0)
12596 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012597 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012598 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012599 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012600 {
12601 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012602
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012603 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012604 if (pt == NULL)
12605 vim_free(name);
12606 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012607 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012608 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012609 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012610 listitem_T *li;
12611 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012612 int arg_len = 0;
12613 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012614
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012615 if (arg_pt != NULL)
12616 arg_len = arg_pt->pt_argc;
12617 if (list != NULL)
12618 lv_len = list->lv_len;
12619 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012620 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012621 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012622 if (pt->pt_argv == NULL)
12623 {
12624 vim_free(pt);
12625 vim_free(name);
12626 return;
12627 }
12628 else
12629 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012630 for (i = 0; i < arg_len; i++)
12631 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12632 if (lv_len > 0)
12633 for (li = list->lv_first; li != NULL;
12634 li = li->li_next)
12635 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012636 }
12637 }
12638
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012639 /* For "function(dict.func, [], dict)" and "func" is a partial
12640 * use "dict". That is backwards compatible. */
12641 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012642 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012643 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012644 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12645 ++pt->pt_dict->dv_refcount;
12646 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012647 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012648 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012649 /* If the dict was bound automatically the result is also
12650 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012651 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012652 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012653 if (pt->pt_dict != NULL)
12654 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012655 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012656
12657 pt->pt_refcount = 1;
12658 pt->pt_name = name;
12659 func_ref(pt->pt_name);
12660 }
12661 rettv->v_type = VAR_PARTIAL;
12662 rettv->vval.v_partial = pt;
12663 }
12664 else
12665 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012666 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012667 rettv->v_type = VAR_FUNC;
12668 rettv->vval.v_string = name;
12669 func_ref(name);
12670 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012671 }
12672}
12673
12674/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012675 * "garbagecollect()" function
12676 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012678f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012679{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012680 /* This is postponed until we are back at the toplevel, because we may be
12681 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12682 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012683
12684 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12685 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012686}
12687
12688/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012689 * "get()" function
12690 */
12691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012692f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012693{
Bram Moolenaar33570922005-01-25 22:26:29 +000012694 listitem_T *li;
12695 list_T *l;
12696 dictitem_T *di;
12697 dict_T *d;
12698 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012699
Bram Moolenaare9a41262005-01-15 22:18:47 +000012700 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012701 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012702 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012704 int error = FALSE;
12705
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012706 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012708 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012709 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012710 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012711 else if (argvars[0].v_type == VAR_DICT)
12712 {
12713 if ((d = argvars[0].vval.v_dict) != NULL)
12714 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012715 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012716 if (di != NULL)
12717 tv = &di->di_tv;
12718 }
12719 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012720 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012721 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012722 partial_T *pt;
12723 partial_T fref_pt;
12724
12725 if (argvars[0].v_type == VAR_PARTIAL)
12726 pt = argvars[0].vval.v_partial;
12727 else
12728 {
12729 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12730 fref_pt.pt_name = argvars[0].vval.v_string;
12731 pt = &fref_pt;
12732 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012733
12734 if (pt != NULL)
12735 {
12736 char_u *what = get_tv_string(&argvars[1]);
12737
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012738 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012739 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012740 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012741 if (pt->pt_name == NULL)
12742 rettv->vval.v_string = NULL;
12743 else
12744 rettv->vval.v_string = vim_strsave(pt->pt_name);
12745 }
12746 else if (STRCMP(what, "dict") == 0)
12747 {
12748 rettv->v_type = VAR_DICT;
12749 rettv->vval.v_dict = pt->pt_dict;
12750 if (pt->pt_dict != NULL)
12751 ++pt->pt_dict->dv_refcount;
12752 }
12753 else if (STRCMP(what, "args") == 0)
12754 {
12755 rettv->v_type = VAR_LIST;
12756 if (rettv_list_alloc(rettv) == OK)
12757 {
12758 int i;
12759
12760 for (i = 0; i < pt->pt_argc; ++i)
12761 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12762 }
12763 }
12764 else
12765 EMSG2(_(e_invarg2), what);
12766 return;
12767 }
12768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012769 else
12770 EMSG2(_(e_listdictarg), "get()");
12771
12772 if (tv == NULL)
12773 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012774 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012775 copy_tv(&argvars[2], rettv);
12776 }
12777 else
12778 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012779}
12780
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012781static 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 +000012782
12783/*
12784 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012785 * Return a range (from start to end) of lines in rettv from the specified
12786 * buffer.
12787 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012788 */
12789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012790get_buffer_lines(
12791 buf_T *buf,
12792 linenr_T start,
12793 linenr_T end,
12794 int retlist,
12795 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012796{
12797 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012798
Bram Moolenaar959a1432013-12-14 12:17:38 +010012799 rettv->v_type = VAR_STRING;
12800 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012801 if (retlist && rettv_list_alloc(rettv) == FAIL)
12802 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012803
12804 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12805 return;
12806
12807 if (!retlist)
12808 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012809 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12810 p = ml_get_buf(buf, start, FALSE);
12811 else
12812 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012813 rettv->vval.v_string = vim_strsave(p);
12814 }
12815 else
12816 {
12817 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012818 return;
12819
12820 if (start < 1)
12821 start = 1;
12822 if (end > buf->b_ml.ml_line_count)
12823 end = buf->b_ml.ml_line_count;
12824 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012825 if (list_append_string(rettv->vval.v_list,
12826 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012827 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012828 }
12829}
12830
12831/*
12832 * "getbufline()" function
12833 */
12834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012835f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012836{
12837 linenr_T lnum;
12838 linenr_T end;
12839 buf_T *buf;
12840
12841 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12842 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012843 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012844 --emsg_off;
12845
Bram Moolenaar661b1822005-07-28 22:36:45 +000012846 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012847 if (argvars[2].v_type == VAR_UNKNOWN)
12848 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012849 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012850 end = get_tv_lnum_buf(&argvars[2], buf);
12851
Bram Moolenaar342337a2005-07-21 21:11:17 +000012852 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012853}
12854
Bram Moolenaar0d660222005-01-07 21:51:51 +000012855/*
12856 * "getbufvar()" function
12857 */
12858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012859f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012860{
12861 buf_T *buf;
12862 buf_T *save_curbuf;
12863 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012865 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012867 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12868 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012869 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012870 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012871
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012872 rettv->v_type = VAR_STRING;
12873 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012874
12875 if (buf != NULL && varname != NULL)
12876 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012877 /* set curbuf to be our buf, temporarily */
12878 save_curbuf = curbuf;
12879 curbuf = buf;
12880
Bram Moolenaar0d660222005-01-07 21:51:51 +000012881 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012882 {
12883 if (get_option_tv(&varname, rettv, TRUE) == OK)
12884 done = TRUE;
12885 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012886 else if (STRCMP(varname, "changedtick") == 0)
12887 {
12888 rettv->v_type = VAR_NUMBER;
12889 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012890 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012891 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012892 else
12893 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012894 /* Look up the variable. */
12895 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12896 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12897 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012898 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012899 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012900 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012901 done = TRUE;
12902 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012903 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012904
12905 /* restore previous notion of curbuf */
12906 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012907 }
12908
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012909 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12910 /* use the default value */
12911 copy_tv(&argvars[2], rettv);
12912
Bram Moolenaar0d660222005-01-07 21:51:51 +000012913 --emsg_off;
12914}
12915
12916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 * "getchar()" function
12918 */
12919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012920f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921{
12922 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012923 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012925 /* Position the cursor. Needed after a message that ends in a space. */
12926 windgoto(msg_row, msg_col);
12927
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928 ++no_mapping;
12929 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012930 for (;;)
12931 {
12932 if (argvars[0].v_type == VAR_UNKNOWN)
12933 /* getchar(): blocking wait. */
12934 n = safe_vgetc();
12935 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12936 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012937 n = vpeekc_any();
12938 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012939 /* illegal argument or getchar(0) and no char avail: return zero */
12940 n = 0;
12941 else
12942 /* getchar(0) and char avail: return char */
12943 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012944
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012945 if (n == K_IGNORE)
12946 continue;
12947 break;
12948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 --no_mapping;
12950 --allow_keys;
12951
Bram Moolenaar219b8702006-11-01 14:32:36 +000012952 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012953 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012954 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12955 vimvars[VV_MOUSE_COL].vv_nr = 0;
12956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 if (IS_SPECIAL(n) || mod_mask != 0)
12959 {
12960 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12961 int i = 0;
12962
12963 /* Turn a special key into three bytes, plus modifier. */
12964 if (mod_mask != 0)
12965 {
12966 temp[i++] = K_SPECIAL;
12967 temp[i++] = KS_MODIFIER;
12968 temp[i++] = mod_mask;
12969 }
12970 if (IS_SPECIAL(n))
12971 {
12972 temp[i++] = K_SPECIAL;
12973 temp[i++] = K_SECOND(n);
12974 temp[i++] = K_THIRD(n);
12975 }
12976#ifdef FEAT_MBYTE
12977 else if (has_mbyte)
12978 i += (*mb_char2bytes)(n, temp + i);
12979#endif
12980 else
12981 temp[i++] = n;
12982 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012983 rettv->v_type = VAR_STRING;
12984 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012985
12986#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012987 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012988 {
12989 int row = mouse_row;
12990 int col = mouse_col;
12991 win_T *win;
12992 linenr_T lnum;
12993# ifdef FEAT_WINDOWS
12994 win_T *wp;
12995# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012996 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012997
12998 if (row >= 0 && col >= 0)
12999 {
13000 /* Find the window at the mouse coordinates and compute the
13001 * text position. */
13002 win = mouse_find_win(&row, &col);
13003 (void)mouse_comp_pos(win, &row, &col, &lnum);
13004# ifdef FEAT_WINDOWS
13005 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000013006 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000013007# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000013008 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020013009 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000013010 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
13011 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
13012 }
13013 }
13014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015 }
13016}
13017
13018/*
13019 * "getcharmod()" function
13020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013022f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025}
13026
13027/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020013028 * "getcharsearch()" function
13029 */
13030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013031f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020013032{
13033 if (rettv_dict_alloc(rettv) != FAIL)
13034 {
13035 dict_T *dict = rettv->vval.v_dict;
13036
13037 dict_add_nr_str(dict, "char", 0L, last_csearch());
13038 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
13039 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
13040 }
13041}
13042
13043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 * "getcmdline()" function
13045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013047f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013049 rettv->v_type = VAR_STRING;
13050 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051}
13052
13053/*
13054 * "getcmdpos()" function
13055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013057f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060}
13061
13062/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013063 * "getcmdtype()" function
13064 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013066f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013067{
13068 rettv->v_type = VAR_STRING;
13069 rettv->vval.v_string = alloc(2);
13070 if (rettv->vval.v_string != NULL)
13071 {
13072 rettv->vval.v_string[0] = get_cmdline_type();
13073 rettv->vval.v_string[1] = NUL;
13074 }
13075}
13076
13077/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013078 * "getcmdwintype()" function
13079 */
13080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013081f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013082{
13083 rettv->v_type = VAR_STRING;
13084 rettv->vval.v_string = NULL;
13085#ifdef FEAT_CMDWIN
13086 rettv->vval.v_string = alloc(2);
13087 if (rettv->vval.v_string != NULL)
13088 {
13089 rettv->vval.v_string[0] = cmdwin_type;
13090 rettv->vval.v_string[1] = NUL;
13091 }
13092#endif
13093}
13094
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020013095#if defined(FEAT_CMDL_COMPL)
13096/*
13097 * "getcompletion()" function
13098 */
13099 static void
13100f_getcompletion(typval_T *argvars, typval_T *rettv)
13101{
13102 char_u *pat;
13103 expand_T xpc;
13104 int options = WILD_KEEP_ALL | WILD_SILENT | WILD_USE_NL
13105 | WILD_LIST_NOTFOUND | WILD_NO_BEEP;
13106
13107 if (p_wic)
13108 options |= WILD_ICASE;
13109
13110 ExpandInit(&xpc);
13111 xpc.xp_pattern = get_tv_string(&argvars[0]);
Bram Moolenaar25065ec2016-07-10 19:22:53 +020013112 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020013113 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
13114 if (xpc.xp_context == EXPAND_NOTHING)
13115 {
13116 if (argvars[1].v_type == VAR_STRING)
13117 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
13118 else
13119 EMSG(_(e_invarg));
13120 return;
13121 }
13122
13123 if (xpc.xp_context == EXPAND_MENUS)
13124 {
13125 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
Bram Moolenaar25065ec2016-07-10 19:22:53 +020013126 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020013127 }
13128
13129 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
13130 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
13131 {
13132 int i;
13133
13134 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
13135
13136 for (i = 0; i < xpc.xp_numfiles; i++)
13137 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13138 }
13139 vim_free(pat);
13140 ExpandCleanup(&xpc);
13141}
13142#endif
13143
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013145 * "getcwd()" function
13146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013148f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013150 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013151 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013154 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010013155
13156 wp = find_tabwin(&argvars[0], &argvars[1]);
13157 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013159 if (wp->w_localdir != NULL)
13160 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar5c719942016-07-09 23:40:45 +020013161 else if (globaldir != NULL)
Bram Moolenaarc9703302016-01-17 21:49:33 +010013162 rettv->vval.v_string = vim_strsave(globaldir);
13163 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020013164 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013165 cwd = alloc(MAXPATHL);
13166 if (cwd != NULL)
13167 {
13168 if (mch_dirname(cwd, MAXPATHL) != FAIL)
13169 rettv->vval.v_string = vim_strsave(cwd);
13170 vim_free(cwd);
13171 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020013172 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010013173#ifdef BACKSLASH_IN_FILENAME
13174 if (rettv->vval.v_string != NULL)
13175 slash_adjust(rettv->vval.v_string);
13176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177 }
13178}
13179
13180/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013181 * "getfontname()" function
13182 */
13183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013184f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013185{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186 rettv->v_type = VAR_STRING;
13187 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013188#ifdef FEAT_GUI
13189 if (gui.in_use)
13190 {
13191 GuiFont font;
13192 char_u *name = NULL;
13193
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013194 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013195 {
13196 /* Get the "Normal" font. Either the name saved by
13197 * hl_set_font_name() or from the font ID. */
13198 font = gui.norm_font;
13199 name = hl_get_font_name();
13200 }
13201 else
13202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013204 if (STRCMP(name, "*") == 0) /* don't use font dialog */
13205 return;
13206 font = gui_mch_get_font(name, FALSE);
13207 if (font == NOFONT)
13208 return; /* Invalid font name, return empty string. */
13209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013211 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013212 gui_mch_free_font(font);
13213 }
13214#endif
13215}
13216
13217/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013218 * "getfperm({fname})" function
13219 */
13220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013221f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013222{
13223 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013224 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013225 char_u *perm = NULL;
13226 char_u flags[] = "rwx";
13227 int i;
13228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013232 if (mch_stat((char *)fname, &st) >= 0)
13233 {
13234 perm = vim_strsave((char_u *)"---------");
13235 if (perm != NULL)
13236 {
13237 for (i = 0; i < 9; i++)
13238 {
13239 if (st.st_mode & (1 << (8 - i)))
13240 perm[i] = flags[i % 3];
13241 }
13242 }
13243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013244 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013245}
13246
13247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 * "getfsize({fname})" function
13249 */
13250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013251f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252{
13253 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013254 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013258 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259
13260 if (mch_stat((char *)fname, &st) >= 0)
13261 {
13262 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013266 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013267
13268 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013269 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013270 rettv->vval.v_number = -2;
13271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 }
13273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013274 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275}
13276
13277/*
13278 * "getftime({fname})" function
13279 */
13280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013281f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282{
13283 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013284 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013286 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287
13288 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013289 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292}
13293
13294/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013295 * "getftype({fname})" function
13296 */
13297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013298f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013299{
13300 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013301 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013302 char_u *type = NULL;
13303 char *t;
13304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013305 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013308 if (mch_lstat((char *)fname, &st) >= 0)
13309 {
13310#ifdef S_ISREG
13311 if (S_ISREG(st.st_mode))
13312 t = "file";
13313 else if (S_ISDIR(st.st_mode))
13314 t = "dir";
13315# ifdef S_ISLNK
13316 else if (S_ISLNK(st.st_mode))
13317 t = "link";
13318# endif
13319# ifdef S_ISBLK
13320 else if (S_ISBLK(st.st_mode))
13321 t = "bdev";
13322# endif
13323# ifdef S_ISCHR
13324 else if (S_ISCHR(st.st_mode))
13325 t = "cdev";
13326# endif
13327# ifdef S_ISFIFO
13328 else if (S_ISFIFO(st.st_mode))
13329 t = "fifo";
13330# endif
13331# ifdef S_ISSOCK
13332 else if (S_ISSOCK(st.st_mode))
13333 t = "fifo";
13334# endif
13335 else
13336 t = "other";
13337#else
13338# ifdef S_IFMT
13339 switch (st.st_mode & S_IFMT)
13340 {
13341 case S_IFREG: t = "file"; break;
13342 case S_IFDIR: t = "dir"; break;
13343# ifdef S_IFLNK
13344 case S_IFLNK: t = "link"; break;
13345# endif
13346# ifdef S_IFBLK
13347 case S_IFBLK: t = "bdev"; break;
13348# endif
13349# ifdef S_IFCHR
13350 case S_IFCHR: t = "cdev"; break;
13351# endif
13352# ifdef S_IFIFO
13353 case S_IFIFO: t = "fifo"; break;
13354# endif
13355# ifdef S_IFSOCK
13356 case S_IFSOCK: t = "socket"; break;
13357# endif
13358 default: t = "other";
13359 }
13360# else
13361 if (mch_isdir(fname))
13362 t = "dir";
13363 else
13364 t = "file";
13365# endif
13366#endif
13367 type = vim_strsave((char_u *)t);
13368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013369 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013370}
13371
13372/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013373 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013374 */
13375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013376f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013377{
13378 linenr_T lnum;
13379 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013380 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013381
13382 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013383 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013384 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013385 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013386 retlist = FALSE;
13387 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013388 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013389 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013390 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013391 retlist = TRUE;
13392 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013393
Bram Moolenaar342337a2005-07-21 21:11:17 +000013394 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013395}
13396
13397/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013398 * "getmatches()" function
13399 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013401f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013402{
13403#ifdef FEAT_SEARCH_EXTRA
13404 dict_T *dict;
13405 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013406 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013407
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013408 if (rettv_list_alloc(rettv) == OK)
13409 {
13410 while (cur != NULL)
13411 {
13412 dict = dict_alloc();
13413 if (dict == NULL)
13414 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013415 if (cur->match.regprog == NULL)
13416 {
13417 /* match added with matchaddpos() */
13418 for (i = 0; i < MAXPOSMATCH; ++i)
13419 {
13420 llpos_T *llpos;
13421 char buf[6];
13422 list_T *l;
13423
13424 llpos = &cur->pos.pos[i];
13425 if (llpos->lnum == 0)
13426 break;
13427 l = list_alloc();
13428 if (l == NULL)
13429 break;
13430 list_append_number(l, (varnumber_T)llpos->lnum);
13431 if (llpos->col > 0)
13432 {
13433 list_append_number(l, (varnumber_T)llpos->col);
13434 list_append_number(l, (varnumber_T)llpos->len);
13435 }
13436 sprintf(buf, "pos%d", i + 1);
13437 dict_add_list(dict, buf, l);
13438 }
13439 }
13440 else
13441 {
13442 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13443 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013444 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013445 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13446 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013447# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013448 if (cur->conceal_char)
13449 {
13450 char_u buf[MB_MAXBYTES + 1];
13451
13452 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13453 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13454 }
13455# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013456 list_append_dict(rettv->vval.v_list, dict);
13457 cur = cur->next;
13458 }
13459 }
13460#endif
13461}
13462
13463/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013464 * "getpid()" function
13465 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013467f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013468{
13469 rettv->vval.v_number = mch_get_pid();
13470}
13471
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013473getpos_both(
13474 typval_T *argvars,
13475 typval_T *rettv,
13476 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013477{
Bram Moolenaara5525202006-03-02 22:52:09 +000013478 pos_T *fp;
13479 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013480 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013481
13482 if (rettv_list_alloc(rettv) == OK)
13483 {
13484 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013485 if (getcurpos)
13486 fp = &curwin->w_cursor;
13487 else
13488 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013489 if (fnum != -1)
13490 list_append_number(l, (varnumber_T)fnum);
13491 else
13492 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013493 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13494 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013495 list_append_number(l, (fp != NULL)
13496 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013497 : (varnumber_T)0);
13498 list_append_number(l,
13499#ifdef FEAT_VIRTUALEDIT
13500 (fp != NULL) ? (varnumber_T)fp->coladd :
13501#endif
13502 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013503 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013504 {
13505 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013506 list_append_number(l, curwin->w_curswant == MAXCOL ?
13507 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013508 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013509 }
13510 else
13511 rettv->vval.v_number = FALSE;
13512}
13513
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013514
13515/*
13516 * "getcurpos()" function
13517 */
13518 static void
13519f_getcurpos(typval_T *argvars, typval_T *rettv)
13520{
13521 getpos_both(argvars, rettv, TRUE);
13522}
13523
13524/*
13525 * "getpos(string)" function
13526 */
13527 static void
13528f_getpos(typval_T *argvars, typval_T *rettv)
13529{
13530 getpos_both(argvars, rettv, FALSE);
13531}
13532
Bram Moolenaara5525202006-03-02 22:52:09 +000013533/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013534 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013535 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013537f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013538{
13539#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013540 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013541#endif
13542
Bram Moolenaar2641f772005-03-25 21:58:17 +000013543#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013544 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013545 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013546 wp = NULL;
13547 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13548 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013549 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013550 if (wp == NULL)
13551 return;
13552 }
13553
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013554 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013555 }
13556#endif
13557}
13558
13559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 * "getreg()" function
13561 */
13562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013563f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564{
13565 char_u *strregname;
13566 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013567 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013568 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013571 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013573 strregname = get_tv_string_chk(&argvars[0]);
13574 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013575 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013576 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013577 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013578 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013579 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013580 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013583 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013584
13585 if (error)
13586 return;
13587
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 regname = (strregname == NULL ? '"' : *strregname);
13589 if (regname == 0)
13590 regname = '"';
13591
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013592 if (return_list)
13593 {
13594 rettv->v_type = VAR_LIST;
13595 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13596 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013597 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013598 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013599 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013600 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013601 }
13602 else
13603 {
13604 rettv->v_type = VAR_STRING;
13605 rettv->vval.v_string = get_reg_contents(regname,
13606 arg2 ? GREG_EXPR_SRC : 0);
13607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608}
13609
13610/*
13611 * "getregtype()" function
13612 */
13613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013614f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615{
13616 char_u *strregname;
13617 int regname;
13618 char_u buf[NUMBUFLEN + 2];
13619 long reglen = 0;
13620
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013621 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013622 {
13623 strregname = get_tv_string_chk(&argvars[0]);
13624 if (strregname == NULL) /* type error; errmsg already given */
13625 {
13626 rettv->v_type = VAR_STRING;
13627 rettv->vval.v_string = NULL;
13628 return;
13629 }
13630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 else
13632 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013633 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634
13635 regname = (strregname == NULL ? '"' : *strregname);
13636 if (regname == 0)
13637 regname = '"';
13638
13639 buf[0] = NUL;
13640 buf[1] = NUL;
13641 switch (get_reg_type(regname, &reglen))
13642 {
13643 case MLINE: buf[0] = 'V'; break;
13644 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 case MBLOCK:
13646 buf[0] = Ctrl_V;
13647 sprintf((char *)buf + 1, "%ld", reglen + 1);
13648 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650 rettv->v_type = VAR_STRING;
13651 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652}
13653
13654/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013655 * "gettabvar()" function
13656 */
13657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013658f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013659{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013660 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013661 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013662 dictitem_T *v;
13663 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013664 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013665
13666 rettv->v_type = VAR_STRING;
13667 rettv->vval.v_string = NULL;
13668
13669 varname = get_tv_string_chk(&argvars[1]);
13670 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13671 if (tp != NULL && varname != NULL)
13672 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013673 /* Set tp to be our tabpage, temporarily. Also set the window to the
13674 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013675 if (switch_win(&oldcurwin, &oldtabpage,
13676 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013677 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013678 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013679 /* look up the variable */
13680 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13681 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13682 if (v != NULL)
13683 {
13684 copy_tv(&v->di_tv, rettv);
13685 done = TRUE;
13686 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013687 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013688
13689 /* restore previous notion of curwin */
13690 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013691 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013692
13693 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013694 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013695}
13696
13697/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013698 * "gettabwinvar()" function
13699 */
13700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013701f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013702{
13703 getwinvar(argvars, rettv, 1);
13704}
13705
13706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 * "getwinposx()" function
13708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013710f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713#ifdef FEAT_GUI
13714 if (gui.in_use)
13715 {
13716 int x, y;
13717
13718 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 }
13721#endif
13722}
13723
13724/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013725 * "win_findbuf()" function
13726 */
13727 static void
13728f_win_findbuf(typval_T *argvars, typval_T *rettv)
13729{
13730 if (rettv_list_alloc(rettv) != FAIL)
13731 win_findbuf(argvars, rettv->vval.v_list);
13732}
13733
13734/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013735 * "win_getid()" function
13736 */
13737 static void
13738f_win_getid(typval_T *argvars, typval_T *rettv)
13739{
13740 rettv->vval.v_number = win_getid(argvars);
13741}
13742
13743/*
13744 * "win_gotoid()" function
13745 */
13746 static void
13747f_win_gotoid(typval_T *argvars, typval_T *rettv)
13748{
13749 rettv->vval.v_number = win_gotoid(argvars);
13750}
13751
13752/*
13753 * "win_id2tabwin()" function
13754 */
13755 static void
13756f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13757{
13758 if (rettv_list_alloc(rettv) != FAIL)
13759 win_id2tabwin(argvars, rettv->vval.v_list);
13760}
13761
13762/*
13763 * "win_id2win()" function
13764 */
13765 static void
13766f_win_id2win(typval_T *argvars, typval_T *rettv)
13767{
13768 rettv->vval.v_number = win_id2win(argvars);
13769}
13770
13771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772 * "getwinposy()" function
13773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013775f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013777 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778#ifdef FEAT_GUI
13779 if (gui.in_use)
13780 {
13781 int x, y;
13782
13783 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013784 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 }
13786#endif
13787}
13788
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013789/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013790 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013791 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013792 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013793find_win_by_nr(
13794 typval_T *vp,
13795 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013796{
13797#ifdef FEAT_WINDOWS
13798 win_T *wp;
13799#endif
13800 int nr;
13801
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013802 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013803
13804#ifdef FEAT_WINDOWS
13805 if (nr < 0)
13806 return NULL;
13807 if (nr == 0)
13808 return curwin;
13809
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013810 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13811 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013812 if (nr >= LOWEST_WIN_ID)
13813 {
13814 if (wp->w_id == nr)
13815 return wp;
13816 }
13817 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013818 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013819 if (nr >= LOWEST_WIN_ID)
13820 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013821 return wp;
13822#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013823 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013824 return curwin;
13825 return NULL;
13826#endif
13827}
13828
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013830 * Find window specified by "wvp" in tabpage "tvp".
13831 */
13832 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013833find_tabwin(
13834 typval_T *wvp, /* VAR_UNKNOWN for current window */
13835 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013836{
13837 win_T *wp = NULL;
13838 tabpage_T *tp = NULL;
13839 long n;
13840
13841 if (wvp->v_type != VAR_UNKNOWN)
13842 {
13843 if (tvp->v_type != VAR_UNKNOWN)
13844 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013845 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013846 if (n >= 0)
13847 tp = find_tabpage(n);
13848 }
13849 else
13850 tp = curtab;
13851
13852 if (tp != NULL)
13853 wp = find_win_by_nr(wvp, tp);
13854 }
13855 else
13856 wp = curwin;
13857
13858 return wp;
13859}
13860
13861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 * "getwinvar()" function
13863 */
13864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013865f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013867 getwinvar(argvars, rettv, 0);
13868}
13869
13870/*
13871 * getwinvar() and gettabwinvar()
13872 */
13873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013874getwinvar(
13875 typval_T *argvars,
13876 typval_T *rettv,
13877 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013878{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013879 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013881 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013882 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013883 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013884#ifdef FEAT_WINDOWS
13885 win_T *oldcurwin;
13886 tabpage_T *oldtabpage;
13887 int need_switch_win;
13888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013890#ifdef FEAT_WINDOWS
13891 if (off == 1)
13892 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13893 else
13894 tp = curtab;
13895#endif
13896 win = find_win_by_nr(&argvars[off], tp);
13897 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013900 rettv->v_type = VAR_STRING;
13901 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902
13903 if (win != NULL && varname != NULL)
13904 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013905#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013906 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013907 * otherwise the window is not valid. Only do this when needed,
13908 * autocommands get blocked. */
13909 need_switch_win = !(tp == curtab && win == curwin);
13910 if (!need_switch_win
13911 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13912#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013913 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013914 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013915 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013916 if (get_option_tv(&varname, rettv, 1) == OK)
13917 done = TRUE;
13918 }
13919 else
13920 {
13921 /* Look up the variable. */
13922 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13923 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13924 varname, FALSE);
13925 if (v != NULL)
13926 {
13927 copy_tv(&v->di_tv, rettv);
13928 done = TRUE;
13929 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013932
Bram Moolenaarba117c22015-09-29 16:53:22 +020013933#ifdef FEAT_WINDOWS
13934 if (need_switch_win)
13935 /* restore previous notion of curwin */
13936 restore_win(oldcurwin, oldtabpage, TRUE);
13937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 }
13939
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013940 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13941 /* use the default return value */
13942 copy_tv(&argvars[off + 2], rettv);
13943
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 --emsg_off;
13945}
13946
13947/*
13948 * "glob()" function
13949 */
13950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013951f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013953 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013955 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013957 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013958 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013959 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013960 if (argvars[1].v_type != VAR_UNKNOWN)
13961 {
13962 if (get_tv_number_chk(&argvars[1], &error))
13963 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013965 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013966 if (get_tv_number_chk(&argvars[2], &error))
13967 {
13968 rettv->v_type = VAR_LIST;
13969 rettv->vval.v_list = NULL;
13970 }
13971 if (argvars[3].v_type != VAR_UNKNOWN
13972 && get_tv_number_chk(&argvars[3], &error))
13973 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013974 }
13975 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013976 if (!error)
13977 {
13978 ExpandInit(&xpc);
13979 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013980 if (p_wic)
13981 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013982 if (rettv->v_type == VAR_STRING)
13983 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013984 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013985 else if (rettv_list_alloc(rettv) != FAIL)
13986 {
13987 int i;
13988
13989 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13990 NULL, options, WILD_ALL_KEEP);
13991 for (i = 0; i < xpc.xp_numfiles; i++)
13992 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13993
13994 ExpandCleanup(&xpc);
13995 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013996 }
13997 else
13998 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999}
14000
14001/*
14002 * "globpath()" function
14003 */
14004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014005f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014007 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014010 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014011 garray_T ga;
14012 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014014 /* When the optional second argument is non-zero, don't remove matches
14015 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014016 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014017 if (argvars[2].v_type != VAR_UNKNOWN)
14018 {
14019 if (get_tv_number_chk(&argvars[2], &error))
14020 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010014021 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014022 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010014023 if (get_tv_number_chk(&argvars[3], &error))
14024 {
14025 rettv->v_type = VAR_LIST;
14026 rettv->vval.v_list = NULL;
14027 }
14028 if (argvars[4].v_type != VAR_UNKNOWN
14029 && get_tv_number_chk(&argvars[4], &error))
14030 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014031 }
14032 }
14033 if (file != NULL && !error)
14034 {
14035 ga_init2(&ga, (int)sizeof(char_u *), 10);
14036 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
14037 if (rettv->v_type == VAR_STRING)
14038 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
14039 else if (rettv_list_alloc(rettv) != FAIL)
14040 for (i = 0; i < ga.ga_len; ++i)
14041 list_append_string(rettv->vval.v_list,
14042 ((char_u **)(ga.ga_data))[i], -1);
14043 ga_clear_strings(&ga);
14044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014046 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047}
14048
14049/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014050 * "glob2regpat()" function
14051 */
14052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014053f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014054{
14055 char_u *pat = get_tv_string_chk(&argvars[0]);
14056
14057 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010014058 rettv->vval.v_string = (pat == NULL)
14059 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014060}
14061
14062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063 * "has()" function
14064 */
14065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014066f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067{
14068 int i;
14069 char_u *name;
14070 int n = FALSE;
14071 static char *(has_list[]) =
14072 {
14073#ifdef AMIGA
14074 "amiga",
14075# ifdef FEAT_ARP
14076 "arp",
14077# endif
14078#endif
14079#ifdef __BEOS__
14080 "beos",
14081#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000014082#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 "mac",
14084#endif
14085#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010014086 "macunix", /* built with 'darwin' enabled */
14087#endif
14088#if defined(__APPLE__) && __APPLE__ == 1
14089 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091#ifdef __QNX__
14092 "qnx",
14093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094#ifdef UNIX
14095 "unix",
14096#endif
14097#ifdef VMS
14098 "vms",
14099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100#ifdef WIN32
14101 "win32",
14102#endif
14103#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
14104 "win32unix",
14105#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010014106#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 "win64",
14108#endif
14109#ifdef EBCDIC
14110 "ebcdic",
14111#endif
14112#ifndef CASE_INSENSITIVE_FILENAME
14113 "fname_case",
14114#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014115#ifdef HAVE_ACL
14116 "acl",
14117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118#ifdef FEAT_ARABIC
14119 "arabic",
14120#endif
14121#ifdef FEAT_AUTOCMD
14122 "autocmd",
14123#endif
14124#ifdef FEAT_BEVAL
14125 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000014126# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
14127 "balloon_multiline",
14128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129#endif
14130#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
14131 "builtin_terms",
14132# ifdef ALL_BUILTIN_TCAPS
14133 "all_builtin_terms",
14134# endif
14135#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020014136#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
14137 || defined(FEAT_GUI_W32) \
14138 || defined(FEAT_GUI_MOTIF))
14139 "browsefilter",
14140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141#ifdef FEAT_BYTEOFF
14142 "byte_offset",
14143#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014144#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010014145 "channel",
14146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147#ifdef FEAT_CINDENT
14148 "cindent",
14149#endif
14150#ifdef FEAT_CLIENTSERVER
14151 "clientserver",
14152#endif
14153#ifdef FEAT_CLIPBOARD
14154 "clipboard",
14155#endif
14156#ifdef FEAT_CMDL_COMPL
14157 "cmdline_compl",
14158#endif
14159#ifdef FEAT_CMDHIST
14160 "cmdline_hist",
14161#endif
14162#ifdef FEAT_COMMENTS
14163 "comments",
14164#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014165#ifdef FEAT_CONCEAL
14166 "conceal",
14167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168#ifdef FEAT_CRYPT
14169 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010014170 "crypt-blowfish",
14171 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172#endif
14173#ifdef FEAT_CSCOPE
14174 "cscope",
14175#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014176#ifdef FEAT_CURSORBIND
14177 "cursorbind",
14178#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014179#ifdef CURSOR_SHAPE
14180 "cursorshape",
14181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182#ifdef DEBUG
14183 "debug",
14184#endif
14185#ifdef FEAT_CON_DIALOG
14186 "dialog_con",
14187#endif
14188#ifdef FEAT_GUI_DIALOG
14189 "dialog_gui",
14190#endif
14191#ifdef FEAT_DIFF
14192 "diff",
14193#endif
14194#ifdef FEAT_DIGRAPHS
14195 "digraphs",
14196#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020014197#ifdef FEAT_DIRECTX
14198 "directx",
14199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200#ifdef FEAT_DND
14201 "dnd",
14202#endif
14203#ifdef FEAT_EMACS_TAGS
14204 "emacs_tags",
14205#endif
14206 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010014207 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208#ifdef FEAT_SEARCH_EXTRA
14209 "extra_search",
14210#endif
14211#ifdef FEAT_FKMAP
14212 "farsi",
14213#endif
14214#ifdef FEAT_SEARCHPATH
14215 "file_in_path",
14216#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020014217#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014218 "filterpipe",
14219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220#ifdef FEAT_FIND_ID
14221 "find_in_path",
14222#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014223#ifdef FEAT_FLOAT
14224 "float",
14225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226#ifdef FEAT_FOLDING
14227 "folding",
14228#endif
14229#ifdef FEAT_FOOTER
14230 "footer",
14231#endif
14232#if !defined(USE_SYSTEM) && defined(UNIX)
14233 "fork",
14234#endif
14235#ifdef FEAT_GETTEXT
14236 "gettext",
14237#endif
14238#ifdef FEAT_GUI
14239 "gui",
14240#endif
14241#ifdef FEAT_GUI_ATHENA
14242# ifdef FEAT_GUI_NEXTAW
14243 "gui_neXtaw",
14244# else
14245 "gui_athena",
14246# endif
14247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248#ifdef FEAT_GUI_GTK
14249 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014250# ifdef USE_GTK3
14251 "gui_gtk3",
14252# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014256#ifdef FEAT_GUI_GNOME
14257 "gui_gnome",
14258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259#ifdef FEAT_GUI_MAC
14260 "gui_mac",
14261#endif
14262#ifdef FEAT_GUI_MOTIF
14263 "gui_motif",
14264#endif
14265#ifdef FEAT_GUI_PHOTON
14266 "gui_photon",
14267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268#ifdef FEAT_GUI_W32
14269 "gui_win32",
14270#endif
14271#ifdef FEAT_HANGULIN
14272 "hangul_input",
14273#endif
14274#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14275 "iconv",
14276#endif
14277#ifdef FEAT_INS_EXPAND
14278 "insert_expand",
14279#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014280#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014281 "job",
14282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283#ifdef FEAT_JUMPLIST
14284 "jumplist",
14285#endif
14286#ifdef FEAT_KEYMAP
14287 "keymap",
14288#endif
14289#ifdef FEAT_LANGMAP
14290 "langmap",
14291#endif
14292#ifdef FEAT_LIBCALL
14293 "libcall",
14294#endif
14295#ifdef FEAT_LINEBREAK
14296 "linebreak",
14297#endif
14298#ifdef FEAT_LISP
14299 "lispindent",
14300#endif
14301#ifdef FEAT_LISTCMDS
14302 "listcmds",
14303#endif
14304#ifdef FEAT_LOCALMAP
14305 "localmap",
14306#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014307#ifdef FEAT_LUA
14308# ifndef DYNAMIC_LUA
14309 "lua",
14310# endif
14311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312#ifdef FEAT_MENU
14313 "menu",
14314#endif
14315#ifdef FEAT_SESSION
14316 "mksession",
14317#endif
14318#ifdef FEAT_MODIFY_FNAME
14319 "modify_fname",
14320#endif
14321#ifdef FEAT_MOUSE
14322 "mouse",
14323#endif
14324#ifdef FEAT_MOUSESHAPE
14325 "mouseshape",
14326#endif
14327#if defined(UNIX) || defined(VMS)
14328# ifdef FEAT_MOUSE_DEC
14329 "mouse_dec",
14330# endif
14331# ifdef FEAT_MOUSE_GPM
14332 "mouse_gpm",
14333# endif
14334# ifdef FEAT_MOUSE_JSB
14335 "mouse_jsbterm",
14336# endif
14337# ifdef FEAT_MOUSE_NET
14338 "mouse_netterm",
14339# endif
14340# ifdef FEAT_MOUSE_PTERM
14341 "mouse_pterm",
14342# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014343# ifdef FEAT_MOUSE_SGR
14344 "mouse_sgr",
14345# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014346# ifdef FEAT_SYSMOUSE
14347 "mouse_sysmouse",
14348# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014349# ifdef FEAT_MOUSE_URXVT
14350 "mouse_urxvt",
14351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352# ifdef FEAT_MOUSE_XTERM
14353 "mouse_xterm",
14354# endif
14355#endif
14356#ifdef FEAT_MBYTE
14357 "multi_byte",
14358#endif
14359#ifdef FEAT_MBYTE_IME
14360 "multi_byte_ime",
14361#endif
14362#ifdef FEAT_MULTI_LANG
14363 "multi_lang",
14364#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014365#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014366#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014367 "mzscheme",
14368#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014369#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014370#ifdef FEAT_NUM64
14371 "num64",
14372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373#ifdef FEAT_OLE
14374 "ole",
14375#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014376 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377#ifdef FEAT_PATH_EXTRA
14378 "path_extra",
14379#endif
14380#ifdef FEAT_PERL
14381#ifndef DYNAMIC_PERL
14382 "perl",
14383#endif
14384#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014385#ifdef FEAT_PERSISTENT_UNDO
14386 "persistent_undo",
14387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388#ifdef FEAT_PYTHON
14389#ifndef DYNAMIC_PYTHON
14390 "python",
14391#endif
14392#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014393#ifdef FEAT_PYTHON3
14394#ifndef DYNAMIC_PYTHON3
14395 "python3",
14396#endif
14397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398#ifdef FEAT_POSTSCRIPT
14399 "postscript",
14400#endif
14401#ifdef FEAT_PRINTER
14402 "printer",
14403#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014404#ifdef FEAT_PROFILE
14405 "profile",
14406#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014407#ifdef FEAT_RELTIME
14408 "reltime",
14409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410#ifdef FEAT_QUICKFIX
14411 "quickfix",
14412#endif
14413#ifdef FEAT_RIGHTLEFT
14414 "rightleft",
14415#endif
14416#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14417 "ruby",
14418#endif
14419#ifdef FEAT_SCROLLBIND
14420 "scrollbind",
14421#endif
14422#ifdef FEAT_CMDL_INFO
14423 "showcmd",
14424 "cmdline_info",
14425#endif
14426#ifdef FEAT_SIGNS
14427 "signs",
14428#endif
14429#ifdef FEAT_SMARTINDENT
14430 "smartindent",
14431#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014432#ifdef STARTUPTIME
14433 "startuptime",
14434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435#ifdef FEAT_STL_OPT
14436 "statusline",
14437#endif
14438#ifdef FEAT_SUN_WORKSHOP
14439 "sun_workshop",
14440#endif
14441#ifdef FEAT_NETBEANS_INTG
14442 "netbeans_intg",
14443#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014444#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014445 "spell",
14446#endif
14447#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 "syntax",
14449#endif
14450#if defined(USE_SYSTEM) || !defined(UNIX)
14451 "system",
14452#endif
14453#ifdef FEAT_TAG_BINS
14454 "tag_binary",
14455#endif
14456#ifdef FEAT_TAG_OLDSTATIC
14457 "tag_old_static",
14458#endif
14459#ifdef FEAT_TAG_ANYWHITE
14460 "tag_any_white",
14461#endif
14462#ifdef FEAT_TCL
14463# ifndef DYNAMIC_TCL
14464 "tcl",
14465# endif
14466#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014467#ifdef FEAT_TERMGUICOLORS
14468 "termguicolors",
14469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470#ifdef TERMINFO
14471 "terminfo",
14472#endif
14473#ifdef FEAT_TERMRESPONSE
14474 "termresponse",
14475#endif
14476#ifdef FEAT_TEXTOBJ
14477 "textobjects",
14478#endif
14479#ifdef HAVE_TGETENT
14480 "tgetent",
14481#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014482#ifdef FEAT_TIMERS
14483 "timers",
14484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485#ifdef FEAT_TITLE
14486 "title",
14487#endif
14488#ifdef FEAT_TOOLBAR
14489 "toolbar",
14490#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014491#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14492 "unnamedplus",
14493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494#ifdef FEAT_USR_CMDS
14495 "user-commands", /* was accidentally included in 5.4 */
14496 "user_commands",
14497#endif
14498#ifdef FEAT_VIMINFO
14499 "viminfo",
14500#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014501#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 "vertsplit",
14503#endif
14504#ifdef FEAT_VIRTUALEDIT
14505 "virtualedit",
14506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508#ifdef FEAT_VISUALEXTRA
14509 "visualextra",
14510#endif
14511#ifdef FEAT_VREPLACE
14512 "vreplace",
14513#endif
14514#ifdef FEAT_WILDIGN
14515 "wildignore",
14516#endif
14517#ifdef FEAT_WILDMENU
14518 "wildmenu",
14519#endif
14520#ifdef FEAT_WINDOWS
14521 "windows",
14522#endif
14523#ifdef FEAT_WAK
14524 "winaltkeys",
14525#endif
14526#ifdef FEAT_WRITEBACKUP
14527 "writebackup",
14528#endif
14529#ifdef FEAT_XIM
14530 "xim",
14531#endif
14532#ifdef FEAT_XFONTSET
14533 "xfontset",
14534#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014535#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014536 "xpm",
14537 "xpm_w32", /* for backward compatibility */
14538#else
14539# if defined(HAVE_XPM)
14540 "xpm",
14541# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543#ifdef USE_XSMP
14544 "xsmp",
14545#endif
14546#ifdef USE_XSMP_INTERACT
14547 "xsmp_interact",
14548#endif
14549#ifdef FEAT_XCLIPBOARD
14550 "xterm_clipboard",
14551#endif
14552#ifdef FEAT_XTERM_SAVE
14553 "xterm_save",
14554#endif
14555#if defined(UNIX) && defined(FEAT_X11)
14556 "X11",
14557#endif
14558 NULL
14559 };
14560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014561 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 for (i = 0; has_list[i] != NULL; ++i)
14563 if (STRICMP(name, has_list[i]) == 0)
14564 {
14565 n = TRUE;
14566 break;
14567 }
14568
14569 if (n == FALSE)
14570 {
14571 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014572 {
14573 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014574 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014575 && vim_isdigit(name[6])
14576 && vim_isdigit(name[8])
14577 && vim_isdigit(name[10]))
14578 {
14579 int major = atoi((char *)name + 6);
14580 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014581
14582 /* Expect "patch-9.9.01234". */
14583 n = (major < VIM_VERSION_MAJOR
14584 || (major == VIM_VERSION_MAJOR
14585 && (minor < VIM_VERSION_MINOR
14586 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014587 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014588 }
14589 else
14590 n = has_patch(atoi((char *)name + 5));
14591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 else if (STRICMP(name, "vim_starting") == 0)
14593 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014594#ifdef FEAT_MBYTE
14595 else if (STRICMP(name, "multi_byte_encoding") == 0)
14596 n = has_mbyte;
14597#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014598#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14599 else if (STRICMP(name, "balloon_multiline") == 0)
14600 n = multiline_balloon_available();
14601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602#ifdef DYNAMIC_TCL
14603 else if (STRICMP(name, "tcl") == 0)
14604 n = tcl_enabled(FALSE);
14605#endif
14606#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14607 else if (STRICMP(name, "iconv") == 0)
14608 n = iconv_enabled(FALSE);
14609#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014610#ifdef DYNAMIC_LUA
14611 else if (STRICMP(name, "lua") == 0)
14612 n = lua_enabled(FALSE);
14613#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014614#ifdef DYNAMIC_MZSCHEME
14615 else if (STRICMP(name, "mzscheme") == 0)
14616 n = mzscheme_enabled(FALSE);
14617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618#ifdef DYNAMIC_RUBY
14619 else if (STRICMP(name, "ruby") == 0)
14620 n = ruby_enabled(FALSE);
14621#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014622#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623#ifdef DYNAMIC_PYTHON
14624 else if (STRICMP(name, "python") == 0)
14625 n = python_enabled(FALSE);
14626#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014627#endif
14628#ifdef FEAT_PYTHON3
14629#ifdef DYNAMIC_PYTHON3
14630 else if (STRICMP(name, "python3") == 0)
14631 n = python3_enabled(FALSE);
14632#endif
14633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634#ifdef DYNAMIC_PERL
14635 else if (STRICMP(name, "perl") == 0)
14636 n = perl_enabled(FALSE);
14637#endif
14638#ifdef FEAT_GUI
14639 else if (STRICMP(name, "gui_running") == 0)
14640 n = (gui.in_use || gui.starting);
14641# ifdef FEAT_GUI_W32
14642 else if (STRICMP(name, "gui_win32s") == 0)
14643 n = gui_is_win32s();
14644# endif
14645# ifdef FEAT_BROWSE
14646 else if (STRICMP(name, "browse") == 0)
14647 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14648# endif
14649#endif
14650#ifdef FEAT_SYN_HL
14651 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014652 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653#endif
14654#if defined(WIN3264)
14655 else if (STRICMP(name, "win95") == 0)
14656 n = mch_windows95();
14657#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014658#ifdef FEAT_NETBEANS_INTG
14659 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014660 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662 }
14663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014664 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665}
14666
14667/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014668 * "has_key()" function
14669 */
14670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014671f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014672{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014673 if (argvars[0].v_type != VAR_DICT)
14674 {
14675 EMSG(_(e_dictreq));
14676 return;
14677 }
14678 if (argvars[0].vval.v_dict == NULL)
14679 return;
14680
14681 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014682 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014683}
14684
14685/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014686 * "haslocaldir()" function
14687 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014689f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014690{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014691 win_T *wp = NULL;
14692
14693 wp = find_tabwin(&argvars[0], &argvars[1]);
14694 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014695}
14696
14697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 * "hasmapto()" function
14699 */
14700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014701f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702{
14703 char_u *name;
14704 char_u *mode;
14705 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014706 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014708 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014709 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710 mode = (char_u *)"nvo";
14711 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014713 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014714 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014715 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717
Bram Moolenaar2c932302006-03-18 21:42:09 +000014718 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014719 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014721 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722}
14723
14724/*
14725 * "histadd()" function
14726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014728f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729{
14730#ifdef FEAT_CMDHIST
14731 int histype;
14732 char_u *str;
14733 char_u buf[NUMBUFLEN];
14734#endif
14735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014736 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737 if (check_restricted() || check_secure())
14738 return;
14739#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014740 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14741 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 if (histype >= 0)
14743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014744 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 if (*str != NUL)
14746 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014747 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014749 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 return;
14751 }
14752 }
14753#endif
14754}
14755
14756/*
14757 * "histdel()" function
14758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014760f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761{
14762#ifdef FEAT_CMDHIST
14763 int n;
14764 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014765 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014767 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14768 if (str == NULL)
14769 n = 0;
14770 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014772 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014773 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014775 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014776 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777 else
14778 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014779 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014780 get_tv_string_buf(&argvars[1], buf));
14781 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782#endif
14783}
14784
14785/*
14786 * "histget()" function
14787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014789f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790{
14791#ifdef FEAT_CMDHIST
14792 int type;
14793 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14797 if (str == NULL)
14798 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014800 {
14801 type = get_histtype(str);
14802 if (argvars[1].v_type == VAR_UNKNOWN)
14803 idx = get_history_idx(type);
14804 else
14805 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14806 /* -1 on type error */
14807 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014810 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014812 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813}
14814
14815/*
14816 * "histnr()" function
14817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014819f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820{
14821 int i;
14822
14823#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014824 char_u *history = get_tv_string_chk(&argvars[0]);
14825
14826 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 if (i >= HIST_CMD && i < HIST_COUNT)
14828 i = get_history_idx(i);
14829 else
14830#endif
14831 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014832 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833}
14834
14835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836 * "highlightID(name)" function
14837 */
14838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014839f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014841 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842}
14843
14844/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014845 * "highlight_exists()" function
14846 */
14847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014848f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014849{
14850 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14851}
14852
14853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 * "hostname()" function
14855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014857f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858{
14859 char_u hostname[256];
14860
14861 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014862 rettv->v_type = VAR_STRING;
14863 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864}
14865
14866/*
14867 * iconv() function
14868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014870f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871{
14872#ifdef FEAT_MBYTE
14873 char_u buf1[NUMBUFLEN];
14874 char_u buf2[NUMBUFLEN];
14875 char_u *from, *to, *str;
14876 vimconv_T vimconv;
14877#endif
14878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014879 rettv->v_type = VAR_STRING;
14880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881
14882#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014883 str = get_tv_string(&argvars[0]);
14884 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14885 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886 vimconv.vc_type = CONV_NONE;
14887 convert_setup(&vimconv, from, to);
14888
14889 /* If the encodings are equal, no conversion needed. */
14890 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014891 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014893 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894
14895 convert_setup(&vimconv, NULL, NULL);
14896 vim_free(from);
14897 vim_free(to);
14898#endif
14899}
14900
14901/*
14902 * "indent()" function
14903 */
14904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014905f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906{
14907 linenr_T lnum;
14908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014911 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914}
14915
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014916/*
14917 * "index()" function
14918 */
14919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014920f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014921{
Bram Moolenaar33570922005-01-25 22:26:29 +000014922 list_T *l;
14923 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014924 long idx = 0;
14925 int ic = FALSE;
14926
14927 rettv->vval.v_number = -1;
14928 if (argvars[0].v_type != VAR_LIST)
14929 {
14930 EMSG(_(e_listreq));
14931 return;
14932 }
14933 l = argvars[0].vval.v_list;
14934 if (l != NULL)
14935 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014936 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014937 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014939 int error = FALSE;
14940
Bram Moolenaar758711c2005-02-02 23:11:38 +000014941 /* Start at specified item. Use the cached index that list_find()
14942 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014943 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014944 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014945 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014946 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014947 if (error)
14948 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014949 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014950
Bram Moolenaar758711c2005-02-02 23:11:38 +000014951 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014952 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014953 {
14954 rettv->vval.v_number = idx;
14955 break;
14956 }
14957 }
14958}
14959
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960static int inputsecret_flag = 0;
14961
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014962static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014963
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014965 * This function is used by f_input() and f_inputdialog() functions. The third
14966 * argument to f_input() specifies the type of completion to use at the
14967 * prompt. The third argument to f_inputdialog() specifies the value to return
14968 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 */
14970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014971get_user_input(
14972 typval_T *argvars,
14973 typval_T *rettv,
14974 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014976 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 char_u *p = NULL;
14978 int c;
14979 char_u buf[NUMBUFLEN];
14980 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014981 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014982 int xp_type = EXPAND_NOTHING;
14983 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014985 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014986 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987
14988#ifdef NO_CONSOLE_INPUT
14989 /* While starting up, there is no place to enter text. */
14990 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992#endif
14993
14994 cmd_silent = FALSE; /* Want to see the prompt. */
14995 if (prompt != NULL)
14996 {
14997 /* Only the part of the message after the last NL is considered as
14998 * prompt for the command line */
14999 p = vim_strrchr(prompt, '\n');
15000 if (p == NULL)
15001 p = prompt;
15002 else
15003 {
15004 ++p;
15005 c = *p;
15006 *p = NUL;
15007 msg_start();
15008 msg_clr_eos();
15009 msg_puts_attr(prompt, echo_attr);
15010 msg_didout = FALSE;
15011 msg_starthere();
15012 *p = c;
15013 }
15014 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015016 if (argvars[1].v_type != VAR_UNKNOWN)
15017 {
15018 defstr = get_tv_string_buf_chk(&argvars[1], buf);
15019 if (defstr != NULL)
15020 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015022 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015023 {
15024 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000015025 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000015026 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015027
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020015028 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000015029 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015030
Bram Moolenaar4463f292005-09-25 22:20:24 +000015031 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
15032 if (xp_name == NULL)
15033 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015034
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015035 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015036
Bram Moolenaar4463f292005-09-25 22:20:24 +000015037 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
15038 &xp_arg) == FAIL)
15039 return;
15040 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015041 }
15042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015043 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015044 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015045 int save_ex_normal_busy = ex_normal_busy;
15046 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015047 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015048 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
15049 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015050 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015051 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020015052 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020015053 && argvars[1].v_type != VAR_UNKNOWN
15054 && argvars[2].v_type != VAR_UNKNOWN)
15055 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
15056 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015057
15058 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015060 /* since the user typed this, no need to wait for return */
15061 need_wait_return = FALSE;
15062 msg_didout = FALSE;
15063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 cmd_silent = cmd_silent_save;
15065}
15066
15067/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015068 * "input()" function
15069 * Also handles inputsecret() when inputsecret is set.
15070 */
15071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015072f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015073{
15074 get_user_input(argvars, rettv, FALSE);
15075}
15076
15077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078 * "inputdialog()" function
15079 */
15080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015081f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082{
15083#if defined(FEAT_GUI_TEXTDIALOG)
15084 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
15085 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
15086 {
15087 char_u *message;
15088 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015089 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015091 message = get_tv_string_chk(&argvars[0]);
15092 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000015093 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000015094 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 else
15096 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015097 if (message != NULL && defstr != NULL
15098 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010015099 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015100 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101 else
15102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015103 if (message != NULL && defstr != NULL
15104 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015105 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015106 rettv->vval.v_string = vim_strsave(
15107 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015109 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015111 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112 }
15113 else
15114#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015115 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116}
15117
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015118/*
15119 * "inputlist()" function
15120 */
15121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015122f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015123{
15124 listitem_T *li;
15125 int selected;
15126 int mouse_used;
15127
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015128#ifdef NO_CONSOLE_INPUT
15129 /* While starting up, there is no place to enter text. */
15130 if (no_console_input())
15131 return;
15132#endif
15133 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
15134 {
15135 EMSG2(_(e_listarg), "inputlist()");
15136 return;
15137 }
15138
15139 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000015140 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015141 lines_left = Rows; /* avoid more prompt */
15142 msg_scroll = TRUE;
15143 msg_clr_eos();
15144
15145 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
15146 {
15147 msg_puts(get_tv_string(&li->li_tv));
15148 msg_putchar('\n');
15149 }
15150
15151 /* Ask for choice. */
15152 selected = prompt_for_number(&mouse_used);
15153 if (mouse_used)
15154 selected -= lines_left;
15155
15156 rettv->vval.v_number = selected;
15157}
15158
15159
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
15161
15162/*
15163 * "inputrestore()" function
15164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015166f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167{
15168 if (ga_userinput.ga_len > 0)
15169 {
15170 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
15172 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015173 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015174 }
15175 else if (p_verbose > 1)
15176 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000015177 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015178 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 }
15180}
15181
15182/*
15183 * "inputsave()" function
15184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015186f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015188 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189 if (ga_grow(&ga_userinput, 1) == OK)
15190 {
15191 save_typeahead((tasave_T *)(ga_userinput.ga_data)
15192 + ga_userinput.ga_len);
15193 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015194 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 }
15196 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015197 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198}
15199
15200/*
15201 * "inputsecret()" function
15202 */
15203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015204f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205{
15206 ++cmdline_star;
15207 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015208 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 --cmdline_star;
15210 --inputsecret_flag;
15211}
15212
15213/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015214 * "insert()" function
15215 */
15216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015217f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015218{
15219 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015220 listitem_T *item;
15221 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015222 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015223
15224 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015225 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015226 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015227 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015228 {
15229 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015230 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015231 if (error)
15232 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015233
Bram Moolenaar758711c2005-02-02 23:11:38 +000015234 if (before == l->lv_len)
15235 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015236 else
15237 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015238 item = list_find(l, before);
15239 if (item == NULL)
15240 {
15241 EMSGN(_(e_listidx), before);
15242 l = NULL;
15243 }
15244 }
15245 if (l != NULL)
15246 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015247 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015248 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015249 }
15250 }
15251}
15252
15253/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015254 * "invert(expr)" function
15255 */
15256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015257f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015258{
15259 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15260}
15261
15262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 * "isdirectory()" function
15264 */
15265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015266f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015268 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269}
15270
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015271/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015272 * "islocked()" function
15273 */
15274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015275f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015276{
15277 lval_T lv;
15278 char_u *end;
15279 dictitem_T *di;
15280
15281 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015282 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15283 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015284 if (end != NULL && lv.ll_name != NULL)
15285 {
15286 if (*end != NUL)
15287 EMSG(_(e_trailing));
15288 else
15289 {
15290 if (lv.ll_tv == NULL)
15291 {
15292 if (check_changedtick(lv.ll_name))
15293 rettv->vval.v_number = 1; /* always locked */
15294 else
15295 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015296 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015297 if (di != NULL)
15298 {
15299 /* Consider a variable locked when:
15300 * 1. the variable itself is locked
15301 * 2. the value of the variable is locked.
15302 * 3. the List or Dict value is locked.
15303 */
15304 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15305 || tv_islocked(&di->di_tv));
15306 }
15307 }
15308 }
15309 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015310 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015311 else if (lv.ll_newkey != NULL)
15312 EMSG2(_(e_dictkey), lv.ll_newkey);
15313 else if (lv.ll_list != NULL)
15314 /* List item. */
15315 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15316 else
15317 /* Dictionary item. */
15318 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15319 }
15320 }
15321
15322 clear_lval(&lv);
15323}
15324
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015325#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15326/*
15327 * "isnan()" function
15328 */
15329 static void
15330f_isnan(typval_T *argvars, typval_T *rettv)
15331{
15332 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15333 && isnan(argvars[0].vval.v_float);
15334}
15335#endif
15336
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015337static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015338
15339/*
15340 * Turn a dict into a list:
15341 * "what" == 0: list of keys
15342 * "what" == 1: list of values
15343 * "what" == 2: list of items
15344 */
15345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015346dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015347{
Bram Moolenaar33570922005-01-25 22:26:29 +000015348 list_T *l2;
15349 dictitem_T *di;
15350 hashitem_T *hi;
15351 listitem_T *li;
15352 listitem_T *li2;
15353 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015354 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015355
Bram Moolenaar8c711452005-01-14 21:53:12 +000015356 if (argvars[0].v_type != VAR_DICT)
15357 {
15358 EMSG(_(e_dictreq));
15359 return;
15360 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015361 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015362 return;
15363
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015364 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015365 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015366
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015367 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015368 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015370 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015372 --todo;
15373 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015374
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015375 li = listitem_alloc();
15376 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015377 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015378 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015379
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015380 if (what == 0)
15381 {
15382 /* keys() */
15383 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015384 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015385 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15386 }
15387 else if (what == 1)
15388 {
15389 /* values() */
15390 copy_tv(&di->di_tv, &li->li_tv);
15391 }
15392 else
15393 {
15394 /* items() */
15395 l2 = list_alloc();
15396 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015397 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015398 li->li_tv.vval.v_list = l2;
15399 if (l2 == NULL)
15400 break;
15401 ++l2->lv_refcount;
15402
15403 li2 = listitem_alloc();
15404 if (li2 == NULL)
15405 break;
15406 list_append(l2, li2);
15407 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015408 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015409 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15410
15411 li2 = listitem_alloc();
15412 if (li2 == NULL)
15413 break;
15414 list_append(l2, li2);
15415 copy_tv(&di->di_tv, &li2->li_tv);
15416 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015417 }
15418 }
15419}
15420
15421/*
15422 * "items(dict)" function
15423 */
15424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015425f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015426{
15427 dict_list(argvars, rettv, 2);
15428}
15429
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015430#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015431/*
15432 * Get the job from the argument.
15433 * Returns NULL if the job is invalid.
15434 */
15435 static job_T *
15436get_job_arg(typval_T *tv)
15437{
15438 job_T *job;
15439
15440 if (tv->v_type != VAR_JOB)
15441 {
15442 EMSG2(_(e_invarg2), get_tv_string(tv));
15443 return NULL;
15444 }
15445 job = tv->vval.v_job;
15446
15447 if (job == NULL)
15448 EMSG(_("E916: not a valid job"));
15449 return job;
15450}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015451
Bram Moolenaar835dc632016-02-07 14:27:38 +010015452/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015453 * "job_getchannel()" function
15454 */
15455 static void
15456f_job_getchannel(typval_T *argvars, typval_T *rettv)
15457{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015458 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015459
Bram Moolenaar65edff82016-02-21 16:40:11 +010015460 if (job != NULL)
15461 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015462 rettv->v_type = VAR_CHANNEL;
15463 rettv->vval.v_channel = job->jv_channel;
15464 if (job->jv_channel != NULL)
15465 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015466 }
15467}
15468
15469/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015470 * "job_info()" function
15471 */
15472 static void
15473f_job_info(typval_T *argvars, typval_T *rettv)
15474{
15475 job_T *job = get_job_arg(&argvars[0]);
15476
15477 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15478 job_info(job, rettv->vval.v_dict);
15479}
15480
15481/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015482 * "job_setoptions()" function
15483 */
15484 static void
15485f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15486{
15487 job_T *job = get_job_arg(&argvars[0]);
15488 jobopt_T opt;
15489
15490 if (job == NULL)
15491 return;
15492 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015493 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15494 job_set_options(job, &opt);
15495 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015496}
15497
15498/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015499 * "job_start()" function
15500 */
15501 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015502f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015503{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015504 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015505 if (check_restricted() || check_secure())
15506 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015507 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015508}
15509
15510/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015511 * "job_status()" function
15512 */
15513 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015514f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015515{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015516 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015517
Bram Moolenaar65edff82016-02-21 16:40:11 +010015518 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015519 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015520 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015521 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015522 }
15523}
15524
15525/*
15526 * "job_stop()" function
15527 */
15528 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015529f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015530{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015531 job_T *job = get_job_arg(&argvars[0]);
15532
15533 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015534 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015535}
15536#endif
15537
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015539 * "join()" function
15540 */
15541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015542f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015543{
15544 garray_T ga;
15545 char_u *sep;
15546
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015547 if (argvars[0].v_type != VAR_LIST)
15548 {
15549 EMSG(_(e_listreq));
15550 return;
15551 }
15552 if (argvars[0].vval.v_list == NULL)
15553 return;
15554 if (argvars[1].v_type == VAR_UNKNOWN)
15555 sep = (char_u *)" ";
15556 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015557 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015558
15559 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015560
15561 if (sep != NULL)
15562 {
15563 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015564 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015565 ga_append(&ga, NUL);
15566 rettv->vval.v_string = (char_u *)ga.ga_data;
15567 }
15568 else
15569 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015570}
15571
15572/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015573 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015574 */
15575 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015576f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015577{
15578 js_read_T reader;
15579
15580 reader.js_buf = get_tv_string(&argvars[0]);
15581 reader.js_fill = NULL;
15582 reader.js_used = 0;
15583 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15584 EMSG(_(e_invarg));
15585}
15586
15587/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015588 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015589 */
15590 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015591f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015592{
15593 rettv->v_type = VAR_STRING;
15594 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15595}
15596
15597/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015598 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015599 */
15600 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015601f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015602{
15603 js_read_T reader;
15604
15605 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015606 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015607 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015608 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015609 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015610}
15611
15612/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015613 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015614 */
15615 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015616f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015617{
15618 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015619 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015620}
15621
15622/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015623 * "keys()" function
15624 */
15625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015626f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015627{
15628 dict_list(argvars, rettv, 0);
15629}
15630
15631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 * "last_buffer_nr()" function.
15633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015635f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636{
15637 int n = 0;
15638 buf_T *buf;
15639
15640 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15641 if (n < buf->b_fnum)
15642 n = buf->b_fnum;
15643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015644 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015645}
15646
15647/*
15648 * "len()" function
15649 */
15650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015651f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015652{
15653 switch (argvars[0].v_type)
15654 {
15655 case VAR_STRING:
15656 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015657 rettv->vval.v_number = (varnumber_T)STRLEN(
15658 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015659 break;
15660 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015661 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015662 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015663 case VAR_DICT:
15664 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15665 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015666 case VAR_UNKNOWN:
15667 case VAR_SPECIAL:
15668 case VAR_FLOAT:
15669 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015670 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015671 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015672 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015673 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015674 break;
15675 }
15676}
15677
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015678static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015679
15680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015681libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015682{
15683#ifdef FEAT_LIBCALL
15684 char_u *string_in;
15685 char_u **string_result;
15686 int nr_result;
15687#endif
15688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015689 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015690 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015691 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015692
15693 if (check_restricted() || check_secure())
15694 return;
15695
15696#ifdef FEAT_LIBCALL
15697 /* The first two args must be strings, otherwise its meaningless */
15698 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15699 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015700 string_in = NULL;
15701 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015702 string_in = argvars[2].vval.v_string;
15703 if (type == VAR_NUMBER)
15704 string_result = NULL;
15705 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015706 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015707 if (mch_libcall(argvars[0].vval.v_string,
15708 argvars[1].vval.v_string,
15709 string_in,
15710 argvars[2].vval.v_number,
15711 string_result,
15712 &nr_result) == OK
15713 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015714 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015715 }
15716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717}
15718
15719/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015720 * "libcall()" function
15721 */
15722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015723f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724{
15725 libcall_common(argvars, rettv, VAR_STRING);
15726}
15727
15728/*
15729 * "libcallnr()" function
15730 */
15731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015732f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015733{
15734 libcall_common(argvars, rettv, VAR_NUMBER);
15735}
15736
15737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738 * "line(string)" function
15739 */
15740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015741f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742{
15743 linenr_T lnum = 0;
15744 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015745 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015747 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 if (fp != NULL)
15749 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015750 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751}
15752
15753/*
15754 * "line2byte(lnum)" function
15755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015757f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758{
15759#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015760 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761#else
15762 linenr_T lnum;
15763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015764 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015766 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15769 if (rettv->vval.v_number >= 0)
15770 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771#endif
15772}
15773
15774/*
15775 * "lispindent(lnum)" function
15776 */
15777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015778f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779{
15780#ifdef FEAT_LISP
15781 pos_T pos;
15782 linenr_T lnum;
15783
15784 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15787 {
15788 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015789 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 curwin->w_cursor = pos;
15791 }
15792 else
15793#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015794 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795}
15796
15797/*
15798 * "localtime()" function
15799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015801f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015803 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804}
15805
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015806static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015807
15808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015809get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810{
15811 char_u *keys;
15812 char_u *which;
15813 char_u buf[NUMBUFLEN];
15814 char_u *keys_buf = NULL;
15815 char_u *rhs;
15816 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015817 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015818 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015819 mapblock_T *mp;
15820 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821
15822 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015823 rettv->v_type = VAR_STRING;
15824 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015826 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827 if (*keys == NUL)
15828 return;
15829
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015830 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015832 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015833 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015834 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015835 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015836 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015837 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015838 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840 else
15841 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015842 if (which == NULL)
15843 return;
15844
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 mode = get_map_mode(&which, 0);
15846
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015847 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015848 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015850
15851 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015853 /* Return a string. */
15854 if (rhs != NULL)
15855 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856
Bram Moolenaarbd743252010-10-20 21:23:33 +020015857 }
15858 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15859 {
15860 /* Return a dictionary. */
15861 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15862 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15863 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864
Bram Moolenaarbd743252010-10-20 21:23:33 +020015865 dict_add_nr_str(dict, "lhs", 0L, lhs);
15866 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15867 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15868 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15869 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15870 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15871 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015872 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015873 dict_add_nr_str(dict, "mode", 0L, mapmode);
15874
15875 vim_free(lhs);
15876 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 }
15878}
15879
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015880#ifdef FEAT_FLOAT
15881/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015882 * "log()" function
15883 */
15884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015885f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015886{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015887 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015888
15889 rettv->v_type = VAR_FLOAT;
15890 if (get_float_arg(argvars, &f) == OK)
15891 rettv->vval.v_float = log(f);
15892 else
15893 rettv->vval.v_float = 0.0;
15894}
15895
15896/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015897 * "log10()" function
15898 */
15899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015900f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015901{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015902 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015903
15904 rettv->v_type = VAR_FLOAT;
15905 if (get_float_arg(argvars, &f) == OK)
15906 rettv->vval.v_float = log10(f);
15907 else
15908 rettv->vval.v_float = 0.0;
15909}
15910#endif
15911
Bram Moolenaar1dced572012-04-05 16:54:08 +020015912#ifdef FEAT_LUA
15913/*
15914 * "luaeval()" function
15915 */
15916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015917f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015918{
15919 char_u *str;
15920 char_u buf[NUMBUFLEN];
15921
15922 str = get_tv_string_buf(&argvars[0], buf);
15923 do_luaeval(str, argvars + 1, rettv);
15924}
15925#endif
15926
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015928 * "map()" function
15929 */
15930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015931f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015932{
15933 filter_map(argvars, rettv, TRUE);
15934}
15935
15936/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015937 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 */
15939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015940f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015942 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943}
15944
15945/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015946 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 */
15948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015949f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015951 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952}
15953
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015954static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955
15956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015957find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015959 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015960 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015961 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962 char_u *pat;
15963 regmatch_T regmatch;
15964 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015965 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 char_u *save_cpo;
15967 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015968 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015969 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015970 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015971 list_T *l = NULL;
15972 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015973 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015974 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015975
15976 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15977 save_cpo = p_cpo;
15978 p_cpo = (char_u *)"";
15979
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015980 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015981 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015982 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015983 /* type 3: return empty list when there are no matches.
15984 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015985 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015986 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015987 if (type == 4
15988 && (list_append_string(rettv->vval.v_list,
15989 (char_u *)"", 0) == FAIL
15990 || list_append_number(rettv->vval.v_list,
15991 (varnumber_T)-1) == FAIL
15992 || list_append_number(rettv->vval.v_list,
15993 (varnumber_T)-1) == FAIL
15994 || list_append_number(rettv->vval.v_list,
15995 (varnumber_T)-1) == FAIL))
15996 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015997 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015998 rettv->vval.v_list = NULL;
15999 goto theend;
16000 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016001 }
16002 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016004 rettv->v_type = VAR_STRING;
16005 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016008 if (argvars[0].v_type == VAR_LIST)
16009 {
16010 if ((l = argvars[0].vval.v_list) == NULL)
16011 goto theend;
16012 li = l->lv_first;
16013 }
16014 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016015 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016016 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016017 len = (long)STRLEN(str);
16018 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016020 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16021 if (pat == NULL)
16022 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016023
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016024 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016026 int error = FALSE;
16027
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016028 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016029 if (error)
16030 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016031 if (l != NULL)
16032 {
16033 li = list_find(l, start);
16034 if (li == NULL)
16035 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016036 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016037 }
16038 else
16039 {
16040 if (start < 0)
16041 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016042 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016043 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016044 /* When "count" argument is there ignore matches before "start",
16045 * otherwise skip part of the string. Differs when pattern is "^"
16046 * or "\<". */
16047 if (argvars[3].v_type != VAR_UNKNOWN)
16048 startcol = start;
16049 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016050 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016051 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016052 len -= start;
16053 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016054 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016055
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016056 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016057 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016058 if (error)
16059 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060 }
16061
16062 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16063 if (regmatch.regprog != NULL)
16064 {
16065 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016066
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016067 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016068 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016069 if (l != NULL)
16070 {
16071 if (li == NULL)
16072 {
16073 match = FALSE;
16074 break;
16075 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016076 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016077 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016078 if (str == NULL)
16079 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016080 }
16081
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000016082 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016083
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016084 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016085 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016086 if (l == NULL && !match)
16087 break;
16088
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016089 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016090 if (l != NULL)
16091 {
16092 li = li->li_next;
16093 ++idx;
16094 }
16095 else
16096 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016097#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016098 startcol = (colnr_T)(regmatch.startp[0]
16099 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016100#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020016101 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016102#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016103 if (startcol > (colnr_T)len
16104 || str + startcol <= regmatch.startp[0])
16105 {
16106 match = FALSE;
16107 break;
16108 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016109 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016110 }
16111
16112 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016114 if (type == 4)
16115 {
16116 listitem_T *li1 = rettv->vval.v_list->lv_first;
16117 listitem_T *li2 = li1->li_next;
16118 listitem_T *li3 = li2->li_next;
16119 listitem_T *li4 = li3->li_next;
16120
Bram Moolenaar3c809342016-06-01 22:34:48 +020016121 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016122 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
16123 (int)(regmatch.endp[0] - regmatch.startp[0]));
16124 li3->li_tv.vval.v_number =
16125 (varnumber_T)(regmatch.startp[0] - expr);
16126 li4->li_tv.vval.v_number =
16127 (varnumber_T)(regmatch.endp[0] - expr);
16128 if (l != NULL)
16129 li2->li_tv.vval.v_number = (varnumber_T)idx;
16130 }
16131 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016132 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016133 int i;
16134
16135 /* return list with matched string and submatches */
16136 for (i = 0; i < NSUBEXP; ++i)
16137 {
16138 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016139 {
16140 if (list_append_string(rettv->vval.v_list,
16141 (char_u *)"", 0) == FAIL)
16142 break;
16143 }
16144 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016145 regmatch.startp[i],
16146 (int)(regmatch.endp[i] - regmatch.startp[i]))
16147 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016148 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016149 }
16150 }
16151 else if (type == 2)
16152 {
16153 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016154 if (l != NULL)
16155 copy_tv(&li->li_tv, rettv);
16156 else
16157 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016159 }
16160 else if (l != NULL)
16161 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 else
16163 {
16164 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016165 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 (varnumber_T)(regmatch.startp[0] - str);
16167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016168 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016170 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 }
16172 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016173 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 }
16175
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016176 if (type == 4 && l == NULL)
16177 /* matchstrpos() without a list: drop the second item. */
16178 listitem_remove(rettv->vval.v_list,
16179 rettv->vval.v_list->lv_first->li_next);
16180
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016182 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 p_cpo = save_cpo;
16184}
16185
16186/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187 * "match()" function
16188 */
16189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016190f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016191{
16192 find_some_match(argvars, rettv, 1);
16193}
16194
16195/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016196 * "matchadd()" function
16197 */
16198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016199f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016200{
16201#ifdef FEAT_SEARCH_EXTRA
16202 char_u buf[NUMBUFLEN];
16203 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16204 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16205 int prio = 10; /* default priority */
16206 int id = -1;
16207 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016208 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016209
16210 rettv->vval.v_number = -1;
16211
16212 if (grp == NULL || pat == NULL)
16213 return;
16214 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016215 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016216 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016217 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016218 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016219 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016220 if (argvars[4].v_type != VAR_UNKNOWN)
16221 {
16222 if (argvars[4].v_type != VAR_DICT)
16223 {
16224 EMSG(_(e_dictreq));
16225 return;
16226 }
16227 if (dict_find(argvars[4].vval.v_dict,
16228 (char_u *)"conceal", -1) != NULL)
16229 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16230 (char_u *)"conceal", FALSE);
16231 }
16232 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016233 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016234 if (error == TRUE)
16235 return;
16236 if (id >= 1 && id <= 3)
16237 {
16238 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16239 return;
16240 }
16241
Bram Moolenaar6561d522015-07-21 15:48:27 +020016242 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16243 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016244#endif
16245}
16246
16247/*
16248 * "matchaddpos()" function
16249 */
16250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016251f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016252{
16253#ifdef FEAT_SEARCH_EXTRA
16254 char_u buf[NUMBUFLEN];
16255 char_u *group;
16256 int prio = 10;
16257 int id = -1;
16258 int error = FALSE;
16259 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016260 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016261
16262 rettv->vval.v_number = -1;
16263
16264 group = get_tv_string_buf_chk(&argvars[0], buf);
16265 if (group == NULL)
16266 return;
16267
16268 if (argvars[1].v_type != VAR_LIST)
16269 {
16270 EMSG2(_(e_listarg), "matchaddpos()");
16271 return;
16272 }
16273 l = argvars[1].vval.v_list;
16274 if (l == NULL)
16275 return;
16276
16277 if (argvars[2].v_type != VAR_UNKNOWN)
16278 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016279 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016280 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016281 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016282 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016283 if (argvars[4].v_type != VAR_UNKNOWN)
16284 {
16285 if (argvars[4].v_type != VAR_DICT)
16286 {
16287 EMSG(_(e_dictreq));
16288 return;
16289 }
16290 if (dict_find(argvars[4].vval.v_dict,
16291 (char_u *)"conceal", -1) != NULL)
16292 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16293 (char_u *)"conceal", FALSE);
16294 }
16295 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016296 }
16297 if (error == TRUE)
16298 return;
16299
16300 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16301 if (id == 1 || id == 2)
16302 {
16303 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16304 return;
16305 }
16306
Bram Moolenaar6561d522015-07-21 15:48:27 +020016307 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16308 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016309#endif
16310}
16311
16312/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016313 * "matcharg()" function
16314 */
16315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016316f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016317{
16318 if (rettv_list_alloc(rettv) == OK)
16319 {
16320#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016321 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016322 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016323
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016324 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016325 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016326 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16327 {
16328 list_append_string(rettv->vval.v_list,
16329 syn_id2name(m->hlg_id), -1);
16330 list_append_string(rettv->vval.v_list, m->pattern, -1);
16331 }
16332 else
16333 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016334 list_append_string(rettv->vval.v_list, NULL, -1);
16335 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016336 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016337 }
16338#endif
16339 }
16340}
16341
16342/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016343 * "matchdelete()" function
16344 */
16345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016346f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016347{
16348#ifdef FEAT_SEARCH_EXTRA
16349 rettv->vval.v_number = match_delete(curwin,
16350 (int)get_tv_number(&argvars[0]), TRUE);
16351#endif
16352}
16353
16354/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016355 * "matchend()" function
16356 */
16357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016358f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016359{
16360 find_some_match(argvars, rettv, 0);
16361}
16362
16363/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016364 * "matchlist()" function
16365 */
16366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016367f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016368{
16369 find_some_match(argvars, rettv, 3);
16370}
16371
16372/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016373 * "matchstr()" function
16374 */
16375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016376f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016377{
16378 find_some_match(argvars, rettv, 2);
16379}
16380
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016381/*
16382 * "matchstrpos()" function
16383 */
16384 static void
16385f_matchstrpos(typval_T *argvars, typval_T *rettv)
16386{
16387 find_some_match(argvars, rettv, 4);
16388}
16389
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016390static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016391
16392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016393max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016394{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016395 varnumber_T n = 0;
16396 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016397 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016398
16399 if (argvars[0].v_type == VAR_LIST)
16400 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016401 list_T *l;
16402 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016403
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016404 l = argvars[0].vval.v_list;
16405 if (l != NULL)
16406 {
16407 li = l->lv_first;
16408 if (li != NULL)
16409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016410 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016411 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016412 {
16413 li = li->li_next;
16414 if (li == NULL)
16415 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016416 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016417 if (domax ? i > n : i < n)
16418 n = i;
16419 }
16420 }
16421 }
16422 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016423 else if (argvars[0].v_type == VAR_DICT)
16424 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016425 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016426 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016427 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016428 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016429
16430 d = argvars[0].vval.v_dict;
16431 if (d != NULL)
16432 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016433 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016434 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016436 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016437 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016438 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016439 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016440 if (first)
16441 {
16442 n = i;
16443 first = FALSE;
16444 }
16445 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016446 n = i;
16447 }
16448 }
16449 }
16450 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016451 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016452 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016453 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016454}
16455
16456/*
16457 * "max()" function
16458 */
16459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016460f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016461{
16462 max_min(argvars, rettv, TRUE);
16463}
16464
16465/*
16466 * "min()" function
16467 */
16468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016469f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016470{
16471 max_min(argvars, rettv, FALSE);
16472}
16473
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016474static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016475
16476/*
16477 * Create the directory in which "dir" is located, and higher levels when
16478 * needed.
16479 */
16480 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016481mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016482{
16483 char_u *p;
16484 char_u *updir;
16485 int r = FAIL;
16486
16487 /* Get end of directory name in "dir".
16488 * We're done when it's "/" or "c:/". */
16489 p = gettail_sep(dir);
16490 if (p <= get_past_head(dir))
16491 return OK;
16492
16493 /* If the directory exists we're done. Otherwise: create it.*/
16494 updir = vim_strnsave(dir, (int)(p - dir));
16495 if (updir == NULL)
16496 return FAIL;
16497 if (mch_isdir(updir))
16498 r = OK;
16499 else if (mkdir_recurse(updir, prot) == OK)
16500 r = vim_mkdir_emsg(updir, prot);
16501 vim_free(updir);
16502 return r;
16503}
16504
16505#ifdef vim_mkdir
16506/*
16507 * "mkdir()" function
16508 */
16509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016510f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016511{
16512 char_u *dir;
16513 char_u buf[NUMBUFLEN];
16514 int prot = 0755;
16515
16516 rettv->vval.v_number = FAIL;
16517 if (check_restricted() || check_secure())
16518 return;
16519
16520 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016521 if (*dir == NUL)
16522 rettv->vval.v_number = FAIL;
16523 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016524 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016525 if (*gettail(dir) == NUL)
16526 /* remove trailing slashes */
16527 *gettail_sep(dir) = NUL;
16528
16529 if (argvars[1].v_type != VAR_UNKNOWN)
16530 {
16531 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016532 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016533 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16534 mkdir_recurse(dir, prot);
16535 }
16536 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016537 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016538}
16539#endif
16540
Bram Moolenaar0d660222005-01-07 21:51:51 +000016541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 * "mode()" function
16543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016545f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016547 char_u buf[3];
16548
16549 buf[1] = NUL;
16550 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551
Bram Moolenaare381d3d2016-07-07 14:50:41 +020016552 if (time_for_testing == 93784)
16553 {
16554 /* Testing the two-character code. */
16555 buf[0] = 'x';
16556 buf[1] = '!';
16557 }
16558 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016559 {
16560 if (VIsual_select)
16561 buf[0] = VIsual_mode + 's' - 'v';
16562 else
16563 buf[0] = VIsual_mode;
16564 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016565 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016566 || State == CONFIRM)
16567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016569 if (State == ASKMORE)
16570 buf[1] = 'm';
16571 else if (State == CONFIRM)
16572 buf[1] = '?';
16573 }
16574 else if (State == EXTERNCMD)
16575 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576 else if (State & INSERT)
16577 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016578#ifdef FEAT_VREPLACE
16579 if (State & VREPLACE_FLAG)
16580 {
16581 buf[0] = 'R';
16582 buf[1] = 'v';
16583 }
16584 else
16585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 if (State & REPLACE_FLAG)
16587 buf[0] = 'R';
16588 else
16589 buf[0] = 'i';
16590 }
16591 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016594 if (exmode_active)
16595 buf[1] = 'v';
16596 }
16597 else if (exmode_active)
16598 {
16599 buf[0] = 'c';
16600 buf[1] = 'e';
16601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016605 if (finish_op)
16606 buf[1] = 'o';
16607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016609 /* Clear out the minor mode when the argument is not a non-zero number or
16610 * non-empty string. */
16611 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016612 buf[1] = NUL;
16613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016614 rettv->vval.v_string = vim_strsave(buf);
16615 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616}
16617
Bram Moolenaar429fa852013-04-15 12:27:36 +020016618#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016619/*
16620 * "mzeval()" function
16621 */
16622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016623f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016624{
16625 char_u *str;
16626 char_u buf[NUMBUFLEN];
16627
16628 str = get_tv_string_buf(&argvars[0], buf);
16629 do_mzeval(str, rettv);
16630}
Bram Moolenaar75676462013-01-30 14:55:42 +010016631
16632 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016633mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016634{
16635 typval_T argvars[3];
16636
16637 argvars[0].v_type = VAR_STRING;
16638 argvars[0].vval.v_string = name;
16639 copy_tv(args, &argvars[1]);
16640 argvars[2].v_type = VAR_UNKNOWN;
16641 f_call(argvars, rettv);
16642 clear_tv(&argvars[1]);
16643}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016644#endif
16645
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016647 * "nextnonblank()" function
16648 */
16649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016650f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016651{
16652 linenr_T lnum;
16653
16654 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016656 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016657 {
16658 lnum = 0;
16659 break;
16660 }
16661 if (*skipwhite(ml_get(lnum)) != NUL)
16662 break;
16663 }
16664 rettv->vval.v_number = lnum;
16665}
16666
16667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 * "nr2char()" function
16669 */
16670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016671f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672{
16673 char_u buf[NUMBUFLEN];
16674
16675#ifdef FEAT_MBYTE
16676 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016677 {
16678 int utf8 = 0;
16679
16680 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016681 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016682 if (utf8)
16683 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16684 else
16685 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 else
16688#endif
16689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016690 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691 buf[1] = NUL;
16692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016693 rettv->v_type = VAR_STRING;
16694 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016695}
16696
16697/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016698 * "or(expr, expr)" function
16699 */
16700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016701f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016702{
16703 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16704 | get_tv_number_chk(&argvars[1], NULL);
16705}
16706
16707/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016708 * "pathshorten()" function
16709 */
16710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016711f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016712{
16713 char_u *p;
16714
16715 rettv->v_type = VAR_STRING;
16716 p = get_tv_string_chk(&argvars[0]);
16717 if (p == NULL)
16718 rettv->vval.v_string = NULL;
16719 else
16720 {
16721 p = vim_strsave(p);
16722 rettv->vval.v_string = p;
16723 if (p != NULL)
16724 shorten_dir(p);
16725 }
16726}
16727
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016728#ifdef FEAT_PERL
16729/*
16730 * "perleval()" function
16731 */
16732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016733f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016734{
16735 char_u *str;
16736 char_u buf[NUMBUFLEN];
16737
16738 str = get_tv_string_buf(&argvars[0], buf);
16739 do_perleval(str, rettv);
16740}
16741#endif
16742
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016743#ifdef FEAT_FLOAT
16744/*
16745 * "pow()" function
16746 */
16747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016748f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016749{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016750 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016751
16752 rettv->v_type = VAR_FLOAT;
16753 if (get_float_arg(argvars, &fx) == OK
16754 && get_float_arg(&argvars[1], &fy) == OK)
16755 rettv->vval.v_float = pow(fx, fy);
16756 else
16757 rettv->vval.v_float = 0.0;
16758}
16759#endif
16760
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016761/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016762 * "prevnonblank()" function
16763 */
16764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016765f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016766{
16767 linenr_T lnum;
16768
16769 lnum = get_tv_lnum(argvars);
16770 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16771 lnum = 0;
16772 else
16773 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16774 --lnum;
16775 rettv->vval.v_number = lnum;
16776}
16777
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016778/* This dummy va_list is here because:
16779 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16780 * - locally in the function results in a "used before set" warning
16781 * - using va_start() to initialize it gives "function with fixed args" error */
16782static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016783
Bram Moolenaar8c711452005-01-14 21:53:12 +000016784/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016785 * "printf()" function
16786 */
16787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016788f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016789{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016790 char_u buf[NUMBUFLEN];
16791 int len;
16792 char_u *s;
16793 int saved_did_emsg = did_emsg;
16794 char *fmt;
16795
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016796 rettv->v_type = VAR_STRING;
16797 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016798
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016799 /* Get the required length, allocate the buffer and do it for real. */
16800 did_emsg = FALSE;
16801 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16802 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16803 if (!did_emsg)
16804 {
16805 s = alloc(len + 1);
16806 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016807 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016808 rettv->vval.v_string = s;
16809 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016810 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016811 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016812 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016813}
16814
16815/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016816 * "pumvisible()" function
16817 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016819f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016820{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016821#ifdef FEAT_INS_EXPAND
16822 if (pum_visible())
16823 rettv->vval.v_number = 1;
16824#endif
16825}
16826
Bram Moolenaardb913952012-06-29 12:54:53 +020016827#ifdef FEAT_PYTHON3
16828/*
16829 * "py3eval()" function
16830 */
16831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016832f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016833{
16834 char_u *str;
16835 char_u buf[NUMBUFLEN];
16836
16837 str = get_tv_string_buf(&argvars[0], buf);
16838 do_py3eval(str, rettv);
16839}
16840#endif
16841
16842#ifdef FEAT_PYTHON
16843/*
16844 * "pyeval()" function
16845 */
16846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016847f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016848{
16849 char_u *str;
16850 char_u buf[NUMBUFLEN];
16851
16852 str = get_tv_string_buf(&argvars[0], buf);
16853 do_pyeval(str, rettv);
16854}
16855#endif
16856
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016857/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016858 * "range()" function
16859 */
16860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016861f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016862{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016863 varnumber_T start;
16864 varnumber_T end;
16865 varnumber_T stride = 1;
16866 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016867 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016869 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016870 if (argvars[1].v_type == VAR_UNKNOWN)
16871 {
16872 end = start - 1;
16873 start = 0;
16874 }
16875 else
16876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016877 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016878 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016879 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016880 }
16881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016882 if (error)
16883 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016884 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016885 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016886 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016887 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016888 else
16889 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016890 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016891 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016892 if (list_append_number(rettv->vval.v_list,
16893 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016894 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016895 }
16896}
16897
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016898/*
16899 * "readfile()" function
16900 */
16901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016902f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016903{
16904 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016905 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016906 char_u *fname;
16907 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016908 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16909 int io_size = sizeof(buf);
16910 int readlen; /* size of last fread() */
16911 char_u *prev = NULL; /* previously read bytes, if any */
16912 long prevlen = 0; /* length of data in prev */
16913 long prevsize = 0; /* size of prev buffer */
16914 long maxline = MAXLNUM;
16915 long cnt = 0;
16916 char_u *p; /* position in buf */
16917 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016918
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016919 if (argvars[1].v_type != VAR_UNKNOWN)
16920 {
16921 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16922 binary = TRUE;
16923 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016924 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016925 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016926
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016927 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016928 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016929
16930 /* Always open the file in binary mode, library functions have a mind of
16931 * their own about CR-LF conversion. */
16932 fname = get_tv_string(&argvars[0]);
16933 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16934 {
16935 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16936 return;
16937 }
16938
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016939 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016940 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016941 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016942
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016943 /* This for loop processes what was read, but is also entered at end
16944 * of file so that either:
16945 * - an incomplete line gets written
16946 * - a "binary" file gets an empty line at the end if it ends in a
16947 * newline. */
16948 for (p = buf, start = buf;
16949 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16950 ++p)
16951 {
16952 if (*p == '\n' || readlen <= 0)
16953 {
16954 listitem_T *li;
16955 char_u *s = NULL;
16956 long_u len = p - start;
16957
16958 /* Finished a line. Remove CRs before NL. */
16959 if (readlen > 0 && !binary)
16960 {
16961 while (len > 0 && start[len - 1] == '\r')
16962 --len;
16963 /* removal may cross back to the "prev" string */
16964 if (len == 0)
16965 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16966 --prevlen;
16967 }
16968 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016969 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016970 else
16971 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016972 /* Change "prev" buffer to be the right size. This way
16973 * the bytes are only copied once, and very long lines are
16974 * allocated only once. */
16975 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016976 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016977 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016978 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016979 prev = NULL; /* the list will own the string */
16980 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016981 }
16982 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016983 if (s == NULL)
16984 {
16985 do_outofmem_msg((long_u) prevlen + len + 1);
16986 failed = TRUE;
16987 break;
16988 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016989
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016990 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016991 {
16992 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016993 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016994 break;
16995 }
16996 li->li_tv.v_type = VAR_STRING;
16997 li->li_tv.v_lock = 0;
16998 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016999 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017000
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017001 start = p + 1; /* step over newline */
17002 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017003 break;
17004 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017005 else if (*p == NUL)
17006 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020017007#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017008 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
17009 * when finding the BF and check the previous two bytes. */
17010 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020017011 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017012 /* Find the two bytes before the 0xbf. If p is at buf, or buf
17013 * + 1, these may be in the "prev" string. */
17014 char_u back1 = p >= buf + 1 ? p[-1]
17015 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
17016 char_u back2 = p >= buf + 2 ? p[-2]
17017 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
17018 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017019
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017020 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017021 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017022 char_u *dest = p - 2;
17023
17024 /* Usually a BOM is at the beginning of a file, and so at
17025 * the beginning of a line; then we can just step over it.
17026 */
17027 if (start == dest)
17028 start = p + 1;
17029 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020017030 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017031 /* have to shuffle buf to close gap */
17032 int adjust_prevlen = 0;
17033
17034 if (dest < buf)
17035 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017036 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017037 dest = buf;
17038 }
17039 if (readlen > p - buf + 1)
17040 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
17041 readlen -= 3 - adjust_prevlen;
17042 prevlen -= adjust_prevlen;
17043 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020017044 }
17045 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017046 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017047#endif
17048 } /* for */
17049
17050 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
17051 break;
17052 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017053 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017054 /* There's part of a line in buf, store it in "prev". */
17055 if (p - start + prevlen >= prevsize)
17056 {
17057 /* need bigger "prev" buffer */
17058 char_u *newprev;
17059
17060 /* A common use case is ordinary text files and "prev" gets a
17061 * fragment of a line, so the first allocation is made
17062 * small, to avoid repeatedly 'allocing' large and
17063 * 'reallocing' small. */
17064 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017065 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017066 else
17067 {
17068 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017069 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017070 prevsize = grow50pc > growmin ? grow50pc : growmin;
17071 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020017072 newprev = prev == NULL ? alloc(prevsize)
17073 : vim_realloc(prev, prevsize);
17074 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017075 {
17076 do_outofmem_msg((long_u)prevsize);
17077 failed = TRUE;
17078 break;
17079 }
17080 prev = newprev;
17081 }
17082 /* Add the line part to end of "prev". */
17083 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017084 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017085 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017086 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017087
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017088 /*
17089 * For a negative line count use only the lines at the end of the file,
17090 * free the rest.
17091 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017092 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017093 while (cnt > -maxline)
17094 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017095 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017096 --cnt;
17097 }
17098
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017099 if (failed)
17100 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020017101 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017102 /* readfile doc says an empty list is returned on error */
17103 rettv->vval.v_list = list_alloc();
17104 }
17105
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017106 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017107 fclose(fd);
17108}
17109
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017110#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017111static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017112
17113/*
17114 * Convert a List to proftime_T.
17115 * Return FAIL when there is something wrong.
17116 */
17117 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017118list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017119{
17120 long n1, n2;
17121 int error = FALSE;
17122
17123 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
17124 || arg->vval.v_list->lv_len != 2)
17125 return FAIL;
17126 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
17127 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
17128# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017129 tm->HighPart = n1;
17130 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017131# else
17132 tm->tv_sec = n1;
17133 tm->tv_usec = n2;
17134# endif
17135 return error ? FAIL : OK;
17136}
17137#endif /* FEAT_RELTIME */
17138
17139/*
17140 * "reltime()" function
17141 */
17142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017143f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017144{
17145#ifdef FEAT_RELTIME
17146 proftime_T res;
17147 proftime_T start;
17148
17149 if (argvars[0].v_type == VAR_UNKNOWN)
17150 {
17151 /* No arguments: get current time. */
17152 profile_start(&res);
17153 }
17154 else if (argvars[1].v_type == VAR_UNKNOWN)
17155 {
17156 if (list2proftime(&argvars[0], &res) == FAIL)
17157 return;
17158 profile_end(&res);
17159 }
17160 else
17161 {
17162 /* Two arguments: compute the difference. */
17163 if (list2proftime(&argvars[0], &start) == FAIL
17164 || list2proftime(&argvars[1], &res) == FAIL)
17165 return;
17166 profile_sub(&res, &start);
17167 }
17168
17169 if (rettv_list_alloc(rettv) == OK)
17170 {
17171 long n1, n2;
17172
17173# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017174 n1 = res.HighPart;
17175 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017176# else
17177 n1 = res.tv_sec;
17178 n2 = res.tv_usec;
17179# endif
17180 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17181 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17182 }
17183#endif
17184}
17185
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017186#ifdef FEAT_FLOAT
17187/*
17188 * "reltimefloat()" function
17189 */
17190 static void
17191f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17192{
17193# ifdef FEAT_RELTIME
17194 proftime_T tm;
17195# endif
17196
17197 rettv->v_type = VAR_FLOAT;
17198 rettv->vval.v_float = 0;
17199# ifdef FEAT_RELTIME
17200 if (list2proftime(&argvars[0], &tm) == OK)
17201 rettv->vval.v_float = profile_float(&tm);
17202# endif
17203}
17204#endif
17205
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017206/*
17207 * "reltimestr()" function
17208 */
17209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017210f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017211{
17212#ifdef FEAT_RELTIME
17213 proftime_T tm;
17214#endif
17215
17216 rettv->v_type = VAR_STRING;
17217 rettv->vval.v_string = NULL;
17218#ifdef FEAT_RELTIME
17219 if (list2proftime(&argvars[0], &tm) == OK)
17220 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17221#endif
17222}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017223
Bram Moolenaar0d660222005-01-07 21:51:51 +000017224#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017225static void make_connection(void);
17226static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227
17228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017229make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017230{
17231 if (X_DISPLAY == NULL
17232# ifdef FEAT_GUI
17233 && !gui.in_use
17234# endif
17235 )
17236 {
17237 x_force_connect = TRUE;
17238 setup_term_clip();
17239 x_force_connect = FALSE;
17240 }
17241}
17242
17243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017244check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017245{
17246 make_connection();
17247 if (X_DISPLAY == NULL)
17248 {
17249 EMSG(_("E240: No connection to Vim server"));
17250 return FAIL;
17251 }
17252 return OK;
17253}
17254#endif
17255
17256#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017258remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017259{
17260 char_u *server_name;
17261 char_u *keys;
17262 char_u *r = NULL;
17263 char_u buf[NUMBUFLEN];
17264# ifdef WIN32
17265 HWND w;
17266# else
17267 Window w;
17268# endif
17269
17270 if (check_restricted() || check_secure())
17271 return;
17272
17273# ifdef FEAT_X11
17274 if (check_connection() == FAIL)
17275 return;
17276# endif
17277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017278 server_name = get_tv_string_chk(&argvars[0]);
17279 if (server_name == NULL)
17280 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017281 keys = get_tv_string_buf(&argvars[1], buf);
17282# ifdef WIN32
17283 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17284# else
17285 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17286 < 0)
17287# endif
17288 {
17289 if (r != NULL)
17290 EMSG(r); /* sending worked but evaluation failed */
17291 else
17292 EMSG2(_("E241: Unable to send to %s"), server_name);
17293 return;
17294 }
17295
17296 rettv->vval.v_string = r;
17297
17298 if (argvars[2].v_type != VAR_UNKNOWN)
17299 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017300 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017301 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017302 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017303
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017304 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 v.di_tv.v_type = VAR_STRING;
17306 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017307 idvar = get_tv_string_chk(&argvars[2]);
17308 if (idvar != NULL)
17309 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017310 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017311 }
17312}
17313#endif
17314
17315/*
17316 * "remote_expr()" function
17317 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017319f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017320{
17321 rettv->v_type = VAR_STRING;
17322 rettv->vval.v_string = NULL;
17323#ifdef FEAT_CLIENTSERVER
17324 remote_common(argvars, rettv, TRUE);
17325#endif
17326}
17327
17328/*
17329 * "remote_foreground()" function
17330 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017332f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017333{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017334#ifdef FEAT_CLIENTSERVER
17335# ifdef WIN32
17336 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017337 {
17338 char_u *server_name = get_tv_string_chk(&argvars[0]);
17339
17340 if (server_name != NULL)
17341 serverForeground(server_name);
17342 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017343# else
17344 /* Send a foreground() expression to the server. */
17345 argvars[1].v_type = VAR_STRING;
17346 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17347 argvars[2].v_type = VAR_UNKNOWN;
17348 remote_common(argvars, rettv, TRUE);
17349 vim_free(argvars[1].vval.v_string);
17350# endif
17351#endif
17352}
17353
Bram Moolenaar0d660222005-01-07 21:51:51 +000017354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017355f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017356{
17357#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017358 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017359 char_u *s = NULL;
17360# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017361 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017362# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017363 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017364
17365 if (check_restricted() || check_secure())
17366 {
17367 rettv->vval.v_number = -1;
17368 return;
17369 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017370 serverid = get_tv_string_chk(&argvars[0]);
17371 if (serverid == NULL)
17372 {
17373 rettv->vval.v_number = -1;
17374 return; /* type error; errmsg already given */
17375 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017376# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017377 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017378 if (n == 0)
17379 rettv->vval.v_number = -1;
17380 else
17381 {
17382 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17383 rettv->vval.v_number = (s != NULL);
17384 }
17385# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017386 if (check_connection() == FAIL)
17387 return;
17388
17389 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017390 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017391# endif
17392
17393 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017395 char_u *retvar;
17396
Bram Moolenaar33570922005-01-25 22:26:29 +000017397 v.di_tv.v_type = VAR_STRING;
17398 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017399 retvar = get_tv_string_chk(&argvars[1]);
17400 if (retvar != NULL)
17401 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017402 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017403 }
17404#else
17405 rettv->vval.v_number = -1;
17406#endif
17407}
17408
Bram Moolenaar0d660222005-01-07 21:51:51 +000017409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017410f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017411{
17412 char_u *r = NULL;
17413
17414#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017415 char_u *serverid = get_tv_string_chk(&argvars[0]);
17416
17417 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017418 {
17419# ifdef WIN32
17420 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017421 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017422
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017423 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017424 if (n != 0)
17425 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17426 if (r == NULL)
17427# else
17428 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017429 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017430# endif
17431 EMSG(_("E277: Unable to read a server reply"));
17432 }
17433#endif
17434 rettv->v_type = VAR_STRING;
17435 rettv->vval.v_string = r;
17436}
17437
17438/*
17439 * "remote_send()" function
17440 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017442f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017443{
17444 rettv->v_type = VAR_STRING;
17445 rettv->vval.v_string = NULL;
17446#ifdef FEAT_CLIENTSERVER
17447 remote_common(argvars, rettv, FALSE);
17448#endif
17449}
17450
17451/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017452 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017453 */
17454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017455f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017456{
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 list_T *l;
17458 listitem_T *item, *item2;
17459 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017460 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017461 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017462 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017463 dict_T *d;
17464 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017465 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017466
Bram Moolenaar8c711452005-01-14 21:53:12 +000017467 if (argvars[0].v_type == VAR_DICT)
17468 {
17469 if (argvars[2].v_type != VAR_UNKNOWN)
17470 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017471 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017472 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017474 key = get_tv_string_chk(&argvars[1]);
17475 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017477 di = dict_find(d, key, -1);
17478 if (di == NULL)
17479 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017480 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17481 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017482 {
17483 *rettv = di->di_tv;
17484 init_tv(&di->di_tv);
17485 dictitem_remove(d, di);
17486 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017487 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017488 }
17489 }
17490 else if (argvars[0].v_type != VAR_LIST)
17491 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017492 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017493 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017495 int error = FALSE;
17496
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017497 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017498 if (error)
17499 ; /* type error: do nothing, errmsg already given */
17500 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017501 EMSGN(_(e_listidx), idx);
17502 else
17503 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017504 if (argvars[2].v_type == VAR_UNKNOWN)
17505 {
17506 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017507 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017508 *rettv = item->li_tv;
17509 vim_free(item);
17510 }
17511 else
17512 {
17513 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017514 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017515 if (error)
17516 ; /* type error: do nothing */
17517 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017518 EMSGN(_(e_listidx), end);
17519 else
17520 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017521 int cnt = 0;
17522
17523 for (li = item; li != NULL; li = li->li_next)
17524 {
17525 ++cnt;
17526 if (li == item2)
17527 break;
17528 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017529 if (li == NULL) /* didn't find "item2" after "item" */
17530 EMSG(_(e_invrange));
17531 else
17532 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017533 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017534 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017535 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017536 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017537 l->lv_first = item;
17538 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017539 item->li_prev = NULL;
17540 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017541 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017542 }
17543 }
17544 }
17545 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017546 }
17547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548}
17549
17550/*
17551 * "rename({from}, {to})" function
17552 */
17553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017554f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555{
17556 char_u buf[NUMBUFLEN];
17557
17558 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017559 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017561 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17562 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563}
17564
17565/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017566 * "repeat()" function
17567 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017569f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017570{
17571 char_u *p;
17572 int n;
17573 int slen;
17574 int len;
17575 char_u *r;
17576 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017577
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017578 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017579 if (argvars[0].v_type == VAR_LIST)
17580 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017581 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017582 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017583 if (list_extend(rettv->vval.v_list,
17584 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017585 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017586 }
17587 else
17588 {
17589 p = get_tv_string(&argvars[0]);
17590 rettv->v_type = VAR_STRING;
17591 rettv->vval.v_string = NULL;
17592
17593 slen = (int)STRLEN(p);
17594 len = slen * n;
17595 if (len <= 0)
17596 return;
17597
17598 r = alloc(len + 1);
17599 if (r != NULL)
17600 {
17601 for (i = 0; i < n; i++)
17602 mch_memmove(r + i * slen, p, (size_t)slen);
17603 r[len] = NUL;
17604 }
17605
17606 rettv->vval.v_string = r;
17607 }
17608}
17609
17610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611 * "resolve()" function
17612 */
17613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017614f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615{
17616 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017617#ifdef HAVE_READLINK
17618 char_u *buf = NULL;
17619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017621 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622#ifdef FEAT_SHORTCUT
17623 {
17624 char_u *v = NULL;
17625
17626 v = mch_resolve_shortcut(p);
17627 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017628 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017630 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 }
17632#else
17633# ifdef HAVE_READLINK
17634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 char_u *cpy;
17636 int len;
17637 char_u *remain = NULL;
17638 char_u *q;
17639 int is_relative_to_current = FALSE;
17640 int has_trailing_pathsep = FALSE;
17641 int limit = 100;
17642
17643 p = vim_strsave(p);
17644
17645 if (p[0] == '.' && (vim_ispathsep(p[1])
17646 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17647 is_relative_to_current = TRUE;
17648
17649 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017650 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017653 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655
17656 q = getnextcomp(p);
17657 if (*q != NUL)
17658 {
17659 /* Separate the first path component in "p", and keep the
17660 * remainder (beginning with the path separator). */
17661 remain = vim_strsave(q - 1);
17662 q[-1] = NUL;
17663 }
17664
Bram Moolenaard9462e32011-04-11 21:35:11 +020017665 buf = alloc(MAXPATHL + 1);
17666 if (buf == NULL)
17667 goto fail;
17668
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 for (;;)
17670 {
17671 for (;;)
17672 {
17673 len = readlink((char *)p, (char *)buf, MAXPATHL);
17674 if (len <= 0)
17675 break;
17676 buf[len] = NUL;
17677
17678 if (limit-- == 0)
17679 {
17680 vim_free(p);
17681 vim_free(remain);
17682 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017683 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684 goto fail;
17685 }
17686
17687 /* Ensure that the result will have a trailing path separator
17688 * if the argument has one. */
17689 if (remain == NULL && has_trailing_pathsep)
17690 add_pathsep(buf);
17691
17692 /* Separate the first path component in the link value and
17693 * concatenate the remainders. */
17694 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17695 if (*q != NUL)
17696 {
17697 if (remain == NULL)
17698 remain = vim_strsave(q - 1);
17699 else
17700 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017701 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 if (cpy != NULL)
17703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 vim_free(remain);
17705 remain = cpy;
17706 }
17707 }
17708 q[-1] = NUL;
17709 }
17710
17711 q = gettail(p);
17712 if (q > p && *q == NUL)
17713 {
17714 /* Ignore trailing path separator. */
17715 q[-1] = NUL;
17716 q = gettail(p);
17717 }
17718 if (q > p && !mch_isFullName(buf))
17719 {
17720 /* symlink is relative to directory of argument */
17721 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17722 if (cpy != NULL)
17723 {
17724 STRCPY(cpy, p);
17725 STRCPY(gettail(cpy), buf);
17726 vim_free(p);
17727 p = cpy;
17728 }
17729 }
17730 else
17731 {
17732 vim_free(p);
17733 p = vim_strsave(buf);
17734 }
17735 }
17736
17737 if (remain == NULL)
17738 break;
17739
17740 /* Append the first path component of "remain" to "p". */
17741 q = getnextcomp(remain + 1);
17742 len = q - remain - (*q != NUL);
17743 cpy = vim_strnsave(p, STRLEN(p) + len);
17744 if (cpy != NULL)
17745 {
17746 STRNCAT(cpy, remain, len);
17747 vim_free(p);
17748 p = cpy;
17749 }
17750 /* Shorten "remain". */
17751 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017752 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 else
17754 {
17755 vim_free(remain);
17756 remain = NULL;
17757 }
17758 }
17759
17760 /* If the result is a relative path name, make it explicitly relative to
17761 * the current directory if and only if the argument had this form. */
17762 if (!vim_ispathsep(*p))
17763 {
17764 if (is_relative_to_current
17765 && *p != NUL
17766 && !(p[0] == '.'
17767 && (p[1] == NUL
17768 || vim_ispathsep(p[1])
17769 || (p[1] == '.'
17770 && (p[2] == NUL
17771 || vim_ispathsep(p[2]))))))
17772 {
17773 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017774 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775 if (cpy != NULL)
17776 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 vim_free(p);
17778 p = cpy;
17779 }
17780 }
17781 else if (!is_relative_to_current)
17782 {
17783 /* Strip leading "./". */
17784 q = p;
17785 while (q[0] == '.' && vim_ispathsep(q[1]))
17786 q += 2;
17787 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017788 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 }
17790 }
17791
17792 /* Ensure that the result will have no trailing path separator
17793 * if the argument had none. But keep "/" or "//". */
17794 if (!has_trailing_pathsep)
17795 {
17796 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017797 if (after_pathsep(p, q))
17798 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 }
17800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017801 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 }
17803# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017804 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805# endif
17806#endif
17807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017808 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809
17810#ifdef HAVE_READLINK
17811fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017812 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017814 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815}
17816
17817/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017818 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819 */
17820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017821f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822{
Bram Moolenaar33570922005-01-25 22:26:29 +000017823 list_T *l;
17824 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825
Bram Moolenaar0d660222005-01-07 21:51:51 +000017826 if (argvars[0].v_type != VAR_LIST)
17827 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017828 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017829 && !tv_check_lock(l->lv_lock,
17830 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017831 {
17832 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017833 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017834 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017835 while (li != NULL)
17836 {
17837 ni = li->li_prev;
17838 list_append(l, li);
17839 li = ni;
17840 }
17841 rettv->vval.v_list = l;
17842 rettv->v_type = VAR_LIST;
17843 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017844 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846}
17847
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017848#define SP_NOMOVE 0x01 /* don't move cursor */
17849#define SP_REPEAT 0x02 /* repeat to find outer pair */
17850#define SP_RETCOUNT 0x04 /* return matchcount */
17851#define SP_SETPCMARK 0x08 /* set previous context mark */
17852#define SP_START 0x10 /* accept match at start position */
17853#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17854#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017855#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017856
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017857static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017858
17859/*
17860 * Get flags for a search function.
17861 * Possibly sets "p_ws".
17862 * Returns BACKWARD, FORWARD or zero (for an error).
17863 */
17864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017865get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017866{
17867 int dir = FORWARD;
17868 char_u *flags;
17869 char_u nbuf[NUMBUFLEN];
17870 int mask;
17871
17872 if (varp->v_type != VAR_UNKNOWN)
17873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017874 flags = get_tv_string_buf_chk(varp, nbuf);
17875 if (flags == NULL)
17876 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017877 while (*flags != NUL)
17878 {
17879 switch (*flags)
17880 {
17881 case 'b': dir = BACKWARD; break;
17882 case 'w': p_ws = TRUE; break;
17883 case 'W': p_ws = FALSE; break;
17884 default: mask = 0;
17885 if (flagsp != NULL)
17886 switch (*flags)
17887 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017888 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017889 case 'e': mask = SP_END; break;
17890 case 'm': mask = SP_RETCOUNT; break;
17891 case 'n': mask = SP_NOMOVE; break;
17892 case 'p': mask = SP_SUBPAT; break;
17893 case 'r': mask = SP_REPEAT; break;
17894 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017895 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017896 }
17897 if (mask == 0)
17898 {
17899 EMSG2(_(e_invarg2), flags);
17900 dir = 0;
17901 }
17902 else
17903 *flagsp |= mask;
17904 }
17905 if (dir == 0)
17906 break;
17907 ++flags;
17908 }
17909 }
17910 return dir;
17911}
17912
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017914 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017917search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017919 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 char_u *pat;
17921 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017922 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 int save_p_ws = p_ws;
17924 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017925 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017926 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017927 proftime_T tm;
17928#ifdef FEAT_RELTIME
17929 long time_limit = 0;
17930#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017931 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017932 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017934 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017935 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017936 if (dir == 0)
17937 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017938 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017939 if (flags & SP_START)
17940 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017941 if (flags & SP_END)
17942 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017943 if (flags & SP_COLUMN)
17944 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017945
Bram Moolenaar76929292008-01-06 19:07:36 +000017946 /* Optional arguments: line number to stop searching and timeout. */
17947 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017948 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017949 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017950 if (lnum_stop < 0)
17951 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017952#ifdef FEAT_RELTIME
17953 if (argvars[3].v_type != VAR_UNKNOWN)
17954 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017955 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017956 if (time_limit < 0)
17957 goto theend;
17958 }
17959#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017960 }
17961
Bram Moolenaar76929292008-01-06 19:07:36 +000017962#ifdef FEAT_RELTIME
17963 /* Set the time limit, if there is one. */
17964 profile_setlimit(time_limit, &tm);
17965#endif
17966
Bram Moolenaar231334e2005-07-25 20:46:57 +000017967 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017968 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017969 * Check to make sure only those flags are set.
17970 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17971 * flags cannot be set. Check for that condition also.
17972 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017973 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017974 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017975 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017976 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017977 goto theend;
17978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017980 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017981 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017982 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017983 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017985 if (flags & SP_SUBPAT)
17986 retval = subpatnum;
17987 else
17988 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017989 if (flags & SP_SETPCMARK)
17990 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017992 if (match_pos != NULL)
17993 {
17994 /* Store the match cursor position */
17995 match_pos->lnum = pos.lnum;
17996 match_pos->col = pos.col + 1;
17997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998 /* "/$" will put the cursor after the end of the line, may need to
17999 * correct that here */
18000 check_cursor();
18001 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018002
18003 /* If 'n' flag is used: restore cursor position. */
18004 if (flags & SP_NOMOVE)
18005 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000018006 else
18007 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018008theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018010
18011 return retval;
18012}
18013
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018014#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018015
18016/*
18017 * round() is not in C90, use ceil() or floor() instead.
18018 */
18019 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010018020vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018021{
18022 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
18023}
18024
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018025/*
18026 * "round({float})" function
18027 */
18028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018029f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018030{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018031 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018032
18033 rettv->v_type = VAR_FLOAT;
18034 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018035 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018036 else
18037 rettv->vval.v_float = 0.0;
18038}
18039#endif
18040
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018041/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020018042 * "screenattr()" function
18043 */
18044 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010018045f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020018046{
18047 int row;
18048 int col;
18049 int c;
18050
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018051 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
18052 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020018053 if (row < 0 || row >= screen_Rows
18054 || col < 0 || col >= screen_Columns)
18055 c = -1;
18056 else
18057 c = ScreenAttrs[LineOffset[row] + col];
18058 rettv->vval.v_number = c;
18059}
18060
18061/*
18062 * "screenchar()" function
18063 */
18064 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010018065f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020018066{
18067 int row;
18068 int col;
18069 int off;
18070 int c;
18071
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018072 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
18073 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020018074 if (row < 0 || row >= screen_Rows
18075 || col < 0 || col >= screen_Columns)
18076 c = -1;
18077 else
18078 {
18079 off = LineOffset[row] + col;
18080#ifdef FEAT_MBYTE
18081 if (enc_utf8 && ScreenLinesUC[off] != 0)
18082 c = ScreenLinesUC[off];
18083 else
18084#endif
18085 c = ScreenLines[off];
18086 }
18087 rettv->vval.v_number = c;
18088}
18089
18090/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018091 * "screencol()" function
18092 *
18093 * First column is 1 to be consistent with virtcol().
18094 */
18095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018096f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018097{
18098 rettv->vval.v_number = screen_screencol() + 1;
18099}
18100
18101/*
18102 * "screenrow()" function
18103 */
18104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018105f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018106{
18107 rettv->vval.v_number = screen_screenrow() + 1;
18108}
18109
18110/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018111 * "search()" function
18112 */
18113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018114f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018115{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018116 int flags = 0;
18117
18118 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119}
18120
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018122 * "searchdecl()" function
18123 */
18124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018125f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018126{
18127 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018128 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018129 int error = FALSE;
18130 char_u *name;
18131
18132 rettv->vval.v_number = 1; /* default: FAIL */
18133
18134 name = get_tv_string_chk(&argvars[0]);
18135 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000018136 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018137 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018138 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018139 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018140 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018141 if (!error && name != NULL)
18142 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000018143 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018144}
18145
18146/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018147 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018150searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151{
18152 char_u *spat, *mpat, *epat;
18153 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155 int dir;
18156 int flags = 0;
18157 char_u nbuf1[NUMBUFLEN];
18158 char_u nbuf2[NUMBUFLEN];
18159 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018160 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018161 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018162 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018165 spat = get_tv_string_chk(&argvars[0]);
18166 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18167 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18168 if (spat == NULL || mpat == NULL || epat == NULL)
18169 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 /* Handle the optional fourth argument: flags */
18172 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018173 if (dir == 0)
18174 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018175
18176 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018177 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18178 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018179 if ((flags & (SP_END | SP_SUBPAT)) != 0
18180 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018181 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018182 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018183 goto theend;
18184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018186 /* Using 'r' implies 'W', otherwise it doesn't work. */
18187 if (flags & SP_REPEAT)
18188 p_ws = FALSE;
18189
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018190 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018191 if (argvars[3].v_type == VAR_UNKNOWN
18192 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193 skip = (char_u *)"";
18194 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018196 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018197 if (argvars[5].v_type != VAR_UNKNOWN)
18198 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018199 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018200 if (lnum_stop < 0)
18201 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018202#ifdef FEAT_RELTIME
18203 if (argvars[6].v_type != VAR_UNKNOWN)
18204 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018205 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000018206 if (time_limit < 0)
18207 goto theend;
18208 }
18209#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018210 }
18211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018212 if (skip == NULL)
18213 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018215 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018216 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018217
18218theend:
18219 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018220
18221 return retval;
18222}
18223
18224/*
18225 * "searchpair()" function
18226 */
18227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018228f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018229{
18230 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18231}
18232
18233/*
18234 * "searchpairpos()" function
18235 */
18236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018237f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018238{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018239 pos_T match_pos;
18240 int lnum = 0;
18241 int col = 0;
18242
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018243 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018244 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018245
18246 if (searchpair_cmn(argvars, &match_pos) > 0)
18247 {
18248 lnum = match_pos.lnum;
18249 col = match_pos.col;
18250 }
18251
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018252 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18253 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018254}
18255
18256/*
18257 * Search for a start/middle/end thing.
18258 * Used by searchpair(), see its documentation for the details.
18259 * Returns 0 or -1 for no match,
18260 */
18261 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018262do_searchpair(
18263 char_u *spat, /* start pattern */
18264 char_u *mpat, /* middle pattern */
18265 char_u *epat, /* end pattern */
18266 int dir, /* BACKWARD or FORWARD */
18267 char_u *skip, /* skip expression */
18268 int flags, /* SP_SETPCMARK and other SP_ values */
18269 pos_T *match_pos,
18270 linenr_T lnum_stop, /* stop at this line if not zero */
18271 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018272{
18273 char_u *save_cpo;
18274 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18275 long retval = 0;
18276 pos_T pos;
18277 pos_T firstpos;
18278 pos_T foundpos;
18279 pos_T save_cursor;
18280 pos_T save_pos;
18281 int n;
18282 int r;
18283 int nest = 1;
18284 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018285 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018286 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018287
18288 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18289 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018290 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018291
Bram Moolenaar76929292008-01-06 19:07:36 +000018292#ifdef FEAT_RELTIME
18293 /* Set the time limit, if there is one. */
18294 profile_setlimit(time_limit, &tm);
18295#endif
18296
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018297 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18298 * start/middle/end (pat3, for the top pair). */
18299 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18300 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18301 if (pat2 == NULL || pat3 == NULL)
18302 goto theend;
18303 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18304 if (*mpat == NUL)
18305 STRCPY(pat3, pat2);
18306 else
18307 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18308 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018309 if (flags & SP_START)
18310 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018311
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 save_cursor = curwin->w_cursor;
18313 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018314 clearpos(&firstpos);
18315 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 pat = pat3;
18317 for (;;)
18318 {
18319 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018320 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18322 /* didn't find it or found the first match again: FAIL */
18323 break;
18324
18325 if (firstpos.lnum == 0)
18326 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018327 if (equalpos(pos, foundpos))
18328 {
18329 /* Found the same position again. Can happen with a pattern that
18330 * has "\zs" at the end and searching backwards. Advance one
18331 * character and try again. */
18332 if (dir == BACKWARD)
18333 decl(&pos);
18334 else
18335 incl(&pos);
18336 }
18337 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018339 /* clear the start flag to avoid getting stuck here */
18340 options &= ~SEARCH_START;
18341
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 /* If the skip pattern matches, ignore this match. */
18343 if (*skip != NUL)
18344 {
18345 save_pos = curwin->w_cursor;
18346 curwin->w_cursor = pos;
18347 r = eval_to_bool(skip, &err, NULL, FALSE);
18348 curwin->w_cursor = save_pos;
18349 if (err)
18350 {
18351 /* Evaluating {skip} caused an error, break here. */
18352 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018353 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 break;
18355 }
18356 if (r)
18357 continue;
18358 }
18359
18360 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18361 {
18362 /* Found end when searching backwards or start when searching
18363 * forward: nested pair. */
18364 ++nest;
18365 pat = pat2; /* nested, don't search for middle */
18366 }
18367 else
18368 {
18369 /* Found end when searching forward or start when searching
18370 * backward: end of (nested) pair; or found middle in outer pair. */
18371 if (--nest == 1)
18372 pat = pat3; /* outer level, search for middle */
18373 }
18374
18375 if (nest == 0)
18376 {
18377 /* Found the match: return matchcount or line number. */
18378 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018379 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018381 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018382 if (flags & SP_SETPCMARK)
18383 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 curwin->w_cursor = pos;
18385 if (!(flags & SP_REPEAT))
18386 break;
18387 nest = 1; /* search for next unmatched */
18388 }
18389 }
18390
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018391 if (match_pos != NULL)
18392 {
18393 /* Store the match cursor position */
18394 match_pos->lnum = curwin->w_cursor.lnum;
18395 match_pos->col = curwin->w_cursor.col + 1;
18396 }
18397
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018399 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 curwin->w_cursor = save_cursor;
18401
18402theend:
18403 vim_free(pat2);
18404 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018405 if (p_cpo == empty_option)
18406 p_cpo = save_cpo;
18407 else
18408 /* Darn, evaluating the {skip} expression changed the value. */
18409 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018410
18411 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412}
18413
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018414/*
18415 * "searchpos()" function
18416 */
18417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018418f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018419{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018420 pos_T match_pos;
18421 int lnum = 0;
18422 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018423 int n;
18424 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018425
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018426 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018427 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018428
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018429 n = search_cmn(argvars, &match_pos, &flags);
18430 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018431 {
18432 lnum = match_pos.lnum;
18433 col = match_pos.col;
18434 }
18435
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018436 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18437 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018438 if (flags & SP_SUBPAT)
18439 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018440}
18441
Bram Moolenaar0d660222005-01-07 21:51:51 +000018442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018443f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018445#ifdef FEAT_CLIENTSERVER
18446 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018447 char_u *server = get_tv_string_chk(&argvars[0]);
18448 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449
Bram Moolenaar0d660222005-01-07 21:51:51 +000018450 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018451 if (server == NULL || reply == NULL)
18452 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018453 if (check_restricted() || check_secure())
18454 return;
18455# ifdef FEAT_X11
18456 if (check_connection() == FAIL)
18457 return;
18458# endif
18459
18460 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018462 EMSG(_("E258: Unable to send to client"));
18463 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018465 rettv->vval.v_number = 0;
18466#else
18467 rettv->vval.v_number = -1;
18468#endif
18469}
18470
Bram Moolenaar0d660222005-01-07 21:51:51 +000018471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018472f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018473{
18474 char_u *r = NULL;
18475
18476#ifdef FEAT_CLIENTSERVER
18477# ifdef WIN32
18478 r = serverGetVimNames();
18479# else
18480 make_connection();
18481 if (X_DISPLAY != NULL)
18482 r = serverGetVimNames(X_DISPLAY);
18483# endif
18484#endif
18485 rettv->v_type = VAR_STRING;
18486 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487}
18488
18489/*
18490 * "setbufvar()" function
18491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018493f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494{
18495 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018498 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 char_u nbuf[NUMBUFLEN];
18500
18501 if (check_restricted() || check_secure())
18502 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018503 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18504 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018505 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506 varp = &argvars[2];
18507
18508 if (buf != NULL && varname != NULL && varp != NULL)
18509 {
18510 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512
18513 if (*varname == '&')
18514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018515 long numval;
18516 char_u *strval;
18517 int error = FALSE;
18518
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018520 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018521 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018522 if (!error && strval != NULL)
18523 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 }
18525 else
18526 {
18527 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18528 if (bufvarname != NULL)
18529 {
18530 STRCPY(bufvarname, "b:");
18531 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018532 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 vim_free(bufvarname);
18534 }
18535 }
18536
18537 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540}
18541
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018543f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018544{
18545 dict_T *d;
18546 dictitem_T *di;
18547 char_u *csearch;
18548
18549 if (argvars[0].v_type != VAR_DICT)
18550 {
18551 EMSG(_(e_dictreq));
18552 return;
18553 }
18554
18555 if ((d = argvars[0].vval.v_dict) != NULL)
18556 {
18557 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18558 if (csearch != NULL)
18559 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018560#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018561 if (enc_utf8)
18562 {
18563 int pcc[MAX_MCO];
18564 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018565
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018566 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18567 }
18568 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018569#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018570 set_last_csearch(PTR2CHAR(csearch),
18571 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018572 }
18573
18574 di = dict_find(d, (char_u *)"forward", -1);
18575 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018576 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018577 ? FORWARD : BACKWARD);
18578
18579 di = dict_find(d, (char_u *)"until", -1);
18580 if (di != NULL)
18581 set_csearch_until(!!get_tv_number(&di->di_tv));
18582 }
18583}
18584
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585/*
18586 * "setcmdpos()" function
18587 */
18588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018589f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018591 int pos = (int)get_tv_number(&argvars[0]) - 1;
18592
18593 if (pos >= 0)
18594 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595}
18596
18597/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018598 * "setfperm({fname}, {mode})" function
18599 */
18600 static void
18601f_setfperm(typval_T *argvars, typval_T *rettv)
18602{
18603 char_u *fname;
18604 char_u modebuf[NUMBUFLEN];
18605 char_u *mode_str;
18606 int i;
18607 int mask;
18608 int mode = 0;
18609
18610 rettv->vval.v_number = 0;
18611 fname = get_tv_string_chk(&argvars[0]);
18612 if (fname == NULL)
18613 return;
18614 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18615 if (mode_str == NULL)
18616 return;
18617 if (STRLEN(mode_str) != 9)
18618 {
18619 EMSG2(_(e_invarg2), mode_str);
18620 return;
18621 }
18622
18623 mask = 1;
18624 for (i = 8; i >= 0; --i)
18625 {
18626 if (mode_str[i] != '-')
18627 mode |= mask;
18628 mask = mask << 1;
18629 }
18630 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18631}
18632
18633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 * "setline()" function
18635 */
18636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018637f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638{
18639 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018640 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018641 list_T *l = NULL;
18642 listitem_T *li = NULL;
18643 long added = 0;
18644 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018646 lnum = get_tv_lnum(&argvars[0]);
18647 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018649 l = argvars[1].vval.v_list;
18650 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018652 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018653 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018654
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018655 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018656 for (;;)
18657 {
18658 if (l != NULL)
18659 {
18660 /* list argument, get next string */
18661 if (li == NULL)
18662 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018663 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018664 li = li->li_next;
18665 }
18666
18667 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018668 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018669 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018670
18671 /* When coming here from Insert mode, sync undo, so that this can be
18672 * undone separately from what was previously inserted. */
18673 if (u_sync_once == 2)
18674 {
18675 u_sync_once = 1; /* notify that u_sync() was called */
18676 u_sync(TRUE);
18677 }
18678
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018679 if (lnum <= curbuf->b_ml.ml_line_count)
18680 {
18681 /* existing line, replace it */
18682 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18683 {
18684 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018685 if (lnum == curwin->w_cursor.lnum)
18686 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018687 rettv->vval.v_number = 0; /* OK */
18688 }
18689 }
18690 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18691 {
18692 /* lnum is one past the last line, append the line */
18693 ++added;
18694 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18695 rettv->vval.v_number = 0; /* OK */
18696 }
18697
18698 if (l == NULL) /* only one string argument */
18699 break;
18700 ++lnum;
18701 }
18702
18703 if (added > 0)
18704 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705}
18706
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018707static 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 +000018708
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018710 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018711 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018713set_qf_ll_list(
18714 win_T *wp UNUSED,
18715 typval_T *list_arg UNUSED,
18716 typval_T *action_arg UNUSED,
18717 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018718{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018719#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018720 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018721 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018722 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018723#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018724
Bram Moolenaar2641f772005-03-25 21:58:17 +000018725 rettv->vval.v_number = -1;
18726
18727#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018728 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018729 EMSG(_(e_listreq));
18730 else
18731 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018732 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018733
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018734 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018735 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018736 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018737 if (act == NULL)
18738 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018739 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018740 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018741 else
18742 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018743 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018744 else if (action_arg->v_type == VAR_UNKNOWN)
18745 action = ' ';
18746 else
18747 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018748
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018749 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018750 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018751 rettv->vval.v_number = 0;
18752 }
18753#endif
18754}
18755
18756/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018757 * "setloclist()" function
18758 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018760f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018761{
18762 win_T *win;
18763
18764 rettv->vval.v_number = -1;
18765
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018766 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018767 if (win != NULL)
18768 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18769}
18770
18771/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018772 * "setmatches()" function
18773 */
18774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018775f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018776{
18777#ifdef FEAT_SEARCH_EXTRA
18778 list_T *l;
18779 listitem_T *li;
18780 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018781 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018782
18783 rettv->vval.v_number = -1;
18784 if (argvars[0].v_type != VAR_LIST)
18785 {
18786 EMSG(_(e_listreq));
18787 return;
18788 }
18789 if ((l = argvars[0].vval.v_list) != NULL)
18790 {
18791
18792 /* To some extent make sure that we are dealing with a list from
18793 * "getmatches()". */
18794 li = l->lv_first;
18795 while (li != NULL)
18796 {
18797 if (li->li_tv.v_type != VAR_DICT
18798 || (d = li->li_tv.vval.v_dict) == NULL)
18799 {
18800 EMSG(_(e_invarg));
18801 return;
18802 }
18803 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018804 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18805 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018806 && dict_find(d, (char_u *)"priority", -1) != NULL
18807 && dict_find(d, (char_u *)"id", -1) != NULL))
18808 {
18809 EMSG(_(e_invarg));
18810 return;
18811 }
18812 li = li->li_next;
18813 }
18814
18815 clear_matches(curwin);
18816 li = l->lv_first;
18817 while (li != NULL)
18818 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018819 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018820 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018821 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018822 char_u *group;
18823 int priority;
18824 int id;
18825 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018826
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018827 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018828 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18829 {
18830 if (s == NULL)
18831 {
18832 s = list_alloc();
18833 if (s == NULL)
18834 return;
18835 }
18836
18837 /* match from matchaddpos() */
18838 for (i = 1; i < 9; i++)
18839 {
18840 sprintf((char *)buf, (char *)"pos%d", i);
18841 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18842 {
18843 if (di->di_tv.v_type != VAR_LIST)
18844 return;
18845
18846 list_append_tv(s, &di->di_tv);
18847 s->lv_refcount++;
18848 }
18849 else
18850 break;
18851 }
18852 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018853
18854 group = get_dict_string(d, (char_u *)"group", FALSE);
18855 priority = (int)get_dict_number(d, (char_u *)"priority");
18856 id = (int)get_dict_number(d, (char_u *)"id");
18857 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18858 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18859 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018860 if (i == 0)
18861 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018862 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018863 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018864 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018865 }
18866 else
18867 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018868 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018869 list_unref(s);
18870 s = NULL;
18871 }
18872
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018873 li = li->li_next;
18874 }
18875 rettv->vval.v_number = 0;
18876 }
18877#endif
18878}
18879
18880/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018881 * "setpos()" function
18882 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018884f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018885{
18886 pos_T pos;
18887 int fnum;
18888 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018889 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018890
Bram Moolenaar08250432008-02-13 11:42:46 +000018891 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018892 name = get_tv_string_chk(argvars);
18893 if (name != NULL)
18894 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018895 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018896 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018897 if (--pos.col < 0)
18898 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018899 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018900 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018901 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018902 if (fnum == curbuf->b_fnum)
18903 {
18904 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018905 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018906 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018907 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018908 curwin->w_set_curswant = FALSE;
18909 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018910 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018911 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018912 }
18913 else
18914 EMSG(_(e_invarg));
18915 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018916 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18917 {
18918 /* set mark */
18919 if (setmark_pos(name[1], &pos, fnum) == OK)
18920 rettv->vval.v_number = 0;
18921 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018922 else
18923 EMSG(_(e_invarg));
18924 }
18925 }
18926}
18927
18928/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018929 * "setqflist()" function
18930 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018932f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018933{
18934 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18935}
18936
18937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938 * "setreg()" function
18939 */
18940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018941f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942{
18943 int regname;
18944 char_u *strregname;
18945 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018946 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947 int append;
18948 char_u yank_type;
18949 long block_len;
18950
18951 block_len = -1;
18952 yank_type = MAUTO;
18953 append = FALSE;
18954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018955 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018956 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018958 if (strregname == NULL)
18959 return; /* type error; errmsg already given */
18960 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961 if (regname == 0 || regname == '@')
18962 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018966 stropt = get_tv_string_chk(&argvars[2]);
18967 if (stropt == NULL)
18968 return; /* type error */
18969 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 switch (*stropt)
18971 {
18972 case 'a': case 'A': /* append */
18973 append = TRUE;
18974 break;
18975 case 'v': case 'c': /* character-wise selection */
18976 yank_type = MCHAR;
18977 break;
18978 case 'V': case 'l': /* line-wise selection */
18979 yank_type = MLINE;
18980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 case 'b': case Ctrl_V: /* block-wise selection */
18982 yank_type = MBLOCK;
18983 if (VIM_ISDIGIT(stropt[1]))
18984 {
18985 ++stropt;
18986 block_len = getdigits(&stropt) - 1;
18987 --stropt;
18988 }
18989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 }
18991 }
18992
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018993 if (argvars[1].v_type == VAR_LIST)
18994 {
18995 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018996 char_u **allocval;
18997 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018998 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018999 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020019000 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019001 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020019002 int len;
19003
19004 /* If the list is NULL handle like an empty list. */
19005 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019006
Bram Moolenaar7d647822014-04-05 21:28:56 +020019007 /* First half: use for pointers to result lines; second half: use for
19008 * pointers to allocated copies. */
19009 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019010 if (lstval == NULL)
19011 return;
19012 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020019013 allocval = lstval + len + 2;
19014 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019015
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020019016 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019017 li = li->li_next)
19018 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020019019 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019020 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020019021 goto free_lstval;
19022 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019023 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020019024 /* Need to make a copy, next get_tv_string_buf_chk() will
19025 * overwrite the string. */
19026 strval = vim_strsave(buf);
19027 if (strval == NULL)
19028 goto free_lstval;
19029 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019030 }
19031 *curval++ = strval;
19032 }
19033 *curval++ = NULL;
19034
19035 write_reg_contents_lst(regname, lstval, -1,
19036 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020019037free_lstval:
19038 while (curallocval > allocval)
19039 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019040 vim_free(lstval);
19041 }
19042 else
19043 {
19044 strval = get_tv_string_chk(&argvars[1]);
19045 if (strval == NULL)
19046 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019047 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019049 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019050 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051}
19052
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019053/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019054 * "settabvar()" function
19055 */
19056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019057f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019058{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019059#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019060 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019061 tabpage_T *tp;
19062#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019063 char_u *varname, *tabvarname;
19064 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019065
19066 rettv->vval.v_number = 0;
19067
19068 if (check_restricted() || check_secure())
19069 return;
19070
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019071#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019072 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019073#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019074 varname = get_tv_string_chk(&argvars[1]);
19075 varp = &argvars[2];
19076
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019077 if (varname != NULL && varp != NULL
19078#ifdef FEAT_WINDOWS
19079 && tp != NULL
19080#endif
19081 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019082 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019083#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019084 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020019085 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019086#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019087
19088 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
19089 if (tabvarname != NULL)
19090 {
19091 STRCPY(tabvarname, "t:");
19092 STRCPY(tabvarname + 2, varname);
19093 set_var(tabvarname, varp, TRUE);
19094 vim_free(tabvarname);
19095 }
19096
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019097#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019098 /* Restore current tabpage */
19099 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020019100 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019101#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019102 }
19103}
19104
19105/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019106 * "settabwinvar()" function
19107 */
19108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019109f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019110{
19111 setwinvar(argvars, rettv, 1);
19112}
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113
19114/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019115 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019118f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019120 setwinvar(argvars, rettv, 0);
19121}
19122
19123/*
19124 * "setwinvar()" and "settabwinvar()" functions
19125 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020019126
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019128setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019129{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 win_T *win;
19131#ifdef FEAT_WINDOWS
19132 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019133 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020019134 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135#endif
19136 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019137 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019139 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140
19141 if (check_restricted() || check_secure())
19142 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019143
19144#ifdef FEAT_WINDOWS
19145 if (off == 1)
19146 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
19147 else
19148 tp = curtab;
19149#endif
19150 win = find_win_by_nr(&argvars[off], tp);
19151 varname = get_tv_string_chk(&argvars[off + 1]);
19152 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153
19154 if (win != NULL && varname != NULL && varp != NULL)
19155 {
19156#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019157 need_switch_win = !(tp == curtab && win == curwin);
19158 if (!need_switch_win
19159 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019162 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019164 long numval;
19165 char_u *strval;
19166 int error = FALSE;
19167
19168 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019169 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019170 strval = get_tv_string_buf_chk(varp, nbuf);
19171 if (!error && strval != NULL)
19172 set_option_value(varname, numval, strval, OPT_LOCAL);
19173 }
19174 else
19175 {
19176 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19177 if (winvarname != NULL)
19178 {
19179 STRCPY(winvarname, "w:");
19180 STRCPY(winvarname + 2, varname);
19181 set_var(winvarname, varp, TRUE);
19182 vim_free(winvarname);
19183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 }
19185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019187 if (need_switch_win)
19188 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189#endif
19190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191}
19192
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019193#ifdef FEAT_CRYPT
19194/*
19195 * "sha256({string})" function
19196 */
19197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019198f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019199{
19200 char_u *p;
19201
19202 p = get_tv_string(&argvars[0]);
19203 rettv->vval.v_string = vim_strsave(
19204 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19205 rettv->v_type = VAR_STRING;
19206}
19207#endif /* FEAT_CRYPT */
19208
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019210 * "shellescape({string})" function
19211 */
19212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019213f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019214{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019215 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019216 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019217 rettv->v_type = VAR_STRING;
19218}
19219
19220/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019221 * shiftwidth() function
19222 */
19223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019224f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019225{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019226 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019227}
19228
19229/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019230 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 */
19232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019233f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019235 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019236
Bram Moolenaar0d660222005-01-07 21:51:51 +000019237 p = get_tv_string(&argvars[0]);
19238 rettv->vval.v_string = vim_strsave(p);
19239 simplify_filename(rettv->vval.v_string); /* simplify in place */
19240 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241}
19242
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019243#ifdef FEAT_FLOAT
19244/*
19245 * "sin()" function
19246 */
19247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019248f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019249{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019250 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019251
19252 rettv->v_type = VAR_FLOAT;
19253 if (get_float_arg(argvars, &f) == OK)
19254 rettv->vval.v_float = sin(f);
19255 else
19256 rettv->vval.v_float = 0.0;
19257}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019258
19259/*
19260 * "sinh()" function
19261 */
19262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019263f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019264{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019265 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019266
19267 rettv->v_type = VAR_FLOAT;
19268 if (get_float_arg(argvars, &f) == OK)
19269 rettv->vval.v_float = sinh(f);
19270 else
19271 rettv->vval.v_float = 0.0;
19272}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019273#endif
19274
Bram Moolenaar0d660222005-01-07 21:51:51 +000019275static int
19276#ifdef __BORLANDC__
19277 _RTLENTRYF
19278#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019279 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019280static int
19281#ifdef __BORLANDC__
19282 _RTLENTRYF
19283#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019284 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019285
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019286/* struct used in the array that's given to qsort() */
19287typedef struct
19288{
19289 listitem_T *item;
19290 int idx;
19291} sortItem_T;
19292
Bram Moolenaar0b962472016-02-22 22:51:33 +010019293/* struct storing information about current sort */
19294typedef struct
19295{
19296 int item_compare_ic;
19297 int item_compare_numeric;
19298 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019299#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019300 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019301#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019302 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019303 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019304 dict_T *item_compare_selfdict;
19305 int item_compare_func_err;
19306 int item_compare_keep_zero;
19307} sortinfo_T;
19308static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019309static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019310#define ITEM_COMPARE_FAIL 999
19311
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019313 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019315 static int
19316#ifdef __BORLANDC__
19317_RTLENTRYF
19318#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019319item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019321 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019322 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019323 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019324 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019325 int res;
19326 char_u numbuf1[NUMBUFLEN];
19327 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019329 si1 = (sortItem_T *)s1;
19330 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019331 tv1 = &si1->item->li_tv;
19332 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019333
Bram Moolenaar0b962472016-02-22 22:51:33 +010019334 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019335 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019336 varnumber_T v1 = get_tv_number(tv1);
19337 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019338
19339 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19340 }
19341
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019342#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019343 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019344 {
19345 float_T v1 = get_tv_float(tv1);
19346 float_T v2 = get_tv_float(tv2);
19347
19348 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19349 }
19350#endif
19351
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019352 /* tv2string() puts quotes around a string and allocates memory. Don't do
19353 * that for string variables. Use a single quote when comparing with a
19354 * non-string to do what the docs promise. */
19355 if (tv1->v_type == VAR_STRING)
19356 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019357 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019358 p1 = (char_u *)"'";
19359 else
19360 p1 = tv1->vval.v_string;
19361 }
19362 else
19363 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19364 if (tv2->v_type == VAR_STRING)
19365 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019366 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019367 p2 = (char_u *)"'";
19368 else
19369 p2 = tv2->vval.v_string;
19370 }
19371 else
19372 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019373 if (p1 == NULL)
19374 p1 = (char_u *)"";
19375 if (p2 == NULL)
19376 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019377 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019378 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019379 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019380 res = STRICMP(p1, p2);
19381 else
19382 res = STRCMP(p1, p2);
19383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019385 {
19386 double n1, n2;
19387 n1 = strtod((char *)p1, (char **)&p1);
19388 n2 = strtod((char *)p2, (char **)&p2);
19389 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19390 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019391
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019392 /* When the result would be zero, compare the item indexes. Makes the
19393 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019394 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019395 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019396
Bram Moolenaar0d660222005-01-07 21:51:51 +000019397 vim_free(tofree1);
19398 vim_free(tofree2);
19399 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400}
19401
19402 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019403#ifdef __BORLANDC__
19404_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019406item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019407{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019408 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019409 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019410 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019411 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019412 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019413 char_u *func_name;
19414 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019416 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019417 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019418 return 0;
19419
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019420 si1 = (sortItem_T *)s1;
19421 si2 = (sortItem_T *)s2;
19422
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019423 if (partial == NULL)
19424 func_name = sortinfo->item_compare_func;
19425 else
19426 func_name = partial->pt_name;
19427
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019428 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019429 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019430 copy_tv(&si1->item->li_tv, &argv[0]);
19431 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019432
19433 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019434 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019435 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019436 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019437 clear_tv(&argv[0]);
19438 clear_tv(&argv[1]);
19439
19440 if (res == FAIL)
19441 res = ITEM_COMPARE_FAIL;
19442 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019443 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010019444 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019445 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019446 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019447
19448 /* When the result would be zero, compare the pointers themselves. Makes
19449 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019450 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019451 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019452
Bram Moolenaar0d660222005-01-07 21:51:51 +000019453 return res;
19454}
19455
19456/*
19457 * "sort({list})" function
19458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019460do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461{
Bram Moolenaar33570922005-01-25 22:26:29 +000019462 list_T *l;
19463 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019464 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019465 sortinfo_T *old_sortinfo;
19466 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019467 long len;
19468 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469
Bram Moolenaar0b962472016-02-22 22:51:33 +010019470 /* Pointer to current info struct used in compare function. Save and
19471 * restore the current one for nested calls. */
19472 old_sortinfo = sortinfo;
19473 sortinfo = &info;
19474
Bram Moolenaar0d660222005-01-07 21:51:51 +000019475 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019476 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 else
19478 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019479 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019480 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019481 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19482 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019483 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019484 rettv->vval.v_list = l;
19485 rettv->v_type = VAR_LIST;
19486 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487
Bram Moolenaar0d660222005-01-07 21:51:51 +000019488 len = list_len(l);
19489 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019490 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491
Bram Moolenaar0b962472016-02-22 22:51:33 +010019492 info.item_compare_ic = FALSE;
19493 info.item_compare_numeric = FALSE;
19494 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019495#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019496 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019497#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019498 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019499 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019500 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019501 if (argvars[1].v_type != VAR_UNKNOWN)
19502 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019503 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019504 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019505 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019506 else if (argvars[1].v_type == VAR_PARTIAL)
19507 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019508 else
19509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019510 int error = FALSE;
19511
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019512 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019513 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019514 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019515 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019516 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019517 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019518 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019519 else if (i != 0)
19520 {
19521 EMSG(_(e_invarg));
19522 goto theend;
19523 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019524 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019525 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019526 if (*info.item_compare_func == NUL)
19527 {
19528 /* empty string means default sort */
19529 info.item_compare_func = NULL;
19530 }
19531 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019532 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019533 info.item_compare_func = NULL;
19534 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019535 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019536 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019537 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019538 info.item_compare_func = NULL;
19539 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019540 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019541#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019542 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019543 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019544 info.item_compare_func = NULL;
19545 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019546 }
19547#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019548 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019549 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019550 info.item_compare_func = NULL;
19551 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019552 }
19553 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019554 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019555
19556 if (argvars[2].v_type != VAR_UNKNOWN)
19557 {
19558 /* optional third argument: {dict} */
19559 if (argvars[2].v_type != VAR_DICT)
19560 {
19561 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019562 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019563 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019564 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019565 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567
Bram Moolenaar0d660222005-01-07 21:51:51 +000019568 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019569 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019570 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019571 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572
Bram Moolenaar327aa022014-03-25 18:24:23 +010019573 i = 0;
19574 if (sort)
19575 {
19576 /* sort(): ptrs will be the list to sort */
19577 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019578 {
19579 ptrs[i].item = li;
19580 ptrs[i].idx = i;
19581 ++i;
19582 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019583
Bram Moolenaar0b962472016-02-22 22:51:33 +010019584 info.item_compare_func_err = FALSE;
19585 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019586 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019587 if ((info.item_compare_func != NULL
19588 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019589 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019590 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019591 EMSG(_("E702: Sort compare function failed"));
19592 else
19593 {
19594 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019595 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019596 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019597 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019598 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019599
Bram Moolenaar0b962472016-02-22 22:51:33 +010019600 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019601 {
19602 /* Clear the List and append the items in sorted order. */
19603 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19604 l->lv_len = 0;
19605 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019606 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019607 }
19608 }
19609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019611 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019612 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019613
19614 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019615 info.item_compare_func_err = FALSE;
19616 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019617 item_compare_func_ptr = info.item_compare_func != NULL
19618 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019619 ? item_compare2 : item_compare;
19620
19621 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19622 li = li->li_next)
19623 {
19624 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19625 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019626 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019627 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019628 {
19629 EMSG(_("E882: Uniq compare function failed"));
19630 break;
19631 }
19632 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019633
Bram Moolenaar0b962472016-02-22 22:51:33 +010019634 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019635 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019636 while (--i >= 0)
19637 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019638 li = ptrs[i].item->li_next;
19639 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019640 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019641 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019642 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019643 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019644 list_fix_watch(l, li);
19645 listitem_free(li);
19646 l->lv_len--;
19647 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019648 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019649 }
19650
19651 vim_free(ptrs);
19652 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019653theend:
19654 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019655}
19656
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019657/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019658 * "sort({list})" function
19659 */
19660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019661f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019662{
19663 do_sort_uniq(argvars, rettv, TRUE);
19664}
19665
19666/*
19667 * "uniq({list})" function
19668 */
19669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019670f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019671{
19672 do_sort_uniq(argvars, rettv, FALSE);
19673}
19674
19675/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019676 * "soundfold({word})" function
19677 */
19678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019679f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019680{
19681 char_u *s;
19682
19683 rettv->v_type = VAR_STRING;
19684 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019685#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019686 rettv->vval.v_string = eval_soundfold(s);
19687#else
19688 rettv->vval.v_string = vim_strsave(s);
19689#endif
19690}
19691
19692/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019693 * "spellbadword()" function
19694 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019696f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019697{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019698 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019699 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019700 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019701
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019702 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019703 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019704
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019705#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019706 if (argvars[0].v_type == VAR_UNKNOWN)
19707 {
19708 /* Find the start and length of the badly spelled word. */
19709 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19710 if (len != 0)
19711 word = ml_get_cursor();
19712 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019713 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019714 {
19715 char_u *str = get_tv_string_chk(&argvars[0]);
19716 int capcol = -1;
19717
19718 if (str != NULL)
19719 {
19720 /* Check the argument for spelling. */
19721 while (*str != NUL)
19722 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019723 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019724 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019725 {
19726 word = str;
19727 break;
19728 }
19729 str += len;
19730 }
19731 }
19732 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019733#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019734
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019735 list_append_string(rettv->vval.v_list, word, len);
19736 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019737 attr == HLF_SPB ? "bad" :
19738 attr == HLF_SPR ? "rare" :
19739 attr == HLF_SPL ? "local" :
19740 attr == HLF_SPC ? "caps" :
19741 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019742}
19743
19744/*
19745 * "spellsuggest()" function
19746 */
19747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019748f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019749{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019750#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019751 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019752 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019753 int maxcount;
19754 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019755 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019756 listitem_T *li;
19757 int need_capital = FALSE;
19758#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019759
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019760 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019761 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019762
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019763#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019764 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019765 {
19766 str = get_tv_string(&argvars[0]);
19767 if (argvars[1].v_type != VAR_UNKNOWN)
19768 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019769 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019770 if (maxcount <= 0)
19771 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019772 if (argvars[2].v_type != VAR_UNKNOWN)
19773 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019774 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019775 if (typeerr)
19776 return;
19777 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019778 }
19779 else
19780 maxcount = 25;
19781
Bram Moolenaar4770d092006-01-12 23:22:24 +000019782 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019783
19784 for (i = 0; i < ga.ga_len; ++i)
19785 {
19786 str = ((char_u **)ga.ga_data)[i];
19787
19788 li = listitem_alloc();
19789 if (li == NULL)
19790 vim_free(str);
19791 else
19792 {
19793 li->li_tv.v_type = VAR_STRING;
19794 li->li_tv.v_lock = 0;
19795 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019796 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019797 }
19798 }
19799 ga_clear(&ga);
19800 }
19801#endif
19802}
19803
Bram Moolenaar0d660222005-01-07 21:51:51 +000019804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019805f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019806{
19807 char_u *str;
19808 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019809 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019810 regmatch_T regmatch;
19811 char_u patbuf[NUMBUFLEN];
19812 char_u *save_cpo;
19813 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019814 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019815 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019816 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019817
19818 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19819 save_cpo = p_cpo;
19820 p_cpo = (char_u *)"";
19821
19822 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019823 if (argvars[1].v_type != VAR_UNKNOWN)
19824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019825 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19826 if (pat == NULL)
19827 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019828 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019829 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019830 }
19831 if (pat == NULL || *pat == NUL)
19832 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019833
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019834 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019836 if (typeerr)
19837 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838
Bram Moolenaar0d660222005-01-07 21:51:51 +000019839 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19840 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019842 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019843 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019844 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019845 if (*str == NUL)
19846 match = FALSE; /* empty item at the end */
19847 else
19848 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019849 if (match)
19850 end = regmatch.startp[0];
19851 else
19852 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019853 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19854 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019855 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019856 if (list_append_string(rettv->vval.v_list, str,
19857 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019858 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019859 }
19860 if (!match)
19861 break;
19862 /* Advance to just after the match. */
19863 if (regmatch.endp[0] > str)
19864 col = 0;
19865 else
19866 {
19867 /* Don't get stuck at the same match. */
19868#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019869 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019870#else
19871 col = 1;
19872#endif
19873 }
19874 str = regmatch.endp[0];
19875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876
Bram Moolenaar473de612013-06-08 18:19:48 +020019877 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879
Bram Moolenaar0d660222005-01-07 21:51:51 +000019880 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881}
19882
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019883#ifdef FEAT_FLOAT
19884/*
19885 * "sqrt()" function
19886 */
19887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019888f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019889{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019890 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019891
19892 rettv->v_type = VAR_FLOAT;
19893 if (get_float_arg(argvars, &f) == OK)
19894 rettv->vval.v_float = sqrt(f);
19895 else
19896 rettv->vval.v_float = 0.0;
19897}
19898
19899/*
19900 * "str2float()" function
19901 */
19902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019903f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019904{
19905 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19906
19907 if (*p == '+')
19908 p = skipwhite(p + 1);
19909 (void)string2float(p, &rettv->vval.v_float);
19910 rettv->v_type = VAR_FLOAT;
19911}
19912#endif
19913
Bram Moolenaar2c932302006-03-18 21:42:09 +000019914/*
19915 * "str2nr()" function
19916 */
19917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019918f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019919{
19920 int base = 10;
19921 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019922 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019923 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019924
19925 if (argvars[1].v_type != VAR_UNKNOWN)
19926 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019927 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019928 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019929 {
19930 EMSG(_(e_invarg));
19931 return;
19932 }
19933 }
19934
19935 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019936 if (*p == '+')
19937 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019938 switch (base)
19939 {
19940 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19941 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19942 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19943 default: what = 0;
19944 }
19945 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019946 rettv->vval.v_number = n;
19947}
19948
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949#ifdef HAVE_STRFTIME
19950/*
19951 * "strftime({format}[, {time}])" function
19952 */
19953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019954f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955{
19956 char_u result_buf[256];
19957 struct tm *curtime;
19958 time_t seconds;
19959 char_u *p;
19960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019961 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019963 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019964 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 seconds = time(NULL);
19966 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019967 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 curtime = localtime(&seconds);
19969 /* MSVC returns NULL for an invalid value of seconds. */
19970 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 else
19973 {
19974# ifdef FEAT_MBYTE
19975 vimconv_T conv;
19976 char_u *enc;
19977
19978 conv.vc_type = CONV_NONE;
19979 enc = enc_locale();
19980 convert_setup(&conv, p_enc, enc);
19981 if (conv.vc_type != CONV_NONE)
19982 p = string_convert(&conv, p, NULL);
19983# endif
19984 if (p != NULL)
19985 (void)strftime((char *)result_buf, sizeof(result_buf),
19986 (char *)p, curtime);
19987 else
19988 result_buf[0] = NUL;
19989
19990# ifdef FEAT_MBYTE
19991 if (conv.vc_type != CONV_NONE)
19992 vim_free(p);
19993 convert_setup(&conv, enc, p_enc);
19994 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019995 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 else
19997# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019998 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999
20000# ifdef FEAT_MBYTE
20001 /* Release conversion descriptors */
20002 convert_setup(&conv, NULL, NULL);
20003 vim_free(enc);
20004# endif
20005 }
20006}
20007#endif
20008
20009/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020010 * "strgetchar()" function
20011 */
20012 static void
20013f_strgetchar(typval_T *argvars, typval_T *rettv)
20014{
20015 char_u *str;
20016 int len;
20017 int error = FALSE;
20018 int charidx;
20019
20020 rettv->vval.v_number = -1;
20021 str = get_tv_string_chk(&argvars[0]);
20022 if (str == NULL)
20023 return;
20024 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020025 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020026 if (error)
20027 return;
20028#ifdef FEAT_MBYTE
20029 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020020030 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020031
20032 while (charidx >= 0 && byteidx < len)
20033 {
20034 if (charidx == 0)
20035 {
20036 rettv->vval.v_number = mb_ptr2char(str + byteidx);
20037 break;
20038 }
20039 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020020040 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020041 }
20042 }
20043#else
20044 if (charidx < len)
20045 rettv->vval.v_number = str[charidx];
20046#endif
20047}
20048
20049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 * "stridx()" function
20051 */
20052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020053f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054{
20055 char_u buf[NUMBUFLEN];
20056 char_u *needle;
20057 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000020058 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020062 needle = get_tv_string_chk(&argvars[1]);
20063 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000020064 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020065 if (needle == NULL || haystack == NULL)
20066 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067
Bram Moolenaar33570922005-01-25 22:26:29 +000020068 if (argvars[2].v_type != VAR_UNKNOWN)
20069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020070 int error = FALSE;
20071
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020072 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020073 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000020074 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020075 if (start_idx >= 0)
20076 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 }
20078
20079 pos = (char_u *)strstr((char *)haystack, (char *)needle);
20080 if (pos != NULL)
20081 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082}
20083
20084/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020085 * "string()" function
20086 */
20087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020088f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020089{
20090 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020091 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020093 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010020094 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
20095 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020096 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020097 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099}
20100
20101/*
20102 * "strlen()" function
20103 */
20104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020105f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020107 rettv->vval.v_number = (varnumber_T)(STRLEN(
20108 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109}
20110
20111/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020112 * "strchars()" function
20113 */
20114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020115f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020116{
20117 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020118 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020020119#ifdef FEAT_MBYTE
20120 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020121 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020020122#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020123
20124 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020125 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020126 if (skipcc < 0 || skipcc > 1)
20127 EMSG(_(e_invarg));
20128 else
20129 {
20130#ifdef FEAT_MBYTE
20131 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
20132 while (*s != NUL)
20133 {
20134 func_mb_ptr2char_adv(&s);
20135 ++len;
20136 }
20137 rettv->vval.v_number = len;
20138#else
20139 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
20140#endif
20141 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020020142}
20143
20144/*
Bram Moolenaardc536092010-07-18 15:45:49 +020020145 * "strdisplaywidth()" function
20146 */
20147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020148f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020020149{
20150 char_u *s = get_tv_string(&argvars[0]);
20151 int col = 0;
20152
20153 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020154 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020020155
Bram Moolenaar8a09b982010-07-22 22:20:57 +020020156 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020020157}
20158
20159/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020160 * "strwidth()" function
20161 */
20162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020163f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020164{
20165 char_u *s = get_tv_string(&argvars[0]);
20166
20167 rettv->vval.v_number = (varnumber_T)(
20168#ifdef FEAT_MBYTE
20169 mb_string2cells(s, -1)
20170#else
20171 STRLEN(s)
20172#endif
20173 );
20174}
20175
20176/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020177 * "strcharpart()" function
20178 */
20179 static void
20180f_strcharpart(typval_T *argvars, typval_T *rettv)
20181{
20182#ifdef FEAT_MBYTE
20183 char_u *p;
20184 int nchar;
20185 int nbyte = 0;
20186 int charlen;
20187 int len = 0;
20188 int slen;
20189 int error = FALSE;
20190
20191 p = get_tv_string(&argvars[0]);
20192 slen = (int)STRLEN(p);
20193
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020194 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020195 if (!error)
20196 {
20197 if (nchar > 0)
20198 while (nchar > 0 && nbyte < slen)
20199 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020020200 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020201 --nchar;
20202 }
20203 else
20204 nbyte = nchar;
20205 if (argvars[2].v_type != VAR_UNKNOWN)
20206 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020207 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020208 while (charlen > 0 && nbyte + len < slen)
20209 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020020210 int off = nbyte + len;
20211
20212 if (off < 0)
20213 len += 1;
20214 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020020215 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020216 --charlen;
20217 }
20218 }
20219 else
20220 len = slen - nbyte; /* default: all bytes that are available. */
20221 }
20222
20223 /*
20224 * Only return the overlap between the specified part and the actual
20225 * string.
20226 */
20227 if (nbyte < 0)
20228 {
20229 len += nbyte;
20230 nbyte = 0;
20231 }
20232 else if (nbyte > slen)
20233 nbyte = slen;
20234 if (len < 0)
20235 len = 0;
20236 else if (nbyte + len > slen)
20237 len = slen - nbyte;
20238
20239 rettv->v_type = VAR_STRING;
20240 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
20241#else
20242 f_strpart(argvars, rettv);
20243#endif
20244}
20245
20246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 * "strpart()" function
20248 */
20249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020250f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251{
20252 char_u *p;
20253 int n;
20254 int len;
20255 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020256 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020258 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259 slen = (int)STRLEN(p);
20260
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020261 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020262 if (error)
20263 len = 0;
20264 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020265 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 else
20267 len = slen - n; /* default len: all bytes that are available. */
20268
20269 /*
20270 * Only return the overlap between the specified part and the actual
20271 * string.
20272 */
20273 if (n < 0)
20274 {
20275 len += n;
20276 n = 0;
20277 }
20278 else if (n > slen)
20279 n = slen;
20280 if (len < 0)
20281 len = 0;
20282 else if (n + len > slen)
20283 len = slen - n;
20284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020285 rettv->v_type = VAR_STRING;
20286 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287}
20288
20289/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020290 * "strridx()" function
20291 */
20292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020293f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020294{
20295 char_u buf[NUMBUFLEN];
20296 char_u *needle;
20297 char_u *haystack;
20298 char_u *rest;
20299 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020300 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020302 needle = get_tv_string_chk(&argvars[1]);
20303 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020304
20305 rettv->vval.v_number = -1;
20306 if (needle == NULL || haystack == NULL)
20307 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020308
20309 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020310 if (argvars[2].v_type != VAR_UNKNOWN)
20311 {
20312 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020313 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020314 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020315 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020316 }
20317 else
20318 end_idx = haystack_len;
20319
Bram Moolenaar0d660222005-01-07 21:51:51 +000020320 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020321 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020322 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020323 lastmatch = haystack + end_idx;
20324 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020325 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020326 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020327 for (rest = haystack; *rest != '\0'; ++rest)
20328 {
20329 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020330 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020331 break;
20332 lastmatch = rest;
20333 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020334 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020335
20336 if (lastmatch == NULL)
20337 rettv->vval.v_number = -1;
20338 else
20339 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20340}
20341
20342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 * "strtrans()" function
20344 */
20345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020346f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020348 rettv->v_type = VAR_STRING;
20349 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350}
20351
20352/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020353 * "submatch()" function
20354 */
20355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020356f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020357{
Bram Moolenaar41571762014-04-02 19:00:58 +020020358 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020359 int no;
20360 int retList = 0;
20361
20362 no = (int)get_tv_number_chk(&argvars[0], &error);
20363 if (error)
20364 return;
20365 error = FALSE;
20366 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020367 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020020368 if (error)
20369 return;
20370
20371 if (retList == 0)
20372 {
20373 rettv->v_type = VAR_STRING;
20374 rettv->vval.v_string = reg_submatch(no);
20375 }
20376 else
20377 {
20378 rettv->v_type = VAR_LIST;
20379 rettv->vval.v_list = reg_submatch_list(no);
20380 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020381}
20382
20383/*
20384 * "substitute()" function
20385 */
20386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020387f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020388{
20389 char_u patbuf[NUMBUFLEN];
20390 char_u subbuf[NUMBUFLEN];
20391 char_u flagsbuf[NUMBUFLEN];
20392
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020393 char_u *str = get_tv_string_chk(&argvars[0]);
20394 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20395 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20396 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20397
Bram Moolenaar0d660222005-01-07 21:51:51 +000020398 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020399 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20400 rettv->vval.v_string = NULL;
20401 else
20402 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020403}
20404
20405/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020406 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020409f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410{
20411 int id = 0;
20412#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020413 linenr_T lnum;
20414 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020416 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020418 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020419 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20420 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020422 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020423 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020424 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425#endif
20426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020427 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428}
20429
20430/*
20431 * "synIDattr(id, what [, mode])" function
20432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020434f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435{
20436 char_u *p = NULL;
20437#ifdef FEAT_SYN_HL
20438 int id;
20439 char_u *what;
20440 char_u *mode;
20441 char_u modebuf[NUMBUFLEN];
20442 int modec;
20443
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020444 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020445 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020446 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020448 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020450 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 modec = 0; /* replace invalid with current */
20452 }
20453 else
20454 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020455#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020456 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 modec = 'g';
20458 else
20459#endif
20460 if (t_colors > 1)
20461 modec = 'c';
20462 else
20463 modec = 't';
20464 }
20465
20466
20467 switch (TOLOWER_ASC(what[0]))
20468 {
20469 case 'b':
20470 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20471 p = highlight_color(id, what, modec);
20472 else /* bold */
20473 p = highlight_has_attr(id, HL_BOLD, modec);
20474 break;
20475
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020476 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477 p = highlight_color(id, what, modec);
20478 break;
20479
20480 case 'i':
20481 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20482 p = highlight_has_attr(id, HL_INVERSE, modec);
20483 else /* italic */
20484 p = highlight_has_attr(id, HL_ITALIC, modec);
20485 break;
20486
20487 case 'n': /* name */
20488 p = get_highlight_name(NULL, id - 1);
20489 break;
20490
20491 case 'r': /* reverse */
20492 p = highlight_has_attr(id, HL_INVERSE, modec);
20493 break;
20494
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020495 case 's':
20496 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20497 p = highlight_color(id, what, modec);
20498 else /* standout */
20499 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500 break;
20501
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020502 case 'u':
20503 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20504 /* underline */
20505 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20506 else
20507 /* undercurl */
20508 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509 break;
20510 }
20511
20512 if (p != NULL)
20513 p = vim_strsave(p);
20514#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020515 rettv->v_type = VAR_STRING;
20516 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517}
20518
20519/*
20520 * "synIDtrans(id)" function
20521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020523f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524{
20525 int id;
20526
20527#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020528 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529
20530 if (id > 0)
20531 id = syn_get_final_id(id);
20532 else
20533#endif
20534 id = 0;
20535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020536 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537}
20538
20539/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020540 * "synconcealed(lnum, col)" function
20541 */
20542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020543f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020544{
20545#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020546 linenr_T lnum;
20547 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020548 int syntax_flags = 0;
20549 int cchar;
20550 int matchid = 0;
20551 char_u str[NUMBUFLEN];
20552#endif
20553
20554 rettv->v_type = VAR_LIST;
20555 rettv->vval.v_list = NULL;
20556
20557#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20558 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020559 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020560
20561 vim_memset(str, NUL, sizeof(str));
20562
20563 if (rettv_list_alloc(rettv) != FAIL)
20564 {
20565 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20566 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20567 && curwin->w_p_cole > 0)
20568 {
20569 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20570 syntax_flags = get_syntax_info(&matchid);
20571
20572 /* get the conceal character */
20573 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20574 {
20575 cchar = syn_get_sub_char();
20576 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20577 cchar = lcs_conceal;
20578 if (cchar != NUL)
20579 {
20580# ifdef FEAT_MBYTE
20581 if (has_mbyte)
20582 (*mb_char2bytes)(cchar, str);
20583 else
20584# endif
20585 str[0] = cchar;
20586 }
20587 }
20588 }
20589
20590 list_append_number(rettv->vval.v_list,
20591 (syntax_flags & HL_CONCEAL) != 0);
20592 /* -1 to auto-determine strlen */
20593 list_append_string(rettv->vval.v_list, str, -1);
20594 list_append_number(rettv->vval.v_list, matchid);
20595 }
20596#endif
20597}
20598
20599/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020600 * "synstack(lnum, col)" function
20601 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020603f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020604{
20605#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020606 linenr_T lnum;
20607 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020608 int i;
20609 int id;
20610#endif
20611
20612 rettv->v_type = VAR_LIST;
20613 rettv->vval.v_list = NULL;
20614
20615#ifdef FEAT_SYN_HL
20616 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020617 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020618
20619 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020620 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020621 && rettv_list_alloc(rettv) != FAIL)
20622 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020623 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020624 for (i = 0; ; ++i)
20625 {
20626 id = syn_get_stack_item(i);
20627 if (id < 0)
20628 break;
20629 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20630 break;
20631 }
20632 }
20633#endif
20634}
20635
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020637get_cmd_output_as_rettv(
20638 typval_T *argvars,
20639 typval_T *rettv,
20640 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020642 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020644 char_u *infile = NULL;
20645 char_u buf[NUMBUFLEN];
20646 int err = FALSE;
20647 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020648 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020649 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020651 rettv->v_type = VAR_STRING;
20652 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020653 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020654 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020655
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020656 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020657 {
20658 /*
20659 * Write the string to a temp file, to be used for input of the shell
20660 * command.
20661 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020662 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020663 {
20664 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020665 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020666 }
20667
20668 fd = mch_fopen((char *)infile, WRITEBIN);
20669 if (fd == NULL)
20670 {
20671 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020672 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020673 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020674 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020675 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020676 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20677 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020678 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020679 else
20680 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020681 size_t len;
20682
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020683 p = get_tv_string_buf_chk(&argvars[1], buf);
20684 if (p == NULL)
20685 {
20686 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020687 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020688 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020689 len = STRLEN(p);
20690 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020691 err = TRUE;
20692 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020693 if (fclose(fd) != 0)
20694 err = TRUE;
20695 if (err)
20696 {
20697 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020698 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020699 }
20700 }
20701
Bram Moolenaar52a72462014-08-29 15:53:52 +020020702 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20703 * echoes typeahead, that messes up the display. */
20704 if (!msg_silent)
20705 flags += SHELL_COOKED;
20706
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020707 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020709 int len;
20710 listitem_T *li;
20711 char_u *s = NULL;
20712 char_u *start;
20713 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020714 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715
Bram Moolenaar52a72462014-08-29 15:53:52 +020020716 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020717 if (res == NULL)
20718 goto errret;
20719
20720 list = list_alloc();
20721 if (list == NULL)
20722 goto errret;
20723
20724 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020726 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020727 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020728 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020729 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020730
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020731 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020732 if (s == NULL)
20733 goto errret;
20734
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020735 for (p = s; start < end; ++p, ++start)
20736 *p = *start == NUL ? NL : *start;
20737 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020738
20739 li = listitem_alloc();
20740 if (li == NULL)
20741 {
20742 vim_free(s);
20743 goto errret;
20744 }
20745 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020746 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020747 li->li_tv.vval.v_string = s;
20748 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020750
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020751 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020752 rettv->v_type = VAR_LIST;
20753 rettv->vval.v_list = list;
20754 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020756 else
20757 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020758 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020759#ifdef USE_CR
20760 /* translate <CR> into <NL> */
20761 if (res != NULL)
20762 {
20763 char_u *s;
20764
20765 for (s = res; *s; ++s)
20766 {
20767 if (*s == CAR)
20768 *s = NL;
20769 }
20770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771#else
20772# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020773 /* translate <CR><NL> into <NL> */
20774 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020776 char_u *s, *d;
20777
20778 d = res;
20779 for (s = res; *s; ++s)
20780 {
20781 if (s[0] == CAR && s[1] == NL)
20782 ++s;
20783 *d++ = *s;
20784 }
20785 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787# endif
20788#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020789 rettv->vval.v_string = res;
20790 res = NULL;
20791 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020792
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020793errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020794 if (infile != NULL)
20795 {
20796 mch_remove(infile);
20797 vim_free(infile);
20798 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020799 if (res != NULL)
20800 vim_free(res);
20801 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020802 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020803}
20804
20805/*
20806 * "system()" function
20807 */
20808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020809f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020810{
20811 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20812}
20813
20814/*
20815 * "systemlist()" function
20816 */
20817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020818f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020819{
20820 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821}
20822
20823/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020824 * "tabpagebuflist()" function
20825 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020827f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020828{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020829#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020830 tabpage_T *tp;
20831 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020832
20833 if (argvars[0].v_type == VAR_UNKNOWN)
20834 wp = firstwin;
20835 else
20836 {
20837 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20838 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020839 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020840 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020841 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020842 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020843 for (; wp != NULL; wp = wp->w_next)
20844 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020845 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020846 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020847 }
20848#endif
20849}
20850
20851
20852/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020853 * "tabpagenr()" function
20854 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020856f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020857{
20858 int nr = 1;
20859#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020860 char_u *arg;
20861
20862 if (argvars[0].v_type != VAR_UNKNOWN)
20863 {
20864 arg = get_tv_string_chk(&argvars[0]);
20865 nr = 0;
20866 if (arg != NULL)
20867 {
20868 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020869 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020870 else
20871 EMSG2(_(e_invexpr2), arg);
20872 }
20873 }
20874 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020875 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020876#endif
20877 rettv->vval.v_number = nr;
20878}
20879
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020880
20881#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020882static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020883
20884/*
20885 * Common code for tabpagewinnr() and winnr().
20886 */
20887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020888get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020889{
20890 win_T *twin;
20891 int nr = 1;
20892 win_T *wp;
20893 char_u *arg;
20894
20895 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20896 if (argvar->v_type != VAR_UNKNOWN)
20897 {
20898 arg = get_tv_string_chk(argvar);
20899 if (arg == NULL)
20900 nr = 0; /* type error; errmsg already given */
20901 else if (STRCMP(arg, "$") == 0)
20902 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20903 else if (STRCMP(arg, "#") == 0)
20904 {
20905 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20906 if (twin == NULL)
20907 nr = 0;
20908 }
20909 else
20910 {
20911 EMSG2(_(e_invexpr2), arg);
20912 nr = 0;
20913 }
20914 }
20915
20916 if (nr > 0)
20917 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20918 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020919 {
20920 if (wp == NULL)
20921 {
20922 /* didn't find it in this tabpage */
20923 nr = 0;
20924 break;
20925 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020926 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020927 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020928 return nr;
20929}
20930#endif
20931
20932/*
20933 * "tabpagewinnr()" function
20934 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020936f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020937{
20938 int nr = 1;
20939#ifdef FEAT_WINDOWS
20940 tabpage_T *tp;
20941
20942 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20943 if (tp == NULL)
20944 nr = 0;
20945 else
20946 nr = get_winnr(tp, &argvars[1]);
20947#endif
20948 rettv->vval.v_number = nr;
20949}
20950
20951
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020952/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020953 * "tagfiles()" function
20954 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020956f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020957{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020958 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020959 tagname_T tn;
20960 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020961
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020962 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020963 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020964 fname = alloc(MAXPATHL);
20965 if (fname == NULL)
20966 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020967
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020968 for (first = TRUE; ; first = FALSE)
20969 if (get_tagfname(&tn, first, fname) == FAIL
20970 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020971 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020972 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020973 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020974}
20975
20976/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020977 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020978 */
20979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020980f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020981{
20982 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020983
20984 tag_pattern = get_tv_string(&argvars[0]);
20985
20986 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020987 if (*tag_pattern == NUL)
20988 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020989
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020990 if (rettv_list_alloc(rettv) == OK)
20991 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020992}
20993
20994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 * "tempname()" function
20996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020998f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999{
21000 static int x = 'A';
21001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021002 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020021003 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004
21005 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
21006 * names. Skip 'I' and 'O', they are used for shell redirection. */
21007 do
21008 {
21009 if (x == 'Z')
21010 x = '0';
21011 else if (x == '9')
21012 x = 'A';
21013 else
21014 {
21015#ifdef EBCDIC
21016 if (x == 'I')
21017 x = 'J';
21018 else if (x == 'R')
21019 x = 'S';
21020 else
21021#endif
21022 ++x;
21023 }
21024 } while (x == 'I' || x == 'O');
21025}
21026
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021027#ifdef FEAT_FLOAT
21028/*
21029 * "tan()" function
21030 */
21031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021032f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021033{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021034 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021035
21036 rettv->v_type = VAR_FLOAT;
21037 if (get_float_arg(argvars, &f) == OK)
21038 rettv->vval.v_float = tan(f);
21039 else
21040 rettv->vval.v_float = 0.0;
21041}
21042
21043/*
21044 * "tanh()" function
21045 */
21046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021047f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021048{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021049 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021050
21051 rettv->v_type = VAR_FLOAT;
21052 if (get_float_arg(argvars, &f) == OK)
21053 rettv->vval.v_float = tanh(f);
21054 else
21055 rettv->vval.v_float = 0.0;
21056}
21057#endif
21058
Bram Moolenaar574860b2016-05-24 17:33:34 +020021059/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021060 * "test_alloc_fail(id, countdown, repeat)" function
21061 */
21062 static void
21063f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
21064{
21065 if (argvars[0].v_type != VAR_NUMBER
21066 || argvars[0].vval.v_number <= 0
21067 || argvars[1].v_type != VAR_NUMBER
21068 || argvars[1].vval.v_number < 0
21069 || argvars[2].v_type != VAR_NUMBER)
21070 EMSG(_(e_invarg));
21071 else
21072 {
21073 alloc_fail_id = argvars[0].vval.v_number;
21074 if (alloc_fail_id >= aid_last)
21075 EMSG(_(e_invarg));
21076 alloc_fail_countdown = argvars[1].vval.v_number;
21077 alloc_fail_repeat = argvars[2].vval.v_number;
21078 did_outofmem_msg = FALSE;
21079 }
21080}
21081
21082/*
Bram Moolenaar5c719942016-07-09 23:40:45 +020021083 * "test_autochdir()"
21084 */
21085 static void
21086f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
21087{
21088#if defined(FEAT_AUTOCHDIR)
21089 test_autochdir = TRUE;
21090#endif
21091}
21092
21093/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021094 * "test_disable_char_avail({expr})" function
21095 */
21096 static void
21097f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
21098{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021099 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021100}
21101
21102/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020021103 * "test_garbagecollect_now()" function
21104 */
21105 static void
21106f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
21107{
21108 /* This is dangerous, any Lists and Dicts used internally may be freed
21109 * while still in use. */
21110 garbage_collect(TRUE);
21111}
21112
21113#ifdef FEAT_JOB_CHANNEL
21114 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021115f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021116{
21117 rettv->v_type = VAR_CHANNEL;
21118 rettv->vval.v_channel = NULL;
21119}
21120#endif
21121
21122 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021123f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021124{
21125 rettv->v_type = VAR_DICT;
21126 rettv->vval.v_dict = NULL;
21127}
21128
21129#ifdef FEAT_JOB_CHANNEL
21130 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021131f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021132{
21133 rettv->v_type = VAR_JOB;
21134 rettv->vval.v_job = NULL;
21135}
21136#endif
21137
21138 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021139f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021140{
21141 rettv->v_type = VAR_LIST;
21142 rettv->vval.v_list = NULL;
21143}
21144
21145 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021146f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021147{
21148 rettv->v_type = VAR_PARTIAL;
21149 rettv->vval.v_partial = NULL;
21150}
21151
21152 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021153f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021154{
21155 rettv->v_type = VAR_STRING;
21156 rettv->vval.v_string = NULL;
21157}
21158
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021159 static void
21160f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
21161{
21162 time_for_testing = (time_t)get_tv_number(&argvars[0]);
21163}
21164
Bram Moolenaar975b5272016-03-15 23:10:59 +010021165#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
21166/*
21167 * Get a callback from "arg". It can be a Funcref or a function name.
21168 * When "arg" is zero return an empty string.
21169 * Return NULL for an invalid argument.
21170 */
21171 char_u *
21172get_callback(typval_T *arg, partial_T **pp)
21173{
21174 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
21175 {
21176 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010021177 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021178 return (*pp)->pt_name;
21179 }
21180 *pp = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +020021181 if (arg->v_type == VAR_FUNC)
21182 {
21183 func_ref(arg->vval.v_string);
21184 return arg->vval.v_string;
21185 }
21186 if (arg->v_type == VAR_STRING)
Bram Moolenaar975b5272016-03-15 23:10:59 +010021187 return arg->vval.v_string;
21188 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
21189 return (char_u *)"";
21190 EMSG(_("E921: Invalid callback argument"));
21191 return NULL;
21192}
Bram Moolenaar1436d8d2016-07-11 22:41:15 +020021193
21194/*
21195 * Unref/free "callback" and "partial" retured by get_callback().
21196 */
21197 void
21198free_callback(char_u *callback, partial_T *partial)
21199{
21200 if (partial != NULL)
21201 partial_unref(partial);
21202 else if (callback != NULL)
21203 {
21204 func_unref(callback);
21205 vim_free(callback);
21206 }
21207}
Bram Moolenaar975b5272016-03-15 23:10:59 +010021208#endif
21209
21210#ifdef FEAT_TIMERS
21211/*
21212 * "timer_start(time, callback [, options])" function
21213 */
21214 static void
21215f_timer_start(typval_T *argvars, typval_T *rettv)
21216{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021217 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010021218 timer_T *timer;
21219 int repeat = 0;
21220 char_u *callback;
21221 dict_T *dict;
21222
Bram Moolenaar38499922016-04-22 20:46:52 +020021223 if (check_secure())
21224 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021225 if (argvars[2].v_type != VAR_UNKNOWN)
21226 {
21227 if (argvars[2].v_type != VAR_DICT
21228 || (dict = argvars[2].vval.v_dict) == NULL)
21229 {
21230 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
21231 return;
21232 }
21233 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
21234 repeat = get_dict_number(dict, (char_u *)"repeat");
21235 }
21236
21237 timer = create_timer(msec, repeat);
21238 callback = get_callback(&argvars[1], &timer->tr_partial);
21239 if (callback == NULL)
21240 {
21241 stop_timer(timer);
21242 rettv->vval.v_number = -1;
21243 }
21244 else
21245 {
21246 timer->tr_callback = vim_strsave(callback);
21247 rettv->vval.v_number = timer->tr_id;
21248 }
21249}
21250
21251/*
21252 * "timer_stop(timer)" function
21253 */
21254 static void
21255f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
21256{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021257 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021258
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021259 if (argvars[0].v_type != VAR_NUMBER)
21260 {
Bram Moolenaared59aa62016-07-09 17:41:12 +020021261 EMSG(_(e_number_exp));
21262 return;
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021263 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021264 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010021265 if (timer != NULL)
21266 stop_timer(timer);
21267}
21268#endif
21269
Bram Moolenaard52d9742005-08-21 22:20:28 +000021270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 * "tolower(string)" function
21272 */
21273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021274f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275{
21276 char_u *p;
21277
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021278 p = vim_strsave(get_tv_string(&argvars[0]));
21279 rettv->v_type = VAR_STRING;
21280 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281
21282 if (p != NULL)
21283 while (*p != NUL)
21284 {
21285#ifdef FEAT_MBYTE
21286 int l;
21287
21288 if (enc_utf8)
21289 {
21290 int c, lc;
21291
21292 c = utf_ptr2char(p);
21293 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021294 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 /* TODO: reallocate string when byte count changes. */
21296 if (utf_char2len(lc) == l)
21297 utf_char2bytes(lc, p);
21298 p += l;
21299 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021300 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 p += l; /* skip multi-byte character */
21302 else
21303#endif
21304 {
21305 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21306 ++p;
21307 }
21308 }
21309}
21310
21311/*
21312 * "toupper(string)" function
21313 */
21314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021315f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021317 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021318 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319}
21320
21321/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021322 * "tr(string, fromstr, tostr)" function
21323 */
21324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021325f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021326{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021327 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021328 char_u *fromstr;
21329 char_u *tostr;
21330 char_u *p;
21331#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021332 int inlen;
21333 int fromlen;
21334 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021335 int idx;
21336 char_u *cpstr;
21337 int cplen;
21338 int first = TRUE;
21339#endif
21340 char_u buf[NUMBUFLEN];
21341 char_u buf2[NUMBUFLEN];
21342 garray_T ga;
21343
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021344 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021345 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21346 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021347
21348 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021349 rettv->v_type = VAR_STRING;
21350 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021351 if (fromstr == NULL || tostr == NULL)
21352 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021353 ga_init2(&ga, (int)sizeof(char), 80);
21354
21355#ifdef FEAT_MBYTE
21356 if (!has_mbyte)
21357#endif
21358 /* not multi-byte: fromstr and tostr must be the same length */
21359 if (STRLEN(fromstr) != STRLEN(tostr))
21360 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021361#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021362error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021363#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021364 EMSG2(_(e_invarg2), fromstr);
21365 ga_clear(&ga);
21366 return;
21367 }
21368
21369 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021370 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021371 {
21372#ifdef FEAT_MBYTE
21373 if (has_mbyte)
21374 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021375 inlen = (*mb_ptr2len)(in_str);
21376 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021377 cplen = inlen;
21378 idx = 0;
21379 for (p = fromstr; *p != NUL; p += fromlen)
21380 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021381 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021382 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021383 {
21384 for (p = tostr; *p != NUL; p += tolen)
21385 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021386 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021387 if (idx-- == 0)
21388 {
21389 cplen = tolen;
21390 cpstr = p;
21391 break;
21392 }
21393 }
21394 if (*p == NUL) /* tostr is shorter than fromstr */
21395 goto error;
21396 break;
21397 }
21398 ++idx;
21399 }
21400
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021401 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021402 {
21403 /* Check that fromstr and tostr have the same number of
21404 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021405 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021406 first = FALSE;
21407 for (p = tostr; *p != NUL; p += tolen)
21408 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021409 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021410 --idx;
21411 }
21412 if (idx != 0)
21413 goto error;
21414 }
21415
Bram Moolenaarcde88542015-08-11 19:14:00 +020021416 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021417 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021418 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021419
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021420 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021421 }
21422 else
21423#endif
21424 {
21425 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021426 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021427 if (p != NULL)
21428 ga_append(&ga, tostr[p - fromstr]);
21429 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021430 ga_append(&ga, *in_str);
21431 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021432 }
21433 }
21434
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021435 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021436 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021437 ga_append(&ga, NUL);
21438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021439 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021440}
21441
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021442#ifdef FEAT_FLOAT
21443/*
21444 * "trunc({float})" function
21445 */
21446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021447f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021448{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021449 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021450
21451 rettv->v_type = VAR_FLOAT;
21452 if (get_float_arg(argvars, &f) == OK)
21453 /* trunc() is not in C90, use floor() or ceil() instead. */
21454 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21455 else
21456 rettv->vval.v_float = 0.0;
21457}
21458#endif
21459
Bram Moolenaar8299df92004-07-10 09:47:34 +000021460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 * "type(expr)" function
21462 */
21463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021464f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021466 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021467
21468 switch (argvars[0].v_type)
21469 {
21470 case VAR_NUMBER: n = 0; break;
21471 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021472 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021473 case VAR_FUNC: n = 2; break;
21474 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021475 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021476 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021477 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021478 if (argvars[0].vval.v_number == VVAL_FALSE
21479 || argvars[0].vval.v_number == VVAL_TRUE)
21480 n = 6;
21481 else
21482 n = 7;
21483 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021484 case VAR_JOB: n = 8; break;
21485 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021486 case VAR_UNKNOWN:
21487 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21488 n = -1;
21489 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021490 }
21491 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492}
21493
21494/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021495 * "undofile(name)" function
21496 */
21497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021498f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021499{
21500 rettv->v_type = VAR_STRING;
21501#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021502 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021503 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021504
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021505 if (*fname == NUL)
21506 {
21507 /* If there is no file name there will be no undo file. */
21508 rettv->vval.v_string = NULL;
21509 }
21510 else
21511 {
21512 char_u *ffname = FullName_save(fname, FALSE);
21513
21514 if (ffname != NULL)
21515 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21516 vim_free(ffname);
21517 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021518 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021519#else
21520 rettv->vval.v_string = NULL;
21521#endif
21522}
21523
21524/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021525 * "undotree()" function
21526 */
21527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021528f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021529{
21530 if (rettv_dict_alloc(rettv) == OK)
21531 {
21532 dict_T *dict = rettv->vval.v_dict;
21533 list_T *list;
21534
Bram Moolenaar730cde92010-06-27 05:18:54 +020021535 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021536 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021537 dict_add_nr_str(dict, "save_last",
21538 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021539 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21540 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021541 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021542
21543 list = list_alloc();
21544 if (list != NULL)
21545 {
21546 u_eval_tree(curbuf->b_u_oldhead, list);
21547 dict_add_list(dict, "entries", list);
21548 }
21549 }
21550}
21551
21552/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021553 * "values(dict)" function
21554 */
21555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021556f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021557{
21558 dict_list(argvars, rettv, 1);
21559}
21560
21561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562 * "virtcol(string)" function
21563 */
21564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021565f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566{
21567 colnr_T vcol = 0;
21568 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021569 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021571 fp = var2fpos(&argvars[0], FALSE, &fnum);
21572 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21573 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 {
21575 getvvcol(curwin, fp, NULL, NULL, &vcol);
21576 ++vcol;
21577 }
21578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021579 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580}
21581
21582/*
21583 * "visualmode()" function
21584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021586f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 char_u str[2];
21589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021590 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 str[0] = curbuf->b_visual_mode_eval;
21592 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021593 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594
21595 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021596 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598}
21599
21600/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021601 * "wildmenumode()" function
21602 */
21603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021604f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021605{
21606#ifdef FEAT_WILDMENU
21607 if (wild_menu_showing)
21608 rettv->vval.v_number = 1;
21609#endif
21610}
21611
21612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 * "winbufnr(nr)" function
21614 */
21615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021616f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617{
21618 win_T *wp;
21619
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021620 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021624 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625}
21626
21627/*
21628 * "wincol()" function
21629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021631f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632{
21633 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021634 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635}
21636
21637/*
21638 * "winheight(nr)" function
21639 */
21640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021641f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642{
21643 win_T *wp;
21644
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021645 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021647 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021649 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650}
21651
21652/*
21653 * "winline()" function
21654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021656f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657{
21658 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021659 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660}
21661
21662/*
21663 * "winnr()" function
21664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021666f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667{
21668 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021669
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021671 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021673 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674}
21675
21676/*
21677 * "winrestcmd()" function
21678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021680f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681{
21682#ifdef FEAT_WINDOWS
21683 win_T *wp;
21684 int winnr = 1;
21685 garray_T ga;
21686 char_u buf[50];
21687
21688 ga_init2(&ga, (int)sizeof(char), 70);
21689 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21690 {
21691 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21692 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21694 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 ++winnr;
21696 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021697 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021699 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021701 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021703 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704}
21705
21706/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021707 * "winrestview()" function
21708 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021710f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021711{
21712 dict_T *dict;
21713
21714 if (argvars[0].v_type != VAR_DICT
21715 || (dict = argvars[0].vval.v_dict) == NULL)
21716 EMSG(_(e_invarg));
21717 else
21718 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021719 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021720 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021721 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021722 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021723#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021724 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021725 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021726#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021727 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21728 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021729 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021730 curwin->w_set_curswant = FALSE;
21731 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021732
Bram Moolenaar82c25852014-05-28 16:47:16 +020021733 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021734 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021735#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021736 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021737 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021738#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021739 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021740 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021741 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021742 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021743
21744 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021745 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021746# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021747 win_new_width(curwin, W_WIDTH(curwin));
21748# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021749 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021750
Bram Moolenaarb851a962014-10-31 15:45:52 +010021751 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021752 curwin->w_topline = 1;
21753 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21754 curwin->w_topline = curbuf->b_ml.ml_line_count;
21755#ifdef FEAT_DIFF
21756 check_topfill(curwin, TRUE);
21757#endif
21758 }
21759}
21760
21761/*
21762 * "winsaveview()" function
21763 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021765f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021766{
21767 dict_T *dict;
21768
Bram Moolenaara800b422010-06-27 01:15:55 +020021769 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021770 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021771 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021772
21773 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21774 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21775#ifdef FEAT_VIRTUALEDIT
21776 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21777#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021778 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021779 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21780
21781 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21782#ifdef FEAT_DIFF
21783 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21784#endif
21785 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21786 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21787}
21788
21789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 * "winwidth(nr)" function
21791 */
21792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021793f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794{
21795 win_T *wp;
21796
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021797 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021799 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021801#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021802 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021804 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805#endif
21806}
21807
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021809 * "wordcount()" function
21810 */
21811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021812f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021813{
21814 if (rettv_dict_alloc(rettv) == FAIL)
21815 return;
21816 cursor_pos_info(rettv->vval.v_dict);
21817}
21818
21819/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021820 * Write list of strings to file
21821 */
21822 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021823write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021824{
21825 listitem_T *li;
21826 int c;
21827 int ret = OK;
21828 char_u *s;
21829
21830 for (li = list->lv_first; li != NULL; li = li->li_next)
21831 {
21832 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21833 {
21834 if (*s == '\n')
21835 c = putc(NUL, fd);
21836 else
21837 c = putc(*s, fd);
21838 if (c == EOF)
21839 {
21840 ret = FAIL;
21841 break;
21842 }
21843 }
21844 if (!binary || li->li_next != NULL)
21845 if (putc('\n', fd) == EOF)
21846 {
21847 ret = FAIL;
21848 break;
21849 }
21850 if (ret == FAIL)
21851 {
21852 EMSG(_(e_write));
21853 break;
21854 }
21855 }
21856 return ret;
21857}
21858
21859/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021860 * "writefile()" function
21861 */
21862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021863f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021864{
21865 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021866 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021867 char_u *fname;
21868 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021869 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021870
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021871 if (check_restricted() || check_secure())
21872 return;
21873
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021874 if (argvars[0].v_type != VAR_LIST)
21875 {
21876 EMSG2(_(e_listarg), "writefile()");
21877 return;
21878 }
21879 if (argvars[0].vval.v_list == NULL)
21880 return;
21881
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021882 if (argvars[2].v_type != VAR_UNKNOWN)
21883 {
21884 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21885 binary = TRUE;
21886 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21887 append = TRUE;
21888 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021889
21890 /* Always open the file in binary mode, library functions have a mind of
21891 * their own about CR-LF conversion. */
21892 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021893 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21894 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021895 {
21896 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21897 ret = -1;
21898 }
21899 else
21900 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021901 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21902 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021903 fclose(fd);
21904 }
21905
21906 rettv->vval.v_number = ret;
21907}
21908
21909/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021910 * "xor(expr, expr)" function
21911 */
21912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021913f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021914{
21915 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21916 ^ get_tv_number_chk(&argvars[1], NULL);
21917}
21918
21919
21920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021922 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 */
21924 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021925var2fpos(
21926 typval_T *varp,
21927 int dollar_lnum, /* TRUE when $ is last line */
21928 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021930 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021932 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933
Bram Moolenaara5525202006-03-02 22:52:09 +000021934 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021935 if (varp->v_type == VAR_LIST)
21936 {
21937 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021938 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021939 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021940 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021941
21942 l = varp->vval.v_list;
21943 if (l == NULL)
21944 return NULL;
21945
21946 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021947 pos.lnum = list_find_nr(l, 0L, &error);
21948 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021949 return NULL; /* invalid line number */
21950
21951 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021952 pos.col = list_find_nr(l, 1L, &error);
21953 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021954 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021955 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021956
21957 /* We accept "$" for the column number: last column. */
21958 li = list_find(l, 1L);
21959 if (li != NULL && li->li_tv.v_type == VAR_STRING
21960 && li->li_tv.vval.v_string != NULL
21961 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21962 pos.col = len + 1;
21963
Bram Moolenaara5525202006-03-02 22:52:09 +000021964 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021965 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021966 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021967 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021968
Bram Moolenaara5525202006-03-02 22:52:09 +000021969#ifdef FEAT_VIRTUALEDIT
21970 /* Get the virtual offset. Defaults to zero. */
21971 pos.coladd = list_find_nr(l, 2L, &error);
21972 if (error)
21973 pos.coladd = 0;
21974#endif
21975
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021976 return &pos;
21977 }
21978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021979 name = get_tv_string_chk(varp);
21980 if (name == NULL)
21981 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021982 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021984 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21985 {
21986 if (VIsual_active)
21987 return &VIsual;
21988 return &curwin->w_cursor;
21989 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021990 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021992 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21994 return NULL;
21995 return pp;
21996 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021997
21998#ifdef FEAT_VIRTUALEDIT
21999 pos.coladd = 0;
22000#endif
22001
Bram Moolenaar477933c2007-07-17 14:32:23 +000022002 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000022003 {
22004 pos.col = 0;
22005 if (name[1] == '0') /* "w0": first visible line */
22006 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000022007 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000022008 pos.lnum = curwin->w_topline;
22009 return &pos;
22010 }
22011 else if (name[1] == '$') /* "w$": last visible line */
22012 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000022013 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000022014 pos.lnum = curwin->w_botline - 1;
22015 return &pos;
22016 }
22017 }
22018 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000022020 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 {
22022 pos.lnum = curbuf->b_ml.ml_line_count;
22023 pos.col = 0;
22024 }
22025 else
22026 {
22027 pos.lnum = curwin->w_cursor.lnum;
22028 pos.col = (colnr_T)STRLEN(ml_get_curline());
22029 }
22030 return &pos;
22031 }
22032 return NULL;
22033}
22034
22035/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022036 * Convert list in "arg" into a position and optional file number.
22037 * When "fnump" is NULL there is no file number, only 3 items.
22038 * Note that the column is passed on as-is, the caller may want to decrement
22039 * it to use 1 for the first column.
22040 * Return FAIL when conversion is not possible, doesn't check the position for
22041 * validity.
22042 */
22043 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022044list2fpos(
22045 typval_T *arg,
22046 pos_T *posp,
22047 int *fnump,
22048 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022049{
22050 list_T *l = arg->vval.v_list;
22051 long i = 0;
22052 long n;
22053
Bram Moolenaar493c1782014-05-28 14:34:46 +020022054 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
22055 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000022056 if (arg->v_type != VAR_LIST
22057 || l == NULL
22058 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020022059 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022060 return FAIL;
22061
22062 if (fnump != NULL)
22063 {
22064 n = list_find_nr(l, i++, NULL); /* fnum */
22065 if (n < 0)
22066 return FAIL;
22067 if (n == 0)
22068 n = curbuf->b_fnum; /* current buffer */
22069 *fnump = n;
22070 }
22071
22072 n = list_find_nr(l, i++, NULL); /* lnum */
22073 if (n < 0)
22074 return FAIL;
22075 posp->lnum = n;
22076
22077 n = list_find_nr(l, i++, NULL); /* col */
22078 if (n < 0)
22079 return FAIL;
22080 posp->col = n;
22081
22082#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020022083 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022084 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000022085 posp->coladd = 0;
22086 else
22087 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022088#endif
22089
Bram Moolenaar493c1782014-05-28 14:34:46 +020022090 if (curswantp != NULL)
22091 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
22092
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022093 return OK;
22094}
22095
22096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 * Get the length of an environment variable name.
22098 * Advance "arg" to the first character after the name.
22099 * Return 0 for error.
22100 */
22101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022102get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103{
22104 char_u *p;
22105 int len;
22106
22107 for (p = *arg; vim_isIDc(*p); ++p)
22108 ;
22109 if (p == *arg) /* no name found */
22110 return 0;
22111
22112 len = (int)(p - *arg);
22113 *arg = p;
22114 return len;
22115}
22116
22117/*
22118 * Get the length of the name of a function or internal variable.
22119 * "arg" is advanced to the first non-white character after the name.
22120 * Return 0 if something is wrong.
22121 */
22122 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022123get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124{
22125 char_u *p;
22126 int len;
22127
22128 /* Find the end of the name. */
22129 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022130 {
22131 if (*p == ':')
22132 {
22133 /* "s:" is start of "s:var", but "n:" is not and can be used in
22134 * slice "[n:]". Also "xx:" is not a namespace. */
22135 len = (int)(p - *arg);
22136 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
22137 || len > 1)
22138 break;
22139 }
22140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 if (p == *arg) /* no name found */
22142 return 0;
22143
22144 len = (int)(p - *arg);
22145 *arg = skipwhite(p);
22146
22147 return len;
22148}
22149
22150/*
Bram Moolenaara7043832005-01-21 11:56:39 +000022151 * Get the length of the name of a variable or function.
22152 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022154 * Return -1 if curly braces expansion failed.
22155 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 * If the name contains 'magic' {}'s, expand them and return the
22157 * expanded name in an allocated string via 'alias' - caller must free.
22158 */
22159 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022160get_name_len(
22161 char_u **arg,
22162 char_u **alias,
22163 int evaluate,
22164 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165{
22166 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 char_u *p;
22168 char_u *expr_start;
22169 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170
22171 *alias = NULL; /* default to no alias */
22172
22173 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
22174 && (*arg)[2] == (int)KE_SNR)
22175 {
22176 /* hard coded <SNR>, already translated */
22177 *arg += 3;
22178 return get_id_len(arg) + 3;
22179 }
22180 len = eval_fname_script(*arg);
22181 if (len > 0)
22182 {
22183 /* literal "<SID>", "s:" or "<SNR>" */
22184 *arg += len;
22185 }
22186
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022188 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022190 p = find_name_end(*arg, &expr_start, &expr_end,
22191 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192 if (expr_start != NULL)
22193 {
22194 char_u *temp_string;
22195
22196 if (!evaluate)
22197 {
22198 len += (int)(p - *arg);
22199 *arg = skipwhite(p);
22200 return len;
22201 }
22202
22203 /*
22204 * Include any <SID> etc in the expanded string:
22205 * Thus the -len here.
22206 */
22207 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
22208 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022209 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 *alias = temp_string;
22211 *arg = skipwhite(p);
22212 return (int)STRLEN(temp_string);
22213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214
22215 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022216 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 EMSG2(_(e_invexpr2), *arg);
22218
22219 return len;
22220}
22221
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022222/*
22223 * Find the end of a variable or function name, taking care of magic braces.
22224 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
22225 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022226 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022227 * Return a pointer to just after the name. Equal to "arg" if there is no
22228 * valid name.
22229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022231find_name_end(
22232 char_u *arg,
22233 char_u **expr_start,
22234 char_u **expr_end,
22235 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022237 int mb_nest = 0;
22238 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022240 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022242 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022244 *expr_start = NULL;
22245 *expr_end = NULL;
22246 }
22247
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022248 /* Quick check for valid starting character. */
22249 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
22250 return arg;
22251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022252 for (p = arg; *p != NUL
22253 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022254 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022255 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022256 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000022257 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022258 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000022259 if (*p == '\'')
22260 {
22261 /* skip over 'string' to avoid counting [ and ] inside it. */
22262 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
22263 ;
22264 if (*p == NUL)
22265 break;
22266 }
22267 else if (*p == '"')
22268 {
22269 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
22270 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
22271 if (*p == '\\' && p[1] != NUL)
22272 ++p;
22273 if (*p == NUL)
22274 break;
22275 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022276 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
22277 {
22278 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022279 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022280 len = (int)(p - arg);
22281 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022282 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022283 break;
22284 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022286 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022288 if (*p == '[')
22289 ++br_nest;
22290 else if (*p == ']')
22291 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022294 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022296 if (*p == '{')
22297 {
22298 mb_nest++;
22299 if (expr_start != NULL && *expr_start == NULL)
22300 *expr_start = p;
22301 }
22302 else if (*p == '}')
22303 {
22304 mb_nest--;
22305 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22306 *expr_end = p;
22307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 }
22310
22311 return p;
22312}
22313
22314/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022315 * Expands out the 'magic' {}'s in a variable/function name.
22316 * Note that this can call itself recursively, to deal with
22317 * constructs like foo{bar}{baz}{bam}
22318 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22319 * "in_start" ^
22320 * "expr_start" ^
22321 * "expr_end" ^
22322 * "in_end" ^
22323 *
22324 * Returns a new allocated string, which the caller must free.
22325 * Returns NULL for failure.
22326 */
22327 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022328make_expanded_name(
22329 char_u *in_start,
22330 char_u *expr_start,
22331 char_u *expr_end,
22332 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022333{
22334 char_u c1;
22335 char_u *retval = NULL;
22336 char_u *temp_result;
22337 char_u *nextcmd = NULL;
22338
22339 if (expr_end == NULL || in_end == NULL)
22340 return NULL;
22341 *expr_start = NUL;
22342 *expr_end = NUL;
22343 c1 = *in_end;
22344 *in_end = NUL;
22345
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022346 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022347 if (temp_result != NULL && nextcmd == NULL)
22348 {
22349 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22350 + (in_end - expr_end) + 1));
22351 if (retval != NULL)
22352 {
22353 STRCPY(retval, in_start);
22354 STRCAT(retval, temp_result);
22355 STRCAT(retval, expr_end + 1);
22356 }
22357 }
22358 vim_free(temp_result);
22359
22360 *in_end = c1; /* put char back for error messages */
22361 *expr_start = '{';
22362 *expr_end = '}';
22363
22364 if (retval != NULL)
22365 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022366 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022367 if (expr_start != NULL)
22368 {
22369 /* Further expansion! */
22370 temp_result = make_expanded_name(retval, expr_start,
22371 expr_end, temp_result);
22372 vim_free(retval);
22373 retval = temp_result;
22374 }
22375 }
22376
22377 return retval;
22378}
22379
22380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022382 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 */
22384 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022385eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022387 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22388}
22389
22390/*
22391 * Return TRUE if character "c" can be used as the first character in a
22392 * variable or function name (excluding '{' and '}').
22393 */
22394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022395eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022396{
22397 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398}
22399
22400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 * Set number v: variable to "val".
22402 */
22403 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022404set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022406 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407}
22408
22409/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022410 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022412 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022413get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022415 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416}
22417
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022418/*
22419 * Get string v: variable value. Uses a static buffer, can only be used once.
22420 */
22421 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022422get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022423{
22424 return get_tv_string(&vimvars[idx].vv_tv);
22425}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022426
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022428 * Get List v: variable value. Caller must take care of reference count when
22429 * needed.
22430 */
22431 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022432get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022433{
22434 return vimvars[idx].vv_list;
22435}
22436
22437/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022438 * Set v:char to character "c".
22439 */
22440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022441set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022442{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022443 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022444
22445#ifdef FEAT_MBYTE
22446 if (has_mbyte)
22447 buf[(*mb_char2bytes)(c, buf)] = NUL;
22448 else
22449#endif
22450 {
22451 buf[0] = c;
22452 buf[1] = NUL;
22453 }
22454 set_vim_var_string(VV_CHAR, buf, -1);
22455}
22456
22457/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022458 * Set v:count to "count" and v:count1 to "count1".
22459 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460 */
22461 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022462set_vcount(
22463 long count,
22464 long count1,
22465 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022467 if (set_prevcount)
22468 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022469 vimvars[VV_COUNT].vv_nr = count;
22470 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471}
22472
22473/*
22474 * Set string v: variable to a copy of "val".
22475 */
22476 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022477set_vim_var_string(
22478 int idx,
22479 char_u *val,
22480 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481{
Bram Moolenaara542c682016-01-31 16:28:04 +010022482 clear_tv(&vimvars[idx].vv_di.di_tv);
22483 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022485 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022487 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022489 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490}
22491
22492/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022493 * Set List v: variable to "val".
22494 */
22495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022496set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022497{
Bram Moolenaara542c682016-01-31 16:28:04 +010022498 clear_tv(&vimvars[idx].vv_di.di_tv);
22499 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022500 vimvars[idx].vv_list = val;
22501 if (val != NULL)
22502 ++val->lv_refcount;
22503}
22504
22505/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022506 * Set Dictionary v: variable to "val".
22507 */
22508 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022509set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022510{
22511 int todo;
22512 hashitem_T *hi;
22513
Bram Moolenaara542c682016-01-31 16:28:04 +010022514 clear_tv(&vimvars[idx].vv_di.di_tv);
22515 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022516 vimvars[idx].vv_dict = val;
22517 if (val != NULL)
22518 {
22519 ++val->dv_refcount;
22520
22521 /* Set readonly */
22522 todo = (int)val->dv_hashtab.ht_used;
22523 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22524 {
22525 if (HASHITEM_EMPTY(hi))
22526 continue;
22527 --todo;
22528 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22529 }
22530 }
22531}
22532
22533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 * Set v:register if needed.
22535 */
22536 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022537set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538{
22539 char_u regname;
22540
22541 if (c == 0 || c == ' ')
22542 regname = '"';
22543 else
22544 regname = c;
22545 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022546 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 set_vim_var_string(VV_REG, &regname, 1);
22548}
22549
22550/*
22551 * Get or set v:exception. If "oldval" == NULL, return the current value.
22552 * Otherwise, restore the value to "oldval" and return NULL.
22553 * Must always be called in pairs to save and restore v:exception! Does not
22554 * take care of memory allocations.
22555 */
22556 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022557v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558{
22559 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022560 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561
Bram Moolenaare9a41262005-01-15 22:18:47 +000022562 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 return NULL;
22564}
22565
22566/*
22567 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22568 * Otherwise, restore the value to "oldval" and return NULL.
22569 * Must always be called in pairs to save and restore v:throwpoint! Does not
22570 * take care of memory allocations.
22571 */
22572 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022573v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574{
22575 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022576 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577
Bram Moolenaare9a41262005-01-15 22:18:47 +000022578 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 return NULL;
22580}
22581
22582#if defined(FEAT_AUTOCMD) || defined(PROTO)
22583/*
22584 * Set v:cmdarg.
22585 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22586 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22587 * Must always be called in pairs!
22588 */
22589 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022590set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591{
22592 char_u *oldval;
22593 char_u *newval;
22594 unsigned len;
22595
Bram Moolenaare9a41262005-01-15 22:18:47 +000022596 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022597 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022599 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022600 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022601 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 }
22603
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022604 if (eap->force_bin == FORCE_BIN)
22605 len = 6;
22606 else if (eap->force_bin == FORCE_NOBIN)
22607 len = 8;
22608 else
22609 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022610
22611 if (eap->read_edit)
22612 len += 7;
22613
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022614 if (eap->force_ff != 0)
22615 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22616# ifdef FEAT_MBYTE
22617 if (eap->force_enc != 0)
22618 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022619 if (eap->bad_char != 0)
22620 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022621# endif
22622
22623 newval = alloc(len + 1);
22624 if (newval == NULL)
22625 return NULL;
22626
22627 if (eap->force_bin == FORCE_BIN)
22628 sprintf((char *)newval, " ++bin");
22629 else if (eap->force_bin == FORCE_NOBIN)
22630 sprintf((char *)newval, " ++nobin");
22631 else
22632 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022633
22634 if (eap->read_edit)
22635 STRCAT(newval, " ++edit");
22636
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022637 if (eap->force_ff != 0)
22638 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22639 eap->cmd + eap->force_ff);
22640# ifdef FEAT_MBYTE
22641 if (eap->force_enc != 0)
22642 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22643 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022644 if (eap->bad_char == BAD_KEEP)
22645 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22646 else if (eap->bad_char == BAD_DROP)
22647 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22648 else if (eap->bad_char != 0)
22649 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022650# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022651 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022652 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653}
22654#endif
22655
22656/*
22657 * Get the value of internal variable "name".
22658 * Return OK or FAIL.
22659 */
22660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022661get_var_tv(
22662 char_u *name,
22663 int len, /* length of "name" */
22664 typval_T *rettv, /* NULL when only checking existence */
22665 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22666 int verbose, /* may give error message */
22667 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668{
22669 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022670 typval_T *tv = NULL;
22671 typval_T atv;
22672 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674
22675 /* truncate the name, so that we can use strcmp() */
22676 cc = name[len];
22677 name[len] = NUL;
22678
22679 /*
22680 * Check for "b:changedtick".
22681 */
22682 if (STRCMP(name, "b:changedtick") == 0)
22683 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022684 atv.v_type = VAR_NUMBER;
22685 atv.vval.v_number = curbuf->b_changedtick;
22686 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 }
22688
22689 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 * Check for user-defined variables.
22691 */
22692 else
22693 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022694 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022696 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022697 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022698 if (dip != NULL)
22699 *dip = v;
22700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 }
22702
Bram Moolenaare9a41262005-01-15 22:18:47 +000022703 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022705 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022706 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 ret = FAIL;
22708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022709 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022710 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711
22712 name[len] = cc;
22713
22714 return ret;
22715}
22716
22717/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022718 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22719 * Also handle function call with Funcref variable: func(expr)
22720 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22721 */
22722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022723handle_subscript(
22724 char_u **arg,
22725 typval_T *rettv,
22726 int evaluate, /* do more than finding the end */
22727 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022728{
22729 int ret = OK;
22730 dict_T *selfdict = NULL;
22731 char_u *s;
22732 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022733 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022734
22735 while (ret == OK
22736 && (**arg == '['
22737 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022738 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22739 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022740 && !vim_iswhite(*(*arg - 1)))
22741 {
22742 if (**arg == '(')
22743 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022744 partial_T *pt = NULL;
22745
Bram Moolenaard9fba312005-06-26 22:34:35 +000022746 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022747 if (evaluate)
22748 {
22749 functv = *rettv;
22750 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022751
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022752 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022753 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022754 {
22755 pt = functv.vval.v_partial;
22756 s = pt->pt_name;
22757 }
22758 else
22759 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022760 }
22761 else
22762 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022763 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022764 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022765 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022766
22767 /* Clear the funcref afterwards, so that deleting it while
22768 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022769 if (evaluate)
22770 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022771
22772 /* Stop the expression evaluation when immediately aborting on
22773 * error, or when an interrupt occurred or an exception was thrown
22774 * but not caught. */
22775 if (aborting())
22776 {
22777 if (ret == OK)
22778 clear_tv(rettv);
22779 ret = FAIL;
22780 }
22781 dict_unref(selfdict);
22782 selfdict = NULL;
22783 }
22784 else /* **arg == '[' || **arg == '.' */
22785 {
22786 dict_unref(selfdict);
22787 if (rettv->v_type == VAR_DICT)
22788 {
22789 selfdict = rettv->vval.v_dict;
22790 if (selfdict != NULL)
22791 ++selfdict->dv_refcount;
22792 }
22793 else
22794 selfdict = NULL;
22795 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22796 {
22797 clear_tv(rettv);
22798 ret = FAIL;
22799 }
22800 }
22801 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022802
Bram Moolenaar1d429612016-05-24 15:44:17 +020022803 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22804 * Don't do this when "Func" is already a partial that was bound
22805 * explicitly (pt_auto is FALSE). */
22806 if (selfdict != NULL
22807 && (rettv->v_type == VAR_FUNC
22808 || (rettv->v_type == VAR_PARTIAL
22809 && (rettv->vval.v_partial->pt_auto
22810 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022811 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022812 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22813 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022814 char_u *tofree = NULL;
22815 ufunc_T *fp;
22816 char_u fname_buf[FLEN_FIXED + 1];
22817 int error;
22818
22819 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022820 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022821 fp = find_func(fname);
22822 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022823
Bram Moolenaar65639032016-03-16 21:40:30 +010022824 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022825 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022826 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22827
22828 if (pt != NULL)
22829 {
22830 pt->pt_refcount = 1;
22831 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022832 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022833 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022834 if (rettv->v_type == VAR_FUNC)
22835 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022836 /* Just a function: Take over the function name and use
22837 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022838 pt->pt_name = rettv->vval.v_string;
22839 }
22840 else
22841 {
22842 partial_T *ret_pt = rettv->vval.v_partial;
22843 int i;
22844
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022845 /* Partial: copy the function name, use selfdict and copy
22846 * args. Can't take over name or args, the partial might
22847 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022848 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022849 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022850 if (ret_pt->pt_argc > 0)
22851 {
22852 pt->pt_argv = (typval_T *)alloc(
22853 sizeof(typval_T) * ret_pt->pt_argc);
22854 if (pt->pt_argv == NULL)
22855 /* out of memory: drop the arguments */
22856 pt->pt_argc = 0;
22857 else
22858 {
22859 pt->pt_argc = ret_pt->pt_argc;
22860 for (i = 0; i < pt->pt_argc; i++)
22861 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22862 }
22863 }
22864 partial_unref(ret_pt);
22865 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022866 rettv->v_type = VAR_PARTIAL;
22867 rettv->vval.v_partial = pt;
22868 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022869 }
22870 }
22871
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022872 dict_unref(selfdict);
22873 return ret;
22874}
22875
22876/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022877 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022878 * value).
22879 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022880 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022881alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022882{
Bram Moolenaar33570922005-01-25 22:26:29 +000022883 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022884}
22885
22886/*
22887 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 * The string "s" must have been allocated, it is consumed.
22889 * Return NULL for out of memory, the variable otherwise.
22890 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022891 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022892alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893{
Bram Moolenaar33570922005-01-25 22:26:29 +000022894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022896 rettv = alloc_tv();
22897 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022899 rettv->v_type = VAR_STRING;
22900 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 }
22902 else
22903 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022904 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905}
22906
22907/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022908 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022910 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022911free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912{
22913 if (varp != NULL)
22914 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022915 switch (varp->v_type)
22916 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022917 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022918 func_unref(varp->vval.v_string);
22919 /*FALLTHROUGH*/
22920 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022921 vim_free(varp->vval.v_string);
22922 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022923 case VAR_PARTIAL:
22924 partial_unref(varp->vval.v_partial);
22925 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022926 case VAR_LIST:
22927 list_unref(varp->vval.v_list);
22928 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022929 case VAR_DICT:
22930 dict_unref(varp->vval.v_dict);
22931 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022932 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022933#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022934 job_unref(varp->vval.v_job);
22935 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022936#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022937 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022938#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022939 channel_unref(varp->vval.v_channel);
22940 break;
22941#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022942 case VAR_NUMBER:
22943 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022944 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022945 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022946 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 vim_free(varp);
22949 }
22950}
22951
22952/*
22953 * Free the memory for a variable value and set the value to NULL or 0.
22954 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022955 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022956clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957{
22958 if (varp != NULL)
22959 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022960 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022962 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022963 func_unref(varp->vval.v_string);
22964 /*FALLTHROUGH*/
22965 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022966 vim_free(varp->vval.v_string);
22967 varp->vval.v_string = NULL;
22968 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022969 case VAR_PARTIAL:
22970 partial_unref(varp->vval.v_partial);
22971 varp->vval.v_partial = NULL;
22972 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022973 case VAR_LIST:
22974 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022975 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022976 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022977 case VAR_DICT:
22978 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022979 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022980 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022981 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022982 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022983 varp->vval.v_number = 0;
22984 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022985 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022986#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022987 varp->vval.v_float = 0.0;
22988 break;
22989#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022990 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022991#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022992 job_unref(varp->vval.v_job);
22993 varp->vval.v_job = NULL;
22994#endif
22995 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022996 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022997#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022998 channel_unref(varp->vval.v_channel);
22999 varp->vval.v_channel = NULL;
23000#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023001 case VAR_UNKNOWN:
23002 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023004 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 }
23006}
23007
23008/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023009 * Set the value of a variable to NULL without freeing items.
23010 */
23011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023012init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023013{
23014 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023015 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023016}
23017
23018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 * Get the number value of a variable.
23020 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023021 * For incompatible types, return 0.
23022 * get_tv_number_chk() is similar to get_tv_number(), but informs the
23023 * caller of incompatible types: it sets *denote to TRUE if "denote"
23024 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023026 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023027get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023029 int error = FALSE;
23030
23031 return get_tv_number_chk(varp, &error); /* return 0L on error */
23032}
23033
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023034 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023035get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023036{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023037 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023039 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023041 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023042 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023043 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023044#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000023045 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023046 break;
23047#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023048 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023049 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023050 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023051 break;
23052 case VAR_STRING:
23053 if (varp->vval.v_string != NULL)
23054 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010023055 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023056 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023057 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023058 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023059 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023060 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023061 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023062 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010023063 case VAR_SPECIAL:
23064 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
23065 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023066 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023067#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023068 EMSG(_("E910: Using a Job as a Number"));
23069 break;
23070#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023071 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023072#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023073 EMSG(_("E913: Using a Channel as a Number"));
23074 break;
23075#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010023076 case VAR_UNKNOWN:
23077 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023080 if (denote == NULL) /* useful for values that must be unsigned */
23081 n = -1;
23082 else
23083 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023084 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085}
23086
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023087#ifdef FEAT_FLOAT
23088 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023089get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023090{
23091 switch (varp->v_type)
23092 {
23093 case VAR_NUMBER:
23094 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023095 case VAR_FLOAT:
23096 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023097 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023098 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023099 EMSG(_("E891: Using a Funcref as a Float"));
23100 break;
23101 case VAR_STRING:
23102 EMSG(_("E892: Using a String as a Float"));
23103 break;
23104 case VAR_LIST:
23105 EMSG(_("E893: Using a List as a Float"));
23106 break;
23107 case VAR_DICT:
23108 EMSG(_("E894: Using a Dictionary as a Float"));
23109 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023110 case VAR_SPECIAL:
23111 EMSG(_("E907: Using a special value as a Float"));
23112 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023113 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023114# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023115 EMSG(_("E911: Using a Job as a Float"));
23116 break;
23117# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023118 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023119# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023120 EMSG(_("E914: Using a Channel as a Float"));
23121 break;
23122# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010023123 case VAR_UNKNOWN:
23124 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023125 break;
23126 }
23127 return 0;
23128}
23129#endif
23130
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000023132 * Get the lnum from the first argument.
23133 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023134 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 */
23136 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023137get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138{
Bram Moolenaar33570922005-01-25 22:26:29 +000023139 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 linenr_T lnum;
23141
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023142 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143 if (lnum == 0) /* no valid number, try using line() */
23144 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023145 rettv.v_type = VAR_NUMBER;
23146 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023147 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023148 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 }
23150 return lnum;
23151}
23152
23153/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000023154 * Get the lnum from the first argument.
23155 * Also accepts "$", then "buf" is used.
23156 * Returns 0 on error.
23157 */
23158 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023159get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000023160{
23161 if (argvars[0].v_type == VAR_STRING
23162 && argvars[0].vval.v_string != NULL
23163 && argvars[0].vval.v_string[0] == '$'
23164 && buf != NULL)
23165 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023166 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000023167}
23168
23169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 * Get the string value of a variable.
23171 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000023172 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23173 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 * If the String variable has never been set, return an empty string.
23175 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023176 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
23177 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023179 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023180get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181{
23182 static char_u mybuf[NUMBUFLEN];
23183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023184 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185}
23186
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023187 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023188get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023190 char_u *res = get_tv_string_buf_chk(varp, buf);
23191
23192 return res != NULL ? res : (char_u *)"";
23193}
23194
Bram Moolenaar7d647822014-04-05 21:28:56 +020023195/*
23196 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23197 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000023198 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023199get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023200{
23201 static char_u mybuf[NUMBUFLEN];
23202
23203 return get_tv_string_buf_chk(varp, mybuf);
23204}
23205
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023206 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023207get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023208{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023209 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023211 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023212 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
23213 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023214 return buf;
23215 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023216 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023217 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023218 break;
23219 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023220 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000023221 break;
23222 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023223 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023224 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023225 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023226#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020023227 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023228 break;
23229#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023230 case VAR_STRING:
23231 if (varp->vval.v_string != NULL)
23232 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023233 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010023234 case VAR_SPECIAL:
23235 STRCPY(buf, get_var_special_name(varp->vval.v_number));
23236 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023237 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023238#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023239 {
23240 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010023241 char *status;
23242
23243 if (job == NULL)
23244 return (char_u *)"no process";
23245 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010023246 : job->jv_status == JOB_ENDED ? "dead"
23247 : "run";
23248# ifdef UNIX
23249 vim_snprintf((char *)buf, NUMBUFLEN,
23250 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023251# elif defined(WIN32)
23252 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010023253 "process %ld %s",
23254 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023255 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010023256# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023257 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010023258 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
23259# endif
23260 return buf;
23261 }
23262#endif
23263 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010023264 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023265#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023266 {
23267 channel_T *channel = varp->vval.v_channel;
23268 char *status = channel_status(channel);
23269
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023270 if (channel == NULL)
23271 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
23272 else
23273 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010023274 "channel %d %s", channel->ch_id, status);
23275 return buf;
23276 }
23277#endif
23278 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023279 case VAR_UNKNOWN:
23280 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023283 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284}
23285
23286/*
23287 * Find variable "name" in the list of variables.
23288 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023289 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023290 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023291 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023293 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023294find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023297 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298
Bram Moolenaara7043832005-01-21 11:56:39 +000023299 ht = find_var_ht(name, &varname);
23300 if (htp != NULL)
23301 *htp = ht;
23302 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023304 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305}
23306
23307/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023308 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023309 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023311 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023312find_var_in_ht(
23313 hashtab_T *ht,
23314 int htname,
23315 char_u *varname,
23316 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023317{
Bram Moolenaar33570922005-01-25 22:26:29 +000023318 hashitem_T *hi;
23319
23320 if (*varname == NUL)
23321 {
23322 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023323 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023324 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023325 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023326 case 'g': return &globvars_var;
23327 case 'v': return &vimvars_var;
23328 case 'b': return &curbuf->b_bufvar;
23329 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023330#ifdef FEAT_WINDOWS
23331 case 't': return &curtab->tp_winvar;
23332#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023333 case 'l': return current_funccal == NULL
23334 ? NULL : &current_funccal->l_vars_var;
23335 case 'a': return current_funccal == NULL
23336 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023337 }
23338 return NULL;
23339 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023340
23341 hi = hash_find(ht, varname);
23342 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023343 {
23344 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023345 * worked find the variable again. Don't auto-load a script if it was
23346 * loaded already, otherwise it would be loaded every time when
23347 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023348 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023349 {
23350 /* Note: script_autoload() may make "hi" invalid. It must either
23351 * be obtained again or not used. */
23352 if (!script_autoload(varname, FALSE) || aborting())
23353 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023354 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023355 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023356 if (HASHITEM_EMPTY(hi))
23357 return NULL;
23358 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023359 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023360}
23361
23362/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023363 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023364 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023365 * Set "varname" to the start of name without ':'.
23366 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023367 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023368find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023370 hashitem_T *hi;
23371
Bram Moolenaar73627d02015-08-11 15:46:09 +020023372 if (name[0] == NUL)
23373 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 if (name[1] != ':')
23375 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023376 /* The name must not start with a colon or #. */
23377 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 return NULL;
23379 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023380
23381 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023382 hi = hash_find(&compat_hashtab, name);
23383 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023384 return &compat_hashtab;
23385
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023387 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023388 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389 }
23390 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023391 if (*name == 'g') /* global variable */
23392 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023393 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23394 */
23395 if (vim_strchr(name + 2, ':') != NULL
23396 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023397 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023399 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023401 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023402#ifdef FEAT_WINDOWS
23403 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023404 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023405#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023406 if (*name == 'v') /* v: variable */
23407 return &vimvarht;
23408 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023409 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023410 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023411 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412 if (*name == 's' /* script variable */
23413 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23414 return &SCRIPT_VARS(current_SID);
23415 return NULL;
23416}
23417
23418/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023419 * Get function call environment based on bactrace debug level
23420 */
23421 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023422get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023423{
23424 int i;
23425 funccall_T *funccal;
23426 funccall_T *temp_funccal;
23427
23428 funccal = current_funccal;
23429 if (debug_backtrace_level > 0)
23430 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023431 for (i = 0; i < debug_backtrace_level; i++)
23432 {
23433 temp_funccal = funccal->caller;
23434 if (temp_funccal)
23435 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023436 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023437 /* backtrace level overflow. reset to max */
23438 debug_backtrace_level = i;
23439 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023440 }
23441 return funccal;
23442}
23443
23444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023446 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447 * Returns NULL when it doesn't exist.
23448 */
23449 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023450get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451{
Bram Moolenaar33570922005-01-25 22:26:29 +000023452 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023454 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 if (v == NULL)
23456 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023457 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458}
23459
23460/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023461 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 * sourcing this script and when executing functions defined in the script.
23463 */
23464 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023465new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466{
Bram Moolenaara7043832005-01-21 11:56:39 +000023467 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023468 hashtab_T *ht;
23469 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023470
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23472 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023473 /* Re-allocating ga_data means that an ht_array pointing to
23474 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023475 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023476 for (i = 1; i <= ga_scripts.ga_len; ++i)
23477 {
23478 ht = &SCRIPT_VARS(i);
23479 if (ht->ht_mask == HT_INIT_SIZE - 1)
23480 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023481 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023482 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023483 }
23484
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 while (ga_scripts.ga_len < id)
23486 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023487 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023488 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023489 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491 }
23492 }
23493}
23494
23495/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023496 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23497 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498 */
23499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023500init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501{
Bram Moolenaar33570922005-01-25 22:26:29 +000023502 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023503 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023504 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023505 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023506 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023507 dict_var->di_tv.vval.v_dict = dict;
23508 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023509 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023510 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23511 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512}
23513
23514/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023515 * Unreference a dictionary initialized by init_var_dict().
23516 */
23517 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023518unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023519{
23520 /* Now the dict needs to be freed if no one else is using it, go back to
23521 * normal reference counting. */
23522 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23523 dict_unref(dict);
23524}
23525
23526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023528 * Frees all allocated variables and the value they contain.
23529 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 */
23531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023532vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023533{
23534 vars_clear_ext(ht, TRUE);
23535}
23536
23537/*
23538 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23539 */
23540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023541vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542{
Bram Moolenaara7043832005-01-21 11:56:39 +000023543 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023544 hashitem_T *hi;
23545 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546
Bram Moolenaar33570922005-01-25 22:26:29 +000023547 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023548 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023549 for (hi = ht->ht_array; todo > 0; ++hi)
23550 {
23551 if (!HASHITEM_EMPTY(hi))
23552 {
23553 --todo;
23554
Bram Moolenaar33570922005-01-25 22:26:29 +000023555 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023556 * ht_array might change then. hash_clear() takes care of it
23557 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023558 v = HI2DI(hi);
23559 if (free_val)
23560 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023561 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023562 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023563 }
23564 }
23565 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023566 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567}
23568
Bram Moolenaara7043832005-01-21 11:56:39 +000023569/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023570 * Delete a variable from hashtab "ht" at item "hi".
23571 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023574delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575{
Bram Moolenaar33570922005-01-25 22:26:29 +000023576 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023577
23578 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023579 clear_tv(&di->di_tv);
23580 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581}
23582
23583/*
23584 * List the value of one internal variable.
23585 */
23586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023587list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023589 char_u *tofree;
23590 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023591 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023592
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023593 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023594 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023595 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023596 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597}
23598
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023600list_one_var_a(
23601 char_u *prefix,
23602 char_u *name,
23603 int type,
23604 char_u *string,
23605 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606{
Bram Moolenaar31859182007-08-14 20:41:13 +000023607 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23608 msg_start();
23609 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 if (name != NULL) /* "a:" vars don't have a name stored */
23611 msg_puts(name);
23612 msg_putchar(' ');
23613 msg_advance(22);
23614 if (type == VAR_NUMBER)
23615 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023616 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023617 msg_putchar('*');
23618 else if (type == VAR_LIST)
23619 {
23620 msg_putchar('[');
23621 if (*string == '[')
23622 ++string;
23623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023624 else if (type == VAR_DICT)
23625 {
23626 msg_putchar('{');
23627 if (*string == '{')
23628 ++string;
23629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 else
23631 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023632
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023634
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023635 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023636 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023637 if (*first)
23638 {
23639 msg_clr_eos();
23640 *first = FALSE;
23641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642}
23643
23644/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023645 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 * If the variable already exists, the value is updated.
23647 * Otherwise the variable is created.
23648 */
23649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023650set_var(
23651 char_u *name,
23652 typval_T *tv,
23653 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654{
Bram Moolenaar33570922005-01-25 22:26:29 +000023655 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023657 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023659 ht = find_var_ht(name, &varname);
23660 if (ht == NULL || *varname == NUL)
23661 {
23662 EMSG2(_(e_illvar), name);
23663 return;
23664 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023665 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023666
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023667 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23668 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023669 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023670
Bram Moolenaar33570922005-01-25 22:26:29 +000023671 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023673 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023674 if (var_check_ro(v->di_flags, name, FALSE)
23675 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023676 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023677
23678 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023679 * Handle setting internal v: variables separately where needed to
23680 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023681 */
23682 if (ht == &vimvarht)
23683 {
23684 if (v->di_tv.v_type == VAR_STRING)
23685 {
23686 vim_free(v->di_tv.vval.v_string);
23687 if (copy || tv->v_type != VAR_STRING)
23688 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23689 else
23690 {
23691 /* Take over the string to avoid an extra alloc/free. */
23692 v->di_tv.vval.v_string = tv->vval.v_string;
23693 tv->vval.v_string = NULL;
23694 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023695 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023696 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023697 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023698 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023699 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023700 if (STRCMP(varname, "searchforward") == 0)
23701 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023702#ifdef FEAT_SEARCH_EXTRA
23703 else if (STRCMP(varname, "hlsearch") == 0)
23704 {
23705 no_hlsearch = !v->di_tv.vval.v_number;
23706 redraw_all_later(SOME_VALID);
23707 }
23708#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023709 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023710 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023711 else if (v->di_tv.v_type != tv->v_type)
23712 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023713 }
23714
23715 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 }
23717 else /* add a new variable */
23718 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023719 /* Can't add "v:" variable. */
23720 if (ht == &vimvarht)
23721 {
23722 EMSG2(_(e_illvar), name);
23723 return;
23724 }
23725
Bram Moolenaar92124a32005-06-17 22:03:40 +000023726 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023727 if (!valid_varname(varname))
23728 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023729
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023730 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23731 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023732 if (v == NULL)
23733 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023734 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023735 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023737 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 return;
23739 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023740 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023742
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023743 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023744 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023745 else
23746 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023747 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023748 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023749 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751}
23752
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023753/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023754 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023755 * Also give an error message.
23756 */
23757 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023758var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023759{
23760 if (flags & DI_FLAGS_RO)
23761 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023762 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023763 return TRUE;
23764 }
23765 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23766 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023767 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023768 return TRUE;
23769 }
23770 return FALSE;
23771}
23772
23773/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023774 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23775 * Also give an error message.
23776 */
23777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023778var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023779{
23780 if (flags & DI_FLAGS_FIX)
23781 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023782 EMSG2(_("E795: Cannot delete variable %s"),
23783 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023784 return TRUE;
23785 }
23786 return FALSE;
23787}
23788
23789/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023790 * Check if a funcref is assigned to a valid variable name.
23791 * Return TRUE and give an error if not.
23792 */
23793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023794var_check_func_name(
23795 char_u *name, /* points to start of variable name */
23796 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023797{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023798 /* Allow for w: b: s: and t:. */
23799 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023800 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23801 ? name[2] : name[0]))
23802 {
23803 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23804 name);
23805 return TRUE;
23806 }
23807 /* Don't allow hiding a function. When "v" is not NULL we might be
23808 * assigning another function to the same var, the type is checked
23809 * below. */
23810 if (new_var && function_exists(name))
23811 {
23812 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23813 name);
23814 return TRUE;
23815 }
23816 return FALSE;
23817}
23818
23819/*
23820 * Check if a variable name is valid.
23821 * Return FALSE and give an error if not.
23822 */
23823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023824valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023825{
23826 char_u *p;
23827
23828 for (p = varname; *p != NUL; ++p)
23829 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23830 && *p != AUTOLOAD_CHAR)
23831 {
23832 EMSG2(_(e_illvar), varname);
23833 return FALSE;
23834 }
23835 return TRUE;
23836}
23837
23838/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023839 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023840 * Also give an error message, using "name" or _("name") when use_gettext is
23841 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023842 */
23843 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023844tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023845{
23846 if (lock & VAR_LOCKED)
23847 {
23848 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023849 name == NULL ? (char_u *)_("Unknown")
23850 : use_gettext ? (char_u *)_(name)
23851 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023852 return TRUE;
23853 }
23854 if (lock & VAR_FIXED)
23855 {
23856 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023857 name == NULL ? (char_u *)_("Unknown")
23858 : use_gettext ? (char_u *)_(name)
23859 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023860 return TRUE;
23861 }
23862 return FALSE;
23863}
23864
23865/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023866 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023867 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023868 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023869 * It is OK for "from" and "to" to point to the same item. This is used to
23870 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023871 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023872 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023873copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023875 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023876 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023877 switch (from->v_type)
23878 {
23879 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023880 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023881 to->vval.v_number = from->vval.v_number;
23882 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023883 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023884#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023885 to->vval.v_float = from->vval.v_float;
23886 break;
23887#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023888 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023889#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023890 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023891 if (to->vval.v_job != NULL)
23892 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023893 break;
23894#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023895 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023896#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023897 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023898 if (to->vval.v_channel != NULL)
23899 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023900 break;
23901#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023902 case VAR_STRING:
23903 case VAR_FUNC:
23904 if (from->vval.v_string == NULL)
23905 to->vval.v_string = NULL;
23906 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023907 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023908 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023909 if (from->v_type == VAR_FUNC)
23910 func_ref(to->vval.v_string);
23911 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023912 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023913 case VAR_PARTIAL:
23914 if (from->vval.v_partial == NULL)
23915 to->vval.v_partial = NULL;
23916 else
23917 {
23918 to->vval.v_partial = from->vval.v_partial;
23919 ++to->vval.v_partial->pt_refcount;
23920 }
23921 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023922 case VAR_LIST:
23923 if (from->vval.v_list == NULL)
23924 to->vval.v_list = NULL;
23925 else
23926 {
23927 to->vval.v_list = from->vval.v_list;
23928 ++to->vval.v_list->lv_refcount;
23929 }
23930 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023931 case VAR_DICT:
23932 if (from->vval.v_dict == NULL)
23933 to->vval.v_dict = NULL;
23934 else
23935 {
23936 to->vval.v_dict = from->vval.v_dict;
23937 ++to->vval.v_dict->dv_refcount;
23938 }
23939 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023940 case VAR_UNKNOWN:
23941 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023942 break;
23943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944}
23945
23946/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023947 * Make a copy of an item.
23948 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023949 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23950 * reference to an already copied list/dict can be used.
23951 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023952 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023954item_copy(
23955 typval_T *from,
23956 typval_T *to,
23957 int deep,
23958 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023959{
23960 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023961 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023962
Bram Moolenaar33570922005-01-25 22:26:29 +000023963 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023964 {
23965 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023966 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023967 }
23968 ++recurse;
23969
23970 switch (from->v_type)
23971 {
23972 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023973 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023974 case VAR_STRING:
23975 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023976 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023977 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023978 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023979 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023980 copy_tv(from, to);
23981 break;
23982 case VAR_LIST:
23983 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023984 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023985 if (from->vval.v_list == NULL)
23986 to->vval.v_list = NULL;
23987 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23988 {
23989 /* use the copy made earlier */
23990 to->vval.v_list = from->vval.v_list->lv_copylist;
23991 ++to->vval.v_list->lv_refcount;
23992 }
23993 else
23994 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23995 if (to->vval.v_list == NULL)
23996 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023997 break;
23998 case VAR_DICT:
23999 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024000 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024001 if (from->vval.v_dict == NULL)
24002 to->vval.v_dict = NULL;
24003 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
24004 {
24005 /* use the copy made earlier */
24006 to->vval.v_dict = from->vval.v_dict->dv_copydict;
24007 ++to->vval.v_dict->dv_refcount;
24008 }
24009 else
24010 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
24011 if (to->vval.v_dict == NULL)
24012 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024013 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024014 case VAR_UNKNOWN:
24015 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024016 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024017 }
24018 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024019 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024020}
24021
24022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 * ":echo expr1 ..." print each argument separated with a space, add a
24024 * newline at the end.
24025 * ":echon expr1 ..." print each argument plain.
24026 */
24027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024028ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024029{
24030 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024031 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024032 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 char_u *p;
24034 int needclr = TRUE;
24035 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024036 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037
24038 if (eap->skip)
24039 ++emsg_skip;
24040 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
24041 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024042 /* If eval1() causes an error message the text from the command may
24043 * still need to be cleared. E.g., "echo 22,44". */
24044 need_clr_eos = needclr;
24045
Bram Moolenaar071d4272004-06-13 20:20:40 +000024046 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024047 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 {
24049 /*
24050 * Report the invalid expression unless the expression evaluation
24051 * has been cancelled due to an aborting error, an interrupt, or an
24052 * exception.
24053 */
24054 if (!aborting())
24055 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024056 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057 break;
24058 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024059 need_clr_eos = FALSE;
24060
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 if (!eap->skip)
24062 {
24063 if (atstart)
24064 {
24065 atstart = FALSE;
24066 /* Call msg_start() after eval1(), evaluating the expression
24067 * may cause a message to appear. */
24068 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010024069 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020024070 /* Mark the saved text as finishing the line, so that what
24071 * follows is displayed on a new line when scrolling back
24072 * at the more prompt. */
24073 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024074 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010024075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076 }
24077 else if (eap->cmdidx == CMD_echo)
24078 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010024079 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024080 if (p != NULL)
24081 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024083 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024085 if (*p != TAB && needclr)
24086 {
24087 /* remove any text still there from the command */
24088 msg_clr_eos();
24089 needclr = FALSE;
24090 }
24091 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024092 }
24093 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024094 {
24095#ifdef FEAT_MBYTE
24096 if (has_mbyte)
24097 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000024098 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024099
24100 (void)msg_outtrans_len_attr(p, i, echo_attr);
24101 p += i - 1;
24102 }
24103 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024105 (void)msg_outtrans_len_attr(p, 1, echo_attr);
24106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024108 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024110 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 arg = skipwhite(arg);
24112 }
24113 eap->nextcmd = check_nextcmd(arg);
24114
24115 if (eap->skip)
24116 --emsg_skip;
24117 else
24118 {
24119 /* remove text that may still be there from the command */
24120 if (needclr)
24121 msg_clr_eos();
24122 if (eap->cmdidx == CMD_echo)
24123 msg_end();
24124 }
24125}
24126
24127/*
24128 * ":echohl {name}".
24129 */
24130 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024131ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024132{
24133 int id;
24134
24135 id = syn_name2id(eap->arg);
24136 if (id == 0)
24137 echo_attr = 0;
24138 else
24139 echo_attr = syn_id2attr(id);
24140}
24141
24142/*
24143 * ":execute expr1 ..." execute the result of an expression.
24144 * ":echomsg expr1 ..." Print a message
24145 * ":echoerr expr1 ..." Print an error
24146 * Each gets spaces around each argument and a newline at the end for
24147 * echo commands
24148 */
24149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024150ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151{
24152 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024153 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 int ret = OK;
24155 char_u *p;
24156 garray_T ga;
24157 int len;
24158 int save_did_emsg;
24159
24160 ga_init2(&ga, 1, 80);
24161
24162 if (eap->skip)
24163 ++emsg_skip;
24164 while (*arg != NUL && *arg != '|' && *arg != '\n')
24165 {
24166 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024167 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168 {
24169 /*
24170 * Report the invalid expression unless the expression evaluation
24171 * has been cancelled due to an aborting error, an interrupt, or an
24172 * exception.
24173 */
24174 if (!aborting())
24175 EMSG2(_(e_invexpr2), p);
24176 ret = FAIL;
24177 break;
24178 }
24179
24180 if (!eap->skip)
24181 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024182 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 len = (int)STRLEN(p);
24184 if (ga_grow(&ga, len + 2) == FAIL)
24185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024186 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 ret = FAIL;
24188 break;
24189 }
24190 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193 ga.ga_len += len;
24194 }
24195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024196 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197 arg = skipwhite(arg);
24198 }
24199
24200 if (ret != FAIL && ga.ga_data != NULL)
24201 {
24202 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000024203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000024205 out_flush();
24206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 else if (eap->cmdidx == CMD_echoerr)
24208 {
24209 /* We don't want to abort following commands, restore did_emsg. */
24210 save_did_emsg = did_emsg;
24211 EMSG((char_u *)ga.ga_data);
24212 if (!force_abort)
24213 did_emsg = save_did_emsg;
24214 }
24215 else if (eap->cmdidx == CMD_execute)
24216 do_cmdline((char_u *)ga.ga_data,
24217 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
24218 }
24219
24220 ga_clear(&ga);
24221
24222 if (eap->skip)
24223 --emsg_skip;
24224
24225 eap->nextcmd = check_nextcmd(arg);
24226}
24227
24228/*
24229 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
24230 * "arg" points to the "&" or '+' when called, to "option" when returning.
24231 * Returns NULL when no option name found. Otherwise pointer to the char
24232 * after the option name.
24233 */
24234 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024235find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236{
24237 char_u *p = *arg;
24238
24239 ++p;
24240 if (*p == 'g' && p[1] == ':')
24241 {
24242 *opt_flags = OPT_GLOBAL;
24243 p += 2;
24244 }
24245 else if (*p == 'l' && p[1] == ':')
24246 {
24247 *opt_flags = OPT_LOCAL;
24248 p += 2;
24249 }
24250 else
24251 *opt_flags = 0;
24252
24253 if (!ASCII_ISALPHA(*p))
24254 return NULL;
24255 *arg = p;
24256
24257 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
24258 p += 4; /* termcap option */
24259 else
24260 while (ASCII_ISALPHA(*p))
24261 ++p;
24262 return p;
24263}
24264
24265/*
24266 * ":function"
24267 */
24268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024269ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270{
24271 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024272 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273 int j;
24274 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024276 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 char_u *name = NULL;
24278 char_u *p;
24279 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024280 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 garray_T newargs;
24282 garray_T newlines;
24283 int varargs = FALSE;
24284 int mustend = FALSE;
24285 int flags = 0;
24286 ufunc_T *fp;
24287 int indent;
24288 int nesting;
24289 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024290 dictitem_T *v;
24291 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024292 static int func_nr = 0; /* number for nameless function */
24293 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024294 hashtab_T *ht;
24295 int todo;
24296 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024297 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298
24299 /*
24300 * ":function" without argument: list functions.
24301 */
24302 if (ends_excmd(*eap->arg))
24303 {
24304 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024305 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024306 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024307 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024308 {
24309 if (!HASHITEM_EMPTY(hi))
24310 {
24311 --todo;
24312 fp = HI2UF(hi);
24313 if (!isdigit(*fp->uf_name))
24314 list_func_head(fp, FALSE);
24315 }
24316 }
24317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318 eap->nextcmd = check_nextcmd(eap->arg);
24319 return;
24320 }
24321
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024322 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024323 * ":function /pat": list functions matching pattern.
24324 */
24325 if (*eap->arg == '/')
24326 {
24327 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24328 if (!eap->skip)
24329 {
24330 regmatch_T regmatch;
24331
24332 c = *p;
24333 *p = NUL;
24334 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24335 *p = c;
24336 if (regmatch.regprog != NULL)
24337 {
24338 regmatch.rm_ic = p_ic;
24339
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024340 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024341 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24342 {
24343 if (!HASHITEM_EMPTY(hi))
24344 {
24345 --todo;
24346 fp = HI2UF(hi);
24347 if (!isdigit(*fp->uf_name)
24348 && vim_regexec(&regmatch, fp->uf_name, 0))
24349 list_func_head(fp, FALSE);
24350 }
24351 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024352 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024353 }
24354 }
24355 if (*p == '/')
24356 ++p;
24357 eap->nextcmd = check_nextcmd(p);
24358 return;
24359 }
24360
24361 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024362 * Get the function name. There are these situations:
24363 * func normal function name
24364 * "name" == func, "fudi.fd_dict" == NULL
24365 * dict.func new dictionary entry
24366 * "name" == NULL, "fudi.fd_dict" set,
24367 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24368 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024369 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024370 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24371 * dict.func existing dict entry that's not a Funcref
24372 * "name" == NULL, "fudi.fd_dict" set,
24373 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024374 * s:func script-local function name
24375 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024378 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024379 paren = (vim_strchr(p, '(') != NULL);
24380 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381 {
24382 /*
24383 * Return on an invalid expression in braces, unless the expression
24384 * evaluation has been cancelled due to an aborting error, an
24385 * interrupt, or an exception.
24386 */
24387 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024388 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024389 if (!eap->skip && fudi.fd_newkey != NULL)
24390 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024391 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 else
24395 eap->skip = TRUE;
24396 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024397
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398 /* An error in a function call during evaluation of an expression in magic
24399 * braces should not cause the function not to be defined. */
24400 saved_did_emsg = did_emsg;
24401 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024402
24403 /*
24404 * ":function func" with only function name: list function.
24405 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024406 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407 {
24408 if (!ends_excmd(*skipwhite(p)))
24409 {
24410 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024411 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 }
24413 eap->nextcmd = check_nextcmd(p);
24414 if (eap->nextcmd != NULL)
24415 *p = NUL;
24416 if (!eap->skip && !got_int)
24417 {
24418 fp = find_func(name);
24419 if (fp != NULL)
24420 {
24421 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024422 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024423 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024424 if (FUNCLINE(fp, j) == NULL)
24425 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024426 msg_putchar('\n');
24427 msg_outnum((long)(j + 1));
24428 if (j < 9)
24429 msg_putchar(' ');
24430 if (j < 99)
24431 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024432 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024433 out_flush(); /* show a line at a time */
24434 ui_breakcheck();
24435 }
24436 if (!got_int)
24437 {
24438 msg_putchar('\n');
24439 msg_puts((char_u *)" endfunction");
24440 }
24441 }
24442 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024443 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024444 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024445 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 }
24447
24448 /*
24449 * ":function name(arg1, arg2)" Define function.
24450 */
24451 p = skipwhite(p);
24452 if (*p != '(')
24453 {
24454 if (!eap->skip)
24455 {
24456 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024457 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458 }
24459 /* attempt to continue by skipping some text */
24460 if (vim_strchr(p, '(') != NULL)
24461 p = vim_strchr(p, '(');
24462 }
24463 p = skipwhite(p + 1);
24464
24465 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24466 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24467
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024468 if (!eap->skip)
24469 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024470 /* Check the name of the function. Unless it's a dictionary function
24471 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024472 if (name != NULL)
24473 arg = name;
24474 else
24475 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024476 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024477 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24478 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024479 {
24480 if (*arg == K_SPECIAL)
24481 j = 3;
24482 else
24483 j = 0;
24484 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24485 : eval_isnamec(arg[j])))
24486 ++j;
24487 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024488 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024489 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024490 /* Disallow using the g: dict. */
24491 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24492 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024493 }
24494
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495 /*
24496 * Isolate the arguments: "arg1, arg2, ...)"
24497 */
24498 while (*p != ')')
24499 {
24500 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24501 {
24502 varargs = TRUE;
24503 p += 3;
24504 mustend = TRUE;
24505 }
24506 else
24507 {
24508 arg = p;
24509 while (ASCII_ISALNUM(*p) || *p == '_')
24510 ++p;
24511 if (arg == p || isdigit(*arg)
24512 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24513 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24514 {
24515 if (!eap->skip)
24516 EMSG2(_("E125: Illegal argument: %s"), arg);
24517 break;
24518 }
24519 if (ga_grow(&newargs, 1) == FAIL)
24520 goto erret;
24521 c = *p;
24522 *p = NUL;
24523 arg = vim_strsave(arg);
24524 if (arg == NULL)
24525 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024526
24527 /* Check for duplicate argument name. */
24528 for (i = 0; i < newargs.ga_len; ++i)
24529 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24530 {
24531 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024532 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024533 goto erret;
24534 }
24535
Bram Moolenaar071d4272004-06-13 20:20:40 +000024536 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24537 *p = c;
24538 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539 if (*p == ',')
24540 ++p;
24541 else
24542 mustend = TRUE;
24543 }
24544 p = skipwhite(p);
24545 if (mustend && *p != ')')
24546 {
24547 if (!eap->skip)
24548 EMSG2(_(e_invarg2), eap->arg);
24549 break;
24550 }
24551 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024552 if (*p != ')')
24553 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024554 ++p; /* skip the ')' */
24555
Bram Moolenaare9a41262005-01-15 22:18:47 +000024556 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024557 for (;;)
24558 {
24559 p = skipwhite(p);
24560 if (STRNCMP(p, "range", 5) == 0)
24561 {
24562 flags |= FC_RANGE;
24563 p += 5;
24564 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024565 else if (STRNCMP(p, "dict", 4) == 0)
24566 {
24567 flags |= FC_DICT;
24568 p += 4;
24569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 else if (STRNCMP(p, "abort", 5) == 0)
24571 {
24572 flags |= FC_ABORT;
24573 p += 5;
24574 }
24575 else
24576 break;
24577 }
24578
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024579 /* When there is a line break use what follows for the function body.
24580 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24581 if (*p == '\n')
24582 line_arg = p + 1;
24583 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 EMSG(_(e_trailing));
24585
24586 /*
24587 * Read the body of the function, until ":endfunction" is found.
24588 */
24589 if (KeyTyped)
24590 {
24591 /* Check if the function already exists, don't let the user type the
24592 * whole function before telling him it doesn't work! For a script we
24593 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024594 if (!eap->skip && !eap->forceit)
24595 {
24596 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24597 EMSG(_(e_funcdict));
24598 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024599 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024602 if (!eap->skip && did_emsg)
24603 goto erret;
24604
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605 msg_putchar('\n'); /* don't overwrite the function name */
24606 cmdline_row = msg_row;
24607 }
24608
24609 indent = 2;
24610 nesting = 0;
24611 for (;;)
24612 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024613 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024614 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024615 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024616 saved_wait_return = FALSE;
24617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024619 sourcing_lnum_off = sourcing_lnum;
24620
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024621 if (line_arg != NULL)
24622 {
24623 /* Use eap->arg, split up in parts by line breaks. */
24624 theline = line_arg;
24625 p = vim_strchr(theline, '\n');
24626 if (p == NULL)
24627 line_arg += STRLEN(line_arg);
24628 else
24629 {
24630 *p = NUL;
24631 line_arg = p + 1;
24632 }
24633 }
24634 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635 theline = getcmdline(':', 0L, indent);
24636 else
24637 theline = eap->getline(':', eap->cookie, indent);
24638 if (KeyTyped)
24639 lines_left = Rows - 1;
24640 if (theline == NULL)
24641 {
24642 EMSG(_("E126: Missing :endfunction"));
24643 goto erret;
24644 }
24645
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024646 /* Detect line continuation: sourcing_lnum increased more than one. */
24647 if (sourcing_lnum > sourcing_lnum_off + 1)
24648 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24649 else
24650 sourcing_lnum_off = 0;
24651
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652 if (skip_until != NULL)
24653 {
24654 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24655 * don't check for ":endfunc". */
24656 if (STRCMP(theline, skip_until) == 0)
24657 {
24658 vim_free(skip_until);
24659 skip_until = NULL;
24660 }
24661 }
24662 else
24663 {
24664 /* skip ':' and blanks*/
24665 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24666 ;
24667
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024668 /* Check for "endfunction". */
24669 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024671 if (line_arg == NULL)
24672 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 break;
24674 }
24675
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024676 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677 * at "end". */
24678 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24679 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024680 else if (STRNCMP(p, "if", 2) == 0
24681 || STRNCMP(p, "wh", 2) == 0
24682 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024683 || STRNCMP(p, "try", 3) == 0)
24684 indent += 2;
24685
24686 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024687 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024689 if (*p == '!')
24690 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024691 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024692 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024693 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024695 ++nesting;
24696 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697 }
24698 }
24699
24700 /* Check for ":append" or ":insert". */
24701 p = skip_range(p, NULL);
24702 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24703 || (p[0] == 'i'
24704 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24705 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24706 skip_until = vim_strsave((char_u *)".");
24707
24708 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24709 arg = skipwhite(skiptowhite(p));
24710 if (arg[0] == '<' && arg[1] =='<'
24711 && ((p[0] == 'p' && p[1] == 'y'
24712 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24713 || (p[0] == 'p' && p[1] == 'e'
24714 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24715 || (p[0] == 't' && p[1] == 'c'
24716 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024717 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24718 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24720 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024721 || (p[0] == 'm' && p[1] == 'z'
24722 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723 ))
24724 {
24725 /* ":python <<" continues until a dot, like ":append" */
24726 p = skipwhite(arg + 2);
24727 if (*p == NUL)
24728 skip_until = vim_strsave((char_u *)".");
24729 else
24730 skip_until = vim_strsave(p);
24731 }
24732 }
24733
24734 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024735 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024736 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024737 if (line_arg == NULL)
24738 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024740 }
24741
24742 /* Copy the line to newly allocated memory. get_one_sourceline()
24743 * allocates 250 bytes per line, this saves 80% on average. The cost
24744 * is an extra alloc/free. */
24745 p = vim_strsave(theline);
24746 if (p != NULL)
24747 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024748 if (line_arg == NULL)
24749 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024750 theline = p;
24751 }
24752
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024753 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24754
24755 /* Add NULL lines for continuation lines, so that the line count is
24756 * equal to the index in the growarray. */
24757 while (sourcing_lnum_off-- > 0)
24758 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024759
24760 /* Check for end of eap->arg. */
24761 if (line_arg != NULL && *line_arg == NUL)
24762 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763 }
24764
24765 /* Don't define the function when skipping commands or when an error was
24766 * detected. */
24767 if (eap->skip || did_emsg)
24768 goto erret;
24769
24770 /*
24771 * If there are no errors, add the function
24772 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024773 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024774 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024775 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024776 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024777 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024778 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024779 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024780 goto erret;
24781 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024782
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024783 fp = find_func(name);
24784 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024786 if (!eap->forceit)
24787 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024788 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024789 goto erret;
24790 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024791 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024792 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024793 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024794 name);
24795 goto erret;
24796 }
24797 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024798 ga_clear_strings(&(fp->uf_args));
24799 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024800 vim_free(name);
24801 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803 }
24804 else
24805 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024806 char numbuf[20];
24807
24808 fp = NULL;
24809 if (fudi.fd_newkey == NULL && !eap->forceit)
24810 {
24811 EMSG(_(e_funcdict));
24812 goto erret;
24813 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024814 if (fudi.fd_di == NULL)
24815 {
24816 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024817 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024818 goto erret;
24819 }
24820 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024821 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024822 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024823
24824 /* Give the function a sequential number. Can only be used with a
24825 * Funcref! */
24826 vim_free(name);
24827 sprintf(numbuf, "%d", ++func_nr);
24828 name = vim_strsave((char_u *)numbuf);
24829 if (name == NULL)
24830 goto erret;
24831 }
24832
24833 if (fp == NULL)
24834 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024835 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024836 {
24837 int slen, plen;
24838 char_u *scriptname;
24839
24840 /* Check that the autoload name matches the script name. */
24841 j = FAIL;
24842 if (sourcing_name != NULL)
24843 {
24844 scriptname = autoload_name(name);
24845 if (scriptname != NULL)
24846 {
24847 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024848 plen = (int)STRLEN(p);
24849 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024850 if (slen > plen && fnamecmp(p,
24851 sourcing_name + slen - plen) == 0)
24852 j = OK;
24853 vim_free(scriptname);
24854 }
24855 }
24856 if (j == FAIL)
24857 {
24858 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24859 goto erret;
24860 }
24861 }
24862
24863 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 if (fp == NULL)
24865 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024866
24867 if (fudi.fd_dict != NULL)
24868 {
24869 if (fudi.fd_di == NULL)
24870 {
24871 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024872 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024873 if (fudi.fd_di == NULL)
24874 {
24875 vim_free(fp);
24876 goto erret;
24877 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024878 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24879 {
24880 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024881 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024882 goto erret;
24883 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024884 }
24885 else
24886 /* overwrite existing dict entry */
24887 clear_tv(&fudi.fd_di->di_tv);
24888 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024889 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024890 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024891 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024892
24893 /* behave like "dict" was used */
24894 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024895 }
24896
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024898 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024899 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24900 {
24901 vim_free(fp);
24902 goto erret;
24903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024904 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024905 fp->uf_args = newargs;
24906 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024907#ifdef FEAT_PROFILE
24908 fp->uf_tml_count = NULL;
24909 fp->uf_tml_total = NULL;
24910 fp->uf_tml_self = NULL;
24911 fp->uf_profiling = FALSE;
24912 if (prof_def_func())
24913 func_do_profile(fp);
24914#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024915 fp->uf_varargs = varargs;
24916 fp->uf_flags = flags;
24917 fp->uf_calls = 0;
24918 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024919 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920
24921erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 ga_clear_strings(&newargs);
24923 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024924ret_free:
24925 vim_free(skip_until);
24926 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024929 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930}
24931
24932/*
24933 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024934 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024936 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024937 * TFN_INT: internal function name OK
24938 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024939 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 * Advances "pp" to just after the function name (if no error).
24941 */
24942 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024943trans_function_name(
24944 char_u **pp,
24945 int skip, /* only find the end, don't evaluate */
24946 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024947 funcdict_T *fdp, /* return: info about dictionary used */
24948 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024949{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024950 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951 char_u *start;
24952 char_u *end;
24953 int lead;
24954 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024956 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024957
24958 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024959 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024960 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024961
24962 /* Check for hard coded <SNR>: already translated function ID (from a user
24963 * command). */
24964 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24965 && (*pp)[2] == (int)KE_SNR)
24966 {
24967 *pp += 3;
24968 len = get_id_len(pp) + 3;
24969 return vim_strnsave(start, len);
24970 }
24971
24972 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24973 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024974 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024975 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024976 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024977
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024978 /* Note that TFN_ flags use the same values as GLV_ flags. */
24979 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024980 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024981 if (end == start)
24982 {
24983 if (!skip)
24984 EMSG(_("E129: Function name required"));
24985 goto theend;
24986 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024987 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024988 {
24989 /*
24990 * Report an invalid expression in braces, unless the expression
24991 * evaluation has been cancelled due to an aborting error, an
24992 * interrupt, or an exception.
24993 */
24994 if (!aborting())
24995 {
24996 if (end != NULL)
24997 EMSG2(_(e_invarg2), start);
24998 }
24999 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025000 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025001 goto theend;
25002 }
25003
25004 if (lv.ll_tv != NULL)
25005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025006 if (fdp != NULL)
25007 {
25008 fdp->fd_dict = lv.ll_dict;
25009 fdp->fd_newkey = lv.ll_newkey;
25010 lv.ll_newkey = NULL;
25011 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025012 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025013 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
25014 {
25015 name = vim_strsave(lv.ll_tv->vval.v_string);
25016 *pp = end;
25017 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010025018 else if (lv.ll_tv->v_type == VAR_PARTIAL
25019 && lv.ll_tv->vval.v_partial != NULL)
25020 {
25021 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
25022 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010025023 if (partial != NULL)
25024 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010025025 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025026 else
25027 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025028 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
25029 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025030 EMSG(_(e_funcref));
25031 else
25032 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025033 name = NULL;
25034 }
25035 goto theend;
25036 }
25037
25038 if (lv.ll_name == NULL)
25039 {
25040 /* Error found, but continue after the function name. */
25041 *pp = end;
25042 goto theend;
25043 }
25044
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025045 /* Check if the name is a Funcref. If so, use the value. */
25046 if (lv.ll_exp_name != NULL)
25047 {
25048 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010025049 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025050 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025051 if (name == lv.ll_exp_name)
25052 name = NULL;
25053 }
25054 else
25055 {
25056 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010025057 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025058 if (name == *pp)
25059 name = NULL;
25060 }
25061 if (name != NULL)
25062 {
25063 name = vim_strsave(name);
25064 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020025065 if (STRNCMP(name, "<SNR>", 5) == 0)
25066 {
25067 /* Change "<SNR>" to the byte sequence. */
25068 name[0] = K_SPECIAL;
25069 name[1] = KS_EXTRA;
25070 name[2] = (int)KE_SNR;
25071 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
25072 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025073 goto theend;
25074 }
25075
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025076 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000025077 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025078 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000025079 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
25080 && STRNCMP(lv.ll_name, "s:", 2) == 0)
25081 {
25082 /* When there was "s:" already or the name expanded to get a
25083 * leading "s:" then remove it. */
25084 lv.ll_name += 2;
25085 len -= 2;
25086 lead = 2;
25087 }
25088 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025089 else
Bram Moolenaara7043832005-01-21 11:56:39 +000025090 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025091 /* skip over "s:" and "g:" */
25092 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000025093 lv.ll_name += 2;
25094 len = (int)(end - lv.ll_name);
25095 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025096
25097 /*
25098 * Copy the function name to allocated memory.
25099 * Accept <SID>name() inside a script, translate into <SNR>123_name().
25100 * Accept <SNR>123_name() outside a script.
25101 */
25102 if (skip)
25103 lead = 0; /* do nothing */
25104 else if (lead > 0)
25105 {
25106 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000025107 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
25108 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025109 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000025110 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025111 if (current_SID <= 0)
25112 {
25113 EMSG(_(e_usingsid));
25114 goto theend;
25115 }
25116 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
25117 lead += (int)STRLEN(sid_buf);
25118 }
25119 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025120 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025121 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025122 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025123 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025124 goto theend;
25125 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025126 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025127 {
25128 char_u *cp = vim_strchr(lv.ll_name, ':');
25129
25130 if (cp != NULL && cp < end)
25131 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025132 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025133 goto theend;
25134 }
25135 }
25136
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025137 name = alloc((unsigned)(len + lead + 1));
25138 if (name != NULL)
25139 {
25140 if (lead > 0)
25141 {
25142 name[0] = K_SPECIAL;
25143 name[1] = KS_EXTRA;
25144 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000025145 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025146 STRCPY(name + 3, sid_buf);
25147 }
25148 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025149 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025150 }
25151 *pp = end;
25152
25153theend:
25154 clear_lval(&lv);
25155 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156}
25157
25158/*
25159 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
25160 * Return 2 if "p" starts with "s:".
25161 * Return 0 otherwise.
25162 */
25163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025164eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025165{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010025166 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
25167 * the standard library function. */
25168 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
25169 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025170 return 5;
25171 if (p[0] == 's' && p[1] == ':')
25172 return 2;
25173 return 0;
25174}
25175
25176/*
25177 * Return TRUE if "p" starts with "<SID>" or "s:".
25178 * Only works if eval_fname_script() returned non-zero for "p"!
25179 */
25180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025181eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182{
25183 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
25184}
25185
25186/*
25187 * List the head of the function: "name(arg1, arg2)".
25188 */
25189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025190list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025191{
25192 int j;
25193
25194 msg_start();
25195 if (indent)
25196 MSG_PUTS(" ");
25197 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025198 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199 {
25200 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025201 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202 }
25203 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025204 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025206 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207 {
25208 if (j)
25209 MSG_PUTS(", ");
25210 msg_puts(FUNCARG(fp, j));
25211 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025212 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025213 {
25214 if (j)
25215 MSG_PUTS(", ");
25216 MSG_PUTS("...");
25217 }
25218 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020025219 if (fp->uf_flags & FC_ABORT)
25220 MSG_PUTS(" abort");
25221 if (fp->uf_flags & FC_RANGE)
25222 MSG_PUTS(" range");
25223 if (fp->uf_flags & FC_DICT)
25224 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025225 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000025226 if (p_verbose > 0)
25227 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228}
25229
25230/*
25231 * Find a function by name, return pointer to it in ufuncs.
25232 * Return NULL for unknown function.
25233 */
25234 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025235find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025237 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025238
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025239 hi = hash_find(&func_hashtab, name);
25240 if (!HASHITEM_EMPTY(hi))
25241 return HI2UF(hi);
25242 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243}
25244
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025245#if defined(EXITFREE) || defined(PROTO)
25246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025247free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025248{
25249 hashitem_T *hi;
25250
25251 /* Need to start all over every time, because func_free() may change the
25252 * hash table. */
25253 while (func_hashtab.ht_used > 0)
25254 for (hi = func_hashtab.ht_array; ; ++hi)
25255 if (!HASHITEM_EMPTY(hi))
25256 {
25257 func_free(HI2UF(hi));
25258 break;
25259 }
25260}
25261#endif
25262
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025264translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025265{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025266 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025267 return find_internal_func(name) >= 0;
25268 return find_func(name) != NULL;
25269}
25270
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025271/*
25272 * Return TRUE if a function "name" exists.
25273 */
25274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025275function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025276{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000025277 char_u *nm = name;
25278 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025279 int n = FALSE;
25280
Bram Moolenaar6d977d62014-01-14 15:24:39 +010025281 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025282 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025283 nm = skipwhite(nm);
25284
25285 /* Only accept "funcname", "funcname ", "funcname (..." and
25286 * "funcname(...", not "funcname!...". */
25287 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025288 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025289 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025290 return n;
25291}
25292
Bram Moolenaara1544c02013-05-30 12:35:52 +020025293 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025294get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025295{
25296 char_u *nm = name;
25297 char_u *p;
25298
Bram Moolenaar65639032016-03-16 21:40:30 +010025299 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025300
25301 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025302 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025303 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025304
Bram Moolenaara1544c02013-05-30 12:35:52 +020025305 vim_free(p);
25306 return NULL;
25307}
25308
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025309/*
25310 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025311 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25312 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025313 */
25314 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025315builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025316{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025317 char_u *p;
25318
25319 if (!ASCII_ISLOWER(name[0]))
25320 return FALSE;
25321 p = vim_strchr(name, AUTOLOAD_CHAR);
25322 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025323}
25324
Bram Moolenaar05159a02005-02-26 23:04:13 +000025325#if defined(FEAT_PROFILE) || defined(PROTO)
25326/*
25327 * Start profiling function "fp".
25328 */
25329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025330func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025331{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025332 int len = fp->uf_lines.ga_len;
25333
25334 if (len == 0)
25335 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025336 fp->uf_tm_count = 0;
25337 profile_zero(&fp->uf_tm_self);
25338 profile_zero(&fp->uf_tm_total);
25339 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025340 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025341 if (fp->uf_tml_total == NULL)
25342 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025343 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025344 if (fp->uf_tml_self == NULL)
25345 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025346 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025347 fp->uf_tml_idx = -1;
25348 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25349 || fp->uf_tml_self == NULL)
25350 return; /* out of memory */
25351
25352 fp->uf_profiling = TRUE;
25353}
25354
25355/*
25356 * Dump the profiling results for all functions in file "fd".
25357 */
25358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025359func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025360{
25361 hashitem_T *hi;
25362 int todo;
25363 ufunc_T *fp;
25364 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025365 ufunc_T **sorttab;
25366 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025367
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025368 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025369 if (todo == 0)
25370 return; /* nothing to dump */
25371
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025372 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025373
Bram Moolenaar05159a02005-02-26 23:04:13 +000025374 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25375 {
25376 if (!HASHITEM_EMPTY(hi))
25377 {
25378 --todo;
25379 fp = HI2UF(hi);
25380 if (fp->uf_profiling)
25381 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025382 if (sorttab != NULL)
25383 sorttab[st_len++] = fp;
25384
Bram Moolenaar05159a02005-02-26 23:04:13 +000025385 if (fp->uf_name[0] == K_SPECIAL)
25386 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25387 else
25388 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25389 if (fp->uf_tm_count == 1)
25390 fprintf(fd, "Called 1 time\n");
25391 else
25392 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25393 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25394 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25395 fprintf(fd, "\n");
25396 fprintf(fd, "count total (s) self (s)\n");
25397
25398 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25399 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025400 if (FUNCLINE(fp, i) == NULL)
25401 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025402 prof_func_line(fd, fp->uf_tml_count[i],
25403 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025404 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25405 }
25406 fprintf(fd, "\n");
25407 }
25408 }
25409 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025410
25411 if (sorttab != NULL && st_len > 0)
25412 {
25413 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25414 prof_total_cmp);
25415 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25416 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25417 prof_self_cmp);
25418 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25419 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025420
25421 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025422}
Bram Moolenaar73830342005-02-28 22:48:19 +000025423
25424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025425prof_sort_list(
25426 FILE *fd,
25427 ufunc_T **sorttab,
25428 int st_len,
25429 char *title,
25430 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025431{
25432 int i;
25433 ufunc_T *fp;
25434
25435 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25436 fprintf(fd, "count total (s) self (s) function\n");
25437 for (i = 0; i < 20 && i < st_len; ++i)
25438 {
25439 fp = sorttab[i];
25440 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25441 prefer_self);
25442 if (fp->uf_name[0] == K_SPECIAL)
25443 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25444 else
25445 fprintf(fd, " %s()\n", fp->uf_name);
25446 }
25447 fprintf(fd, "\n");
25448}
25449
25450/*
25451 * Print the count and times for one function or function line.
25452 */
25453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025454prof_func_line(
25455 FILE *fd,
25456 int count,
25457 proftime_T *total,
25458 proftime_T *self,
25459 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025460{
25461 if (count > 0)
25462 {
25463 fprintf(fd, "%5d ", count);
25464 if (prefer_self && profile_equal(total, self))
25465 fprintf(fd, " ");
25466 else
25467 fprintf(fd, "%s ", profile_msg(total));
25468 if (!prefer_self && profile_equal(total, self))
25469 fprintf(fd, " ");
25470 else
25471 fprintf(fd, "%s ", profile_msg(self));
25472 }
25473 else
25474 fprintf(fd, " ");
25475}
25476
25477/*
25478 * Compare function for total time sorting.
25479 */
25480 static int
25481#ifdef __BORLANDC__
25482_RTLENTRYF
25483#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025484prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025485{
25486 ufunc_T *p1, *p2;
25487
25488 p1 = *(ufunc_T **)s1;
25489 p2 = *(ufunc_T **)s2;
25490 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25491}
25492
25493/*
25494 * Compare function for self time sorting.
25495 */
25496 static int
25497#ifdef __BORLANDC__
25498_RTLENTRYF
25499#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025500prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025501{
25502 ufunc_T *p1, *p2;
25503
25504 p1 = *(ufunc_T **)s1;
25505 p2 = *(ufunc_T **)s2;
25506 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25507}
25508
Bram Moolenaar05159a02005-02-26 23:04:13 +000025509#endif
25510
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025511/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025512 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025513 * Return TRUE if a package was loaded.
25514 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025516script_autoload(
25517 char_u *name,
25518 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025519{
25520 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025521 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025522 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025523 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025524
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025525 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025526 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025527 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025528 return FALSE;
25529
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025530 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025531
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025532 /* Find the name in the list of previously loaded package names. Skip
25533 * "autoload/", it's always the same. */
25534 for (i = 0; i < ga_loaded.ga_len; ++i)
25535 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25536 break;
25537 if (!reload && i < ga_loaded.ga_len)
25538 ret = FALSE; /* was loaded already */
25539 else
25540 {
25541 /* Remember the name if it wasn't loaded already. */
25542 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25543 {
25544 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25545 tofree = NULL;
25546 }
25547
25548 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025549 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025550 ret = TRUE;
25551 }
25552
25553 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025554 return ret;
25555}
25556
25557/*
25558 * Return the autoload script name for a function or variable name.
25559 * Returns NULL when out of memory.
25560 */
25561 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025562autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025563{
25564 char_u *p;
25565 char_u *scriptname;
25566
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025567 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025568 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25569 if (scriptname == NULL)
25570 return FALSE;
25571 STRCPY(scriptname, "autoload/");
25572 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025573 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025574 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025575 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025576 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025577 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025578}
25579
Bram Moolenaar071d4272004-06-13 20:20:40 +000025580#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25581
25582/*
25583 * Function given to ExpandGeneric() to obtain the list of user defined
25584 * function names.
25585 */
25586 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025587get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025588{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025589 static long_u done;
25590 static hashitem_T *hi;
25591 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592
25593 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025595 done = 0;
25596 hi = func_hashtab.ht_array;
25597 }
25598 if (done < func_hashtab.ht_used)
25599 {
25600 if (done++ > 0)
25601 ++hi;
25602 while (HASHITEM_EMPTY(hi))
25603 ++hi;
25604 fp = HI2UF(hi);
25605
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025606 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025607 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025608
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025609 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25610 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611
25612 cat_func_name(IObuff, fp);
25613 if (xp->xp_context != EXPAND_USER_FUNC)
25614 {
25615 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025616 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 STRCAT(IObuff, ")");
25618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619 return IObuff;
25620 }
25621 return NULL;
25622}
25623
25624#endif /* FEAT_CMDL_COMPL */
25625
25626/*
25627 * Copy the function name of "fp" to buffer "buf".
25628 * "buf" must be able to hold the function name plus three bytes.
25629 * Takes care of script-local function names.
25630 */
25631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025632cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025633{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025634 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635 {
25636 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025637 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638 }
25639 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025640 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641}
25642
25643/*
25644 * ":delfunction {name}"
25645 */
25646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025647ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025649 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650 char_u *p;
25651 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025652 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653
25654 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025655 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025656 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025658 {
25659 if (fudi.fd_dict != NULL && !eap->skip)
25660 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663 if (!ends_excmd(*skipwhite(p)))
25664 {
25665 vim_free(name);
25666 EMSG(_(e_trailing));
25667 return;
25668 }
25669 eap->nextcmd = check_nextcmd(p);
25670 if (eap->nextcmd != NULL)
25671 *p = NUL;
25672
25673 if (!eap->skip)
25674 fp = find_func(name);
25675 vim_free(name);
25676
25677 if (!eap->skip)
25678 {
25679 if (fp == NULL)
25680 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025681 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682 return;
25683 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025684 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025685 {
25686 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25687 return;
25688 }
25689
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025690 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025691 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025692 /* Delete the dict item that refers to the function, it will
25693 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025694 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025695 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025696 else
25697 func_free(fp);
25698 }
25699}
25700
25701/*
25702 * Free a function and remove it from the list of functions.
25703 */
25704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025705func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025706{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025707 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025708
25709 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025710 ga_clear_strings(&(fp->uf_args));
25711 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025712#ifdef FEAT_PROFILE
25713 vim_free(fp->uf_tml_count);
25714 vim_free(fp->uf_tml_total);
25715 vim_free(fp->uf_tml_self);
25716#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025717
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025718 /* remove the function from the function hashtable */
25719 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25720 if (HASHITEM_EMPTY(hi))
25721 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025722 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025723 hash_remove(&func_hashtab, hi);
25724
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025725 vim_free(fp);
25726}
25727
25728/*
25729 * Unreference a Function: decrement the reference count and free it when it
25730 * becomes zero. Only for numbered functions.
25731 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025732 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025733func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025734{
25735 ufunc_T *fp;
25736
25737 if (name != NULL && isdigit(*name))
25738 {
25739 fp = find_func(name);
25740 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025741 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025742#ifdef EXITFREE
25743 if (!entered_free_all_mem)
25744#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025745 EMSG2(_(e_intern2), "func_unref()");
25746 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025747 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025748 {
25749 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025750 * when "uf_calls" becomes zero. */
25751 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025752 func_free(fp);
25753 }
25754 }
25755}
25756
25757/*
25758 * Count a reference to a Function.
25759 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025761func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025762{
25763 ufunc_T *fp;
25764
25765 if (name != NULL && isdigit(*name))
25766 {
25767 fp = find_func(name);
25768 if (fp == NULL)
25769 EMSG2(_(e_intern2), "func_ref()");
25770 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025771 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025772 }
25773}
25774
25775/*
25776 * Call a user function.
25777 */
25778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025779call_user_func(
25780 ufunc_T *fp, /* pointer to function */
25781 int argcount, /* nr of args */
25782 typval_T *argvars, /* arguments */
25783 typval_T *rettv, /* return value */
25784 linenr_T firstline, /* first line of range */
25785 linenr_T lastline, /* last line of range */
25786 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787{
Bram Moolenaar33570922005-01-25 22:26:29 +000025788 char_u *save_sourcing_name;
25789 linenr_T save_sourcing_lnum;
25790 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025791 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025792 int save_did_emsg;
25793 static int depth = 0;
25794 dictitem_T *v;
25795 int fixvar_idx = 0; /* index in fixvar[] */
25796 int i;
25797 int ai;
25798 char_u numbuf[NUMBUFLEN];
25799 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025800 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025801#ifdef FEAT_PROFILE
25802 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025803 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025805
25806 /* If depth of calling is getting too high, don't execute the function */
25807 if (depth >= p_mfd)
25808 {
25809 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025810 rettv->v_type = VAR_NUMBER;
25811 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812 return;
25813 }
25814 ++depth;
25815
25816 line_breakcheck(); /* check for CTRL-C hit */
25817
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025818 fc = (funccall_T *)alloc(sizeof(funccall_T));
25819 fc->caller = current_funccal;
25820 current_funccal = fc;
25821 fc->func = fp;
25822 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025823 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025824 fc->linenr = 0;
25825 fc->returned = FALSE;
25826 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025827 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025828 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25829 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830
Bram Moolenaar33570922005-01-25 22:26:29 +000025831 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025832 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025833 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25834 * each argument variable and saves a lot of time.
25835 */
25836 /*
25837 * Init l: variables.
25838 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025839 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025840 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025841 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025842 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25843 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025844 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025845 name = v->di_key;
25846 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025847 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025848 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025849 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025850 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025851 v->di_tv.vval.v_dict = selfdict;
25852 ++selfdict->dv_refcount;
25853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025854
Bram Moolenaar33570922005-01-25 22:26:29 +000025855 /*
25856 * Init a: variables.
25857 * Set a:0 to "argcount".
25858 * Set a:000 to a list with room for the "..." arguments.
25859 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025860 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025861 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025862 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025863 /* Use "name" to avoid a warning from some compiler that checks the
25864 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025865 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025866 name = v->di_key;
25867 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025868 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025869 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025870 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025871 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025872 v->di_tv.vval.v_list = &fc->l_varlist;
25873 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25874 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25875 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025876
25877 /*
25878 * Set a:firstline to "firstline" and a:lastline to "lastline".
25879 * Set a:name to named arguments.
25880 * Set a:N to the "..." arguments.
25881 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025882 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025883 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025884 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025885 (varnumber_T)lastline);
25886 for (i = 0; i < argcount; ++i)
25887 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025888 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025889 if (ai < 0)
25890 /* named argument a:name */
25891 name = FUNCARG(fp, i);
25892 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025893 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025894 /* "..." argument a:1, a:2, etc. */
25895 sprintf((char *)numbuf, "%d", ai + 1);
25896 name = numbuf;
25897 }
25898 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25899 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025900 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025901 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25902 }
25903 else
25904 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025905 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25906 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025907 if (v == NULL)
25908 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025909 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025910 }
25911 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025912 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025913
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025914 /* Note: the values are copied directly to avoid alloc/free.
25915 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025916 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025917 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025918
25919 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25920 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025921 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25922 fc->l_listitems[ai].li_tv = argvars[i];
25923 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025924 }
25925 }
25926
Bram Moolenaar071d4272004-06-13 20:20:40 +000025927 /* Don't redraw while executing the function. */
25928 ++RedrawingDisabled;
25929 save_sourcing_name = sourcing_name;
25930 save_sourcing_lnum = sourcing_lnum;
25931 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025932 /* need space for function name + ("function " + 3) or "[number]" */
25933 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25934 + STRLEN(fp->uf_name) + 20;
25935 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936 if (sourcing_name != NULL)
25937 {
25938 if (save_sourcing_name != NULL
25939 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025940 sprintf((char *)sourcing_name, "%s[%d]..",
25941 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942 else
25943 STRCPY(sourcing_name, "function ");
25944 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25945
25946 if (p_verbose >= 12)
25947 {
25948 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025949 verbose_enter_scroll();
25950
Bram Moolenaar555b2802005-05-19 21:08:39 +000025951 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952 if (p_verbose >= 14)
25953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025955 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025956 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025957 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025958
25959 msg_puts((char_u *)"(");
25960 for (i = 0; i < argcount; ++i)
25961 {
25962 if (i > 0)
25963 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025964 if (argvars[i].v_type == VAR_NUMBER)
25965 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966 else
25967 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025968 /* Do not want errors such as E724 here. */
25969 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025970 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025971 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025972 if (s != NULL)
25973 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025974 if (vim_strsize(s) > MSG_BUF_CLEN)
25975 {
25976 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25977 s = buf;
25978 }
25979 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025980 vim_free(tofree);
25981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025982 }
25983 }
25984 msg_puts((char_u *)")");
25985 }
25986 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025987
25988 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989 --no_wait_return;
25990 }
25991 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025992#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025993 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025994 {
25995 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25996 func_do_profile(fp);
25997 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025998 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025999 {
26000 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000026001 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026002 profile_zero(&fp->uf_tm_children);
26003 }
26004 script_prof_save(&wait_start);
26005 }
26006#endif
26007
Bram Moolenaar071d4272004-06-13 20:20:40 +000026008 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026009 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026010 save_did_emsg = did_emsg;
26011 did_emsg = FALSE;
26012
26013 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026014 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
26016
26017 --RedrawingDisabled;
26018
26019 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026020 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026022 clear_tv(rettv);
26023 rettv->v_type = VAR_NUMBER;
26024 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025 }
26026
Bram Moolenaar05159a02005-02-26 23:04:13 +000026027#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026028 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026029 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000026030 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000026031 profile_end(&call_start);
26032 profile_sub_wait(&wait_start, &call_start);
26033 profile_add(&fp->uf_tm_total, &call_start);
26034 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026035 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026036 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026037 profile_add(&fc->caller->func->uf_tm_children, &call_start);
26038 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026039 }
26040 }
26041#endif
26042
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043 /* when being verbose, mention the return value */
26044 if (p_verbose >= 12)
26045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000026046 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026047 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026048
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000026050 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026051 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000026052 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026053 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000026054 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000026055 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000026056 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000026057 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000026058 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026059 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000026060
Bram Moolenaar555b2802005-05-19 21:08:39 +000026061 /* The value may be very long. Skip the middle part, so that we
26062 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020026063 * truncate it at the end. Don't want errors such as E724 here. */
26064 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026065 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020026066 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026067 if (s != NULL)
26068 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010026069 if (vim_strsize(s) > MSG_BUF_CLEN)
26070 {
26071 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
26072 s = buf;
26073 }
26074 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026075 vim_free(tofree);
26076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077 }
26078 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026079
26080 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081 --no_wait_return;
26082 }
26083
26084 vim_free(sourcing_name);
26085 sourcing_name = save_sourcing_name;
26086 sourcing_lnum = save_sourcing_lnum;
26087 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026088#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026089 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026090 script_prof_restore(&wait_start);
26091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026092
26093 if (p_verbose >= 12 && sourcing_name != NULL)
26094 {
26095 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026096 verbose_enter_scroll();
26097
Bram Moolenaar555b2802005-05-19 21:08:39 +000026098 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026099 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026100
26101 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102 --no_wait_return;
26103 }
26104
26105 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026106 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026107 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026108
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000026109 /* If the a:000 list and the l: and a: dicts are not referenced we can
26110 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026111 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
26112 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
26113 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
26114 {
26115 free_funccal(fc, FALSE);
26116 }
26117 else
26118 {
26119 hashitem_T *hi;
26120 listitem_T *li;
26121 int todo;
26122
26123 /* "fc" is still in use. This can happen when returning "a:000" or
26124 * assigning "l:" to a global variable.
26125 * Link "fc" in the list for garbage collection later. */
26126 fc->caller = previous_funccal;
26127 previous_funccal = fc;
26128
26129 /* Make a copy of the a: variables, since we didn't do that above. */
26130 todo = (int)fc->l_avars.dv_hashtab.ht_used;
26131 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
26132 {
26133 if (!HASHITEM_EMPTY(hi))
26134 {
26135 --todo;
26136 v = HI2DI(hi);
26137 copy_tv(&v->di_tv, &v->di_tv);
26138 }
26139 }
26140
26141 /* Make a copy of the a:000 items, since we didn't do that above. */
26142 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26143 copy_tv(&li->li_tv, &li->li_tv);
26144 }
26145}
26146
26147/*
26148 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000026149 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026150 */
26151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026152can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026153{
26154 return (fc->l_varlist.lv_copyID != copyID
26155 && fc->l_vars.dv_copyID != copyID
26156 && fc->l_avars.dv_copyID != copyID);
26157}
26158
26159/*
26160 * Free "fc" and what it contains.
26161 */
26162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026163free_funccal(
26164 funccall_T *fc,
26165 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026166{
26167 listitem_T *li;
26168
26169 /* The a: variables typevals may not have been allocated, only free the
26170 * allocated variables. */
26171 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
26172
26173 /* free all l: variables */
26174 vars_clear(&fc->l_vars.dv_hashtab);
26175
26176 /* Free the a:000 variables if they were allocated. */
26177 if (free_val)
26178 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26179 clear_tv(&li->li_tv);
26180
26181 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026182}
26183
26184/*
Bram Moolenaar33570922005-01-25 22:26:29 +000026185 * Add a number variable "name" to dict "dp" with value "nr".
26186 */
26187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026188add_nr_var(
26189 dict_T *dp,
26190 dictitem_T *v,
26191 char *name,
26192 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000026193{
26194 STRCPY(v->di_key, name);
26195 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
26196 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
26197 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000026198 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000026199 v->di_tv.vval.v_number = nr;
26200}
26201
26202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000026203 * ":return [expr]"
26204 */
26205 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026206ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026207{
26208 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000026209 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026210 int returning = FALSE;
26211
26212 if (current_funccal == NULL)
26213 {
26214 EMSG(_("E133: :return not inside a function"));
26215 return;
26216 }
26217
26218 if (eap->skip)
26219 ++emsg_skip;
26220
26221 eap->nextcmd = NULL;
26222 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026223 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026224 {
26225 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026226 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026227 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026228 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026229 }
26230 /* It's safer to return also on error. */
26231 else if (!eap->skip)
26232 {
26233 /*
26234 * Return unless the expression evaluation has been cancelled due to an
26235 * aborting error, an interrupt, or an exception.
26236 */
26237 if (!aborting())
26238 returning = do_return(eap, FALSE, TRUE, NULL);
26239 }
26240
26241 /* When skipping or the return gets pending, advance to the next command
26242 * in this line (!returning). Otherwise, ignore the rest of the line.
26243 * Following lines will be ignored by get_func_line(). */
26244 if (returning)
26245 eap->nextcmd = NULL;
26246 else if (eap->nextcmd == NULL) /* no argument */
26247 eap->nextcmd = check_nextcmd(arg);
26248
26249 if (eap->skip)
26250 --emsg_skip;
26251}
26252
26253/*
26254 * Return from a function. Possibly makes the return pending. Also called
26255 * for a pending return at the ":endtry" or after returning from an extra
26256 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000026257 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026258 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026259 * FALSE when the return gets pending.
26260 */
26261 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026262do_return(
26263 exarg_T *eap,
26264 int reanimate,
26265 int is_cmd,
26266 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026267{
26268 int idx;
26269 struct condstack *cstack = eap->cstack;
26270
26271 if (reanimate)
26272 /* Undo the return. */
26273 current_funccal->returned = FALSE;
26274
26275 /*
26276 * Cleanup (and inactivate) conditionals, but stop when a try conditional
26277 * not in its finally clause (which then is to be executed next) is found.
26278 * In this case, make the ":return" pending for execution at the ":endtry".
26279 * Otherwise, return normally.
26280 */
26281 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26282 if (idx >= 0)
26283 {
26284 cstack->cs_pending[idx] = CSTP_RETURN;
26285
26286 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026287 /* A pending return again gets pending. "rettv" points to an
26288 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026289 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026290 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026291 else
26292 {
26293 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026294 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026296 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026298 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026299 {
26300 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026301 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026302 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026303 else
26304 EMSG(_(e_outofmem));
26305 }
26306 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026307 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026308
26309 if (reanimate)
26310 {
26311 /* The pending return value could be overwritten by a ":return"
26312 * without argument in a finally clause; reset the default
26313 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026314 current_funccal->rettv->v_type = VAR_NUMBER;
26315 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026316 }
26317 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026318 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026319 }
26320 else
26321 {
26322 current_funccal->returned = TRUE;
26323
26324 /* If the return is carried out now, store the return value. For
26325 * a return immediately after reanimation, the value is already
26326 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026327 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026328 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026329 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026330 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026331 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026332 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026333 }
26334 }
26335
26336 return idx < 0;
26337}
26338
26339/*
26340 * Free the variable with a pending return value.
26341 */
26342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026343discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026344{
Bram Moolenaar33570922005-01-25 22:26:29 +000026345 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026346}
26347
26348/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026349 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026350 * is an allocated string. Used by report_pending() for verbose messages.
26351 */
26352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026353get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026354{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026355 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026356 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026357 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026358
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026359 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026360 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026361 if (s == NULL)
26362 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026363
26364 STRCPY(IObuff, ":return ");
26365 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26366 if (STRLEN(s) + 8 >= IOSIZE)
26367 STRCPY(IObuff + IOSIZE - 4, "...");
26368 vim_free(tofree);
26369 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026370}
26371
26372/*
26373 * Get next function line.
26374 * Called by do_cmdline() to get the next line.
26375 * Returns allocated string, or NULL for end of function.
26376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026377 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026378get_func_line(
26379 int c UNUSED,
26380 void *cookie,
26381 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382{
Bram Moolenaar33570922005-01-25 22:26:29 +000026383 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026384 ufunc_T *fp = fcp->func;
26385 char_u *retval;
26386 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026387
26388 /* If breakpoints have been added/deleted need to check for it. */
26389 if (fcp->dbg_tick != debug_tick)
26390 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026391 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026392 sourcing_lnum);
26393 fcp->dbg_tick = debug_tick;
26394 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026395#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026396 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026397 func_line_end(cookie);
26398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026399
Bram Moolenaar05159a02005-02-26 23:04:13 +000026400 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026401 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26402 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026403 retval = NULL;
26404 else
26405 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026406 /* Skip NULL lines (continuation lines). */
26407 while (fcp->linenr < gap->ga_len
26408 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26409 ++fcp->linenr;
26410 if (fcp->linenr >= gap->ga_len)
26411 retval = NULL;
26412 else
26413 {
26414 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26415 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026416#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026417 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026418 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026419#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421 }
26422
26423 /* Did we encounter a breakpoint? */
26424 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26425 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026426 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026427 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026428 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026429 sourcing_lnum);
26430 fcp->dbg_tick = debug_tick;
26431 }
26432
26433 return retval;
26434}
26435
Bram Moolenaar05159a02005-02-26 23:04:13 +000026436#if defined(FEAT_PROFILE) || defined(PROTO)
26437/*
26438 * Called when starting to read a function line.
26439 * "sourcing_lnum" must be correct!
26440 * When skipping lines it may not actually be executed, but we won't find out
26441 * until later and we need to store the time now.
26442 */
26443 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026444func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026445{
26446 funccall_T *fcp = (funccall_T *)cookie;
26447 ufunc_T *fp = fcp->func;
26448
26449 if (fp->uf_profiling && sourcing_lnum >= 1
26450 && sourcing_lnum <= fp->uf_lines.ga_len)
26451 {
26452 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026453 /* Skip continuation lines. */
26454 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26455 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026456 fp->uf_tml_execed = FALSE;
26457 profile_start(&fp->uf_tml_start);
26458 profile_zero(&fp->uf_tml_children);
26459 profile_get_wait(&fp->uf_tml_wait);
26460 }
26461}
26462
26463/*
26464 * Called when actually executing a function line.
26465 */
26466 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026467func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026468{
26469 funccall_T *fcp = (funccall_T *)cookie;
26470 ufunc_T *fp = fcp->func;
26471
26472 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26473 fp->uf_tml_execed = TRUE;
26474}
26475
26476/*
26477 * Called when done with a function line.
26478 */
26479 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026480func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026481{
26482 funccall_T *fcp = (funccall_T *)cookie;
26483 ufunc_T *fp = fcp->func;
26484
26485 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26486 {
26487 if (fp->uf_tml_execed)
26488 {
26489 ++fp->uf_tml_count[fp->uf_tml_idx];
26490 profile_end(&fp->uf_tml_start);
26491 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026492 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026493 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26494 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026495 }
26496 fp->uf_tml_idx = -1;
26497 }
26498}
26499#endif
26500
Bram Moolenaar071d4272004-06-13 20:20:40 +000026501/*
26502 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026503 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026504 */
26505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026506func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026507{
Bram Moolenaar33570922005-01-25 22:26:29 +000026508 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026509
26510 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26511 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026512 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026513 || fcp->returned);
26514}
26515
26516/*
26517 * return TRUE if cookie indicates a function which "abort"s on errors.
26518 */
26519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026520func_has_abort(
26521 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026522{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026523 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524}
26525
26526#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26527typedef enum
26528{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026529 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26530 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26531 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532} var_flavour_T;
26533
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026534static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026535
26536 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026537var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026538{
26539 char_u *p = varname;
26540
26541 if (ASCII_ISUPPER(*p))
26542 {
26543 while (*(++p))
26544 if (ASCII_ISLOWER(*p))
26545 return VAR_FLAVOUR_SESSION;
26546 return VAR_FLAVOUR_VIMINFO;
26547 }
26548 else
26549 return VAR_FLAVOUR_DEFAULT;
26550}
26551#endif
26552
26553#if defined(FEAT_VIMINFO) || defined(PROTO)
26554/*
26555 * Restore global vars that start with a capital from the viminfo file
26556 */
26557 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026558read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026559{
26560 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026561 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026562 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026563 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026564
26565 if (!writing && (find_viminfo_parameter('!') != NULL))
26566 {
26567 tab = vim_strchr(virp->vir_line + 1, '\t');
26568 if (tab != NULL)
26569 {
26570 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026571 switch (*tab)
26572 {
26573 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026574#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026575 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026576#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026577 case 'D': type = VAR_DICT; break;
26578 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026579 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026581
26582 tab = vim_strchr(tab, '\t');
26583 if (tab != NULL)
26584 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026585 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026586 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026587 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026588 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026589#ifdef FEAT_FLOAT
26590 else if (type == VAR_FLOAT)
26591 (void)string2float(tab + 1, &tv.vval.v_float);
26592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026593 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026594 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026595 if (type == VAR_DICT || type == VAR_LIST)
26596 {
26597 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26598
26599 if (etv == NULL)
26600 /* Failed to parse back the dict or list, use it as a
26601 * string. */
26602 tv.v_type = VAR_STRING;
26603 else
26604 {
26605 vim_free(tv.vval.v_string);
26606 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026607 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026608 }
26609 }
26610
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026611 /* when in a function use global variables */
26612 save_funccal = current_funccal;
26613 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026614 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026615 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026616
26617 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026618 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026619 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26620 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026621 }
26622 }
26623 }
26624
26625 return viminfo_readline(virp);
26626}
26627
26628/*
26629 * Write global vars that start with a capital to the viminfo file
26630 */
26631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026632write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026633{
Bram Moolenaar33570922005-01-25 22:26:29 +000026634 hashitem_T *hi;
26635 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026636 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026637 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026638 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026639 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026640 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026641
26642 if (find_viminfo_parameter('!') == NULL)
26643 return;
26644
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026645 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026646
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026647 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026648 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026649 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026650 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026651 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026652 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026653 this_var = HI2DI(hi);
26654 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026655 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026656 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026657 {
26658 case VAR_STRING: s = "STR"; break;
26659 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026660 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026661 case VAR_DICT: s = "DIC"; break;
26662 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026663 case VAR_SPECIAL: s = "XPL"; break;
26664
26665 case VAR_UNKNOWN:
26666 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026667 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026668 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026669 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026670 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026671 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026672 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026673 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026674 if (p != NULL)
26675 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026676 vim_free(tofree);
26677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026678 }
26679 }
26680}
26681#endif
26682
26683#if defined(FEAT_SESSION) || defined(PROTO)
26684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026685store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026686{
Bram Moolenaar33570922005-01-25 22:26:29 +000026687 hashitem_T *hi;
26688 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026689 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026690 char_u *p, *t;
26691
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026692 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026693 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026694 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026695 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026696 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026697 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026698 this_var = HI2DI(hi);
26699 if ((this_var->di_tv.v_type == VAR_NUMBER
26700 || this_var->di_tv.v_type == VAR_STRING)
26701 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026702 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026703 /* Escape special characters with a backslash. Turn a LF and
26704 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026705 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026706 (char_u *)"\\\"\n\r");
26707 if (p == NULL) /* out of memory */
26708 break;
26709 for (t = p; *t != NUL; ++t)
26710 if (*t == '\n')
26711 *t = 'n';
26712 else if (*t == '\r')
26713 *t = 'r';
26714 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026715 this_var->di_key,
26716 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26717 : ' ',
26718 p,
26719 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26720 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026721 || put_eol(fd) == FAIL)
26722 {
26723 vim_free(p);
26724 return FAIL;
26725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026726 vim_free(p);
26727 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026728#ifdef FEAT_FLOAT
26729 else if (this_var->di_tv.v_type == VAR_FLOAT
26730 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26731 {
26732 float_T f = this_var->di_tv.vval.v_float;
26733 int sign = ' ';
26734
26735 if (f < 0)
26736 {
26737 f = -f;
26738 sign = '-';
26739 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026740 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026741 this_var->di_key, sign, f) < 0)
26742 || put_eol(fd) == FAIL)
26743 return FAIL;
26744 }
26745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026746 }
26747 }
26748 return OK;
26749}
26750#endif
26751
Bram Moolenaar661b1822005-07-28 22:36:45 +000026752/*
26753 * Display script name where an item was last set.
26754 * Should only be invoked when 'verbose' is non-zero.
26755 */
26756 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026757last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026758{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026759 char_u *p;
26760
Bram Moolenaar661b1822005-07-28 22:36:45 +000026761 if (scriptID != 0)
26762 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026763 p = home_replace_save(NULL, get_scriptname(scriptID));
26764 if (p != NULL)
26765 {
26766 verbose_enter();
26767 MSG_PUTS(_("\n\tLast set from "));
26768 MSG_PUTS(p);
26769 vim_free(p);
26770 verbose_leave();
26771 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026772 }
26773}
26774
Bram Moolenaard812df62008-11-09 12:46:09 +000026775/*
26776 * List v:oldfiles in a nice way.
26777 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026778 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026779ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026780{
26781 list_T *l = vimvars[VV_OLDFILES].vv_list;
26782 listitem_T *li;
26783 int nr = 0;
26784
26785 if (l == NULL)
26786 msg((char_u *)_("No old files"));
26787 else
26788 {
26789 msg_start();
26790 msg_scroll = TRUE;
26791 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26792 {
26793 msg_outnum((long)++nr);
26794 MSG_PUTS(": ");
26795 msg_outtrans(get_tv_string(&li->li_tv));
26796 msg_putchar('\n');
26797 out_flush(); /* output one line at a time */
26798 ui_breakcheck();
26799 }
26800 /* Assume "got_int" was set to truncate the listing. */
26801 got_int = FALSE;
26802
26803#ifdef FEAT_BROWSE_CMD
26804 if (cmdmod.browse)
26805 {
26806 quit_more = FALSE;
26807 nr = prompt_for_number(FALSE);
26808 msg_starthere();
26809 if (nr > 0)
26810 {
26811 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26812 (long)nr);
26813
26814 if (p != NULL)
26815 {
26816 p = expand_env_save(p);
26817 eap->arg = p;
26818 eap->cmdidx = CMD_edit;
26819 cmdmod.browse = FALSE;
26820 do_exedit(eap, NULL);
26821 vim_free(p);
26822 }
26823 }
26824 }
26825#endif
26826 }
26827}
26828
Bram Moolenaar53744302015-07-17 17:38:22 +020026829/* reset v:option_new, v:option_old and v:option_type */
26830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026831reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026832{
26833 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26834 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26835 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26836}
26837
26838
Bram Moolenaar071d4272004-06-13 20:20:40 +000026839#endif /* FEAT_EVAL */
26840
Bram Moolenaar071d4272004-06-13 20:20:40 +000026841
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026842#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026843
26844#ifdef WIN3264
26845/*
26846 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26847 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026848static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26849static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26850static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026851
26852/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026853 * Get the short path (8.3) for the filename in "fnamep".
26854 * Only works for a valid file name.
26855 * When the path gets longer "fnamep" is changed and the allocated buffer
26856 * is put in "bufp".
26857 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26858 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026859 */
26860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026861get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026862{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026863 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026864 char_u *newbuf;
26865
26866 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026867 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026868 if (l > len - 1)
26869 {
26870 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026871 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026872 newbuf = vim_strnsave(*fnamep, l);
26873 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026874 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026875
26876 vim_free(*bufp);
26877 *fnamep = *bufp = newbuf;
26878
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026879 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026880 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026881 }
26882
26883 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026884 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026885}
26886
26887/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026888 * Get the short path (8.3) for the filename in "fname". The converted
26889 * path is returned in "bufp".
26890 *
26891 * Some of the directories specified in "fname" may not exist. This function
26892 * will shorten the existing directories at the beginning of the path and then
26893 * append the remaining non-existing path.
26894 *
26895 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026896 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026897 * bufp - Pointer to an allocated buffer for the filename.
26898 * fnamelen - Length of the filename pointed to by fname
26899 *
26900 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026901 */
26902 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026903shortpath_for_invalid_fname(
26904 char_u **fname,
26905 char_u **bufp,
26906 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026907{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026908 char_u *short_fname, *save_fname, *pbuf_unused;
26909 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026910 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026911 int old_len, len;
26912 int new_len, sfx_len;
26913 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026914
26915 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026916 old_len = *fnamelen;
26917 save_fname = vim_strnsave(*fname, old_len);
26918 pbuf_unused = NULL;
26919 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026920
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026921 endp = save_fname + old_len - 1; /* Find the end of the copy */
26922 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026923
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026924 /*
26925 * Try shortening the supplied path till it succeeds by removing one
26926 * directory at a time from the tail of the path.
26927 */
26928 len = 0;
26929 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026930 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026931 /* go back one path-separator */
26932 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26933 --endp;
26934 if (endp <= save_fname)
26935 break; /* processed the complete path */
26936
26937 /*
26938 * Replace the path separator with a NUL and try to shorten the
26939 * resulting path.
26940 */
26941 ch = *endp;
26942 *endp = 0;
26943 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026944 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026945 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26946 {
26947 retval = FAIL;
26948 goto theend;
26949 }
26950 *endp = ch; /* preserve the string */
26951
26952 if (len > 0)
26953 break; /* successfully shortened the path */
26954
26955 /* failed to shorten the path. Skip the path separator */
26956 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026957 }
26958
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026959 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026960 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026961 /*
26962 * Succeeded in shortening the path. Now concatenate the shortened
26963 * path with the remaining path at the tail.
26964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026965
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026966 /* Compute the length of the new path. */
26967 sfx_len = (int)(save_endp - endp) + 1;
26968 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026969
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026970 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026971 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026972 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026973 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026974 /* There is not enough space in the currently allocated string,
26975 * copy it to a buffer big enough. */
26976 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026977 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026978 {
26979 retval = FAIL;
26980 goto theend;
26981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026982 }
26983 else
26984 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026985 /* Transfer short_fname to the main buffer (it's big enough),
26986 * unless get_short_pathname() did its work in-place. */
26987 *fname = *bufp = save_fname;
26988 if (short_fname != save_fname)
26989 vim_strncpy(save_fname, short_fname, len);
26990 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026991 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026992
26993 /* concat the not-shortened part of the path */
26994 vim_strncpy(*fname + len, endp, sfx_len);
26995 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026996 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026997
26998theend:
26999 vim_free(pbuf_unused);
27000 vim_free(save_fname);
27001
27002 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027003}
27004
27005/*
27006 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027007 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027008 */
27009 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010027010shortpath_for_partial(
27011 char_u **fnamep,
27012 char_u **bufp,
27013 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027014{
27015 int sepcount, len, tflen;
27016 char_u *p;
27017 char_u *pbuf, *tfname;
27018 int hasTilde;
27019
Bram Moolenaar8c8de832008-06-24 22:58:06 +000027020 /* Count up the path separators from the RHS.. so we know which part
27021 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027022 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027023 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000027024 if (vim_ispathsep(*p))
27025 ++sepcount;
27026
27027 /* Need full path first (use expand_env() to remove a "~/") */
27028 hasTilde = (**fnamep == '~');
27029 if (hasTilde)
27030 pbuf = tfname = expand_env_save(*fnamep);
27031 else
27032 pbuf = tfname = FullName_save(*fnamep, FALSE);
27033
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000027034 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027035
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027036 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
27037 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027038
27039 if (len == 0)
27040 {
27041 /* Don't have a valid filename, so shorten the rest of the
27042 * path if we can. This CAN give us invalid 8.3 filenames, but
27043 * there's not a lot of point in guessing what it might be.
27044 */
27045 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027046 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
27047 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027048 }
27049
27050 /* Count the paths backward to find the beginning of the desired string. */
27051 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027052 {
27053#ifdef FEAT_MBYTE
27054 if (has_mbyte)
27055 p -= mb_head_off(tfname, p);
27056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027057 if (vim_ispathsep(*p))
27058 {
27059 if (sepcount == 0 || (hasTilde && sepcount == 1))
27060 break;
27061 else
27062 sepcount --;
27063 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027065 if (hasTilde)
27066 {
27067 --p;
27068 if (p >= tfname)
27069 *p = '~';
27070 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027071 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027072 }
27073 else
27074 ++p;
27075
27076 /* Copy in the string - p indexes into tfname - allocated at pbuf */
27077 vim_free(*bufp);
27078 *fnamelen = (int)STRLEN(p);
27079 *bufp = pbuf;
27080 *fnamep = p;
27081
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027082 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027083}
27084#endif /* WIN3264 */
27085
27086/*
27087 * Adjust a filename, according to a string of modifiers.
27088 * *fnamep must be NUL terminated when called. When returning, the length is
27089 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027090 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027091 * When there is an error, *fnamep is set to NULL.
27092 */
27093 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010027094modify_fname(
27095 char_u *src, /* string with modifiers */
27096 int *usedlen, /* characters after src that are used */
27097 char_u **fnamep, /* file name so far */
27098 char_u **bufp, /* buffer for allocated file name or NULL */
27099 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027100{
27101 int valid = 0;
27102 char_u *tail;
27103 char_u *s, *p, *pbuf;
27104 char_u dirname[MAXPATHL];
27105 int c;
27106 int has_fullname = 0;
27107#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027108 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027109 int has_shortname = 0;
27110#endif
27111
27112repeat:
27113 /* ":p" - full path/file_name */
27114 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
27115 {
27116 has_fullname = 1;
27117
27118 valid |= VALID_PATH;
27119 *usedlen += 2;
27120
27121 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
27122 if ((*fnamep)[0] == '~'
27123#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
27124 && ((*fnamep)[1] == '/'
27125# ifdef BACKSLASH_IN_FILENAME
27126 || (*fnamep)[1] == '\\'
27127# endif
27128 || (*fnamep)[1] == NUL)
27129
27130#endif
27131 )
27132 {
27133 *fnamep = expand_env_save(*fnamep);
27134 vim_free(*bufp); /* free any allocated file name */
27135 *bufp = *fnamep;
27136 if (*fnamep == NULL)
27137 return -1;
27138 }
27139
27140 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027141 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000027142 {
27143 if (vim_ispathsep(*p)
27144 && p[1] == '.'
27145 && (p[2] == NUL
27146 || vim_ispathsep(p[2])
27147 || (p[2] == '.'
27148 && (p[3] == NUL || vim_ispathsep(p[3])))))
27149 break;
27150 }
27151
27152 /* FullName_save() is slow, don't use it when not needed. */
27153 if (*p != NUL || !vim_isAbsName(*fnamep))
27154 {
27155 *fnamep = FullName_save(*fnamep, *p != NUL);
27156 vim_free(*bufp); /* free any allocated file name */
27157 *bufp = *fnamep;
27158 if (*fnamep == NULL)
27159 return -1;
27160 }
27161
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027162#ifdef WIN3264
27163# if _WIN32_WINNT >= 0x0500
27164 if (vim_strchr(*fnamep, '~') != NULL)
27165 {
27166 /* Expand 8.3 filename to full path. Needed to make sure the same
27167 * file does not have two different names.
27168 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
27169 p = alloc(_MAX_PATH + 1);
27170 if (p != NULL)
27171 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010027172 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027173 {
27174 vim_free(*bufp);
27175 *bufp = *fnamep = p;
27176 }
27177 else
27178 vim_free(p);
27179 }
27180 }
27181# endif
27182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027183 /* Append a path separator to a directory. */
27184 if (mch_isdir(*fnamep))
27185 {
27186 /* Make room for one or two extra characters. */
27187 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
27188 vim_free(*bufp); /* free any allocated file name */
27189 *bufp = *fnamep;
27190 if (*fnamep == NULL)
27191 return -1;
27192 add_pathsep(*fnamep);
27193 }
27194 }
27195
27196 /* ":." - path relative to the current directory */
27197 /* ":~" - path relative to the home directory */
27198 /* ":8" - shortname path - postponed till after */
27199 while (src[*usedlen] == ':'
27200 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
27201 {
27202 *usedlen += 2;
27203 if (c == '8')
27204 {
27205#ifdef WIN3264
27206 has_shortname = 1; /* Postpone this. */
27207#endif
27208 continue;
27209 }
27210 pbuf = NULL;
27211 /* Need full path first (use expand_env() to remove a "~/") */
27212 if (!has_fullname)
27213 {
27214 if (c == '.' && **fnamep == '~')
27215 p = pbuf = expand_env_save(*fnamep);
27216 else
27217 p = pbuf = FullName_save(*fnamep, FALSE);
27218 }
27219 else
27220 p = *fnamep;
27221
27222 has_fullname = 0;
27223
27224 if (p != NULL)
27225 {
27226 if (c == '.')
27227 {
27228 mch_dirname(dirname, MAXPATHL);
27229 s = shorten_fname(p, dirname);
27230 if (s != NULL)
27231 {
27232 *fnamep = s;
27233 if (pbuf != NULL)
27234 {
27235 vim_free(*bufp); /* free any allocated file name */
27236 *bufp = pbuf;
27237 pbuf = NULL;
27238 }
27239 }
27240 }
27241 else
27242 {
27243 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
27244 /* Only replace it when it starts with '~' */
27245 if (*dirname == '~')
27246 {
27247 s = vim_strsave(dirname);
27248 if (s != NULL)
27249 {
27250 *fnamep = s;
27251 vim_free(*bufp);
27252 *bufp = s;
27253 }
27254 }
27255 }
27256 vim_free(pbuf);
27257 }
27258 }
27259
27260 tail = gettail(*fnamep);
27261 *fnamelen = (int)STRLEN(*fnamep);
27262
27263 /* ":h" - head, remove "/file_name", can be repeated */
27264 /* Don't remove the first "/" or "c:\" */
27265 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
27266 {
27267 valid |= VALID_HEAD;
27268 *usedlen += 2;
27269 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027270 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027271 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027272 *fnamelen = (int)(tail - *fnamep);
27273#ifdef VMS
27274 if (*fnamelen > 0)
27275 *fnamelen += 1; /* the path separator is part of the path */
27276#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027277 if (*fnamelen == 0)
27278 {
27279 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
27280 p = vim_strsave((char_u *)".");
27281 if (p == NULL)
27282 return -1;
27283 vim_free(*bufp);
27284 *bufp = *fnamep = tail = p;
27285 *fnamelen = 1;
27286 }
27287 else
27288 {
27289 while (tail > s && !after_pathsep(s, tail))
27290 mb_ptr_back(*fnamep, tail);
27291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027292 }
27293
27294 /* ":8" - shortname */
27295 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27296 {
27297 *usedlen += 2;
27298#ifdef WIN3264
27299 has_shortname = 1;
27300#endif
27301 }
27302
27303#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027304 /*
27305 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027306 */
27307 if (has_shortname)
27308 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027309 /* Copy the string if it is shortened by :h and when it wasn't copied
27310 * yet, because we are going to change it in place. Avoids changing
27311 * the buffer name for "%:8". */
27312 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027313 {
27314 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027315 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027316 return -1;
27317 vim_free(*bufp);
27318 *bufp = *fnamep = p;
27319 }
27320
27321 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027322 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027323 if (!has_fullname && !vim_isAbsName(*fnamep))
27324 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027325 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027326 return -1;
27327 }
27328 else
27329 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027330 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027331
Bram Moolenaardc935552011-08-17 15:23:23 +020027332 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027333 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027334 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027335 return -1;
27336
27337 if (l == 0)
27338 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027339 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027340 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027341 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027342 return -1;
27343 }
27344 *fnamelen = l;
27345 }
27346 }
27347#endif /* WIN3264 */
27348
27349 /* ":t" - tail, just the basename */
27350 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27351 {
27352 *usedlen += 2;
27353 *fnamelen -= (int)(tail - *fnamep);
27354 *fnamep = tail;
27355 }
27356
27357 /* ":e" - extension, can be repeated */
27358 /* ":r" - root, without extension, can be repeated */
27359 while (src[*usedlen] == ':'
27360 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27361 {
27362 /* find a '.' in the tail:
27363 * - for second :e: before the current fname
27364 * - otherwise: The last '.'
27365 */
27366 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27367 s = *fnamep - 2;
27368 else
27369 s = *fnamep + *fnamelen - 1;
27370 for ( ; s > tail; --s)
27371 if (s[0] == '.')
27372 break;
27373 if (src[*usedlen + 1] == 'e') /* :e */
27374 {
27375 if (s > tail)
27376 {
27377 *fnamelen += (int)(*fnamep - (s + 1));
27378 *fnamep = s + 1;
27379#ifdef VMS
27380 /* cut version from the extension */
27381 s = *fnamep + *fnamelen - 1;
27382 for ( ; s > *fnamep; --s)
27383 if (s[0] == ';')
27384 break;
27385 if (s > *fnamep)
27386 *fnamelen = s - *fnamep;
27387#endif
27388 }
27389 else if (*fnamep <= tail)
27390 *fnamelen = 0;
27391 }
27392 else /* :r */
27393 {
27394 if (s > tail) /* remove one extension */
27395 *fnamelen = (int)(s - *fnamep);
27396 }
27397 *usedlen += 2;
27398 }
27399
27400 /* ":s?pat?foo?" - substitute */
27401 /* ":gs?pat?foo?" - global substitute */
27402 if (src[*usedlen] == ':'
27403 && (src[*usedlen + 1] == 's'
27404 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27405 {
27406 char_u *str;
27407 char_u *pat;
27408 char_u *sub;
27409 int sep;
27410 char_u *flags;
27411 int didit = FALSE;
27412
27413 flags = (char_u *)"";
27414 s = src + *usedlen + 2;
27415 if (src[*usedlen + 1] == 'g')
27416 {
27417 flags = (char_u *)"g";
27418 ++s;
27419 }
27420
27421 sep = *s++;
27422 if (sep)
27423 {
27424 /* find end of pattern */
27425 p = vim_strchr(s, sep);
27426 if (p != NULL)
27427 {
27428 pat = vim_strnsave(s, (int)(p - s));
27429 if (pat != NULL)
27430 {
27431 s = p + 1;
27432 /* find end of substitution */
27433 p = vim_strchr(s, sep);
27434 if (p != NULL)
27435 {
27436 sub = vim_strnsave(s, (int)(p - s));
27437 str = vim_strnsave(*fnamep, *fnamelen);
27438 if (sub != NULL && str != NULL)
27439 {
27440 *usedlen = (int)(p + 1 - src);
27441 s = do_string_sub(str, pat, sub, flags);
27442 if (s != NULL)
27443 {
27444 *fnamep = s;
27445 *fnamelen = (int)STRLEN(s);
27446 vim_free(*bufp);
27447 *bufp = s;
27448 didit = TRUE;
27449 }
27450 }
27451 vim_free(sub);
27452 vim_free(str);
27453 }
27454 vim_free(pat);
27455 }
27456 }
27457 /* after using ":s", repeat all the modifiers */
27458 if (didit)
27459 goto repeat;
27460 }
27461 }
27462
Bram Moolenaar26df0922014-02-23 23:39:13 +010027463 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27464 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027465 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027466 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027467 if (c != NUL)
27468 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027469 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027470 if (c != NUL)
27471 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027472 if (p == NULL)
27473 return -1;
27474 vim_free(*bufp);
27475 *bufp = *fnamep = p;
27476 *fnamelen = (int)STRLEN(p);
27477 *usedlen += 2;
27478 }
27479
Bram Moolenaar071d4272004-06-13 20:20:40 +000027480 return valid;
27481}
27482
27483/*
27484 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27485 * "flags" can be "g" to do a global substitute.
27486 * Returns an allocated string, NULL for error.
27487 */
27488 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027489do_string_sub(
27490 char_u *str,
27491 char_u *pat,
27492 char_u *sub,
27493 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027494{
27495 int sublen;
27496 regmatch_T regmatch;
27497 int i;
27498 int do_all;
27499 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027500 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027501 garray_T ga;
27502 char_u *ret;
27503 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027504 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027505
27506 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27507 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027508 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027509
27510 ga_init2(&ga, 1, 200);
27511
27512 do_all = (flags[0] == 'g');
27513
27514 regmatch.rm_ic = p_ic;
27515 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27516 if (regmatch.regprog != NULL)
27517 {
27518 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027519 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027520 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27521 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027522 /* Skip empty match except for first match. */
27523 if (regmatch.startp[0] == regmatch.endp[0])
27524 {
27525 if (zero_width == regmatch.startp[0])
27526 {
27527 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027528 i = MB_PTR2LEN(tail);
27529 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27530 (size_t)i);
27531 ga.ga_len += i;
27532 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027533 continue;
27534 }
27535 zero_width = regmatch.startp[0];
27536 }
27537
Bram Moolenaar071d4272004-06-13 20:20:40 +000027538 /*
27539 * Get some space for a temporary buffer to do the substitution
27540 * into. It will contain:
27541 * - The text up to where the match is.
27542 * - The substituted text.
27543 * - The text after the match.
27544 */
27545 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027546 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027547 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27548 {
27549 ga_clear(&ga);
27550 break;
27551 }
27552
27553 /* copy the text up to where the match is */
27554 i = (int)(regmatch.startp[0] - tail);
27555 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27556 /* add the substituted text */
27557 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27558 + ga.ga_len + i, TRUE, TRUE, FALSE);
27559 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027560 tail = regmatch.endp[0];
27561 if (*tail == NUL)
27562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027563 if (!do_all)
27564 break;
27565 }
27566
27567 if (ga.ga_data != NULL)
27568 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27569
Bram Moolenaar473de612013-06-08 18:19:48 +020027570 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027571 }
27572
27573 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27574 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027575 if (p_cpo == empty_option)
27576 p_cpo = save_cpo;
27577 else
27578 /* Darn, evaluating {sub} expression changed the value. */
27579 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027580
27581 return ret;
27582}
27583
27584#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */