blob: 50d1cbae8c3b8c15a21dda19505e6cb52a63bfa5 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000355 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000356 {VV_NAME("swapname", VAR_STRING), VV_RO},
357 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000358 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200359 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000360 {VV_NAME("mouse_win", VAR_NUMBER), 0},
361 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
362 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000363 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100365 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200367 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200368 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200369 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200370 {VV_NAME("option_new", VAR_STRING), VV_RO},
371 {VV_NAME("option_old", VAR_STRING), VV_RO},
372 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100373 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100374 {VV_NAME("false", VAR_SPECIAL), VV_RO},
375 {VV_NAME("true", VAR_SPECIAL), VV_RO},
376 {VV_NAME("null", VAR_SPECIAL), VV_RO},
377 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100378 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200379 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000380};
381
382/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_type vv_di.di_tv.v_type
384#define vv_nr vv_di.di_tv.vval.v_number
385#define vv_float vv_di.di_tv.vval.v_float
386#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000387#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200388#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000389#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000390
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200391static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000392#define vimvarht vimvardict.dv_hashtab
393
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100394static void prepare_vimvar(int idx, typval_T *save_tv);
395static void restore_vimvar(int idx, typval_T *save_tv);
396static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
397static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
398static char_u *skip_var_one(char_u *arg);
399static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
400static void list_glob_vars(int *first);
401static void list_buf_vars(int *first);
402static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100404static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_vim_vars(int *first);
407static void list_script_vars(int *first);
408static void list_func_vars(int *first);
409static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
410static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
411static int check_changedtick(char_u *arg);
412static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
413static void clear_lval(lval_T *lp);
414static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
415static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
416static void list_fix_watch(list_T *l, listitem_T *item);
417static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
418static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
419static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
420static void item_lock(typval_T *tv, int deep, int lock);
421static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100423static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
424static int eval1(char_u **arg, typval_T *rettv, int evaluate);
425static int eval2(char_u **arg, typval_T *rettv, int evaluate);
426static int eval3(char_u **arg, typval_T *rettv, int evaluate);
427static int eval4(char_u **arg, typval_T *rettv, int evaluate);
428static int eval5(char_u **arg, typval_T *rettv, int evaluate);
429static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
430static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000431
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100432static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
433static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
434static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
435static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200437static void list_free_contents(list_T *l);
438static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static long list_len(list_T *l);
440static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
441static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
442static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
443static long list_find_nr(list_T *l, long idx, int *errorp);
444static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100445static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
446static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
447static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200448static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
449static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap);
450static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100451static int free_unref_items(int copyID);
452static dictitem_T *dictitem_copy(dictitem_T *org);
453static void dictitem_remove(dict_T *dict, dictitem_T *item);
454static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
455static long dict_len(dict_T *d);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200456static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200458static char_u *echo_string_core(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID, int echo_style, int restore_copyID, int dict_val);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100460static char_u *string_quote(char_u *str, int function);
461static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
462static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100463static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
464static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void emsg_funcname(char *ermsg, char_u *name);
466static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000467
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200468static void dict_free_contents(dict_T *d);
469static void dict_free_dict(dict_T *d);
470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100472static void f_abs(typval_T *argvars, typval_T *rettv);
473static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100475static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100476static void f_and(typval_T *argvars, typval_T *rettv);
477static void f_append(typval_T *argvars, typval_T *rettv);
478static void f_argc(typval_T *argvars, typval_T *rettv);
479static void f_argidx(typval_T *argvars, typval_T *rettv);
480static void f_arglistid(typval_T *argvars, typval_T *rettv);
481static void f_argv(typval_T *argvars, typval_T *rettv);
482static void f_assert_equal(typval_T *argvars, typval_T *rettv);
483static void f_assert_exception(typval_T *argvars, typval_T *rettv);
484static void f_assert_fails(typval_T *argvars, typval_T *rettv);
485static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200486static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200487static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
488static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_asin(typval_T *argvars, typval_T *rettv);
492static void f_atan(typval_T *argvars, typval_T *rettv);
493static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100495static void f_browse(typval_T *argvars, typval_T *rettv);
496static void f_browsedir(typval_T *argvars, typval_T *rettv);
497static void f_bufexists(typval_T *argvars, typval_T *rettv);
498static void f_buflisted(typval_T *argvars, typval_T *rettv);
499static void f_bufloaded(typval_T *argvars, typval_T *rettv);
500static void f_bufname(typval_T *argvars, typval_T *rettv);
501static void f_bufnr(typval_T *argvars, typval_T *rettv);
502static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
503static void f_byte2line(typval_T *argvars, typval_T *rettv);
504static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
505static void f_byteidx(typval_T *argvars, typval_T *rettv);
506static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
507static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100509static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000510#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100513static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
514static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100515static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100516static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100517static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100518static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100519static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
520static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100521static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100523static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
524static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100525static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100526static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100527#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_changenr(typval_T *argvars, typval_T *rettv);
529static void f_char2nr(typval_T *argvars, typval_T *rettv);
530static void f_cindent(typval_T *argvars, typval_T *rettv);
531static void f_clearmatches(typval_T *argvars, typval_T *rettv);
532static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000533#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100534static void f_complete(typval_T *argvars, typval_T *rettv);
535static void f_complete_add(typval_T *argvars, typval_T *rettv);
536static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000537#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100538static void f_confirm(typval_T *argvars, typval_T *rettv);
539static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_cos(typval_T *argvars, typval_T *rettv);
542static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_count(typval_T *argvars, typval_T *rettv);
545static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
546static void f_cursor(typval_T *argsvars, typval_T *rettv);
547static void f_deepcopy(typval_T *argvars, typval_T *rettv);
548static void f_delete(typval_T *argvars, typval_T *rettv);
549static void f_did_filetype(typval_T *argvars, typval_T *rettv);
550static void f_diff_filler(typval_T *argvars, typval_T *rettv);
551static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
552static void f_empty(typval_T *argvars, typval_T *rettv);
553static void f_escape(typval_T *argvars, typval_T *rettv);
554static void f_eval(typval_T *argvars, typval_T *rettv);
555static void f_eventhandler(typval_T *argvars, typval_T *rettv);
556static void f_executable(typval_T *argvars, typval_T *rettv);
557static void f_exepath(typval_T *argvars, typval_T *rettv);
558static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200559#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_expand(typval_T *argvars, typval_T *rettv);
563static void f_extend(typval_T *argvars, typval_T *rettv);
564static void f_feedkeys(typval_T *argvars, typval_T *rettv);
565static void f_filereadable(typval_T *argvars, typval_T *rettv);
566static void f_filewritable(typval_T *argvars, typval_T *rettv);
567static void f_filter(typval_T *argvars, typval_T *rettv);
568static void f_finddir(typval_T *argvars, typval_T *rettv);
569static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000570#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100571static void f_float2nr(typval_T *argvars, typval_T *rettv);
572static void f_floor(typval_T *argvars, typval_T *rettv);
573static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000574#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100575static void f_fnameescape(typval_T *argvars, typval_T *rettv);
576static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
577static void f_foldclosed(typval_T *argvars, typval_T *rettv);
578static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
579static void f_foldlevel(typval_T *argvars, typval_T *rettv);
580static void f_foldtext(typval_T *argvars, typval_T *rettv);
581static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
582static void f_foreground(typval_T *argvars, typval_T *rettv);
583static void f_function(typval_T *argvars, typval_T *rettv);
584static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
585static void f_get(typval_T *argvars, typval_T *rettv);
586static void f_getbufline(typval_T *argvars, typval_T *rettv);
587static void f_getbufvar(typval_T *argvars, typval_T *rettv);
588static void f_getchar(typval_T *argvars, typval_T *rettv);
589static void f_getcharmod(typval_T *argvars, typval_T *rettv);
590static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
591static void f_getcmdline(typval_T *argvars, typval_T *rettv);
592static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
593static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
594static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
595static void f_getcwd(typval_T *argvars, typval_T *rettv);
596static void f_getfontname(typval_T *argvars, typval_T *rettv);
597static void f_getfperm(typval_T *argvars, typval_T *rettv);
598static void f_getfsize(typval_T *argvars, typval_T *rettv);
599static void f_getftime(typval_T *argvars, typval_T *rettv);
600static void f_getftype(typval_T *argvars, typval_T *rettv);
601static void f_getline(typval_T *argvars, typval_T *rettv);
602static void f_getmatches(typval_T *argvars, typval_T *rettv);
603static void f_getpid(typval_T *argvars, typval_T *rettv);
604static void f_getcurpos(typval_T *argvars, typval_T *rettv);
605static void f_getpos(typval_T *argvars, typval_T *rettv);
606static void f_getqflist(typval_T *argvars, typval_T *rettv);
607static void f_getreg(typval_T *argvars, typval_T *rettv);
608static void f_getregtype(typval_T *argvars, typval_T *rettv);
609static void f_gettabvar(typval_T *argvars, typval_T *rettv);
610static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
611static void f_getwinposx(typval_T *argvars, typval_T *rettv);
612static void f_getwinposy(typval_T *argvars, typval_T *rettv);
613static void f_getwinvar(typval_T *argvars, typval_T *rettv);
614static void f_glob(typval_T *argvars, typval_T *rettv);
615static void f_globpath(typval_T *argvars, typval_T *rettv);
616static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
617static void f_has(typval_T *argvars, typval_T *rettv);
618static void f_has_key(typval_T *argvars, typval_T *rettv);
619static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
620static void f_hasmapto(typval_T *argvars, typval_T *rettv);
621static void f_histadd(typval_T *argvars, typval_T *rettv);
622static void f_histdel(typval_T *argvars, typval_T *rettv);
623static void f_histget(typval_T *argvars, typval_T *rettv);
624static void f_histnr(typval_T *argvars, typval_T *rettv);
625static void f_hlID(typval_T *argvars, typval_T *rettv);
626static void f_hlexists(typval_T *argvars, typval_T *rettv);
627static void f_hostname(typval_T *argvars, typval_T *rettv);
628static void f_iconv(typval_T *argvars, typval_T *rettv);
629static void f_indent(typval_T *argvars, typval_T *rettv);
630static void f_index(typval_T *argvars, typval_T *rettv);
631static void f_input(typval_T *argvars, typval_T *rettv);
632static void f_inputdialog(typval_T *argvars, typval_T *rettv);
633static void f_inputlist(typval_T *argvars, typval_T *rettv);
634static void f_inputrestore(typval_T *argvars, typval_T *rettv);
635static void f_inputsave(typval_T *argvars, typval_T *rettv);
636static void f_inputsecret(typval_T *argvars, typval_T *rettv);
637static void f_insert(typval_T *argvars, typval_T *rettv);
638static void f_invert(typval_T *argvars, typval_T *rettv);
639static void f_isdirectory(typval_T *argvars, typval_T *rettv);
640static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100641#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
642static void f_isnan(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100645#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100646static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100647static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100648static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100649static void f_job_start(typval_T *argvars, typval_T *rettv);
650static void f_job_stop(typval_T *argvars, typval_T *rettv);
651static void f_job_status(typval_T *argvars, typval_T *rettv);
652#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100653static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100654static void f_js_decode(typval_T *argvars, typval_T *rettv);
655static void f_js_encode(typval_T *argvars, typval_T *rettv);
656static void f_json_decode(typval_T *argvars, typval_T *rettv);
657static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_keys(typval_T *argvars, typval_T *rettv);
659static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
660static void f_len(typval_T *argvars, typval_T *rettv);
661static void f_libcall(typval_T *argvars, typval_T *rettv);
662static void f_libcallnr(typval_T *argvars, typval_T *rettv);
663static void f_line(typval_T *argvars, typval_T *rettv);
664static void f_line2byte(typval_T *argvars, typval_T *rettv);
665static void f_lispindent(typval_T *argvars, typval_T *rettv);
666static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000667#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100668static void f_log(typval_T *argvars, typval_T *rettv);
669static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200671#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200673#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_map(typval_T *argvars, typval_T *rettv);
675static void f_maparg(typval_T *argvars, typval_T *rettv);
676static void f_mapcheck(typval_T *argvars, typval_T *rettv);
677static void f_match(typval_T *argvars, typval_T *rettv);
678static void f_matchadd(typval_T *argvars, typval_T *rettv);
679static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
680static void f_matcharg(typval_T *argvars, typval_T *rettv);
681static void f_matchdelete(typval_T *argvars, typval_T *rettv);
682static void f_matchend(typval_T *argvars, typval_T *rettv);
683static void f_matchlist(typval_T *argvars, typval_T *rettv);
684static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200685static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_max(typval_T *argvars, typval_T *rettv);
687static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000688#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100692#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
696static void f_nr2char(typval_T *argvars, typval_T *rettv);
697static void f_or(typval_T *argvars, typval_T *rettv);
698static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100699#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100701#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
706static void f_printf(typval_T *argvars, typval_T *rettv);
707static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200708#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200710#endif
711#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200713#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100714static void f_range(typval_T *argvars, typval_T *rettv);
715static void f_readfile(typval_T *argvars, typval_T *rettv);
716static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100717#ifdef FEAT_FLOAT
718static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
719#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_reltimestr(typval_T *argvars, typval_T *rettv);
721static void f_remote_expr(typval_T *argvars, typval_T *rettv);
722static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
723static void f_remote_peek(typval_T *argvars, typval_T *rettv);
724static void f_remote_read(typval_T *argvars, typval_T *rettv);
725static void f_remote_send(typval_T *argvars, typval_T *rettv);
726static void f_remove(typval_T *argvars, typval_T *rettv);
727static void f_rename(typval_T *argvars, typval_T *rettv);
728static void f_repeat(typval_T *argvars, typval_T *rettv);
729static void f_resolve(typval_T *argvars, typval_T *rettv);
730static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000731#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100732static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_screenattr(typval_T *argvars, typval_T *rettv);
735static void f_screenchar(typval_T *argvars, typval_T *rettv);
736static void f_screencol(typval_T *argvars, typval_T *rettv);
737static void f_screenrow(typval_T *argvars, typval_T *rettv);
738static void f_search(typval_T *argvars, typval_T *rettv);
739static void f_searchdecl(typval_T *argvars, typval_T *rettv);
740static void f_searchpair(typval_T *argvars, typval_T *rettv);
741static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
742static void f_searchpos(typval_T *argvars, typval_T *rettv);
743static void f_server2client(typval_T *argvars, typval_T *rettv);
744static void f_serverlist(typval_T *argvars, typval_T *rettv);
745static void f_setbufvar(typval_T *argvars, typval_T *rettv);
746static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
747static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100748static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_setline(typval_T *argvars, typval_T *rettv);
750static void f_setloclist(typval_T *argvars, typval_T *rettv);
751static void f_setmatches(typval_T *argvars, typval_T *rettv);
752static void f_setpos(typval_T *argvars, typval_T *rettv);
753static void f_setqflist(typval_T *argvars, typval_T *rettv);
754static void f_setreg(typval_T *argvars, typval_T *rettv);
755static void f_settabvar(typval_T *argvars, typval_T *rettv);
756static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
757static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100758#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100760#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_shellescape(typval_T *argvars, typval_T *rettv);
762static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
763static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_sin(typval_T *argvars, typval_T *rettv);
766static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_sort(typval_T *argvars, typval_T *rettv);
769static void f_soundfold(typval_T *argvars, typval_T *rettv);
770static void f_spellbadword(typval_T *argvars, typval_T *rettv);
771static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
772static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000773#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100774static void f_sqrt(typval_T *argvars, typval_T *rettv);
775static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000776#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_str2nr(typval_T *argvars, typval_T *rettv);
778static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000779#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200782static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100783static void f_stridx(typval_T *argvars, typval_T *rettv);
784static void f_string(typval_T *argvars, typval_T *rettv);
785static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200786static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_strpart(typval_T *argvars, typval_T *rettv);
788static void f_strridx(typval_T *argvars, typval_T *rettv);
789static void f_strtrans(typval_T *argvars, typval_T *rettv);
790static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
791static void f_strwidth(typval_T *argvars, typval_T *rettv);
792static void f_submatch(typval_T *argvars, typval_T *rettv);
793static void f_substitute(typval_T *argvars, typval_T *rettv);
794static void f_synID(typval_T *argvars, typval_T *rettv);
795static void f_synIDattr(typval_T *argvars, typval_T *rettv);
796static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
797static void f_synstack(typval_T *argvars, typval_T *rettv);
798static void f_synconcealed(typval_T *argvars, typval_T *rettv);
799static void f_system(typval_T *argvars, typval_T *rettv);
800static void f_systemlist(typval_T *argvars, typval_T *rettv);
801static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
802static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
803static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
804static void f_taglist(typval_T *argvars, typval_T *rettv);
805static void f_tagfiles(typval_T *argvars, typval_T *rettv);
806static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200807static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
808static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200809static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
810#ifdef FEAT_JOB_CHANNEL
811static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
812#endif
813static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
814#ifdef FEAT_JOB_CHANNEL
815static void f_test_null_job(typval_T *argvars, typval_T *rettv);
816#endif
817static void f_test_null_list(typval_T *argvars, typval_T *rettv);
818static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
819static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200820#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static void f_tan(typval_T *argvars, typval_T *rettv);
822static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200823#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100824#ifdef FEAT_TIMERS
825static void f_timer_start(typval_T *argvars, typval_T *rettv);
826static void f_timer_stop(typval_T *argvars, typval_T *rettv);
827#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100828static void f_tolower(typval_T *argvars, typval_T *rettv);
829static void f_toupper(typval_T *argvars, typval_T *rettv);
830static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000831#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000833#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100834static void f_type(typval_T *argvars, typval_T *rettv);
835static void f_undofile(typval_T *argvars, typval_T *rettv);
836static void f_undotree(typval_T *argvars, typval_T *rettv);
837static void f_uniq(typval_T *argvars, typval_T *rettv);
838static void f_values(typval_T *argvars, typval_T *rettv);
839static void f_virtcol(typval_T *argvars, typval_T *rettv);
840static void f_visualmode(typval_T *argvars, typval_T *rettv);
841static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100842static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100843static void f_win_getid(typval_T *argvars, typval_T *rettv);
844static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
845static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
846static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static void f_winbufnr(typval_T *argvars, typval_T *rettv);
848static void f_wincol(typval_T *argvars, typval_T *rettv);
849static void f_winheight(typval_T *argvars, typval_T *rettv);
850static void f_winline(typval_T *argvars, typval_T *rettv);
851static void f_winnr(typval_T *argvars, typval_T *rettv);
852static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
853static void f_winrestview(typval_T *argvars, typval_T *rettv);
854static void f_winsaveview(typval_T *argvars, typval_T *rettv);
855static void f_winwidth(typval_T *argvars, typval_T *rettv);
856static void f_writefile(typval_T *argvars, typval_T *rettv);
857static void f_wordcount(typval_T *argvars, typval_T *rettv);
858static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100860static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
861static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
862static int get_env_len(char_u **arg);
863static int get_id_len(char_u **arg);
864static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
865static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000866#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
867#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
868 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
870static int eval_isnamec(int c);
871static int eval_isnamec1(int c);
872static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
873static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static typval_T *alloc_string_tv(char_u *string);
875static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100876#ifdef FEAT_FLOAT
877static float_T get_tv_float(typval_T *varp);
878#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879static linenr_T get_tv_lnum(typval_T *argvars);
880static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
882static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
883static hashtab_T *find_var_ht(char_u *name, char_u **varname);
884static funccall_T *get_funccal(void);
885static void vars_clear_ext(hashtab_T *ht, int free_val);
886static void delete_var(hashtab_T *ht, hashitem_T *hi);
887static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
888static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
889static void set_var(char_u *name, typval_T *varp, int copy);
890static int var_check_ro(int flags, char_u *name, int use_gettext);
891static int var_check_fixed(int flags, char_u *name, int use_gettext);
892static int var_check_func_name(char_u *name, int new_var);
893static int valid_varname(char_u *varname);
894static int tv_check_lock(int lock, char_u *name, int use_gettext);
895static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
896static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100897static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100898static int eval_fname_script(char_u *p);
899static int eval_fname_sid(char_u *p);
900static void list_func_head(ufunc_T *fp, int indent);
901static ufunc_T *find_func(char_u *name);
902static int function_exists(char_u *name);
903static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100905static void func_do_profile(ufunc_T *fp);
906static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
907static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000908static int
909# ifdef __BORLANDC__
910 _RTLENTRYF
911# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100912 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000913static int
914# ifdef __BORLANDC__
915 _RTLENTRYF
916# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100917 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000918#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100919static int script_autoload(char_u *name, int reload);
920static char_u *autoload_name(char_u *name);
921static void cat_func_name(char_u *buf, ufunc_T *fp);
922static void func_free(ufunc_T *fp);
923static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
924static int can_free_funccal(funccall_T *fc, int copyID) ;
925static void free_funccal(funccall_T *fc, int free_val);
926static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
927static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
928static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
929static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
930static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
931static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
932static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
933static int write_list(FILE *fd, list_T *list, int binary);
934static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000935
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200936
937#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100938static int compare_func_name(const void *s1, const void *s2);
939static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200940#endif
941
Bram Moolenaar33570922005-01-25 22:26:29 +0000942/*
943 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000944 */
945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000947{
Bram Moolenaar33570922005-01-25 22:26:29 +0000948 int i;
949 struct vimvar *p;
950
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200951 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
952 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200953 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000954 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000956
957 for (i = 0; i < VV_LEN; ++i)
958 {
959 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200960 if (STRLEN(p->vv_name) > 16)
961 {
962 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
963 getout(1);
964 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 STRCPY(p->vv_di.di_key, p->vv_name);
966 if (p->vv_flags & VV_RO)
967 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
968 else if (p->vv_flags & VV_RO_SBX)
969 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
970 else
971 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000972
973 /* add to v: scope dict, unless the value is not always available */
974 if (p->vv_type != VAR_UNKNOWN)
975 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000976 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000977 /* add to compat scope dict */
978 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100980 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
981
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000982 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100983 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200984 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100985 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100986
987 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
988 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
989 set_vim_var_nr(VV_NONE, VVAL_NONE);
990 set_vim_var_nr(VV_NULL, VVAL_NULL);
991
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200992 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200993
994#ifdef EBCDIC
995 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100996 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200997 */
998 sortFunctions();
999#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001000}
1001
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001002#if defined(EXITFREE) || defined(PROTO)
1003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001004eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001005{
1006 int i;
1007 struct vimvar *p;
1008
1009 for (i = 0; i < VV_LEN; ++i)
1010 {
1011 p = &vimvars[i];
1012 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001013 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001014 vim_free(p->vv_str);
1015 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001016 }
1017 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1018 {
1019 list_unref(p->vv_list);
1020 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001021 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001022 }
1023 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001024 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001025 hash_clear(&compat_hashtab);
1026
Bram Moolenaard9fba312005-06-26 22:34:35 +00001027 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001028# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001029 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001030# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001031
1032 /* global variables */
1033 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001034
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001035 /* autoloaded script names */
1036 ga_clear_strings(&ga_loaded);
1037
Bram Moolenaarcca74132013-09-25 21:00:28 +02001038 /* Script-local variables. First clear all the variables and in a second
1039 * loop free the scriptvar_T, because a variable in one script might hold
1040 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001041 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001042 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001043 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001044 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001045 ga_clear(&ga_scripts);
1046
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001047 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001048 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001049
1050 /* functions */
1051 free_all_functions();
1052 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001053}
1054#endif
1055
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 * Return the name of the executed function.
1058 */
1059 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001060func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063}
1064
1065/*
1066 * Return the address holding the next breakpoint line for a funccall cookie.
1067 */
1068 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001069func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaar33570922005-01-25 22:26:29 +00001071 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072}
1073
1074/*
1075 * Return the address holding the debug tick for a funccall cookie.
1076 */
1077 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001078func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar33570922005-01-25 22:26:29 +00001080 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081}
1082
1083/*
1084 * Return the nesting level for a funccall cookie.
1085 */
1086 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001087func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
Bram Moolenaar33570922005-01-25 22:26:29 +00001089 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090}
1091
1092/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001093funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001095/* pointer to list of previously used funccal, still around because some
1096 * item in it is still being used. */
1097funccall_T *previous_funccal = NULL;
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099/*
1100 * Return TRUE when a function was ended by a ":return" command.
1101 */
1102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001103current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 return current_funccal->returned;
1106}
1107
1108
1109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 * Set an internal variable to a string value. Creates the variable if it does
1111 * not already exist.
1112 */
1113 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001114set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001116 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001117 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119 val = vim_strsave(value);
1120 if (val != NULL)
1121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001123 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001125 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 }
1129}
1130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133static char_u *redir_endp = NULL;
1134static char_u *redir_varname = NULL;
1135
1136/*
1137 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001138 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 * Returns OK if successfully completed the setup. FAIL otherwise.
1140 */
1141 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001142var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143{
1144 int save_emsg;
1145 int err;
1146 typval_T tv;
1147
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001148 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001149 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 {
1151 EMSG(_(e_invarg));
1152 return FAIL;
1153 }
1154
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 redir_varname = vim_strsave(name);
1157 if (redir_varname == NULL)
1158 return FAIL;
1159
1160 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1161 if (redir_lval == NULL)
1162 {
1163 var_redir_stop();
1164 return FAIL;
1165 }
1166
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 /* The output is stored in growarray "redir_ga" until redirection ends. */
1168 ga_init2(&redir_ga, (int)sizeof(char), 500);
1169
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001171 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001172 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1174 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001175 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 if (redir_endp != NULL && *redir_endp != NUL)
1177 /* Trailing characters are present after the variable name */
1178 EMSG(_(e_trailing));
1179 else
1180 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001181 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 var_redir_stop();
1183 return FAIL;
1184 }
1185
1186 /* check if we can write to the variable: set it to or append an empty
1187 * string */
1188 save_emsg = did_emsg;
1189 did_emsg = FALSE;
1190 tv.v_type = VAR_STRING;
1191 tv.vval.v_string = (char_u *)"";
1192 if (append)
1193 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1194 else
1195 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001196 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001198 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199 if (err)
1200 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 var_redir_stop();
1203 return FAIL;
1204 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205
1206 return OK;
1207}
1208
1209/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001210 * Append "value[value_len]" to the variable set by var_redir_start().
1211 * The actual appending is postponed until redirection ends, because the value
1212 * appended may in fact be the string we write to, changing it may cause freed
1213 * memory to be used:
1214 * :redir => foo
1215 * :let foo
1216 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217 */
1218 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001219var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001221 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001222
1223 if (redir_lval == NULL)
1224 return;
1225
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001227 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001229 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001230
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001231 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232 {
1233 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001234 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001235 }
1236 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001238}
1239
1240/*
1241 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001242 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001243 */
1244 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001245var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001246{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001247 typval_T tv;
1248
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001249 if (redir_lval != NULL)
1250 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001251 /* If there was no error: assign the text to the variable. */
1252 if (redir_endp != NULL)
1253 {
1254 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1255 tv.v_type = VAR_STRING;
1256 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001257 /* Call get_lval() again, if it's inside a Dict or List it may
1258 * have changed. */
1259 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001260 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001261 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1262 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1263 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001264 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001265
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001266 /* free the collected output */
1267 vim_free(redir_ga.ga_data);
1268 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001269
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001270 vim_free(redir_lval);
1271 redir_lval = NULL;
1272 }
1273 vim_free(redir_varname);
1274 redir_varname = NULL;
1275}
1276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277# if defined(FEAT_MBYTE) || defined(PROTO)
1278 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001279eval_charconvert(
1280 char_u *enc_from,
1281 char_u *enc_to,
1282 char_u *fname_from,
1283 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 int err = FALSE;
1286
1287 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1288 set_vim_var_string(VV_CC_TO, enc_to, -1);
1289 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1290 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1291 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1292 err = TRUE;
1293 set_vim_var_string(VV_CC_FROM, NULL, -1);
1294 set_vim_var_string(VV_CC_TO, NULL, -1);
1295 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1296 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1297
1298 if (err)
1299 return FAIL;
1300 return OK;
1301}
1302# endif
1303
1304# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1305 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001306eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 int err = FALSE;
1309
1310 set_vim_var_string(VV_FNAME_IN, fname, -1);
1311 set_vim_var_string(VV_CMDARG, args, -1);
1312 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1313 err = TRUE;
1314 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1315 set_vim_var_string(VV_CMDARG, NULL, -1);
1316
1317 if (err)
1318 {
1319 mch_remove(fname);
1320 return FAIL;
1321 }
1322 return OK;
1323}
1324# endif
1325
1326# if defined(FEAT_DIFF) || defined(PROTO)
1327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001328eval_diff(
1329 char_u *origfile,
1330 char_u *newfile,
1331 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 int err = FALSE;
1334
1335 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1336 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1337 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1338 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1339 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1340 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1341 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1342}
1343
1344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001345eval_patch(
1346 char_u *origfile,
1347 char_u *difffile,
1348 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349{
1350 int err;
1351
1352 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1353 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1354 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1355 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1356 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1357 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1358 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1359}
1360# endif
1361
1362/*
1363 * Top level evaluation function, returning a boolean.
1364 * Sets "error" to TRUE if there was an error.
1365 * Return TRUE or FALSE.
1366 */
1367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001368eval_to_bool(
1369 char_u *arg,
1370 int *error,
1371 char_u **nextcmd,
1372 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
Bram Moolenaar33570922005-01-25 22:26:29 +00001374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 int retval = FALSE;
1376
1377 if (skip)
1378 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 else
1382 {
1383 *error = FALSE;
1384 if (!skip)
1385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001386 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001387 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 }
1390 if (skip)
1391 --emsg_skip;
1392
1393 return retval;
1394}
1395
1396/*
1397 * Top level evaluation function, returning a string. If "skip" is TRUE,
1398 * only parsing to "nextcmd" is done, without reporting errors. Return
1399 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1400 */
1401 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402eval_to_string_skip(
1403 char_u *arg,
1404 char_u **nextcmd,
1405 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 char_u *retval;
1409
1410 if (skip)
1411 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 retval = NULL;
1414 else
1415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 retval = vim_strsave(get_tv_string(&tv));
1417 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 if (skip)
1420 --emsg_skip;
1421
1422 return retval;
1423}
1424
1425/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001426 * Skip over an expression at "*pp".
1427 * Return FAIL for an error, OK otherwise.
1428 */
1429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001430skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001431{
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001433
1434 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001435 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001436}
1437
1438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001440 * When "convert" is TRUE convert a List into a sequence of lines and convert
1441 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 * Return pointer to allocated memory, or NULL for failure.
1443 */
1444 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001445eval_to_string(
1446 char_u *arg,
1447 char_u **nextcmd,
1448 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
Bram Moolenaar33570922005-01-25 22:26:29 +00001450 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001452 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001453#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001454 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001457 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 retval = NULL;
1459 else
1460 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001461 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001462 {
1463 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001464 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001465 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001466 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001467 if (tv.vval.v_list->lv_len > 0)
1468 ga_append(&ga, NL);
1469 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 ga_append(&ga, NUL);
1471 retval = (char_u *)ga.ga_data;
1472 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001473#ifdef FEAT_FLOAT
1474 else if (convert && tv.v_type == VAR_FLOAT)
1475 {
1476 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1477 retval = vim_strsave(numbuf);
1478 }
1479#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001480 else
1481 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484
1485 return retval;
1486}
1487
1488/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001489 * Call eval_to_string() without using current local variables and using
1490 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 */
1492 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493eval_to_string_safe(
1494 char_u *arg,
1495 char_u **nextcmd,
1496 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 char_u *retval;
1499 void *save_funccalp;
1500
1501 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001502 if (use_sandbox)
1503 ++sandbox;
1504 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001505 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001506 if (use_sandbox)
1507 --sandbox;
1508 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 restore_funccal(save_funccalp);
1510 return retval;
1511}
1512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513/*
1514 * Top level evaluation function, returning a number.
1515 * Evaluates "expr" silently.
1516 * Returns -1 for an error.
1517 */
1518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
Bram Moolenaar33570922005-01-25 22:26:29 +00001521 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001523 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
1525 ++emsg_off;
1526
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 retval = -1;
1529 else
1530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001531 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 --emsg_off;
1535
1536 return retval;
1537}
1538
Bram Moolenaara40058a2005-07-11 22:42:07 +00001539/*
1540 * Prepare v: variable "idx" to be used.
1541 * Save the current typeval in "save_tv".
1542 * When not used yet add the variable to the v: hashtable.
1543 */
1544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001545prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001546{
1547 *save_tv = vimvars[idx].vv_tv;
1548 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1549 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1550}
1551
1552/*
1553 * Restore v: variable "idx" to typeval "save_tv".
1554 * When no longer defined, remove the variable from the v: hashtable.
1555 */
1556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001557restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001558{
1559 hashitem_T *hi;
1560
Bram Moolenaara40058a2005-07-11 22:42:07 +00001561 vimvars[idx].vv_tv = *save_tv;
1562 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1563 {
1564 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1565 if (HASHITEM_EMPTY(hi))
1566 EMSG2(_(e_intern2), "restore_vimvar()");
1567 else
1568 hash_remove(&vimvarht, hi);
1569 }
1570}
1571
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001572#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001573/*
1574 * Evaluate an expression to a list with suggestions.
1575 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001576 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001577 */
1578 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580{
1581 typval_T save_val;
1582 typval_T rettv;
1583 list_T *list = NULL;
1584 char_u *p = skipwhite(expr);
1585
1586 /* Set "v:val" to the bad word. */
1587 prepare_vimvar(VV_VAL, &save_val);
1588 vimvars[VV_VAL].vv_type = VAR_STRING;
1589 vimvars[VV_VAL].vv_str = badword;
1590 if (p_verbose == 0)
1591 ++emsg_off;
1592
1593 if (eval1(&p, &rettv, TRUE) == OK)
1594 {
1595 if (rettv.v_type != VAR_LIST)
1596 clear_tv(&rettv);
1597 else
1598 list = rettv.vval.v_list;
1599 }
1600
1601 if (p_verbose == 0)
1602 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001603 restore_vimvar(VV_VAL, &save_val);
1604
1605 return list;
1606}
1607
1608/*
1609 * "list" is supposed to contain two items: a word and a number. Return the
1610 * word in "pp" and the number as the return value.
1611 * Return -1 if anything isn't right.
1612 * Used to get the good word and score from the eval_spell_expr() result.
1613 */
1614 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001615get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001616{
1617 listitem_T *li;
1618
1619 li = list->lv_first;
1620 if (li == NULL)
1621 return -1;
1622 *pp = get_tv_string(&li->li_tv);
1623
1624 li = li->li_next;
1625 if (li == NULL)
1626 return -1;
1627 return get_tv_number(&li->li_tv);
1628}
1629#endif
1630
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001631/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001632 * Top level evaluation function.
1633 * Returns an allocated typval_T with the result.
1634 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001635 */
1636 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001637eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001638{
1639 typval_T *tv;
1640
1641 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001642 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001643 {
1644 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001645 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001646 }
1647
1648 return tv;
1649}
1650
1651
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001654 * Uses argv[argc] for the function arguments. Only Number and String
1655 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001658 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659call_vim_function(
1660 char_u *func,
1661 int argc,
1662 char_u **argv,
1663 int safe, /* use the sandbox */
1664 int str_arg_only, /* all arguments are strings */
1665 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
Bram Moolenaar33570922005-01-25 22:26:29 +00001667 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 long n;
1669 int len;
1670 int i;
1671 int doesrange;
1672 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001675 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679 for (i = 0; i < argc; i++)
1680 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001681 /* Pass a NULL or empty argument as an empty string */
1682 if (argv[i] == NULL || *argv[i] == NUL)
1683 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001684 argvars[i].v_type = VAR_STRING;
1685 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001686 continue;
1687 }
1688
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001689 if (str_arg_only)
1690 len = 0;
1691 else
1692 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001693 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (len != 0 && len == (int)STRLEN(argv[i]))
1695 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001696 argvars[i].v_type = VAR_NUMBER;
1697 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699 else
1700 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001701 argvars[i].v_type = VAR_STRING;
1702 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 }
1704 }
1705
1706 if (safe)
1707 {
1708 save_funccalp = save_funccal();
1709 ++sandbox;
1710 }
1711
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1713 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001715 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (safe)
1717 {
1718 --sandbox;
1719 restore_funccal(save_funccalp);
1720 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 vim_free(argvars);
1722
1723 if (ret == FAIL)
1724 clear_tv(rettv);
1725
1726 return ret;
1727}
1728
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001729/*
1730 * Call vimL function "func" and return the result as a number.
1731 * Returns -1 when calling the function fails.
1732 * Uses argv[argc] for the function arguments.
1733 */
1734 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001735call_func_retnr(
1736 char_u *func,
1737 int argc,
1738 char_u **argv,
1739 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001740{
1741 typval_T rettv;
1742 long retval;
1743
1744 /* All arguments are passed as strings, no conversion to number. */
1745 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1746 return -1;
1747
1748 retval = get_tv_number_chk(&rettv, NULL);
1749 clear_tv(&rettv);
1750 return retval;
1751}
1752
1753#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1754 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1755
Bram Moolenaar4f688582007-07-24 12:34:30 +00001756# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001758 * Call vimL function "func" and return the result as a string.
1759 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760 * Uses argv[argc] for the function arguments.
1761 */
1762 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763call_func_retstr(
1764 char_u *func,
1765 int argc,
1766 char_u **argv,
1767 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001768{
1769 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001770 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001771
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001772 /* All arguments are passed as strings, no conversion to number. */
1773 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001774 return NULL;
1775
1776 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001777 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 return retval;
1779}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001780# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001781
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001782/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001783 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001784 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001785 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001786 */
1787 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788call_func_retlist(
1789 char_u *func,
1790 int argc,
1791 char_u **argv,
1792 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001793{
1794 typval_T rettv;
1795
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001796 /* All arguments are passed as strings, no conversion to number. */
1797 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001798 return NULL;
1799
1800 if (rettv.v_type != VAR_LIST)
1801 {
1802 clear_tv(&rettv);
1803 return NULL;
1804 }
1805
1806 return rettv.vval.v_list;
1807}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808#endif
1809
1810/*
1811 * Save the current function call pointer, and set it to NULL.
1812 * Used when executing autocommands and for ":source".
1813 */
1814 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001815save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001817 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 current_funccal = NULL;
1820 return (void *)fc;
1821}
1822
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001826 funccall_T *fc = (funccall_T *)vfc;
1827
1828 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829}
1830
Bram Moolenaar05159a02005-02-26 23:04:13 +00001831#if defined(FEAT_PROFILE) || defined(PROTO)
1832/*
1833 * Prepare profiling for entering a child or something else that is not
1834 * counted for the script/function itself.
1835 * Should always be called in pair with prof_child_exit().
1836 */
1837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838prof_child_enter(
1839 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001840{
1841 funccall_T *fc = current_funccal;
1842
1843 if (fc != NULL && fc->func->uf_profiling)
1844 profile_start(&fc->prof_child);
1845 script_prof_save(tm);
1846}
1847
1848/*
1849 * Take care of time spent in a child.
1850 * Should always be called after prof_child_enter().
1851 */
1852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001853prof_child_exit(
1854 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001855{
1856 funccall_T *fc = current_funccal;
1857
1858 if (fc != NULL && fc->func->uf_profiling)
1859 {
1860 profile_end(&fc->prof_child);
1861 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1862 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1863 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1864 }
1865 script_prof_restore(tm);
1866}
1867#endif
1868
1869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#ifdef FEAT_FOLDING
1871/*
1872 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1873 * it in "*cp". Doesn't give error messages.
1874 */
1875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001876eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
Bram Moolenaar33570922005-01-25 22:26:29 +00001878 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 int retval;
1880 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001881 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1882 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001885 if (use_sandbox)
1886 ++sandbox;
1887 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 retval = 0;
1891 else
1892 {
1893 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 if (tv.v_type == VAR_NUMBER)
1895 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001896 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 retval = 0;
1898 else
1899 {
1900 /* If the result is a string, check if there is a non-digit before
1901 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (!VIM_ISDIGIT(*s) && *s != '-')
1904 *cp = *s++;
1905 retval = atol((char *)s);
1906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001910 if (use_sandbox)
1911 --sandbox;
1912 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
1914 return retval;
1915}
1916#endif
1917
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 * ":let" list all variable values
1920 * ":let var1 var2" list variable values
1921 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 * ":let var += expr" assignment command.
1923 * ":let var -= expr" assignment command.
1924 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 */
1927 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001928ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929{
1930 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 int var_count = 0;
1935 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001937 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001938 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
Bram Moolenaardb552d602006-03-23 22:59:57 +00001940 argend = skip_var_list(arg, &var_count, &semicolon);
1941 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001943 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1944 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001945 expr = skipwhite(argend);
1946 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1947 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001949 /*
1950 * ":let" without "=": list variables
1951 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 if (*arg == '[')
1953 EMSG(_(e_invarg));
1954 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001956 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001960 list_glob_vars(&first);
1961 list_buf_vars(&first);
1962 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001963#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001964 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001965#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001966 list_script_vars(&first);
1967 list_func_vars(&first);
1968 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 eap->nextcmd = check_nextcmd(arg);
1971 }
1972 else
1973 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 op[0] = '=';
1975 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001976 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001978 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1979 op[0] = *expr; /* +=, -= or .= */
1980 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001982 else
1983 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (eap->skip)
1986 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (eap->skip)
1989 {
1990 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001991 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 --emsg_skip;
1993 }
1994 else if (i != FAIL)
1995 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 }
2001}
2002
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003/*
2004 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2005 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002006 * When "nextchars" is not NULL it points to a string with characters that
2007 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2008 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 * Returns OK or FAIL;
2010 */
2011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002012ex_let_vars(
2013 char_u *arg_start,
2014 typval_T *tv,
2015 int copy, /* copy values from "tv", don't move */
2016 int semicolon, /* from skip_var_list() */
2017 int var_count, /* from skip_var_list() */
2018 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019{
2020 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002021 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002023 listitem_T *item;
2024 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025
2026 if (*arg != '[')
2027 {
2028 /*
2029 * ":let var = expr" or ":for var in list"
2030 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002031 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 return FAIL;
2033 return OK;
2034 }
2035
2036 /*
2037 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2038 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002039 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 {
2041 EMSG(_(e_listreq));
2042 return FAIL;
2043 }
2044
2045 i = list_len(l);
2046 if (semicolon == 0 && var_count < i)
2047 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002048 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002049 return FAIL;
2050 }
2051 if (var_count - semicolon > i)
2052 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002053 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 return FAIL;
2055 }
2056
2057 item = l->lv_first;
2058 while (*arg != ']')
2059 {
2060 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002061 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 item = item->li_next;
2063 if (arg == NULL)
2064 return FAIL;
2065
2066 arg = skipwhite(arg);
2067 if (*arg == ';')
2068 {
2069 /* Put the rest of the list (may be empty) in the var after ';'.
2070 * Create a new list for this. */
2071 l = list_alloc();
2072 if (l == NULL)
2073 return FAIL;
2074 while (item != NULL)
2075 {
2076 list_append_tv(l, &item->li_tv);
2077 item = item->li_next;
2078 }
2079
2080 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002081 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002082 ltv.vval.v_list = l;
2083 l->lv_refcount = 1;
2084
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002085 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2086 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087 clear_tv(&ltv);
2088 if (arg == NULL)
2089 return FAIL;
2090 break;
2091 }
2092 else if (*arg != ',' && *arg != ']')
2093 {
2094 EMSG2(_(e_intern2), "ex_let_vars()");
2095 return FAIL;
2096 }
2097 }
2098
2099 return OK;
2100}
2101
2102/*
2103 * Skip over assignable variable "var" or list of variables "[var, var]".
2104 * Used for ":let varvar = expr" and ":for varvar in expr".
2105 * For "[var, var]" increment "*var_count" for each variable.
2106 * for "[var, var; var]" set "semicolon".
2107 * Return NULL for an error.
2108 */
2109 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002110skip_var_list(
2111 char_u *arg,
2112 int *var_count,
2113 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002114{
2115 char_u *p, *s;
2116
2117 if (*arg == '[')
2118 {
2119 /* "[var, var]": find the matching ']'. */
2120 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002121 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002122 {
2123 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2124 s = skip_var_one(p);
2125 if (s == p)
2126 {
2127 EMSG2(_(e_invarg2), p);
2128 return NULL;
2129 }
2130 ++*var_count;
2131
2132 p = skipwhite(s);
2133 if (*p == ']')
2134 break;
2135 else if (*p == ';')
2136 {
2137 if (*semicolon == 1)
2138 {
2139 EMSG(_("Double ; in list of variables"));
2140 return NULL;
2141 }
2142 *semicolon = 1;
2143 }
2144 else if (*p != ',')
2145 {
2146 EMSG2(_(e_invarg2), p);
2147 return NULL;
2148 }
2149 }
2150 return p + 1;
2151 }
2152 else
2153 return skip_var_one(arg);
2154}
2155
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002156/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002157 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002158 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002159 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002160 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002162{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002163 if (*arg == '@' && arg[1] != NUL)
2164 return arg + 2;
2165 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2166 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002167}
2168
Bram Moolenaara7043832005-01-21 11:56:39 +00002169/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002170 * List variables for hashtab "ht" with prefix "prefix".
2171 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174list_hashtable_vars(
2175 hashtab_T *ht,
2176 char_u *prefix,
2177 int empty,
2178 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002179{
Bram Moolenaar33570922005-01-25 22:26:29 +00002180 hashitem_T *hi;
2181 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002182 int todo;
2183
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002184 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002185 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2186 {
2187 if (!HASHITEM_EMPTY(hi))
2188 {
2189 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002190 di = HI2DI(hi);
2191 if (empty || di->di_tv.v_type != VAR_STRING
2192 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002194 }
2195 }
2196}
2197
2198/*
2199 * List global variables.
2200 */
2201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002203{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002205}
2206
2207/*
2208 * List buffer variables.
2209 */
2210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002211list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002212{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 char_u numbuf[NUMBUFLEN];
2214
Bram Moolenaar429fa852013-04-15 12:27:36 +02002215 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217
2218 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2220 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002221}
2222
2223/*
2224 * List window variables.
2225 */
2226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002228{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002229 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002231}
2232
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002233#ifdef FEAT_WINDOWS
2234/*
2235 * List tab page variables.
2236 */
2237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002239{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002240 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002242}
2243#endif
2244
Bram Moolenaara7043832005-01-21 11:56:39 +00002245/*
2246 * List Vim variables.
2247 */
2248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002249list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252}
2253
2254/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002255 * List script-local variables, if there is a script.
2256 */
2257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002258list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002259{
2260 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002261 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2262 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002263}
2264
2265/*
2266 * List function variables, if there is a function.
2267 */
2268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002269list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002270{
2271 if (current_funccal != NULL)
2272 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002274}
2275
2276/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 * List variables in "arg".
2278 */
2279 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002280list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281{
2282 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 char_u *name_start;
2286 char_u *arg_subsc;
2287 char_u *tofree;
2288 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289
2290 while (!ends_excmd(*arg) && !got_int)
2291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002294 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2296 {
2297 emsg_severe = TRUE;
2298 EMSG(_(e_trailing));
2299 break;
2300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 /* get_name_len() takes care of expanding curly braces */
2305 name_start = name = arg;
2306 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2307 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309 /* This is mainly to keep test 49 working: when expanding
2310 * curly braces fails overrule the exception error message. */
2311 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 emsg_severe = TRUE;
2314 EMSG2(_(e_invarg2), arg);
2315 break;
2316 }
2317 error = TRUE;
2318 }
2319 else
2320 {
2321 if (tofree != NULL)
2322 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002323 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 else
2326 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002327 /* handle d.key, l[idx], f(expr) */
2328 arg_subsc = arg;
2329 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002330 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002332 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002334 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002337 case 'g': list_glob_vars(first); break;
2338 case 'b': list_buf_vars(first); break;
2339 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002340#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002341 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002342#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002343 case 'v': list_vim_vars(first); break;
2344 case 's': list_script_vars(first); break;
2345 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002346 default:
2347 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002349 }
2350 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002351 {
2352 char_u numbuf[NUMBUFLEN];
2353 char_u *tf;
2354 int c;
2355 char_u *s;
2356
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002357 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002358 c = *arg;
2359 *arg = NUL;
2360 list_one_var_a((char_u *)"",
2361 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002362 tv.v_type,
2363 s == NULL ? (char_u *)"" : s,
2364 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002365 *arg = c;
2366 vim_free(tf);
2367 }
2368 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 }
2371 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002372
2373 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002375
2376 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 }
2378
2379 return arg;
2380}
2381
2382/*
2383 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2384 * Returns a pointer to the char just after the var name.
2385 * Returns NULL if there is an error.
2386 */
2387 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002388ex_let_one(
2389 char_u *arg, /* points to variable name */
2390 typval_T *tv, /* value to assign to variable */
2391 int copy, /* copy value from "tv" */
2392 char_u *endchars, /* valid chars after variable name or NULL */
2393 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394{
2395 int c1;
2396 char_u *name;
2397 char_u *p;
2398 char_u *arg_end = NULL;
2399 int len;
2400 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402
2403 /*
2404 * ":let $VAR = expr": Set environment variable.
2405 */
2406 if (*arg == '$')
2407 {
2408 /* Find the end of the name. */
2409 ++arg;
2410 name = arg;
2411 len = get_env_len(&arg);
2412 if (len == 0)
2413 EMSG2(_(e_invarg2), name - 1);
2414 else
2415 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 if (op != NULL && (*op == '+' || *op == '-'))
2417 EMSG2(_(e_letwrong), op);
2418 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002419 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002421 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 {
2423 c1 = name[len];
2424 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 p = get_tv_string_chk(tv);
2426 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 {
2428 int mustfree = FALSE;
2429 char_u *s = vim_getenv(name, &mustfree);
2430
2431 if (s != NULL)
2432 {
2433 p = tofree = concat_str(s, p);
2434 if (mustfree)
2435 vim_free(s);
2436 }
2437 }
2438 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 if (STRICMP(name, "HOME") == 0)
2442 init_homedir();
2443 else if (didset_vim && STRICMP(name, "VIM") == 0)
2444 didset_vim = FALSE;
2445 else if (didset_vimruntime
2446 && STRICMP(name, "VIMRUNTIME") == 0)
2447 didset_vimruntime = FALSE;
2448 arg_end = arg;
2449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 }
2453 }
2454 }
2455
2456 /*
2457 * ":let &option = expr": Set option value.
2458 * ":let &l:option = expr": Set local option value.
2459 * ":let &g:option = expr": Set global option value.
2460 */
2461 else if (*arg == '&')
2462 {
2463 /* Find the end of the name. */
2464 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002465 if (p == NULL || (endchars != NULL
2466 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 EMSG(_(e_letunexp));
2468 else
2469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 long n;
2471 int opt_type;
2472 long numval;
2473 char_u *stringval = NULL;
2474 char_u *s;
2475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 c1 = *p;
2477 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478
2479 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002480 s = get_tv_string_chk(tv); /* != NULL if number or string */
2481 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 {
2483 opt_type = get_option_value(arg, &numval,
2484 &stringval, opt_flags);
2485 if ((opt_type == 1 && *op == '.')
2486 || (opt_type == 0 && *op != '.'))
2487 EMSG2(_(e_letwrong), op);
2488 else
2489 {
2490 if (opt_type == 1) /* number */
2491 {
2492 if (*op == '+')
2493 n = numval + n;
2494 else
2495 n = numval - n;
2496 }
2497 else if (opt_type == 0 && stringval != NULL) /* string */
2498 {
2499 s = concat_str(stringval, s);
2500 vim_free(stringval);
2501 stringval = s;
2502 }
2503 }
2504 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 if (s != NULL)
2506 {
2507 set_option_value(arg, n, s, opt_flags);
2508 arg_end = p;
2509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 }
2513 }
2514
2515 /*
2516 * ":let @r = expr": Set register contents.
2517 */
2518 else if (*arg == '@')
2519 {
2520 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002521 if (op != NULL && (*op == '+' || *op == '-'))
2522 EMSG2(_(e_letwrong), op);
2523 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002524 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 EMSG(_(e_letunexp));
2526 else
2527 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002528 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 char_u *s;
2530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002531 p = get_tv_string_chk(tv);
2532 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002534 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 if (s != NULL)
2536 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002537 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002538 vim_free(s);
2539 }
2540 }
2541 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002542 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002543 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002544 arg_end = arg + 1;
2545 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002546 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 }
2548 }
2549
2550 /*
2551 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002553 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002558 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2562 EMSG(_(e_letunexp));
2563 else
2564 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 arg_end = p;
2567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 }
2571
2572 else
2573 EMSG2(_(e_invarg2), arg);
2574
2575 return arg_end;
2576}
2577
2578/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2580 */
2581 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002582check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583{
2584 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2585 {
2586 EMSG2(_(e_readonlyvar), arg);
2587 return TRUE;
2588 }
2589 return FALSE;
2590}
2591
2592/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 * Get an lval: variable, Dict item or List item that can be assigned a value
2594 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2595 * "name.key", "name.key[expr]" etc.
2596 * Indexing only works if "name" is an existing List or Dictionary.
2597 * "name" points to the start of the name.
2598 * If "rettv" is not NULL it points to the value to be assigned.
2599 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2600 * wrong; must end in space or cmd separator.
2601 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002602 * flags:
2603 * GLV_QUIET: do not give error messages
2604 * GLV_NO_AUTOLOAD: do not use script autoloading
2605 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002607 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 */
2610 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002611get_lval(
2612 char_u *name,
2613 typval_T *rettv,
2614 lval_T *lp,
2615 int unlet,
2616 int skip,
2617 int flags, /* GLV_ values */
2618 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 char_u *expr_start, *expr_end;
2622 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002623 dictitem_T *v;
2624 typval_T var1;
2625 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002630 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002631 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002634 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635
2636 if (skip)
2637 {
2638 /* When skipping just find the end of the name. */
2639 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002640 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 }
2642
2643 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002644 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (expr_start != NULL)
2646 {
2647 /* Don't expand the name when we already know there is an error. */
2648 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2649 && *p != '[' && *p != '.')
2650 {
2651 EMSG(_(e_trailing));
2652 return NULL;
2653 }
2654
2655 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2656 if (lp->ll_exp_name == NULL)
2657 {
2658 /* Report an invalid expression in braces, unless the
2659 * expression evaluation has been cancelled due to an
2660 * aborting error, an interrupt, or an exception. */
2661 if (!aborting() && !quiet)
2662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002663 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 EMSG2(_(e_invarg2), name);
2665 return NULL;
2666 }
2667 }
2668 lp->ll_name = lp->ll_exp_name;
2669 }
2670 else
2671 lp->ll_name = name;
2672
2673 /* Without [idx] or .key we are done. */
2674 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2675 return p;
2676
2677 cc = *p;
2678 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002679 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (v == NULL && !quiet)
2681 EMSG2(_(e_undefvar), lp->ll_name);
2682 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002683 if (v == NULL)
2684 return NULL;
2685
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 /*
2687 * Loop until no more [idx] or .key is following.
2688 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002689 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2693 && !(lp->ll_tv->v_type == VAR_DICT
2694 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!quiet)
2697 EMSG(_("E689: Can only index a List or Dictionary"));
2698 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!quiet)
2703 EMSG(_("E708: [:] must come last"));
2704 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 len = -1;
2708 if (*p == '.')
2709 {
2710 key = p + 1;
2711 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2712 ;
2713 if (len == 0)
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (!quiet)
2716 EMSG(_(e_emptykey));
2717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 p = key + len;
2720 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002721 else
2722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (*p == ':')
2726 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727 else
2728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 empty1 = FALSE;
2730 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002732 if (get_tv_string_chk(&var1) == NULL)
2733 {
2734 /* not a number or string */
2735 clear_tv(&var1);
2736 return NULL;
2737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 }
2739
2740 /* Optionally get the second index [ :expr]. */
2741 if (*p == ':')
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747 if (!empty1)
2748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (rettv != NULL && (rettv->v_type != VAR_LIST
2752 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (!quiet)
2755 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (!empty1)
2757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760 p = skipwhite(p + 1);
2761 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 else
2764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2767 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (!empty1)
2769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002772 if (get_tv_string_chk(&var2) == NULL)
2773 {
2774 /* not a number or string */
2775 if (!empty1)
2776 clear_tv(&var1);
2777 clear_tv(&var2);
2778 return NULL;
2779 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002785
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (!quiet)
2789 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 if (!empty1)
2791 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 }
2796
2797 /* Skip to past ']'. */
2798 ++p;
2799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 {
2803 if (len == -1)
2804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002806 key = get_tv_string_chk(&var1); /* is number or string */
2807 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 lp->ll_list = NULL;
2814 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002815 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002816
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002817 /* When assigning to a scope dictionary check that a function and
2818 * variable name is valid (only variable name unless it is l: or
2819 * g: dictionary). Disallow overwriting a builtin function. */
2820 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002821 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002822 int prevval;
2823 int wrong;
2824
2825 if (len != -1)
2826 {
2827 prevval = key[len];
2828 key[len] = NUL;
2829 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002830 else
2831 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002832 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2833 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002834 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002835 || !valid_varname(key);
2836 if (len != -1)
2837 key[len] = prevval;
2838 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002839 return NULL;
2840 }
2841
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002844 /* Can't add "v:" variable. */
2845 if (lp->ll_dict == &vimvardict)
2846 {
2847 EMSG2(_(e_illvar), name);
2848 return NULL;
2849 }
2850
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002851 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002855 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 if (len == -1)
2857 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 }
2860 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 if (len == -1)
2865 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 p = NULL;
2868 break;
2869 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002870 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002871 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002872 return NULL;
2873
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 if (len == -1)
2875 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 }
2878 else
2879 {
2880 /*
2881 * Get the number and item for the only or first index of the List.
2882 */
2883 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 else
2886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002887 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 clear_tv(&var1);
2889 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002890 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_list = lp->ll_tv->vval.v_list;
2892 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2893 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002895 if (lp->ll_n1 < 0)
2896 {
2897 lp->ll_n1 = 0;
2898 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2899 }
2900 }
2901 if (lp->ll_li == NULL)
2902 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002904 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002905 if (!quiet)
2906 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002908 }
2909
2910 /*
2911 * May need to find the item or absolute index for the second
2912 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 * When no index given: "lp->ll_empty2" is TRUE.
2914 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002915 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002918 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002919 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002924 {
2925 if (!quiet)
2926 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002930 }
2931
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2933 if (lp->ll_n1 < 0)
2934 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2935 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002936 {
2937 if (!quiet)
2938 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002940 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002941 }
2942
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 }
2946
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 return p;
2948}
2949
2950/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 */
2953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002954clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955{
2956 vim_free(lp->ll_exp_name);
2957 vim_free(lp->ll_newkey);
2958}
2959
2960/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 */
2965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002966set_var_lval(
2967 lval_T *lp,
2968 char_u *endp,
2969 typval_T *rettv,
2970 int copy,
2971 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972{
2973 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002974 listitem_T *ri;
2975 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976
2977 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002980 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 cc = *endp;
2982 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 if (op != NULL && *op != '=')
2984 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002985 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002987 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002988 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002989 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002990 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002991 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002992 if ((di == NULL
2993 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2994 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2995 FALSE)))
2996 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 set_var(lp->ll_name, &tv, FALSE);
2998 clear_tv(&tv);
2999 }
3000 }
3001 else
3002 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003004 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003006 else if (tv_check_lock(lp->ll_newkey == NULL
3007 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003008 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003009 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 else if (lp->ll_range)
3011 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003012 listitem_T *ll_li = lp->ll_li;
3013 int ll_n1 = lp->ll_n1;
3014
3015 /*
3016 * Check whether any of the list items is locked
3017 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003018 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003019 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003020 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003021 return;
3022 ri = ri->li_next;
3023 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3024 break;
3025 ll_li = ll_li->li_next;
3026 ++ll_n1;
3027 }
3028
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 /*
3030 * Assign the List values to the list items.
3031 */
3032 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003033 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 if (op != NULL && *op != '=')
3035 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3036 else
3037 {
3038 clear_tv(&lp->ll_li->li_tv);
3039 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 ri = ri->li_next;
3042 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3043 break;
3044 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003045 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003047 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003048 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 ri = NULL;
3050 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003052 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 lp->ll_li = lp->ll_li->li_next;
3054 ++lp->ll_n1;
3055 }
3056 if (ri != NULL)
3057 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 else if (lp->ll_empty2
3059 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 : lp->ll_n1 != lp->ll_n2)
3061 EMSG(_("E711: List value has not enough items"));
3062 }
3063 else
3064 {
3065 /*
3066 * Assign to a List or Dictionary item.
3067 */
3068 if (lp->ll_newkey != NULL)
3069 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 if (op != NULL && *op != '=')
3071 {
3072 EMSG2(_(e_letwrong), op);
3073 return;
3074 }
3075
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003076 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003077 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003078 if (di == NULL)
3079 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003080 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3081 {
3082 vim_free(di);
3083 return;
3084 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003085 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003086 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 else if (op != NULL && *op != '=')
3088 {
3089 tv_op(lp->ll_tv, rettv, op);
3090 return;
3091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003092 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003093 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003094
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003095 /*
3096 * Assign the value to the variable or list item.
3097 */
3098 if (copy)
3099 copy_tv(rettv, lp->ll_tv);
3100 else
3101 {
3102 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003103 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003104 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003105 }
3106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003107}
3108
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3111 * Returns OK or FAIL.
3112 */
3113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003114tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003115{
3116 long n;
3117 char_u numbuf[NUMBUFLEN];
3118 char_u *s;
3119
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003120 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3121 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3122 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 {
3124 switch (tv1->v_type)
3125 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003126 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 case VAR_DICT:
3128 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003129 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003130 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003131 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003132 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 break;
3134
3135 case VAR_LIST:
3136 if (*op != '+' || tv2->v_type != VAR_LIST)
3137 break;
3138 /* List += List */
3139 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3140 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3141 return OK;
3142
3143 case VAR_NUMBER:
3144 case VAR_STRING:
3145 if (tv2->v_type == VAR_LIST)
3146 break;
3147 if (*op == '+' || *op == '-')
3148 {
3149 /* nr += nr or nr -= nr*/
3150 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151#ifdef FEAT_FLOAT
3152 if (tv2->v_type == VAR_FLOAT)
3153 {
3154 float_T f = n;
3155
3156 if (*op == '+')
3157 f += tv2->vval.v_float;
3158 else
3159 f -= tv2->vval.v_float;
3160 clear_tv(tv1);
3161 tv1->v_type = VAR_FLOAT;
3162 tv1->vval.v_float = f;
3163 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003164 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165#endif
3166 {
3167 if (*op == '+')
3168 n += get_tv_number(tv2);
3169 else
3170 n -= get_tv_number(tv2);
3171 clear_tv(tv1);
3172 tv1->v_type = VAR_NUMBER;
3173 tv1->vval.v_number = n;
3174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003175 }
3176 else
3177 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178 if (tv2->v_type == VAR_FLOAT)
3179 break;
3180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 /* str .= str */
3182 s = get_tv_string(tv1);
3183 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3184 clear_tv(tv1);
3185 tv1->v_type = VAR_STRING;
3186 tv1->vval.v_string = s;
3187 }
3188 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003190 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003191#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192 {
3193 float_T f;
3194
3195 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3196 && tv2->v_type != VAR_NUMBER
3197 && tv2->v_type != VAR_STRING))
3198 break;
3199 if (tv2->v_type == VAR_FLOAT)
3200 f = tv2->vval.v_float;
3201 else
3202 f = get_tv_number(tv2);
3203 if (*op == '+')
3204 tv1->vval.v_float += f;
3205 else
3206 tv1->vval.v_float -= f;
3207 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003208#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003209 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003210 }
3211 }
3212
3213 EMSG2(_(e_letwrong), op);
3214 return FAIL;
3215}
3216
3217/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 * Add a watcher to a list.
3219 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003221list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222{
3223 lw->lw_next = l->lv_watch;
3224 l->lv_watch = lw;
3225}
3226
3227/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003228 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 * No warning when it isn't found...
3230 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 lwp = &l->lv_watch;
3237 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3238 {
3239 if (lw == lwrem)
3240 {
3241 *lwp = lw->lw_next;
3242 break;
3243 }
3244 lwp = &lw->lw_next;
3245 }
3246}
3247
3248/*
3249 * Just before removing an item from a list: advance watchers to the next
3250 * item.
3251 */
3252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003253list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254{
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256
3257 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3258 if (lw->lw_item == item)
3259 lw->lw_item = item->li_next;
3260}
3261
3262/*
3263 * Evaluate the expression used in a ":for var in expr" command.
3264 * "arg" points to "var".
3265 * Set "*errp" to TRUE for an error, FALSE otherwise;
3266 * Return a pointer that holds the info. Null when there is an error.
3267 */
3268 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003269eval_for_line(
3270 char_u *arg,
3271 int *errp,
3272 char_u **nextcmdp,
3273 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274{
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 typval_T tv;
3278 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279
3280 *errp = TRUE; /* default: there is an error */
3281
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 if (fi == NULL)
3284 return NULL;
3285
3286 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3287 if (expr == NULL)
3288 return fi;
3289
3290 expr = skipwhite(expr);
3291 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3292 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003293 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 return fi;
3295 }
3296
3297 if (skip)
3298 ++emsg_skip;
3299 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3300 {
3301 *errp = FALSE;
3302 if (!skip)
3303 {
3304 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003305 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003306 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003308 clear_tv(&tv);
3309 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003310 else if (l == NULL)
3311 {
3312 /* a null list is like an empty list: do nothing */
3313 clear_tv(&tv);
3314 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 else
3316 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003317 /* No need to increment the refcount, it's already set for the
3318 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 fi->fi_list = l;
3320 list_add_watch(l, &fi->fi_lw);
3321 fi->fi_lw.lw_item = l->lv_first;
3322 }
3323 }
3324 }
3325 if (skip)
3326 --emsg_skip;
3327
3328 return fi;
3329}
3330
3331/*
3332 * Use the first item in a ":for" list. Advance to the next.
3333 * Assign the values to the variable (list). "arg" points to the first one.
3334 * Return TRUE when a valid item was found, FALSE when at end of list or
3335 * something wrong.
3336 */
3337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003338next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003340 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003342 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343
3344 item = fi->fi_lw.lw_item;
3345 if (item == NULL)
3346 result = FALSE;
3347 else
3348 {
3349 fi->fi_lw.lw_item = item->li_next;
3350 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3351 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3352 }
3353 return result;
3354}
3355
3356/*
3357 * Free the structure used to store info used by ":for".
3358 */
3359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003360free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361{
Bram Moolenaar33570922005-01-25 22:26:29 +00003362 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003363
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003364 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003365 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003366 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003367 list_unref(fi->fi_list);
3368 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003369 vim_free(fi);
3370}
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3373
3374 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003375set_context_for_expression(
3376 expand_T *xp,
3377 char_u *arg,
3378 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 int got_eq = FALSE;
3381 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003382 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 if (cmdidx == CMD_let)
3385 {
3386 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003387 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003388 {
3389 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003390 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003391 {
3392 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003394 if (vim_iswhite(*p))
3395 break;
3396 }
3397 return;
3398 }
3399 }
3400 else
3401 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3402 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 while ((xp->xp_pattern = vim_strpbrk(arg,
3404 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3405 {
3406 c = *xp->xp_pattern;
3407 if (c == '&')
3408 {
3409 c = xp->xp_pattern[1];
3410 if (c == '&')
3411 {
3412 ++xp->xp_pattern;
3413 xp->xp_context = cmdidx != CMD_let || got_eq
3414 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3415 }
3416 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003419 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3420 xp->xp_pattern += 2;
3421
3422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
3424 else if (c == '$')
3425 {
3426 /* environment variable */
3427 xp->xp_context = EXPAND_ENV_VARS;
3428 }
3429 else if (c == '=')
3430 {
3431 got_eq = TRUE;
3432 xp->xp_context = EXPAND_EXPRESSION;
3433 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003434 else if (c == '#'
3435 && xp->xp_context == EXPAND_EXPRESSION)
3436 {
3437 /* Autoload function/variable contains '#'. */
3438 break;
3439 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003440 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 && xp->xp_context == EXPAND_FUNCTIONS
3442 && vim_strchr(xp->xp_pattern, '(') == NULL)
3443 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003444 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 break;
3446 }
3447 else if (cmdidx != CMD_let || got_eq)
3448 {
3449 if (c == '"') /* string */
3450 {
3451 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3452 if (c == '\\' && xp->xp_pattern[1] != NUL)
3453 ++xp->xp_pattern;
3454 xp->xp_context = EXPAND_NOTHING;
3455 }
3456 else if (c == '\'') /* literal string */
3457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003458 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3460 /* skip */ ;
3461 xp->xp_context = EXPAND_NOTHING;
3462 }
3463 else if (c == '|')
3464 {
3465 if (xp->xp_pattern[1] == '|')
3466 {
3467 ++xp->xp_pattern;
3468 xp->xp_context = EXPAND_EXPRESSION;
3469 }
3470 else
3471 xp->xp_context = EXPAND_COMMANDS;
3472 }
3473 else
3474 xp->xp_context = EXPAND_EXPRESSION;
3475 }
3476 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003477 /* Doesn't look like something valid, expand as an expression
3478 * anyway. */
3479 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 arg = xp->xp_pattern;
3481 if (*arg != NUL)
3482 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3483 /* skip */ ;
3484 }
3485 xp->xp_pattern = arg;
3486}
3487
3488#endif /* FEAT_CMDL_COMPL */
3489
3490/*
3491 * ":1,25call func(arg1, arg2)" function call.
3492 */
3493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 char_u *arg = eap->arg;
3497 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003499 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 linenr_T lnum;
3503 int doesrange;
3504 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003505 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003506 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003508 if (eap->skip)
3509 {
3510 /* trans_function_name() doesn't work well when skipping, use eval0()
3511 * instead to skip to any following command, e.g. for:
3512 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003513 ++emsg_skip;
3514 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3515 clear_tv(&rettv);
3516 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003517 return;
3518 }
3519
Bram Moolenaar65639032016-03-16 21:40:30 +01003520 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003521 if (fudi.fd_newkey != NULL)
3522 {
3523 /* Still need to give an error message for missing key. */
3524 EMSG2(_(e_dictkey), fudi.fd_newkey);
3525 vim_free(fudi.fd_newkey);
3526 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003527 if (tofree == NULL)
3528 return;
3529
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003530 /* Increase refcount on dictionary, it could get deleted when evaluating
3531 * the arguments. */
3532 if (fudi.fd_dict != NULL)
3533 ++fudi.fd_dict->dv_refcount;
3534
Bram Moolenaar65639032016-03-16 21:40:30 +01003535 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3536 * contents. For VAR_PARTIAL get its partial, unless we already have one
3537 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003538 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003539 name = deref_func_name(tofree, &len,
3540 partial != NULL ? NULL : &partial, FALSE);
3541
Bram Moolenaar532c7802005-01-27 14:44:31 +00003542 /* Skip white space to allow ":call func ()". Not good, but required for
3543 * backward compatibility. */
3544 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
3547 if (*startarg != '(')
3548 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003549 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 goto end;
3551 }
3552
3553 /*
3554 * When skipping, evaluate the function once, to find the end of the
3555 * arguments.
3556 * When the function takes a range, this is discovered after the first
3557 * call, and the loop is broken.
3558 */
3559 if (eap->skip)
3560 {
3561 ++emsg_skip;
3562 lnum = eap->line2; /* do it once, also with an invalid range */
3563 }
3564 else
3565 lnum = eap->line1;
3566 for ( ; lnum <= eap->line2; ++lnum)
3567 {
3568 if (!eap->skip && eap->addr_count > 0)
3569 {
3570 curwin->w_cursor.lnum = lnum;
3571 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003572#ifdef FEAT_VIRTUALEDIT
3573 curwin->w_cursor.coladd = 0;
3574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
3576 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003577 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003578 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003579 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 failed = TRUE;
3582 break;
3583 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003584
3585 /* Handle a function returning a Funcref, Dictionary or List. */
3586 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3587 {
3588 failed = TRUE;
3589 break;
3590 }
3591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003592 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 if (doesrange || eap->skip)
3594 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003597 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003598 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003599 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (aborting())
3601 break;
3602 }
3603 if (eap->skip)
3604 --emsg_skip;
3605
3606 if (!failed)
3607 {
3608 /* Check for trailing illegal characters and a following command. */
3609 if (!ends_excmd(*arg))
3610 {
3611 emsg_severe = TRUE;
3612 EMSG(_(e_trailing));
3613 }
3614 else
3615 eap->nextcmd = check_nextcmd(arg);
3616 }
3617
3618end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003619 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003620 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621}
3622
3623/*
3624 * ":unlet[!] var1 ... " command.
3625 */
3626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003627ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003629 ex_unletlock(eap, eap->arg, 0);
3630}
3631
3632/*
3633 * ":lockvar" and ":unlockvar" commands
3634 */
3635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003636ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003639 int deep = 2;
3640
3641 if (eap->forceit)
3642 deep = -1;
3643 else if (vim_isdigit(*arg))
3644 {
3645 deep = getdigits(&arg);
3646 arg = skipwhite(arg);
3647 }
3648
3649 ex_unletlock(eap, arg, deep);
3650}
3651
3652/*
3653 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3654 */
3655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003656ex_unletlock(
3657 exarg_T *eap,
3658 char_u *argstart,
3659 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660{
3661 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 do
3667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003669 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003670 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 if (lv.ll_name == NULL)
3672 error = TRUE; /* error but continue parsing */
3673 if (name_end == NULL || (!vim_iswhite(*name_end)
3674 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 if (name_end != NULL)
3677 {
3678 emsg_severe = TRUE;
3679 EMSG(_(e_trailing));
3680 }
3681 if (!(eap->skip || error))
3682 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 break;
3684 }
3685
3686 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003687 {
3688 if (eap->cmdidx == CMD_unlet)
3689 {
3690 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3691 error = TRUE;
3692 }
3693 else
3694 {
3695 if (do_lock_var(&lv, name_end, deep,
3696 eap->cmdidx == CMD_lockvar) == FAIL)
3697 error = TRUE;
3698 }
3699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 if (!eap->skip)
3702 clear_lval(&lv);
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 arg = skipwhite(name_end);
3705 } while (!ends_excmd(*arg));
3706
3707 eap->nextcmd = check_nextcmd(arg);
3708}
3709
Bram Moolenaar8c711452005-01-14 21:53:12 +00003710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003711do_unlet_var(
3712 lval_T *lp,
3713 char_u *name_end,
3714 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003715{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 int ret = OK;
3717 int cc;
3718
3719 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003720 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 cc = *name_end;
3722 *name_end = NUL;
3723
3724 /* Normal name or expanded name. */
3725 if (check_changedtick(lp->ll_name))
3726 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003728 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003730 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003731 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003732 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003733 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003734 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003735 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003736 else if (lp->ll_range)
3737 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003739 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003740 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003741
3742 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3743 {
3744 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003745 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003746 return FAIL;
3747 ll_li = li;
3748 ++ll_n1;
3749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003750
3751 /* Delete a range of List items. */
3752 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3753 {
3754 li = lp->ll_li->li_next;
3755 listitem_remove(lp->ll_list, lp->ll_li);
3756 lp->ll_li = li;
3757 ++lp->ll_n1;
3758 }
3759 }
3760 else
3761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003762 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003763 /* unlet a List item. */
3764 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003765 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003766 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003767 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003768 }
3769
3770 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003771}
3772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773/*
3774 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 */
3777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003778do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 hashtab_T *ht;
3781 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003783 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003784 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 ht = find_var_ht(name, &varname);
3787 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003789 if (ht == &globvarht)
3790 d = &globvardict;
3791 else if (current_funccal != NULL
3792 && ht == &current_funccal->l_vars.dv_hashtab)
3793 d = &current_funccal->l_vars;
3794 else if (ht == &compat_hashtab)
3795 d = &vimvardict;
3796 else
3797 {
3798 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3799 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3800 }
3801 if (d == NULL)
3802 {
3803 EMSG2(_(e_intern2), "do_unlet()");
3804 return FAIL;
3805 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003806 hi = hash_find(ht, varname);
3807 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003808 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003809 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003810 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003811 || var_check_ro(di->di_flags, name, FALSE)
3812 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003813 return FAIL;
3814
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815 delete_var(ht, hi);
3816 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003819 if (forceit)
3820 return OK;
3821 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 return FAIL;
3823}
3824
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003825/*
3826 * Lock or unlock variable indicated by "lp".
3827 * "deep" is the levels to go (-1 for unlimited);
3828 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3829 */
3830 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003831do_lock_var(
3832 lval_T *lp,
3833 char_u *name_end,
3834 int deep,
3835 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003836{
3837 int ret = OK;
3838 int cc;
3839 dictitem_T *di;
3840
3841 if (deep == 0) /* nothing to do */
3842 return OK;
3843
3844 if (lp->ll_tv == NULL)
3845 {
3846 cc = *name_end;
3847 *name_end = NUL;
3848
3849 /* Normal name or expanded name. */
3850 if (check_changedtick(lp->ll_name))
3851 ret = FAIL;
3852 else
3853 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003854 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003855 if (di == NULL)
3856 ret = FAIL;
3857 else
3858 {
3859 if (lock)
3860 di->di_flags |= DI_FLAGS_LOCK;
3861 else
3862 di->di_flags &= ~DI_FLAGS_LOCK;
3863 item_lock(&di->di_tv, deep, lock);
3864 }
3865 }
3866 *name_end = cc;
3867 }
3868 else if (lp->ll_range)
3869 {
3870 listitem_T *li = lp->ll_li;
3871
3872 /* (un)lock a range of List items. */
3873 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3874 {
3875 item_lock(&li->li_tv, deep, lock);
3876 li = li->li_next;
3877 ++lp->ll_n1;
3878 }
3879 }
3880 else if (lp->ll_list != NULL)
3881 /* (un)lock a List item. */
3882 item_lock(&lp->ll_li->li_tv, deep, lock);
3883 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003884 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003885 item_lock(&lp->ll_di->di_tv, deep, lock);
3886
3887 return ret;
3888}
3889
3890/*
3891 * Lock or unlock an item. "deep" is nr of levels to go.
3892 */
3893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003894item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003895{
3896 static int recurse = 0;
3897 list_T *l;
3898 listitem_T *li;
3899 dict_T *d;
3900 hashitem_T *hi;
3901 int todo;
3902
3903 if (recurse >= DICT_MAXNEST)
3904 {
3905 EMSG(_("E743: variable nested too deep for (un)lock"));
3906 return;
3907 }
3908 if (deep == 0)
3909 return;
3910 ++recurse;
3911
3912 /* lock/unlock the item itself */
3913 if (lock)
3914 tv->v_lock |= VAR_LOCKED;
3915 else
3916 tv->v_lock &= ~VAR_LOCKED;
3917
3918 switch (tv->v_type)
3919 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003920 case VAR_UNKNOWN:
3921 case VAR_NUMBER:
3922 case VAR_STRING:
3923 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003924 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003925 case VAR_FLOAT:
3926 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003927 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003928 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003929 break;
3930
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003931 case VAR_LIST:
3932 if ((l = tv->vval.v_list) != NULL)
3933 {
3934 if (lock)
3935 l->lv_lock |= VAR_LOCKED;
3936 else
3937 l->lv_lock &= ~VAR_LOCKED;
3938 if (deep < 0 || deep > 1)
3939 /* recursive: lock/unlock the items the List contains */
3940 for (li = l->lv_first; li != NULL; li = li->li_next)
3941 item_lock(&li->li_tv, deep - 1, lock);
3942 }
3943 break;
3944 case VAR_DICT:
3945 if ((d = tv->vval.v_dict) != NULL)
3946 {
3947 if (lock)
3948 d->dv_lock |= VAR_LOCKED;
3949 else
3950 d->dv_lock &= ~VAR_LOCKED;
3951 if (deep < 0 || deep > 1)
3952 {
3953 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003954 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003955 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3956 {
3957 if (!HASHITEM_EMPTY(hi))
3958 {
3959 --todo;
3960 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3961 }
3962 }
3963 }
3964 }
3965 }
3966 --recurse;
3967}
3968
Bram Moolenaara40058a2005-07-11 22:42:07 +00003969/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003970 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3971 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003972 */
3973 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003974tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003975{
3976 return (tv->v_lock & VAR_LOCKED)
3977 || (tv->v_type == VAR_LIST
3978 && tv->vval.v_list != NULL
3979 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3980 || (tv->v_type == VAR_DICT
3981 && tv->vval.v_dict != NULL
3982 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3983}
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3986/*
3987 * Delete all "menutrans_" variables.
3988 */
3989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003990del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003993 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
Bram Moolenaar33570922005-01-25 22:26:29 +00003995 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003996 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003998 {
3999 if (!HASHITEM_EMPTY(hi))
4000 {
4001 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4003 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004004 }
4005 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007}
4008#endif
4009
4010#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4011
4012/*
4013 * Local string buffer for the next two functions to store a variable name
4014 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4015 * get_user_var_name().
4016 */
4017
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004018static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
4020static char_u *varnamebuf = NULL;
4021static int varnamebuflen = 0;
4022
4023/*
4024 * Function to concatenate a prefix and a variable name.
4025 */
4026 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004027cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
4029 int len;
4030
4031 len = (int)STRLEN(name) + 3;
4032 if (len > varnamebuflen)
4033 {
4034 vim_free(varnamebuf);
4035 len += 10; /* some additional space */
4036 varnamebuf = alloc(len);
4037 if (varnamebuf == NULL)
4038 {
4039 varnamebuflen = 0;
4040 return NULL;
4041 }
4042 varnamebuflen = len;
4043 }
4044 *varnamebuf = prefix;
4045 varnamebuf[1] = ':';
4046 STRCPY(varnamebuf + 2, name);
4047 return varnamebuf;
4048}
4049
4050/*
4051 * Function given to ExpandGeneric() to obtain the list of user defined
4052 * (global/buffer/window/built-in) variable names.
4053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004055get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004057 static long_u gdone;
4058 static long_u bdone;
4059 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060#ifdef FEAT_WINDOWS
4061 static long_u tdone;
4062#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 static int vidx;
4064 static hashitem_T *hi;
4065 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004068 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004069 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070#ifdef FEAT_WINDOWS
4071 tdone = 0;
4072#endif
4073 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004074
4075 /* Global variables */
4076 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004078 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004080 else
4081 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004082 while (HASHITEM_EMPTY(hi))
4083 ++hi;
4084 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4085 return cat_prefix_varname('g', hi->hi_key);
4086 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004088
4089 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004090 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004091 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004093 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004094 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004095 else
4096 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004097 while (HASHITEM_EMPTY(hi))
4098 ++hi;
4099 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004101 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004103 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 return (char_u *)"b:changedtick";
4105 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004106
4107 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004108 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004109 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004111 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004112 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004113 else
4114 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004115 while (HASHITEM_EMPTY(hi))
4116 ++hi;
4117 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004119
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004120#ifdef FEAT_WINDOWS
4121 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004122 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004123 if (tdone < ht->ht_used)
4124 {
4125 if (tdone++ == 0)
4126 hi = ht->ht_array;
4127 else
4128 ++hi;
4129 while (HASHITEM_EMPTY(hi))
4130 ++hi;
4131 return cat_prefix_varname('t', hi->hi_key);
4132 }
4133#endif
4134
Bram Moolenaar33570922005-01-25 22:26:29 +00004135 /* v: variables */
4136 if (vidx < VV_LEN)
4137 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 vim_free(varnamebuf);
4140 varnamebuf = NULL;
4141 varnamebuflen = 0;
4142 return NULL;
4143}
4144
4145#endif /* FEAT_CMDL_COMPL */
4146
4147/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004148 * Return TRUE if "pat" matches "text".
4149 * Does not use 'cpo' and always uses 'magic'.
4150 */
4151 static int
4152pattern_match(char_u *pat, char_u *text, int ic)
4153{
4154 int matches = FALSE;
4155 char_u *save_cpo;
4156 regmatch_T regmatch;
4157
4158 /* avoid 'l' flag in 'cpoptions' */
4159 save_cpo = p_cpo;
4160 p_cpo = (char_u *)"";
4161 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4162 if (regmatch.regprog != NULL)
4163 {
4164 regmatch.rm_ic = ic;
4165 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4166 vim_regfree(regmatch.regprog);
4167 }
4168 p_cpo = save_cpo;
4169 return matches;
4170}
4171
4172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 * types for expressions.
4174 */
4175typedef enum
4176{
4177 TYPE_UNKNOWN = 0
4178 , TYPE_EQUAL /* == */
4179 , TYPE_NEQUAL /* != */
4180 , TYPE_GREATER /* > */
4181 , TYPE_GEQUAL /* >= */
4182 , TYPE_SMALLER /* < */
4183 , TYPE_SEQUAL /* <= */
4184 , TYPE_MATCH /* =~ */
4185 , TYPE_NOMATCH /* !~ */
4186} exptype_T;
4187
4188/*
4189 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4192 */
4193
4194/*
4195 * Handle zero level expression.
4196 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004198 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 * Return OK or FAIL.
4200 */
4201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004202eval0(
4203 char_u *arg,
4204 typval_T *rettv,
4205 char_u **nextcmd,
4206 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 int ret;
4209 char_u *p;
4210
4211 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if (ret == FAIL || !ends_excmd(*p))
4214 {
4215 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 /*
4218 * Report the invalid expression unless the expression evaluation has
4219 * been cancelled due to an aborting error, an interrupt, or an
4220 * exception.
4221 */
4222 if (!aborting())
4223 EMSG2(_(e_invexpr2), arg);
4224 ret = FAIL;
4225 }
4226 if (nextcmd != NULL)
4227 *nextcmd = check_nextcmd(p);
4228
4229 return ret;
4230}
4231
4232/*
4233 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004234 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 *
4236 * "arg" must point to the first non-white of the expression.
4237 * "arg" is advanced to the next non-white after the recognized expression.
4238 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004239 * Note: "rettv.v_lock" is not set.
4240 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * Return OK or FAIL.
4242 */
4243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004244eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004247 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 /*
4250 * Get the first variable.
4251 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
4254
4255 if ((*arg)[0] == '?')
4256 {
4257 result = FALSE;
4258 if (evaluate)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 int error = FALSE;
4261
4262 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (error)
4266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268
4269 /*
4270 * Get the second variable.
4271 */
4272 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 return FAIL;
4275
4276 /*
4277 * Check for the ":".
4278 */
4279 if ((*arg)[0] != ':')
4280 {
4281 EMSG(_("E109: Missing ':' after '?'"));
4282 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 return FAIL;
4285 }
4286
4287 /*
4288 * Get the third variable.
4289 */
4290 *arg = skipwhite(*arg + 1);
4291 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4292 {
4293 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 return FAIL;
4296 }
4297 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300
4301 return OK;
4302}
4303
4304/*
4305 * Handle first level expression:
4306 * expr2 || expr2 || expr2 logical OR
4307 *
4308 * "arg" must point to the first non-white of the expression.
4309 * "arg" is advanced to the next non-white after the recognized expression.
4310 *
4311 * Return OK or FAIL.
4312 */
4313 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004314eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315{
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 long result;
4318 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
4321 /*
4322 * Get the first variable.
4323 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
4326
4327 /*
4328 * Repeat until there is no following "||".
4329 */
4330 first = TRUE;
4331 result = FALSE;
4332 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4333 {
4334 if (evaluate && first)
4335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004336 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (error)
4340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 first = FALSE;
4342 }
4343
4344 /*
4345 * Get the second variable.
4346 */
4347 *arg = skipwhite(*arg + 2);
4348 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4349 return FAIL;
4350
4351 /*
4352 * Compute the result.
4353 */
4354 if (evaluate && !result)
4355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004356 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004359 if (error)
4360 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 if (evaluate)
4363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 rettv->v_type = VAR_NUMBER;
4365 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 }
4368
4369 return OK;
4370}
4371
4372/*
4373 * Handle second level expression:
4374 * expr3 && expr3 && expr3 logical AND
4375 *
4376 * "arg" must point to the first non-white of the expression.
4377 * "arg" is advanced to the next non-white after the recognized expression.
4378 *
4379 * Return OK or FAIL.
4380 */
4381 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004382eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
Bram Moolenaar33570922005-01-25 22:26:29 +00004384 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 long result;
4386 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 /*
4390 * Get the first variable.
4391 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 return FAIL;
4394
4395 /*
4396 * Repeat until there is no following "&&".
4397 */
4398 first = TRUE;
4399 result = TRUE;
4400 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4401 {
4402 if (evaluate && first)
4403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407 if (error)
4408 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 first = FALSE;
4410 }
4411
4412 /*
4413 * Get the second variable.
4414 */
4415 *arg = skipwhite(*arg + 2);
4416 if (eval4(arg, &var2, evaluate && result) == FAIL)
4417 return FAIL;
4418
4419 /*
4420 * Compute the result.
4421 */
4422 if (evaluate && result)
4423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004424 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004427 if (error)
4428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
4430 if (evaluate)
4431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 rettv->v_type = VAR_NUMBER;
4433 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435 }
4436
4437 return OK;
4438}
4439
4440/*
4441 * Handle third level expression:
4442 * var1 == var2
4443 * var1 =~ var2
4444 * var1 != var2
4445 * var1 !~ var2
4446 * var1 > var2
4447 * var1 >= var2
4448 * var1 < var2
4449 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004450 * var1 is var2
4451 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 *
4453 * "arg" must point to the first non-white of the expression.
4454 * "arg" is advanced to the next non-white after the recognized expression.
4455 *
4456 * Return OK or FAIL.
4457 */
4458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004459eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460{
Bram Moolenaar33570922005-01-25 22:26:29 +00004461 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 char_u *p;
4463 int i;
4464 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 int len = 2;
4467 long n1, n2;
4468 char_u *s1, *s2;
4469 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 /*
4473 * Get the first variable.
4474 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 return FAIL;
4477
4478 p = *arg;
4479 switch (p[0])
4480 {
4481 case '=': if (p[1] == '=')
4482 type = TYPE_EQUAL;
4483 else if (p[1] == '~')
4484 type = TYPE_MATCH;
4485 break;
4486 case '!': if (p[1] == '=')
4487 type = TYPE_NEQUAL;
4488 else if (p[1] == '~')
4489 type = TYPE_NOMATCH;
4490 break;
4491 case '>': if (p[1] != '=')
4492 {
4493 type = TYPE_GREATER;
4494 len = 1;
4495 }
4496 else
4497 type = TYPE_GEQUAL;
4498 break;
4499 case '<': if (p[1] != '=')
4500 {
4501 type = TYPE_SMALLER;
4502 len = 1;
4503 }
4504 else
4505 type = TYPE_SEQUAL;
4506 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004507 case 'i': if (p[1] == 's')
4508 {
4509 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4510 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004511 i = p[len];
4512 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 {
4514 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4515 type_is = TRUE;
4516 }
4517 }
4518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520
4521 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004522 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 */
4524 if (type != TYPE_UNKNOWN)
4525 {
4526 /* extra question mark appended: ignore case */
4527 if (p[len] == '?')
4528 {
4529 ic = TRUE;
4530 ++len;
4531 }
4532 /* extra '#' appended: match case */
4533 else if (p[len] == '#')
4534 {
4535 ic = FALSE;
4536 ++len;
4537 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004538 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 else
4540 ic = p_ic;
4541
4542 /*
4543 * Get the second variable.
4544 */
4545 *arg = skipwhite(p + len);
4546 if (eval5(arg, &var2, evaluate) == FAIL)
4547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004548 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 return FAIL;
4550 }
4551
4552 if (evaluate)
4553 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004554 if (type_is && rettv->v_type != var2.v_type)
4555 {
4556 /* For "is" a different type always means FALSE, for "notis"
4557 * it means TRUE. */
4558 n1 = (type == TYPE_NEQUAL);
4559 }
4560 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4561 {
4562 if (type_is)
4563 {
4564 n1 = (rettv->v_type == var2.v_type
4565 && rettv->vval.v_list == var2.vval.v_list);
4566 if (type == TYPE_NEQUAL)
4567 n1 = !n1;
4568 }
4569 else if (rettv->v_type != var2.v_type
4570 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4571 {
4572 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004573 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004574 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004575 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004576 clear_tv(rettv);
4577 clear_tv(&var2);
4578 return FAIL;
4579 }
4580 else
4581 {
4582 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004583 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4584 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004585 if (type == TYPE_NEQUAL)
4586 n1 = !n1;
4587 }
4588 }
4589
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004590 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4591 {
4592 if (type_is)
4593 {
4594 n1 = (rettv->v_type == var2.v_type
4595 && rettv->vval.v_dict == var2.vval.v_dict);
4596 if (type == TYPE_NEQUAL)
4597 n1 = !n1;
4598 }
4599 else if (rettv->v_type != var2.v_type
4600 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4601 {
4602 if (rettv->v_type != var2.v_type)
4603 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4604 else
4605 EMSG(_("E736: Invalid operation for Dictionary"));
4606 clear_tv(rettv);
4607 clear_tv(&var2);
4608 return FAIL;
4609 }
4610 else
4611 {
4612 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004613 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4614 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004615 if (type == TYPE_NEQUAL)
4616 n1 = !n1;
4617 }
4618 }
4619
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004620 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4621 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004622 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004623 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004624 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004625 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004626 clear_tv(rettv);
4627 clear_tv(&var2);
4628 return FAIL;
4629 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004630 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004631 if (type == TYPE_NEQUAL)
4632 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004633 }
4634
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004635#ifdef FEAT_FLOAT
4636 /*
4637 * If one of the two variables is a float, compare as a float.
4638 * When using "=~" or "!~", always compare as string.
4639 */
4640 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4641 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4642 {
4643 float_T f1, f2;
4644
4645 if (rettv->v_type == VAR_FLOAT)
4646 f1 = rettv->vval.v_float;
4647 else
4648 f1 = get_tv_number(rettv);
4649 if (var2.v_type == VAR_FLOAT)
4650 f2 = var2.vval.v_float;
4651 else
4652 f2 = get_tv_number(&var2);
4653 n1 = FALSE;
4654 switch (type)
4655 {
4656 case TYPE_EQUAL: n1 = (f1 == f2); break;
4657 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4658 case TYPE_GREATER: n1 = (f1 > f2); break;
4659 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4660 case TYPE_SMALLER: n1 = (f1 < f2); break;
4661 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4662 case TYPE_UNKNOWN:
4663 case TYPE_MATCH:
4664 case TYPE_NOMATCH: break; /* avoid gcc warning */
4665 }
4666 }
4667#endif
4668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 /*
4670 * If one of the two variables is a number, compare as a number.
4671 * When using "=~" or "!~", always compare as string.
4672 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004673 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 n1 = get_tv_number(rettv);
4677 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 switch (type)
4679 {
4680 case TYPE_EQUAL: n1 = (n1 == n2); break;
4681 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4682 case TYPE_GREATER: n1 = (n1 > n2); break;
4683 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4684 case TYPE_SMALLER: n1 = (n1 < n2); break;
4685 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4686 case TYPE_UNKNOWN:
4687 case TYPE_MATCH:
4688 case TYPE_NOMATCH: break; /* avoid gcc warning */
4689 }
4690 }
4691 else
4692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 s1 = get_tv_string_buf(rettv, buf1);
4694 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4696 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4697 else
4698 i = 0;
4699 n1 = FALSE;
4700 switch (type)
4701 {
4702 case TYPE_EQUAL: n1 = (i == 0); break;
4703 case TYPE_NEQUAL: n1 = (i != 0); break;
4704 case TYPE_GREATER: n1 = (i > 0); break;
4705 case TYPE_GEQUAL: n1 = (i >= 0); break;
4706 case TYPE_SMALLER: n1 = (i < 0); break;
4707 case TYPE_SEQUAL: n1 = (i <= 0); break;
4708
4709 case TYPE_MATCH:
4710 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004711 n1 = pattern_match(s2, s1, ic);
4712 if (type == TYPE_NOMATCH)
4713 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 break;
4715
4716 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4717 }
4718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 clear_tv(rettv);
4720 clear_tv(&var2);
4721 rettv->v_type = VAR_NUMBER;
4722 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 }
4724 }
4725
4726 return OK;
4727}
4728
4729/*
4730 * Handle fourth level expression:
4731 * + number addition
4732 * - number subtraction
4733 * . string concatenation
4734 *
4735 * "arg" must point to the first non-white of the expression.
4736 * "arg" is advanced to the next non-white after the recognized expression.
4737 *
4738 * Return OK or FAIL.
4739 */
4740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004741eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
Bram Moolenaar33570922005-01-25 22:26:29 +00004743 typval_T var2;
4744 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 int op;
4746 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004747#ifdef FEAT_FLOAT
4748 float_T f1 = 0, f2 = 0;
4749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 char_u *s1, *s2;
4751 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4752 char_u *p;
4753
4754 /*
4755 * Get the first variable.
4756 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004757 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 return FAIL;
4759
4760 /*
4761 * Repeat computing, until no '+', '-' or '.' is following.
4762 */
4763 for (;;)
4764 {
4765 op = **arg;
4766 if (op != '+' && op != '-' && op != '.')
4767 break;
4768
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004769 if ((op != '+' || rettv->v_type != VAR_LIST)
4770#ifdef FEAT_FLOAT
4771 && (op == '.' || rettv->v_type != VAR_FLOAT)
4772#endif
4773 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004774 {
4775 /* For "list + ...", an illegal use of the first operand as
4776 * a number cannot be determined before evaluating the 2nd
4777 * operand: if this is also a list, all is ok.
4778 * For "something . ...", "something - ..." or "non-list + ...",
4779 * we know that the first operand needs to be a string or number
4780 * without evaluating the 2nd operand. So check before to avoid
4781 * side effects after an error. */
4782 if (evaluate && get_tv_string_chk(rettv) == NULL)
4783 {
4784 clear_tv(rettv);
4785 return FAIL;
4786 }
4787 }
4788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 /*
4790 * Get the second variable.
4791 */
4792 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004793 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004795 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 return FAIL;
4797 }
4798
4799 if (evaluate)
4800 {
4801 /*
4802 * Compute the result.
4803 */
4804 if (op == '.')
4805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004806 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4807 s2 = get_tv_string_buf_chk(&var2, buf2);
4808 if (s2 == NULL) /* type error ? */
4809 {
4810 clear_tv(rettv);
4811 clear_tv(&var2);
4812 return FAIL;
4813 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004814 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 clear_tv(rettv);
4816 rettv->v_type = VAR_STRING;
4817 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004819 else if (op == '+' && rettv->v_type == VAR_LIST
4820 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004821 {
4822 /* concatenate Lists */
4823 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4824 &var3) == FAIL)
4825 {
4826 clear_tv(rettv);
4827 clear_tv(&var2);
4828 return FAIL;
4829 }
4830 clear_tv(rettv);
4831 *rettv = var3;
4832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 else
4834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 int error = FALSE;
4836
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#ifdef FEAT_FLOAT
4838 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840 f1 = rettv->vval.v_float;
4841 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004843 else
4844#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846 n1 = get_tv_number_chk(rettv, &error);
4847 if (error)
4848 {
4849 /* This can only happen for "list + non-list". For
4850 * "non-list + ..." or "something - ...", we returned
4851 * before evaluating the 2nd operand. */
4852 clear_tv(rettv);
4853 return FAIL;
4854 }
4855#ifdef FEAT_FLOAT
4856 if (var2.v_type == VAR_FLOAT)
4857 f1 = n1;
4858#endif
4859 }
4860#ifdef FEAT_FLOAT
4861 if (var2.v_type == VAR_FLOAT)
4862 {
4863 f2 = var2.vval.v_float;
4864 n2 = 0;
4865 }
4866 else
4867#endif
4868 {
4869 n2 = get_tv_number_chk(&var2, &error);
4870 if (error)
4871 {
4872 clear_tv(rettv);
4873 clear_tv(&var2);
4874 return FAIL;
4875 }
4876#ifdef FEAT_FLOAT
4877 if (rettv->v_type == VAR_FLOAT)
4878 f2 = n2;
4879#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882
4883#ifdef FEAT_FLOAT
4884 /* If there is a float on either side the result is a float. */
4885 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4886 {
4887 if (op == '+')
4888 f1 = f1 + f2;
4889 else
4890 f1 = f1 - f2;
4891 rettv->v_type = VAR_FLOAT;
4892 rettv->vval.v_float = f1;
4893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895#endif
4896 {
4897 if (op == '+')
4898 n1 = n1 + n2;
4899 else
4900 n1 = n1 - n2;
4901 rettv->v_type = VAR_NUMBER;
4902 rettv->vval.v_number = n1;
4903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004905 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 }
4908 return OK;
4909}
4910
4911/*
4912 * Handle fifth level expression:
4913 * * number multiplication
4914 * / number division
4915 * % number modulo
4916 *
4917 * "arg" must point to the first non-white of the expression.
4918 * "arg" is advanced to the next non-white after the recognized expression.
4919 *
4920 * Return OK or FAIL.
4921 */
4922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004923eval6(
4924 char_u **arg,
4925 typval_T *rettv,
4926 int evaluate,
4927 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
Bram Moolenaar33570922005-01-25 22:26:29 +00004929 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 int op;
4931 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932#ifdef FEAT_FLOAT
4933 int use_float = FALSE;
4934 float_T f1 = 0, f2;
4935#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004936 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 /*
4939 * Get the first variable.
4940 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004941 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 return FAIL;
4943
4944 /*
4945 * Repeat computing, until no '*', '/' or '%' is following.
4946 */
4947 for (;;)
4948 {
4949 op = **arg;
4950 if (op != '*' && op != '/' && op != '%')
4951 break;
4952
4953 if (evaluate)
4954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (rettv->v_type == VAR_FLOAT)
4957 {
4958 f1 = rettv->vval.v_float;
4959 use_float = TRUE;
4960 n1 = 0;
4961 }
4962 else
4963#endif
4964 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004966 if (error)
4967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
4969 else
4970 n1 = 0;
4971
4972 /*
4973 * Get the second variable.
4974 */
4975 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004976 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 return FAIL;
4978
4979 if (evaluate)
4980 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981#ifdef FEAT_FLOAT
4982 if (var2.v_type == VAR_FLOAT)
4983 {
4984 if (!use_float)
4985 {
4986 f1 = n1;
4987 use_float = TRUE;
4988 }
4989 f2 = var2.vval.v_float;
4990 n2 = 0;
4991 }
4992 else
4993#endif
4994 {
4995 n2 = get_tv_number_chk(&var2, &error);
4996 clear_tv(&var2);
4997 if (error)
4998 return FAIL;
4999#ifdef FEAT_FLOAT
5000 if (use_float)
5001 f2 = n2;
5002#endif
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
5005 /*
5006 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005007 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009#ifdef FEAT_FLOAT
5010 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 if (op == '*')
5013 f1 = f1 * f2;
5014 else if (op == '/')
5015 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005016# ifdef VMS
5017 /* VMS crashes on divide by zero, work around it */
5018 if (f2 == 0.0)
5019 {
5020 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005021 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005022 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005023 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005024 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005025 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005026 }
5027 else
5028 f1 = f1 / f2;
5029# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030 /* We rely on the floating point library to handle divide
5031 * by zero to result in "inf" and not a crash. */
5032 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005033# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005036 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005037 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005038 return FAIL;
5039 }
5040 rettv->v_type = VAR_FLOAT;
5041 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
5043 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005046 if (op == '*')
5047 n1 = n1 * n2;
5048 else if (op == '/')
5049 {
5050 if (n2 == 0) /* give an error message? */
5051 {
5052 if (n1 == 0)
5053 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5054 else if (n1 < 0)
5055 n1 = -0x7fffffffL;
5056 else
5057 n1 = 0x7fffffffL;
5058 }
5059 else
5060 n1 = n1 / n2;
5061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005063 {
5064 if (n2 == 0) /* give an error message? */
5065 n1 = 0;
5066 else
5067 n1 = n1 % n2;
5068 }
5069 rettv->v_type = VAR_NUMBER;
5070 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 }
5074
5075 return OK;
5076}
5077
5078/*
5079 * Handle sixth level expression:
5080 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005081 * "string" string constant
5082 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 * &option-name option value
5084 * @r register contents
5085 * identifier variable value
5086 * function() function call
5087 * $VAR environment variable
5088 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005089 * [expr, expr] List
5090 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 *
5092 * Also handle:
5093 * ! in front logical NOT
5094 * - in front unary minus
5095 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 * trailing [] subscript in String or List
5097 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 *
5099 * "arg" must point to the first non-white of the expression.
5100 * "arg" is advanced to the next non-white after the recognized expression.
5101 *
5102 * Return OK or FAIL.
5103 */
5104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005105eval7(
5106 char_u **arg,
5107 typval_T *rettv,
5108 int evaluate,
5109 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 long n;
5112 int len;
5113 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 char_u *start_leader, *end_leader;
5115 int ret = OK;
5116 char_u *alias;
5117
5118 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005119 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124 /*
5125 * Skip '!' and '-' characters. They are handled later.
5126 */
5127 start_leader = *arg;
5128 while (**arg == '!' || **arg == '-' || **arg == '+')
5129 *arg = skipwhite(*arg + 1);
5130 end_leader = *arg;
5131
5132 switch (**arg)
5133 {
5134 /*
5135 * Number constant.
5136 */
5137 case '0':
5138 case '1':
5139 case '2':
5140 case '3':
5141 case '4':
5142 case '5':
5143 case '6':
5144 case '7':
5145 case '8':
5146 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 {
5148#ifdef FEAT_FLOAT
5149 char_u *p = skipdigits(*arg + 1);
5150 int get_float = FALSE;
5151
5152 /* We accept a float when the format matches
5153 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005154 * strict to avoid backwards compatibility problems.
5155 * Don't look for a float after the "." operator, so that
5156 * ":let vers = 1.2.3" doesn't fail. */
5157 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005159 get_float = TRUE;
5160 p = skipdigits(p + 2);
5161 if (*p == 'e' || *p == 'E')
5162 {
5163 ++p;
5164 if (*p == '-' || *p == '+')
5165 ++p;
5166 if (!vim_isdigit(*p))
5167 get_float = FALSE;
5168 else
5169 p = skipdigits(p + 1);
5170 }
5171 if (ASCII_ISALPHA(*p) || *p == '.')
5172 get_float = FALSE;
5173 }
5174 if (get_float)
5175 {
5176 float_T f;
5177
5178 *arg += string2float(*arg, &f);
5179 if (evaluate)
5180 {
5181 rettv->v_type = VAR_FLOAT;
5182 rettv->vval.v_float = f;
5183 }
5184 }
5185 else
5186#endif
5187 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005188 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005189 *arg += len;
5190 if (evaluate)
5191 {
5192 rettv->v_type = VAR_NUMBER;
5193 rettv->vval.v_number = n;
5194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199 /*
5200 * String constant: "string".
5201 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 break;
5204
5205 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209 break;
5210
5211 /*
5212 * List: [expr, expr]
5213 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216
5217 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 * Dictionary: {key: val, key: val}
5219 */
5220 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5221 break;
5222
5223 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005224 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005226 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 break;
5228
5229 /*
5230 * Environment variable: $VAR.
5231 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005232 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 break;
5234
5235 /*
5236 * Register contents: @r.
5237 */
5238 case '@': ++*arg;
5239 if (evaluate)
5240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005241 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005242 rettv->vval.v_string = get_reg_contents(**arg,
5243 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245 if (**arg != NUL)
5246 ++*arg;
5247 break;
5248
5249 /*
5250 * nested expression: (expression).
5251 */
5252 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005253 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 if (**arg == ')')
5255 ++*arg;
5256 else if (ret == OK)
5257 {
5258 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005259 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 ret = FAIL;
5261 }
5262 break;
5263
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 break;
5266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267
5268 if (ret == NOTDONE)
5269 {
5270 /*
5271 * Must be a variable or function name.
5272 * Can also be a curly-braces kind of name: {expr}.
5273 */
5274 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005275 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 if (alias != NULL)
5277 s = alias;
5278
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005279 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 ret = FAIL;
5281 else
5282 {
5283 if (**arg == '(') /* recursive! */
5284 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005285 partial_T *partial;
5286
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 /* If "s" is the name of a variable of type VAR_FUNC
5288 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005289 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290
5291 /* Invoke the function. */
5292 ret = get_func_tv(s, len, rettv, arg,
5293 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005294 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005295
5296 /* If evaluate is FALSE rettv->v_type was not set in
5297 * get_func_tv, but it's needed in handle_subscript() to parse
5298 * what follows. So set it here. */
5299 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5300 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005301 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005302 rettv->v_type = VAR_FUNC;
5303 }
5304
Bram Moolenaar8c711452005-01-14 21:53:12 +00005305 /* Stop the expression evaluation when immediately
5306 * aborting on error, or when an interrupt occurred or
5307 * an exception was thrown but not caught. */
5308 if (aborting())
5309 {
5310 if (ret == OK)
5311 clear_tv(rettv);
5312 ret = FAIL;
5313 }
5314 }
5315 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005316 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005317 else
5318 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005320 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 }
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 *arg = skipwhite(*arg);
5324
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005325 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5326 * expr(expr). */
5327 if (ret == OK)
5328 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 /*
5331 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5332 */
5333 if (ret == OK && evaluate && end_leader > start_leader)
5334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005336 int val = 0;
5337#ifdef FEAT_FLOAT
5338 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005339
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005340 if (rettv->v_type == VAR_FLOAT)
5341 f = rettv->vval.v_float;
5342 else
5343#endif
5344 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005345 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005347 clear_tv(rettv);
5348 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 else
5351 {
5352 while (end_leader > start_leader)
5353 {
5354 --end_leader;
5355 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005356 {
5357#ifdef FEAT_FLOAT
5358 if (rettv->v_type == VAR_FLOAT)
5359 f = !f;
5360 else
5361#endif
5362 val = !val;
5363 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005364 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005365 {
5366#ifdef FEAT_FLOAT
5367 if (rettv->v_type == VAR_FLOAT)
5368 f = -f;
5369 else
5370#endif
5371 val = -val;
5372 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005373 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005374#ifdef FEAT_FLOAT
5375 if (rettv->v_type == VAR_FLOAT)
5376 {
5377 clear_tv(rettv);
5378 rettv->vval.v_float = f;
5379 }
5380 else
5381#endif
5382 {
5383 clear_tv(rettv);
5384 rettv->v_type = VAR_NUMBER;
5385 rettv->vval.v_number = val;
5386 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389
5390 return ret;
5391}
5392
5393/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005394 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5395 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5397 */
5398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005399eval_index(
5400 char_u **arg,
5401 typval_T *rettv,
5402 int evaluate,
5403 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404{
5405 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005406 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005408 long len = -1;
5409 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005411 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412
Bram Moolenaara03f2332016-02-06 18:09:59 +01005413 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005415 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005416 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005417 if (verbose)
5418 EMSG(_("E695: Cannot index a Funcref"));
5419 return FAIL;
5420 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005421#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005422 if (verbose)
5423 EMSG(_(e_float_as_string));
5424 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005425#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005426 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005427 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005428 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005429 if (verbose)
5430 EMSG(_("E909: Cannot index a special variable"));
5431 return FAIL;
5432 case VAR_UNKNOWN:
5433 if (evaluate)
5434 return FAIL;
5435 /* FALLTHROUGH */
5436
5437 case VAR_STRING:
5438 case VAR_NUMBER:
5439 case VAR_LIST:
5440 case VAR_DICT:
5441 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005442 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005444 init_tv(&var1);
5445 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 /*
5449 * dict.name
5450 */
5451 key = *arg + 1;
5452 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5453 ;
5454 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 /*
5461 * something[idx]
5462 *
5463 * Get the (first) variable from inside the [].
5464 */
5465 *arg = skipwhite(*arg + 1);
5466 if (**arg == ':')
5467 empty1 = TRUE;
5468 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5469 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005470 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5471 {
5472 /* not a number or string */
5473 clear_tv(&var1);
5474 return FAIL;
5475 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476
5477 /*
5478 * Get the second variable from inside the [:].
5479 */
5480 if (**arg == ':')
5481 {
5482 range = TRUE;
5483 *arg = skipwhite(*arg + 1);
5484 if (**arg == ']')
5485 empty2 = TRUE;
5486 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005488 if (!empty1)
5489 clear_tv(&var1);
5490 return FAIL;
5491 }
5492 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5493 {
5494 /* not a number or string */
5495 if (!empty1)
5496 clear_tv(&var1);
5497 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 return FAIL;
5499 }
5500 }
5501
5502 /* Check for the ']'. */
5503 if (**arg != ']')
5504 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005505 if (verbose)
5506 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507 clear_tv(&var1);
5508 if (range)
5509 clear_tv(&var2);
5510 return FAIL;
5511 }
5512 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 }
5514
5515 if (evaluate)
5516 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517 n1 = 0;
5518 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520 n1 = get_tv_number(&var1);
5521 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 }
5523 if (range)
5524 {
5525 if (empty2)
5526 n2 = -1;
5527 else
5528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 n2 = get_tv_number(&var2);
5530 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 }
5532 }
5533
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005536 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005537 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005538 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005539 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005540 case VAR_SPECIAL:
5541 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005542 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005543 break; /* not evaluating, skipping over subscript */
5544
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 case VAR_NUMBER:
5546 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 len = (long)STRLEN(s);
5549 if (range)
5550 {
5551 /* The resulting variable is a substring. If the indexes
5552 * are out of range the result is empty. */
5553 if (n1 < 0)
5554 {
5555 n1 = len + n1;
5556 if (n1 < 0)
5557 n1 = 0;
5558 }
5559 if (n2 < 0)
5560 n2 = len + n2;
5561 else if (n2 >= len)
5562 n2 = len;
5563 if (n1 >= len || n2 < 0 || n1 > n2)
5564 s = NULL;
5565 else
5566 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5567 }
5568 else
5569 {
5570 /* The resulting variable is a string of a single
5571 * character. If the index is too big or negative the
5572 * result is empty. */
5573 if (n1 >= len || n1 < 0)
5574 s = NULL;
5575 else
5576 s = vim_strnsave(s + n1, 1);
5577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578 clear_tv(rettv);
5579 rettv->v_type = VAR_STRING;
5580 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 break;
5582
5583 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 if (n1 < 0)
5586 n1 = len + n1;
5587 if (!empty1 && (n1 < 0 || n1 >= len))
5588 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005589 /* For a range we allow invalid values and return an empty
5590 * list. A list index out of range is an error. */
5591 if (!range)
5592 {
5593 if (verbose)
5594 EMSGN(_(e_listidx), n1);
5595 return FAIL;
5596 }
5597 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 }
5599 if (range)
5600 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005601 list_T *l;
5602 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603
5604 if (n2 < 0)
5605 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005606 else if (n2 >= len)
5607 n2 = len - 1;
5608 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005609 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 l = list_alloc();
5611 if (l == NULL)
5612 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 n1 <= n2; ++n1)
5615 {
5616 if (list_append_tv(l, &item->li_tv) == FAIL)
5617 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005618 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 return FAIL;
5620 }
5621 item = item->li_next;
5622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 clear_tv(rettv);
5624 rettv->v_type = VAR_LIST;
5625 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005626 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 }
5628 else
5629 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005630 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 clear_tv(rettv);
5632 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 }
5634 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635
5636 case VAR_DICT:
5637 if (range)
5638 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005639 if (verbose)
5640 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 if (len == -1)
5642 clear_tv(&var1);
5643 return FAIL;
5644 }
5645 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005646 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647
5648 if (len == -1)
5649 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005650 key = get_tv_string_chk(&var1);
5651 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 clear_tv(&var1);
5654 return FAIL;
5655 }
5656 }
5657
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005658 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005660 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005661 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 if (len == -1)
5663 clear_tv(&var1);
5664 if (item == NULL)
5665 return FAIL;
5666
5667 copy_tv(&item->di_tv, &var1);
5668 clear_tv(rettv);
5669 *rettv = var1;
5670 }
5671 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005672 }
5673 }
5674
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 return OK;
5676}
5677
5678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 * Get an option value.
5680 * "arg" points to the '&' or '+' before the option name.
5681 * "arg" is advanced to character after the option name.
5682 * Return OK or FAIL.
5683 */
5684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005685get_option_tv(
5686 char_u **arg,
5687 typval_T *rettv, /* when NULL, only check if option exists */
5688 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 char_u *option_end;
5691 long numval;
5692 char_u *stringval;
5693 int opt_type;
5694 int c;
5695 int working = (**arg == '+'); /* has("+option") */
5696 int ret = OK;
5697 int opt_flags;
5698
5699 /*
5700 * Isolate the option name and find its value.
5701 */
5702 option_end = find_option_end(arg, &opt_flags);
5703 if (option_end == NULL)
5704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 EMSG2(_("E112: Option name missing: %s"), *arg);
5707 return FAIL;
5708 }
5709
5710 if (!evaluate)
5711 {
5712 *arg = option_end;
5713 return OK;
5714 }
5715
5716 c = *option_end;
5717 *option_end = NUL;
5718 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005719 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
5721 if (opt_type == -3) /* invalid name */
5722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005723 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 EMSG2(_("E113: Unknown option: %s"), *arg);
5725 ret = FAIL;
5726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005727 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
5729 if (opt_type == -2) /* hidden string option */
5730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005731 rettv->v_type = VAR_STRING;
5732 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734 else if (opt_type == -1) /* hidden number option */
5735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005736 rettv->v_type = VAR_NUMBER;
5737 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739 else if (opt_type == 1) /* number option */
5740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005741 rettv->v_type = VAR_NUMBER;
5742 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 }
5744 else /* string option */
5745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005746 rettv->v_type = VAR_STRING;
5747 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749 }
5750 else if (working && (opt_type == -2 || opt_type == -1))
5751 ret = FAIL;
5752
5753 *option_end = c; /* put back for error messages */
5754 *arg = option_end;
5755
5756 return ret;
5757}
5758
5759/*
5760 * Allocate a variable for a string constant.
5761 * Return OK or FAIL.
5762 */
5763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005764get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766 char_u *p;
5767 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 int extra = 0;
5769
5770 /*
5771 * Find the end of the string, skipping backslashed characters.
5772 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 if (*p == '\\' && p[1] != NUL)
5776 {
5777 ++p;
5778 /* A "\<x>" form occupies at least 4 characters, and produces up
5779 * to 6 characters: reserve space for 2 extra */
5780 if (*p == '<')
5781 extra += 2;
5782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
5784
5785 if (*p != '"')
5786 {
5787 EMSG2(_("E114: Missing quote: %s"), *arg);
5788 return FAIL;
5789 }
5790
5791 /* If only parsing, set *arg and return here */
5792 if (!evaluate)
5793 {
5794 *arg = p + 1;
5795 return OK;
5796 }
5797
5798 /*
5799 * Copy the string into allocated memory, handling backslashed
5800 * characters.
5801 */
5802 name = alloc((unsigned)(p - *arg + extra));
5803 if (name == NULL)
5804 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 rettv->v_type = VAR_STRING;
5806 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {
5810 if (*p == '\\')
5811 {
5812 switch (*++p)
5813 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 case 'b': *name++ = BS; ++p; break;
5815 case 'e': *name++ = ESC; ++p; break;
5816 case 'f': *name++ = FF; ++p; break;
5817 case 'n': *name++ = NL; ++p; break;
5818 case 'r': *name++ = CAR; ++p; break;
5819 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
5821 case 'X': /* hex: "\x1", "\x12" */
5822 case 'x':
5823 case 'u': /* Unicode: "\u0023" */
5824 case 'U':
5825 if (vim_isxdigit(p[1]))
5826 {
5827 int n, nr;
5828 int c = toupper(*p);
5829
5830 if (c == 'X')
5831 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005832 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005834 else
5835 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 nr = 0;
5837 while (--n >= 0 && vim_isxdigit(p[1]))
5838 {
5839 ++p;
5840 nr = (nr << 4) + hex2nr(*p);
5841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843#ifdef FEAT_MBYTE
5844 /* For "\u" store the number according to
5845 * 'encoding'. */
5846 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 else
5849#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 break;
5853
5854 /* octal: "\1", "\12", "\123" */
5855 case '0':
5856 case '1':
5857 case '2':
5858 case '3':
5859 case '4':
5860 case '5':
5861 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 case '7': *name = *p++ - '0';
5863 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 *name = (*name << 3) + *p++ - '0';
5866 if (*p >= '0' && *p <= '7')
5867 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 break;
5871
5872 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 if (extra != 0)
5875 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 break;
5878 }
5879 /* FALLTHROUGH */
5880
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 break;
5883 }
5884 }
5885 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 *arg = p + 1;
5891
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 return OK;
5893}
5894
5895/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 * Return OK or FAIL.
5898 */
5899 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005900get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
5902 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 int reduce = 0;
5905
5906 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 */
5909 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914 break;
5915 ++reduce;
5916 ++p;
5917 }
5918 }
5919
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 return FAIL;
5924 }
5925
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 if (!evaluate)
5928 {
5929 *arg = p + 1;
5930 return OK;
5931 }
5932
5933 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005935 */
5936 str = alloc((unsigned)((p - *arg) - reduce));
5937 if (str == NULL)
5938 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 rettv->v_type = VAR_STRING;
5940 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005941
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005943 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005944 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005947 break;
5948 ++p;
5949 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005950 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005951 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005952 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005953 *arg = p + 1;
5954
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005955 return OK;
5956}
5957
Bram Moolenaarddecc252016-04-06 22:59:37 +02005958 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005959partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005960{
5961 int i;
5962
5963 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005964 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005965 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005966 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005967 func_unref(pt->pt_name);
5968 vim_free(pt->pt_name);
5969 vim_free(pt);
5970}
5971
5972/*
5973 * Unreference a closure: decrement the reference count and free it when it
5974 * becomes zero.
5975 */
5976 void
5977partial_unref(partial_T *pt)
5978{
5979 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005980 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005981}
5982
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005983/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 * Allocate a variable for a List and fill it from "*arg".
5985 * Return OK or FAIL.
5986 */
5987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005988get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989{
Bram Moolenaar33570922005-01-25 22:26:29 +00005990 list_T *l = NULL;
5991 typval_T tv;
5992 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993
5994 if (evaluate)
5995 {
5996 l = list_alloc();
5997 if (l == NULL)
5998 return FAIL;
5999 }
6000
6001 *arg = skipwhite(*arg + 1);
6002 while (**arg != ']' && **arg != NUL)
6003 {
6004 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6005 goto failret;
6006 if (evaluate)
6007 {
6008 item = listitem_alloc();
6009 if (item != NULL)
6010 {
6011 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006012 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 list_append(l, item);
6014 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006015 else
6016 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 }
6018
6019 if (**arg == ']')
6020 break;
6021 if (**arg != ',')
6022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006023 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 goto failret;
6025 }
6026 *arg = skipwhite(*arg + 1);
6027 }
6028
6029 if (**arg != ']')
6030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006031 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032failret:
6033 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006034 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 return FAIL;
6036 }
6037
6038 *arg = skipwhite(*arg + 1);
6039 if (evaluate)
6040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006041 rettv->v_type = VAR_LIST;
6042 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 ++l->lv_refcount;
6044 }
6045
6046 return OK;
6047}
6048
6049/*
6050 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006051 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006053 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006054list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006056 list_T *l;
6057
6058 l = (list_T *)alloc_clear(sizeof(list_T));
6059 if (l != NULL)
6060 {
6061 /* Prepend the list to the list of lists for garbage collection. */
6062 if (first_list != NULL)
6063 first_list->lv_used_prev = l;
6064 l->lv_used_prev = NULL;
6065 l->lv_used_next = first_list;
6066 first_list = l;
6067 }
6068 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069}
6070
6071/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006072 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006073 * Returns OK or FAIL.
6074 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006075 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006076rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006077{
6078 list_T *l = list_alloc();
6079
6080 if (l == NULL)
6081 return FAIL;
6082
6083 rettv->vval.v_list = l;
6084 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006085 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006086 ++l->lv_refcount;
6087 return OK;
6088}
6089
6090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 * Unreference a list: decrement the reference count and free it when it
6092 * becomes zero.
6093 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006097 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006098 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099}
6100
6101/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006102 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103 * Ignores the reference count.
6104 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006105 static void
6106list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006107{
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006109
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006110 for (item = l->lv_first; item != NULL; item = l->lv_first)
6111 {
6112 /* Remove the item before deleting it. */
6113 l->lv_first = item->li_next;
6114 clear_tv(&item->li_tv);
6115 vim_free(item);
6116 }
6117}
6118
6119 static void
6120list_free_list(list_T *l)
6121{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006122 /* Remove the list from the list of lists for garbage collection. */
6123 if (l->lv_used_prev == NULL)
6124 first_list = l->lv_used_next;
6125 else
6126 l->lv_used_prev->lv_used_next = l->lv_used_next;
6127 if (l->lv_used_next != NULL)
6128 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6129
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130 vim_free(l);
6131}
6132
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006133 void
6134list_free(list_T *l)
6135{
6136 if (!in_free_unref_items)
6137 {
6138 list_free_contents(l);
6139 list_free_list(l);
6140 }
6141}
6142
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143/*
6144 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006145 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006147 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006148listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149{
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151}
6152
6153/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006154 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006159 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160 vim_free(item);
6161}
6162
6163/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006164 * Remove a list item from a List and free it. Also clears the value.
6165 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006168{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006169 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006170 listitem_free(item);
6171}
6172
6173/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 * Get the number of items in a list.
6175 */
6176 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006177list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179 if (l == NULL)
6180 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006181 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182}
6183
6184/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185 * Return TRUE when two lists have exactly the same values.
6186 */
6187 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006188list_equal(
6189 list_T *l1,
6190 list_T *l2,
6191 int ic, /* ignore case for strings */
6192 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193{
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006195
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006196 if (l1 == NULL || l2 == NULL)
6197 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006198 if (l1 == l2)
6199 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006200 if (list_len(l1) != list_len(l2))
6201 return FALSE;
6202
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006203 for (item1 = l1->lv_first, item2 = l2->lv_first;
6204 item1 != NULL && item2 != NULL;
6205 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006207 return FALSE;
6208 return item1 == NULL && item2 == NULL;
6209}
6210
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006211/*
6212 * Return the dictitem that an entry in a hashtable points to.
6213 */
6214 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006215dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006216{
6217 return HI2DI(hi);
6218}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006219
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006220/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006221 * Return TRUE when two dictionaries have exactly the same key/values.
6222 */
6223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006224dict_equal(
6225 dict_T *d1,
6226 dict_T *d2,
6227 int ic, /* ignore case for strings */
6228 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006229{
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 hashitem_T *hi;
6231 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006232 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006233
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006234 if (d1 == NULL && d2 == NULL)
6235 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006236 if (d1 == NULL || d2 == NULL)
6237 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006238 if (d1 == d2)
6239 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006240 if (dict_len(d1) != dict_len(d2))
6241 return FALSE;
6242
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006243 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006245 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006246 if (!HASHITEM_EMPTY(hi))
6247 {
6248 item2 = dict_find(d2, hi->hi_key, -1);
6249 if (item2 == NULL)
6250 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006251 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006252 return FALSE;
6253 --todo;
6254 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006255 }
6256 return TRUE;
6257}
6258
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006259static int tv_equal_recurse_limit;
6260
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006261/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006262 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006263 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006264 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006265 */
6266 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006267tv_equal(
6268 typval_T *tv1,
6269 typval_T *tv2,
6270 int ic, /* ignore case */
6271 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006272{
6273 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006274 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006275 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006276 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006277
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006278 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6279 if ((tv1->v_type == VAR_FUNC
6280 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6281 && (tv2->v_type == VAR_FUNC
6282 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6283 {
6284 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6285 : tv1->vval.v_partial->pt_name;
6286 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6287 : tv2->vval.v_partial->pt_name;
6288 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6289 }
6290
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006291 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006292 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006293
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006294 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006295 * recursiveness to a limit. We guess they are equal then.
6296 * A fixed limit has the problem of still taking an awful long time.
6297 * Reduce the limit every time running into it. That should work fine for
6298 * deeply linked structures that are not recursively linked and catch
6299 * recursiveness quickly. */
6300 if (!recursive)
6301 tv_equal_recurse_limit = 1000;
6302 if (recursive_cnt >= tv_equal_recurse_limit)
6303 {
6304 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006305 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006306 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006307
6308 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006310 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006311 ++recursive_cnt;
6312 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6313 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006314 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006315
6316 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006317 ++recursive_cnt;
6318 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6319 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006320 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006321
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006322 case VAR_NUMBER:
6323 return tv1->vval.v_number == tv2->vval.v_number;
6324
6325 case VAR_STRING:
6326 s1 = get_tv_string_buf(tv1, buf1);
6327 s2 = get_tv_string_buf(tv2, buf2);
6328 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006329
6330 case VAR_SPECIAL:
6331 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006332
6333 case VAR_FLOAT:
6334#ifdef FEAT_FLOAT
6335 return tv1->vval.v_float == tv2->vval.v_float;
6336#endif
6337 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006338#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006339 return tv1->vval.v_job == tv2->vval.v_job;
6340#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006341 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006342#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006343 return tv1->vval.v_channel == tv2->vval.v_channel;
6344#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006345 case VAR_FUNC:
6346 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006347 case VAR_UNKNOWN:
6348 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006349 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006350
Bram Moolenaara03f2332016-02-06 18:09:59 +01006351 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6352 * does not equal anything, not even itself. */
6353 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006354}
6355
6356/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006357 * Locate item with index "n" in list "l" and return it.
6358 * A negative index is counted from the end; -1 is the last item.
6359 * Returns NULL when "n" is out of range.
6360 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006361 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006362list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363{
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006365 long idx;
6366
6367 if (l == NULL)
6368 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006369
6370 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006372 n = l->lv_len + n;
6373
6374 /* Check for index out of range. */
6375 if (n < 0 || n >= l->lv_len)
6376 return NULL;
6377
6378 /* When there is a cached index may start search from there. */
6379 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006381 if (n < l->lv_idx / 2)
6382 {
6383 /* closest to the start of the list */
6384 item = l->lv_first;
6385 idx = 0;
6386 }
6387 else if (n > (l->lv_idx + l->lv_len) / 2)
6388 {
6389 /* closest to the end of the list */
6390 item = l->lv_last;
6391 idx = l->lv_len - 1;
6392 }
6393 else
6394 {
6395 /* closest to the cached index */
6396 item = l->lv_idx_item;
6397 idx = l->lv_idx;
6398 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 }
6400 else
6401 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006402 if (n < l->lv_len / 2)
6403 {
6404 /* closest to the start of the list */
6405 item = l->lv_first;
6406 idx = 0;
6407 }
6408 else
6409 {
6410 /* closest to the end of the list */
6411 item = l->lv_last;
6412 idx = l->lv_len - 1;
6413 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006415
6416 while (n > idx)
6417 {
6418 /* search forward */
6419 item = item->li_next;
6420 ++idx;
6421 }
6422 while (n < idx)
6423 {
6424 /* search backward */
6425 item = item->li_prev;
6426 --idx;
6427 }
6428
6429 /* cache the used index */
6430 l->lv_idx = idx;
6431 l->lv_idx_item = item;
6432
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 return item;
6434}
6435
6436/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006437 * Get list item "l[idx]" as a number.
6438 */
6439 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440list_find_nr(
6441 list_T *l,
6442 long idx,
6443 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006444{
6445 listitem_T *li;
6446
6447 li = list_find(l, idx);
6448 if (li == NULL)
6449 {
6450 if (errorp != NULL)
6451 *errorp = TRUE;
6452 return -1L;
6453 }
6454 return get_tv_number_chk(&li->li_tv, errorp);
6455}
6456
6457/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006458 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6459 */
6460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006461list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006462{
6463 listitem_T *li;
6464
6465 li = list_find(l, idx - 1);
6466 if (li == NULL)
6467 {
6468 EMSGN(_(e_listidx), idx);
6469 return NULL;
6470 }
6471 return get_tv_string(&li->li_tv);
6472}
6473
6474/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006475 * Locate "item" list "l" and return its index.
6476 * Returns -1 when "item" is not in the list.
6477 */
6478 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006479list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006480{
6481 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006483
6484 if (l == NULL)
6485 return -1;
6486 idx = 0;
6487 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6488 ++idx;
6489 if (li == NULL)
6490 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006491 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006492}
6493
6494/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495 * Append item "item" to the end of list "l".
6496 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006498list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499{
6500 if (l->lv_last == NULL)
6501 {
6502 /* empty list */
6503 l->lv_first = item;
6504 l->lv_last = item;
6505 item->li_prev = NULL;
6506 }
6507 else
6508 {
6509 l->lv_last->li_next = item;
6510 item->li_prev = l->lv_last;
6511 l->lv_last = item;
6512 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006513 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 item->li_next = NULL;
6515}
6516
6517/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006522list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006524 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525
Bram Moolenaar05159a02005-02-26 23:04:13 +00006526 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006528 copy_tv(tv, &li->li_tv);
6529 list_append(l, li);
6530 return OK;
6531}
6532
6533/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006534 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006535 * Return FAIL when out of memory.
6536 */
6537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006539{
6540 listitem_T *li = listitem_alloc();
6541
6542 if (li == NULL)
6543 return FAIL;
6544 li->li_tv.v_type = VAR_DICT;
6545 li->li_tv.v_lock = 0;
6546 li->li_tv.vval.v_dict = dict;
6547 list_append(list, li);
6548 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 return OK;
6550}
6551
6552/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006553 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006554 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006555 * Returns FAIL when out of memory.
6556 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006557 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006558list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006559{
6560 listitem_T *li = listitem_alloc();
6561
6562 if (li == NULL)
6563 return FAIL;
6564 list_append(l, li);
6565 li->li_tv.v_type = VAR_STRING;
6566 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006567 if (str == NULL)
6568 li->li_tv.vval.v_string = NULL;
6569 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006570 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006571 return FAIL;
6572 return OK;
6573}
6574
6575/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006576 * Append "n" to list "l".
6577 * Returns FAIL when out of memory.
6578 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006579 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006580list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006581{
6582 listitem_T *li;
6583
6584 li = listitem_alloc();
6585 if (li == NULL)
6586 return FAIL;
6587 li->li_tv.v_type = VAR_NUMBER;
6588 li->li_tv.v_lock = 0;
6589 li->li_tv.vval.v_number = n;
6590 list_append(l, li);
6591 return OK;
6592}
6593
6594/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006595 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006596 * If "item" is NULL append at the end.
6597 * Return FAIL when out of memory.
6598 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006599 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006600list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006601{
Bram Moolenaar33570922005-01-25 22:26:29 +00006602 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006603
6604 if (ni == NULL)
6605 return FAIL;
6606 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006607 list_insert(l, ni, item);
6608 return OK;
6609}
6610
6611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006612list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006613{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006614 if (item == NULL)
6615 /* Append new item at end of list. */
6616 list_append(l, ni);
6617 else
6618 {
6619 /* Insert new item before existing item. */
6620 ni->li_prev = item->li_prev;
6621 ni->li_next = item;
6622 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006623 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006625 ++l->lv_idx;
6626 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006627 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006628 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006630 l->lv_idx_item = NULL;
6631 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006632 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006633 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635}
6636
6637/*
6638 * Extend "l1" with "l2".
6639 * If "bef" is NULL append at the end, otherwise insert before this item.
6640 * Returns FAIL when out of memory.
6641 */
6642 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006643list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644{
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006646 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006648 /* We also quit the loop when we have inserted the original item count of
6649 * the list, avoid a hang when we extend a list with itself. */
6650 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6652 return FAIL;
6653 return OK;
6654}
6655
6656/*
6657 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6658 * Return FAIL when out of memory.
6659 */
6660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006661list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006662{
Bram Moolenaar33570922005-01-25 22:26:29 +00006663 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006665 if (l1 == NULL || l2 == NULL)
6666 return FAIL;
6667
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006668 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006669 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006670 if (l == NULL)
6671 return FAIL;
6672 tv->v_type = VAR_LIST;
6673 tv->vval.v_list = l;
6674
6675 /* append all items from the second list */
6676 return list_extend(l, l2, NULL);
6677}
6678
6679/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006680 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006682 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683 * Returns NULL when out of memory.
6684 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006685 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006686list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687{
Bram Moolenaar33570922005-01-25 22:26:29 +00006688 list_T *copy;
6689 listitem_T *item;
6690 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006691
6692 if (orig == NULL)
6693 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694
6695 copy = list_alloc();
6696 if (copy != NULL)
6697 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006698 if (copyID != 0)
6699 {
6700 /* Do this before adding the items, because one of the items may
6701 * refer back to this list. */
6702 orig->lv_copyID = copyID;
6703 orig->lv_copylist = copy;
6704 }
6705 for (item = orig->lv_first; item != NULL && !got_int;
6706 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707 {
6708 ni = listitem_alloc();
6709 if (ni == NULL)
6710 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006711 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 {
6713 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6714 {
6715 vim_free(ni);
6716 break;
6717 }
6718 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006720 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006721 list_append(copy, ni);
6722 }
6723 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006724 if (item != NULL)
6725 {
6726 list_unref(copy);
6727 copy = NULL;
6728 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006729 }
6730
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006731 return copy;
6732}
6733
6734/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006735 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006736 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006737 * This used to be called list_remove, but that conflicts with a Sun header
6738 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006739 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006740 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006741vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742{
Bram Moolenaar33570922005-01-25 22:26:29 +00006743 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006745 /* notify watchers */
6746 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006747 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006748 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006749 list_fix_watch(l, ip);
6750 if (ip == item2)
6751 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006752 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006753
6754 if (item2->li_next == NULL)
6755 l->lv_last = item->li_prev;
6756 else
6757 item2->li_next->li_prev = item->li_prev;
6758 if (item->li_prev == NULL)
6759 l->lv_first = item2->li_next;
6760 else
6761 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006762 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006763}
6764
6765/*
6766 * Return an allocated string with the string representation of a list.
6767 * May return NULL.
6768 */
6769 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006770list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006771{
6772 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006773
6774 if (tv->vval.v_list == NULL)
6775 return NULL;
6776 ga_init2(&ga, (int)sizeof(char), 80);
6777 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006778 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6779 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006780 {
6781 vim_free(ga.ga_data);
6782 return NULL;
6783 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006784 ga_append(&ga, ']');
6785 ga_append(&ga, NUL);
6786 return (char_u *)ga.ga_data;
6787}
6788
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006789typedef struct join_S {
6790 char_u *s;
6791 char_u *tofree;
6792} join_T;
6793
6794 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006795list_join_inner(
6796 garray_T *gap, /* to store the result in */
6797 list_T *l,
6798 char_u *sep,
6799 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006800 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006801 int copyID,
6802 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006803{
6804 int i;
6805 join_T *p;
6806 int len;
6807 int sumlen = 0;
6808 int first = TRUE;
6809 char_u *tofree;
6810 char_u numbuf[NUMBUFLEN];
6811 listitem_T *item;
6812 char_u *s;
6813
6814 /* Stringify each item in the list. */
6815 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6816 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006817 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6818 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006819 if (s == NULL)
6820 return FAIL;
6821
6822 len = (int)STRLEN(s);
6823 sumlen += len;
6824
Bram Moolenaarcde88542015-08-11 19:14:00 +02006825 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006826 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6827 if (tofree != NULL || s != numbuf)
6828 {
6829 p->s = s;
6830 p->tofree = tofree;
6831 }
6832 else
6833 {
6834 p->s = vim_strnsave(s, len);
6835 p->tofree = p->s;
6836 }
6837
6838 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006839 if (did_echo_string_emsg) /* recursion error, bail out */
6840 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006841 }
6842
6843 /* Allocate result buffer with its total size, avoid re-allocation and
6844 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6845 if (join_gap->ga_len >= 2)
6846 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6847 if (ga_grow(gap, sumlen + 2) == FAIL)
6848 return FAIL;
6849
6850 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6851 {
6852 if (first)
6853 first = FALSE;
6854 else
6855 ga_concat(gap, sep);
6856 p = ((join_T *)join_gap->ga_data) + i;
6857
6858 if (p->s != NULL)
6859 ga_concat(gap, p->s);
6860 line_breakcheck();
6861 }
6862
6863 return OK;
6864}
6865
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006866/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006867 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006868 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006869 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006870 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006872list_join(
6873 garray_T *gap,
6874 list_T *l,
6875 char_u *sep,
6876 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006877 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006878 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006879{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006880 garray_T join_ga;
6881 int retval;
6882 join_T *p;
6883 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006884
Bram Moolenaard39a7512015-04-16 22:51:22 +02006885 if (l->lv_len < 1)
6886 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006887 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006888 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6889 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006890
6891 /* Dispose each item in join_ga. */
6892 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006893 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006894 p = (join_T *)join_ga.ga_data;
6895 for (i = 0; i < join_ga.ga_len; ++i)
6896 {
6897 vim_free(p->tofree);
6898 ++p;
6899 }
6900 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006902
6903 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006904}
6905
6906/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006907 * Return the next (unique) copy ID.
6908 * Used for serializing nested structures.
6909 */
6910 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006911get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006912{
6913 current_copyID += COPYID_INC;
6914 return current_copyID;
6915}
6916
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006917/* Used by get_func_tv() */
6918static garray_T funcargs = GA_EMPTY;
6919
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006920/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 * Garbage collection for lists and dictionaries.
6922 *
6923 * We use reference counts to be able to free most items right away when they
6924 * are no longer used. But for composite items it's possible that it becomes
6925 * unused while the reference count is > 0: When there is a recursive
6926 * reference. Example:
6927 * :let l = [1, 2, 3]
6928 * :let d = {9: l}
6929 * :let l[1] = d
6930 *
6931 * Since this is quite unusual we handle this with garbage collection: every
6932 * once in a while find out which lists and dicts are not referenced from any
6933 * variable.
6934 *
6935 * Here is a good reference text about garbage collection (refers to Python
6936 * but it applies to all reference-counting mechanisms):
6937 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006938 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939
6940/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02006942 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006944 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006946garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006947{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 buf_T *buf;
6951 win_T *wp;
6952 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006953 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006954 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006956#ifdef FEAT_WINDOWS
6957 tabpage_T *tp;
6958#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006960 if (!testing)
6961 {
6962 /* Only do this once. */
6963 want_garbage_collect = FALSE;
6964 may_garbage_collect = FALSE;
6965 garbage_collect_at_exit = FALSE;
6966 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006967
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968 /* We advance by two because we add one for items referenced through
6969 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006970 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972 /*
6973 * 1. Go through all accessible variables and mark all lists and dicts
6974 * with copyID.
6975 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976
6977 /* Don't free variables in the previous_funccal list unless they are only
6978 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006979 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6981 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006982 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6983 NULL);
6984 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6985 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006986 }
6987
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 /* script-local variables */
6989 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006990 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991
6992 /* buffer-local variables */
6993 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006994 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6995 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996
6997 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006998 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006999 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7000 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007001#ifdef FEAT_AUTOCMD
7002 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7004 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007005#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007007#ifdef FEAT_WINDOWS
7008 /* tabpage-local variables */
7009 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007010 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7011 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007012#endif
7013
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016
7017 /* function-local variables */
7018 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7019 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7021 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 }
7023
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007024 /* function call arguments, if v:testing is set. */
7025 for (i = 0; i < funcargs.ga_len; ++i)
7026 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7027 copyID, NULL, NULL);
7028
Bram Moolenaard812df62008-11-09 12:46:09 +00007029 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007031
Bram Moolenaar1dced572012-04-05 16:54:08 +02007032#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007034#endif
7035
Bram Moolenaardb913952012-06-29 12:54:53 +02007036#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007038#endif
7039
7040#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007042#endif
7043
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007044#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007045 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007046 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007047#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007048#ifdef FEAT_NETBEANS_INTG
7049 abort = abort || set_ref_in_nb_channel(copyID);
7050#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007051
Bram Moolenaare3188e22016-05-31 21:13:04 +02007052#ifdef FEAT_TIMERS
7053 abort = abort || set_ref_in_timer(copyID);
7054#endif
7055
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007057 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 /*
7059 * 2. Free lists and dictionaries that are not referenced.
7060 */
7061 did_free = free_unref_items(copyID);
7062
7063 /*
7064 * 3. Check if any funccal can be freed now.
7065 */
7066 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007067 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 if (can_free_funccal(*pfc, copyID))
7069 {
7070 fc = *pfc;
7071 *pfc = fc->caller;
7072 free_funccal(fc, TRUE);
7073 did_free = TRUE;
7074 did_free_funccal = TRUE;
7075 }
7076 else
7077 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007078 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 if (did_free_funccal)
7080 /* When a funccal was freed some more items might be garbage
7081 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007082 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007083 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 else if (p_verbose > 0)
7085 {
7086 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7087 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007088
7089 return did_free;
7090}
7091
7092/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007093 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007094 */
7095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007096free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007097{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007098 dict_T *dd, *dd_next;
7099 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007100 int did_free = FALSE;
7101
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007102 /* Let all "free" functions know that we are here. This means no
7103 * dictionaries, lists, channels or jobs are to be freed, because we will
7104 * do that here. */
7105 in_free_unref_items = TRUE;
7106
7107 /*
7108 * PASS 1: free the contents of the items. We don't free the items
7109 * themselves yet, so that it is possible to decrement refcount counters
7110 */
7111
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007113 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007115 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007116 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007118 /* Free the Dictionary and ordinary items it contains, but don't
7119 * recurse into Lists and Dictionaries, they will be in the list
7120 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007121 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007122 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007123 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124
7125 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007126 * Go through the list of lists and free items without the copyID.
7127 * But don't free a list that has a watcher (used in a for loop), these
7128 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007129 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007130 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7131 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7132 && ll->lv_watch == NULL)
7133 {
7134 /* Free the List and ordinary items it contains, but don't recurse
7135 * into Lists and Dictionaries, they will be in the list of dicts
7136 * or list of lists. */
7137 list_free_contents(ll);
7138 did_free = TRUE;
7139 }
7140
7141#ifdef FEAT_JOB_CHANNEL
7142 /* Go through the list of jobs and free items without the copyID. This
7143 * must happen before doing channels, because jobs refer to channels, but
7144 * the reference from the channel to the job isn't tracked. */
7145 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7146
7147 /* Go through the list of channels and free items without the copyID. */
7148 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7149#endif
7150
7151 /*
7152 * PASS 2: free the items themselves.
7153 */
7154 for (dd = first_dict; dd != NULL; dd = dd_next)
7155 {
7156 dd_next = dd->dv_used_next;
7157 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7158 dict_free_dict(dd);
7159 }
7160
7161 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007162 {
7163 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007164 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7165 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007166 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007167 /* Free the List and ordinary items it contains, but don't recurse
7168 * into Lists and Dictionaries, they will be in the list of dicts
7169 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007170 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007171 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007172 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007173
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007174#ifdef FEAT_JOB_CHANNEL
7175 /* Go through the list of jobs and free items without the copyID. This
7176 * must happen before doing channels, because jobs refer to channels, but
7177 * the reference from the channel to the job isn't tracked. */
7178 free_unused_jobs(copyID, COPYID_MASK);
7179
7180 /* Go through the list of channels and free items without the copyID. */
7181 free_unused_channels(copyID, COPYID_MASK);
7182#endif
7183
7184 in_free_unref_items = FALSE;
7185
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007186 return did_free;
7187}
7188
7189/*
7190 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007191 * "list_stack" is used to add lists to be marked. Can be NULL.
7192 *
7193 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007194 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007195 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007196set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007197{
7198 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007199 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007201 hashtab_T *cur_ht;
7202 ht_stack_T *ht_stack = NULL;
7203 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007204
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007205 cur_ht = ht;
7206 for (;;)
7207 {
7208 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007209 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007210 /* Mark each item in the hashtab. If the item contains a hashtab
7211 * it is added to ht_stack, if it contains a list it is added to
7212 * list_stack. */
7213 todo = (int)cur_ht->ht_used;
7214 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7215 if (!HASHITEM_EMPTY(hi))
7216 {
7217 --todo;
7218 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7219 &ht_stack, list_stack);
7220 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007221 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007222
7223 if (ht_stack == NULL)
7224 break;
7225
7226 /* take an item from the stack */
7227 cur_ht = ht_stack->ht;
7228 tempitem = ht_stack;
7229 ht_stack = ht_stack->prev;
7230 free(tempitem);
7231 }
7232
7233 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007234}
7235
7236/*
7237 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007238 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7239 *
7240 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007241 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007243set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007244{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007245 listitem_T *li;
7246 int abort = FALSE;
7247 list_T *cur_l;
7248 list_stack_T *list_stack = NULL;
7249 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007250
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007251 cur_l = l;
7252 for (;;)
7253 {
7254 if (!abort)
7255 /* Mark each item in the list. If the item contains a hashtab
7256 * it is added to ht_stack, if it contains a list it is added to
7257 * list_stack. */
7258 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7259 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7260 ht_stack, &list_stack);
7261 if (list_stack == NULL)
7262 break;
7263
7264 /* take an item from the stack */
7265 cur_l = list_stack->list;
7266 tempitem = list_stack;
7267 list_stack = list_stack->prev;
7268 free(tempitem);
7269 }
7270
7271 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007272}
7273
7274/*
7275 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007276 * "list_stack" is used to add lists to be marked. Can be NULL.
7277 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7278 *
7279 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007280 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007281 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007282set_ref_in_item(
7283 typval_T *tv,
7284 int copyID,
7285 ht_stack_T **ht_stack,
7286 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007287{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007288 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007289
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007290 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007291 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007292 dict_T *dd = tv->vval.v_dict;
7293
Bram Moolenaara03f2332016-02-06 18:09:59 +01007294 if (dd != NULL && dd->dv_copyID != copyID)
7295 {
7296 /* Didn't see this dict yet. */
7297 dd->dv_copyID = copyID;
7298 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007299 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007300 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7301 }
7302 else
7303 {
7304 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7305 if (newitem == NULL)
7306 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007307 else
7308 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007309 newitem->ht = &dd->dv_hashtab;
7310 newitem->prev = *ht_stack;
7311 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007312 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007313 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007314 }
7315 }
7316 else if (tv->v_type == VAR_LIST)
7317 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007318 list_T *ll = tv->vval.v_list;
7319
Bram Moolenaara03f2332016-02-06 18:09:59 +01007320 if (ll != NULL && ll->lv_copyID != copyID)
7321 {
7322 /* Didn't see this list yet. */
7323 ll->lv_copyID = copyID;
7324 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007325 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007326 abort = set_ref_in_list(ll, copyID, ht_stack);
7327 }
7328 else
7329 {
7330 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007331 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007332 if (newitem == NULL)
7333 abort = TRUE;
7334 else
7335 {
7336 newitem->list = ll;
7337 newitem->prev = *list_stack;
7338 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007339 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007340 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007341 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007342 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007343 else if (tv->v_type == VAR_PARTIAL)
7344 {
7345 partial_T *pt = tv->vval.v_partial;
7346 int i;
7347
7348 /* A partial does not have a copyID, because it cannot contain itself.
7349 */
7350 if (pt != NULL)
7351 {
7352 if (pt->pt_dict != NULL)
7353 {
7354 typval_T dtv;
7355
7356 dtv.v_type = VAR_DICT;
7357 dtv.vval.v_dict = pt->pt_dict;
7358 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7359 }
7360
7361 for (i = 0; i < pt->pt_argc; ++i)
7362 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7363 ht_stack, list_stack);
7364 }
7365 }
7366#ifdef FEAT_JOB_CHANNEL
7367 else if (tv->v_type == VAR_JOB)
7368 {
7369 job_T *job = tv->vval.v_job;
7370 typval_T dtv;
7371
7372 if (job != NULL && job->jv_copyID != copyID)
7373 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007374 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007375 if (job->jv_channel != NULL)
7376 {
7377 dtv.v_type = VAR_CHANNEL;
7378 dtv.vval.v_channel = job->jv_channel;
7379 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7380 }
7381 if (job->jv_exit_partial != NULL)
7382 {
7383 dtv.v_type = VAR_PARTIAL;
7384 dtv.vval.v_partial = job->jv_exit_partial;
7385 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7386 }
7387 }
7388 }
7389 else if (tv->v_type == VAR_CHANNEL)
7390 {
7391 channel_T *ch =tv->vval.v_channel;
7392 int part;
7393 typval_T dtv;
7394 jsonq_T *jq;
7395 cbq_T *cq;
7396
7397 if (ch != NULL && ch->ch_copyID != copyID)
7398 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007399 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007400 for (part = PART_SOCK; part <= PART_IN; ++part)
7401 {
7402 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7403 jq = jq->jq_next)
7404 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7405 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7406 cq = cq->cq_next)
7407 if (cq->cq_partial != NULL)
7408 {
7409 dtv.v_type = VAR_PARTIAL;
7410 dtv.vval.v_partial = cq->cq_partial;
7411 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7412 }
7413 if (ch->ch_part[part].ch_partial != NULL)
7414 {
7415 dtv.v_type = VAR_PARTIAL;
7416 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7417 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7418 }
7419 }
7420 if (ch->ch_partial != NULL)
7421 {
7422 dtv.v_type = VAR_PARTIAL;
7423 dtv.vval.v_partial = ch->ch_partial;
7424 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7425 }
7426 if (ch->ch_close_partial != NULL)
7427 {
7428 dtv.v_type = VAR_PARTIAL;
7429 dtv.vval.v_partial = ch->ch_close_partial;
7430 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7431 }
7432 }
7433 }
7434#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007435 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007436}
7437
7438/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439 * Allocate an empty header for a dictionary.
7440 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007441 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007442dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443{
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007445
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007447 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007448 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007449 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007450 if (first_dict != NULL)
7451 first_dict->dv_used_prev = d;
7452 d->dv_used_next = first_dict;
7453 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007454 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007455
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007457 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007458 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007459 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007460 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007461 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007462 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463}
7464
7465/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007466 * Allocate an empty dict for a return value.
7467 * Returns OK or FAIL.
7468 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007470rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007471{
7472 dict_T *d = dict_alloc();
7473
7474 if (d == NULL)
7475 return FAIL;
7476
7477 rettv->vval.v_dict = d;
7478 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007479 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007480 ++d->dv_refcount;
7481 return OK;
7482}
7483
7484
7485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 * Unreference a Dictionary: decrement the reference count and free it when it
7487 * becomes zero.
7488 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007489 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007490dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007492 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007493 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494}
7495
7496/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007497 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 * Ignores the reference count.
7499 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007500 static void
7501dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007505 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007507 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007508 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007509 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512 if (!HASHITEM_EMPTY(hi))
7513 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007514 /* Remove the item before deleting it, just in case there is
7515 * something recursive causing trouble. */
7516 di = HI2DI(hi);
7517 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007518 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007519 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520 --todo;
7521 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007524}
7525
7526 static void
7527dict_free_dict(dict_T *d)
7528{
7529 /* Remove the dict from the list of dicts for garbage collection. */
7530 if (d->dv_used_prev == NULL)
7531 first_dict = d->dv_used_next;
7532 else
7533 d->dv_used_prev->dv_used_next = d->dv_used_next;
7534 if (d->dv_used_next != NULL)
7535 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536 vim_free(d);
7537}
7538
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007539 void
7540dict_free(dict_T *d)
7541{
7542 if (!in_free_unref_items)
7543 {
7544 dict_free_contents(d);
7545 dict_free_dict(d);
7546 }
7547}
7548
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549/*
7550 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 * The "key" is copied to the new item.
7552 * Note that the value of the item "di_tv" still needs to be initialized!
7553 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007555 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557{
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007560 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007562 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007564 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007565 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567}
7568
7569/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007570 * Make a copy of a Dictionary item.
7571 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007573dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574{
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007576
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007577 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7578 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007579 if (di != NULL)
7580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007581 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007582 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 copy_tv(&org->di_tv, &di->di_tv);
7584 }
7585 return di;
7586}
7587
7588/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007589 * Remove item "item" from Dictionary "dict" and free it.
7590 */
7591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007592dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007593{
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007595
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007597 if (HASHITEM_EMPTY(hi))
7598 EMSG2(_(e_intern2), "dictitem_remove()");
7599 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007600 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007601 dictitem_free(item);
7602}
7603
7604/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 * Free a dict item. Also clears the value.
7606 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007608dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007611 if (item->di_flags & DI_FLAGS_ALLOC)
7612 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613}
7614
7615/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007616 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7617 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007618 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 * Returns NULL when out of memory.
7620 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007622dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007623{
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 dict_T *copy;
7625 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007626 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007628
7629 if (orig == NULL)
7630 return NULL;
7631
7632 copy = dict_alloc();
7633 if (copy != NULL)
7634 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007635 if (copyID != 0)
7636 {
7637 orig->dv_copyID = copyID;
7638 orig->dv_copydict = copy;
7639 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007640 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007641 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007643 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007644 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007645 --todo;
7646
7647 di = dictitem_alloc(hi->hi_key);
7648 if (di == NULL)
7649 break;
7650 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007651 {
7652 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7653 copyID) == FAIL)
7654 {
7655 vim_free(di);
7656 break;
7657 }
7658 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 else
7660 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7661 if (dict_add(copy, di) == FAIL)
7662 {
7663 dictitem_free(di);
7664 break;
7665 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007668
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007670 if (todo > 0)
7671 {
7672 dict_unref(copy);
7673 copy = NULL;
7674 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007675 }
7676
7677 return copy;
7678}
7679
7680/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007682 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007685dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686{
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688}
7689
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007691 * Add a number or string entry to dictionary "d".
7692 * When "str" is NULL use number "nr", otherwise use "str".
7693 * Returns FAIL when out of memory and when key already exists.
7694 */
7695 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007696dict_add_nr_str(
7697 dict_T *d,
7698 char *key,
7699 long nr,
7700 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007701{
7702 dictitem_T *item;
7703
7704 item = dictitem_alloc((char_u *)key);
7705 if (item == NULL)
7706 return FAIL;
7707 item->di_tv.v_lock = 0;
7708 if (str == NULL)
7709 {
7710 item->di_tv.v_type = VAR_NUMBER;
7711 item->di_tv.vval.v_number = nr;
7712 }
7713 else
7714 {
7715 item->di_tv.v_type = VAR_STRING;
7716 item->di_tv.vval.v_string = vim_strsave(str);
7717 }
7718 if (dict_add(d, item) == FAIL)
7719 {
7720 dictitem_free(item);
7721 return FAIL;
7722 }
7723 return OK;
7724}
7725
7726/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007727 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007728 * Returns FAIL when out of memory and when key already exists.
7729 */
7730 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007731dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007732{
7733 dictitem_T *item;
7734
7735 item = dictitem_alloc((char_u *)key);
7736 if (item == NULL)
7737 return FAIL;
7738 item->di_tv.v_lock = 0;
7739 item->di_tv.v_type = VAR_LIST;
7740 item->di_tv.vval.v_list = list;
7741 if (dict_add(d, item) == FAIL)
7742 {
7743 dictitem_free(item);
7744 return FAIL;
7745 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007746 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007747 return OK;
7748}
7749
7750/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007751 * Get the number of items in a Dictionary.
7752 */
7753 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007754dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007755{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007756 if (d == NULL)
7757 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007758 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007759}
7760
7761/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 * Find item "key[len]" in Dictionary "d".
7763 * If "len" is negative use strlen(key).
7764 * Returns NULL when not found.
7765 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007766 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007767dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007768{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007769#define AKEYLEN 200
7770 char_u buf[AKEYLEN];
7771 char_u *akey;
7772 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007775 if (d == NULL)
7776 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007777 if (len < 0)
7778 akey = key;
7779 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007780 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007781 tofree = akey = vim_strnsave(key, len);
7782 if (akey == NULL)
7783 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007784 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007785 else
7786 {
7787 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007788 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007789 akey = buf;
7790 }
7791
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007793 vim_free(tofree);
7794 if (HASHITEM_EMPTY(hi))
7795 return NULL;
7796 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007797}
7798
7799/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007800 * Get a string item from a dictionary.
7801 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007802 * Returns NULL if the entry doesn't exist or out of memory.
7803 */
7804 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007805get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007806{
7807 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007808 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007809
7810 di = dict_find(d, key, -1);
7811 if (di == NULL)
7812 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007813 s = get_tv_string(&di->di_tv);
7814 if (save && s != NULL)
7815 s = vim_strsave(s);
7816 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007817}
7818
7819/*
7820 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007821 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007822 */
7823 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007824get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007825{
7826 dictitem_T *di;
7827
7828 di = dict_find(d, key, -1);
7829 if (di == NULL)
7830 return 0;
7831 return get_tv_number(&di->di_tv);
7832}
7833
7834/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007835 * Return an allocated string with the string representation of a Dictionary.
7836 * May return NULL.
7837 */
7838 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007839dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007840{
7841 garray_T ga;
7842 int first = TRUE;
7843 char_u *tofree;
7844 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007848 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007849
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007850 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007851 return NULL;
7852 ga_init2(&ga, (int)sizeof(char), 80);
7853 ga_append(&ga, '{');
7854
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007855 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007856 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007858 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007859 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007860 --todo;
7861
7862 if (first)
7863 first = FALSE;
7864 else
7865 ga_concat(&ga, (char_u *)", ");
7866
7867 tofree = string_quote(hi->hi_key, FALSE);
7868 if (tofree != NULL)
7869 {
7870 ga_concat(&ga, tofree);
7871 vim_free(tofree);
7872 }
7873 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007874 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7875 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007876 if (s != NULL)
7877 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007878 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007879 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007880 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007881 line_breakcheck();
7882
Bram Moolenaar8c711452005-01-14 21:53:12 +00007883 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007884 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007885 if (todo > 0)
7886 {
7887 vim_free(ga.ga_data);
7888 return NULL;
7889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007890
7891 ga_append(&ga, '}');
7892 ga_append(&ga, NUL);
7893 return (char_u *)ga.ga_data;
7894}
7895
7896/*
7897 * Allocate a variable for a Dictionary and fill it from "*arg".
7898 * Return OK or FAIL. Returns NOTDONE for {expr}.
7899 */
7900 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007901get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007902{
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 dict_T *d = NULL;
7904 typval_T tvkey;
7905 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007906 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007907 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007908 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007909 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007910
7911 /*
7912 * First check if it's not a curly-braces thing: {expr}.
7913 * Must do this without evaluating, otherwise a function may be called
7914 * twice. Unfortunately this means we need to call eval1() twice for the
7915 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007917 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007918 if (*start != '}')
7919 {
7920 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7921 return FAIL;
7922 if (*start == '}')
7923 return NOTDONE;
7924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007925
7926 if (evaluate)
7927 {
7928 d = dict_alloc();
7929 if (d == NULL)
7930 return FAIL;
7931 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007932 tvkey.v_type = VAR_UNKNOWN;
7933 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934
7935 *arg = skipwhite(*arg + 1);
7936 while (**arg != '}' && **arg != NUL)
7937 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007938 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007939 goto failret;
7940 if (**arg != ':')
7941 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007942 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007943 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 goto failret;
7945 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007946 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007948 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007949 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007950 {
7951 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007952 clear_tv(&tvkey);
7953 goto failret;
7954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956
7957 *arg = skipwhite(*arg + 1);
7958 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7959 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007960 if (evaluate)
7961 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007962 goto failret;
7963 }
7964 if (evaluate)
7965 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007966 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 if (item != NULL)
7968 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007969 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007970 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 clear_tv(&tv);
7972 goto failret;
7973 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007974 item = dictitem_alloc(key);
7975 clear_tv(&tvkey);
7976 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007979 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007980 if (dict_add(d, item) == FAIL)
7981 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 }
7983 }
7984
7985 if (**arg == '}')
7986 break;
7987 if (**arg != ',')
7988 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007989 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007990 goto failret;
7991 }
7992 *arg = skipwhite(*arg + 1);
7993 }
7994
7995 if (**arg != '}')
7996 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007997 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007998failret:
7999 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008000 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008001 return FAIL;
8002 }
8003
8004 *arg = skipwhite(*arg + 1);
8005 if (evaluate)
8006 {
8007 rettv->v_type = VAR_DICT;
8008 rettv->vval.v_dict = d;
8009 ++d->dv_refcount;
8010 }
8011
8012 return OK;
8013}
8014
Bram Moolenaar17a13432016-01-24 14:22:10 +01008015 static char *
8016get_var_special_name(int nr)
8017{
8018 switch (nr)
8019 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008020 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008021 case VVAL_TRUE: return "v:true";
8022 case VVAL_NONE: return "v:none";
8023 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008024 }
8025 EMSG2(_(e_intern2), "get_var_special_name()");
8026 return "42";
8027}
8028
Bram Moolenaar8c711452005-01-14 21:53:12 +00008029/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008030 * Return a string with the string representation of a variable.
8031 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008032 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008033 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008034 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8035 * "string()", otherwise does not put quotes around strings, as ":echo"
8036 * displays values.
8037 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8038 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008039 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008040 */
8041 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008042echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008043 typval_T *tv,
8044 char_u **tofree,
8045 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008046 int copyID,
8047 int echo_style,
8048 int restore_copyID,
8049 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008050{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008051 static int recurse = 0;
8052 char_u *r = NULL;
8053
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008055 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008056 if (!did_echo_string_emsg)
8057 {
8058 /* Only give this message once for a recursive call to avoid
8059 * flooding the user with errors. And stop iterating over lists
8060 * and dicts. */
8061 did_echo_string_emsg = TRUE;
8062 EMSG(_("E724: variable nested too deep for displaying"));
8063 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008064 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008065 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008066 }
8067 ++recurse;
8068
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008069 switch (tv->v_type)
8070 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008071 case VAR_STRING:
8072 if (echo_style && !dict_val)
8073 {
8074 *tofree = NULL;
8075 r = get_tv_string_buf(tv, numbuf);
8076 }
8077 else
8078 {
8079 *tofree = string_quote(tv->vval.v_string, FALSE);
8080 r = *tofree;
8081 }
8082 break;
8083
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008084 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008085 if (echo_style)
8086 {
8087 *tofree = NULL;
8088 r = tv->vval.v_string;
8089 }
8090 else
8091 {
8092 *tofree = string_quote(tv->vval.v_string, TRUE);
8093 r = *tofree;
8094 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008095 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008096
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008097 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008098 {
8099 partial_T *pt = tv->vval.v_partial;
8100 char_u *fname = string_quote(pt == NULL ? NULL
8101 : pt->pt_name, FALSE);
8102 garray_T ga;
8103 int i;
8104 char_u *tf;
8105
8106 ga_init2(&ga, 1, 100);
8107 ga_concat(&ga, (char_u *)"function(");
8108 if (fname != NULL)
8109 {
8110 ga_concat(&ga, fname);
8111 vim_free(fname);
8112 }
8113 if (pt != NULL && pt->pt_argc > 0)
8114 {
8115 ga_concat(&ga, (char_u *)", [");
8116 for (i = 0; i < pt->pt_argc; ++i)
8117 {
8118 if (i > 0)
8119 ga_concat(&ga, (char_u *)", ");
8120 ga_concat(&ga,
8121 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8122 vim_free(tf);
8123 }
8124 ga_concat(&ga, (char_u *)"]");
8125 }
8126 if (pt != NULL && pt->pt_dict != NULL)
8127 {
8128 typval_T dtv;
8129
8130 ga_concat(&ga, (char_u *)", ");
8131 dtv.v_type = VAR_DICT;
8132 dtv.vval.v_dict = pt->pt_dict;
8133 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8134 vim_free(tf);
8135 }
8136 ga_concat(&ga, (char_u *)")");
8137
8138 *tofree = ga.ga_data;
8139 r = *tofree;
8140 break;
8141 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008142
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008143 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008144 if (tv->vval.v_list == NULL)
8145 {
8146 *tofree = NULL;
8147 r = NULL;
8148 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008149 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8150 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008151 {
8152 *tofree = NULL;
8153 r = (char_u *)"[...]";
8154 }
8155 else
8156 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008157 int old_copyID = tv->vval.v_list->lv_copyID;
8158
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008159 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008160 *tofree = list2string(tv, copyID, restore_copyID);
8161 if (restore_copyID)
8162 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008163 r = *tofree;
8164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008165 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008166
Bram Moolenaar8c711452005-01-14 21:53:12 +00008167 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008168 if (tv->vval.v_dict == NULL)
8169 {
8170 *tofree = NULL;
8171 r = NULL;
8172 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008173 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8174 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008175 {
8176 *tofree = NULL;
8177 r = (char_u *)"{...}";
8178 }
8179 else
8180 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008181 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008182 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008183 *tofree = dict2string(tv, copyID, restore_copyID);
8184 if (restore_copyID)
8185 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008186 r = *tofree;
8187 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008188 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008189
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008190 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008191 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008192 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008193 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 *tofree = NULL;
8195 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008196 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008197
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008198 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008199#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008200 *tofree = NULL;
8201 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8202 r = numbuf;
8203 break;
8204#endif
8205
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008206 case VAR_SPECIAL:
8207 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008208 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008209 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008210 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008211
Bram Moolenaar8502c702014-06-17 12:51:16 +02008212 if (--recurse == 0)
8213 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008214 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008215}
8216
8217/*
8218 * Return a string with the string representation of a variable.
8219 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8220 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008221 * Does not put quotes around strings, as ":echo" displays values.
8222 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8223 * May return NULL.
8224 */
8225 static char_u *
8226echo_string(
8227 typval_T *tv,
8228 char_u **tofree,
8229 char_u *numbuf,
8230 int copyID)
8231{
8232 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8233}
8234
8235/*
8236 * Return a string with the string representation of a variable.
8237 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8238 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008239 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008240 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008242 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008243tv2string(
8244 typval_T *tv,
8245 char_u **tofree,
8246 char_u *numbuf,
8247 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008248{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008249 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008250}
8251
8252/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 * Return string "str" in ' quotes, doubling ' characters.
8254 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008255 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008256 */
8257 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008258string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008259{
Bram Moolenaar33570922005-01-25 22:26:29 +00008260 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008261 char_u *p, *r, *s;
8262
Bram Moolenaar33570922005-01-25 22:26:29 +00008263 len = (function ? 13 : 3);
8264 if (str != NULL)
8265 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008266 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008267 for (p = str; *p != NUL; mb_ptr_adv(p))
8268 if (*p == '\'')
8269 ++len;
8270 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008271 s = r = alloc(len);
8272 if (r != NULL)
8273 {
8274 if (function)
8275 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008276 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008277 r += 10;
8278 }
8279 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008280 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008281 if (str != NULL)
8282 for (p = str; *p != NUL; )
8283 {
8284 if (*p == '\'')
8285 *r++ = '\'';
8286 MB_COPY_CHAR(p, r);
8287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008288 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008289 if (function)
8290 *r++ = ')';
8291 *r++ = NUL;
8292 }
8293 return s;
8294}
8295
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008296#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008297/*
8298 * Convert the string "text" to a floating point number.
8299 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8300 * this always uses a decimal point.
8301 * Returns the length of the text that was consumed.
8302 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008303 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008304string2float(
8305 char_u *text,
8306 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008307{
8308 char *s = (char *)text;
8309 float_T f;
8310
8311 f = strtod(s, &s);
8312 *value = f;
8313 return (int)((char_u *)s - text);
8314}
8315#endif
8316
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 * Get the value of an environment variable.
8319 * "arg" is pointing to the '$'. It is advanced to after the name.
8320 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008321 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 */
8323 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008324get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325{
8326 char_u *string = NULL;
8327 int len;
8328 int cc;
8329 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008330 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331
8332 ++*arg;
8333 name = *arg;
8334 len = get_env_len(arg);
8335 if (evaluate)
8336 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008337 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008338 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008339
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008340 cc = name[len];
8341 name[len] = NUL;
8342 /* first try vim_getenv(), fast for normal environment vars */
8343 string = vim_getenv(name, &mustfree);
8344 if (string != NULL && *string != NUL)
8345 {
8346 if (!mustfree)
8347 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008349 else
8350 {
8351 if (mustfree)
8352 vim_free(string);
8353
8354 /* next try expanding things like $VIM and ${HOME} */
8355 string = expand_env_save(name - 1);
8356 if (string != NULL && *string == '$')
8357 {
8358 vim_free(string);
8359 string = NULL;
8360 }
8361 }
8362 name[len] = cc;
8363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008364 rettv->v_type = VAR_STRING;
8365 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 }
8367
8368 return OK;
8369}
8370
8371/*
8372 * Array with names and number of arguments of all internal functions
8373 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8374 */
8375static struct fst
8376{
8377 char *f_name; /* function name */
8378 char f_min_argc; /* minimal number of arguments */
8379 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008380 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008381 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382} functions[] =
8383{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008384#ifdef FEAT_FLOAT
8385 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008386 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008387#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008388 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008389 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"append", 2, 2, f_append},
8391 {"argc", 0, 0, f_argc},
8392 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008393 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008394 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008395#ifdef FEAT_FLOAT
8396 {"asin", 1, 1, f_asin}, /* WJMc */
8397#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008398 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008399 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008400 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008401 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008402 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008403 {"assert_notequal", 2, 3, f_assert_notequal},
8404 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008405 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008406#ifdef FEAT_FLOAT
8407 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008408 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008411 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"bufexists", 1, 1, f_bufexists},
8413 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8414 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8415 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8416 {"buflisted", 1, 1, f_buflisted},
8417 {"bufloaded", 1, 1, f_bufloaded},
8418 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008419 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"bufwinnr", 1, 1, f_bufwinnr},
8421 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008422 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008423 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008424 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008425#ifdef FEAT_FLOAT
8426 {"ceil", 1, 1, f_ceil},
8427#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008428#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008429 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008430 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8431 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008432 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008433 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008434 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008435 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008436 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008437 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008438 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008439 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008440 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8441 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008442 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008443 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008444#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008445 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008446 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008448 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008450#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008451 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008452 {"complete_add", 1, 1, f_complete_add},
8453 {"complete_check", 0, 0, f_complete_check},
8454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008456 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008457#ifdef FEAT_FLOAT
8458 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008459 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008460#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008461 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008463 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008464 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008465 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008467 {"diff_filler", 1, 1, f_diff_filler},
8468 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008469 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008471 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 {"eventhandler", 0, 0, f_eventhandler},
8473 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008474 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008476#ifdef FEAT_FLOAT
8477 {"exp", 1, 1, f_exp},
8478#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008479 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008480 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008481 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8483 {"filereadable", 1, 1, f_filereadable},
8484 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008485 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008486 {"finddir", 1, 3, f_finddir},
8487 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008488#ifdef FEAT_FLOAT
8489 {"float2nr", 1, 1, f_float2nr},
8490 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008491 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008492#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008493 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {"fnamemodify", 2, 2, f_fnamemodify},
8495 {"foldclosed", 1, 1, f_foldclosed},
8496 {"foldclosedend", 1, 1, f_foldclosedend},
8497 {"foldlevel", 1, 1, f_foldlevel},
8498 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008499 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008501 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008502 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008503 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008504 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008505 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"getchar", 0, 1, f_getchar},
8507 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008508 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {"getcmdline", 0, 0, f_getcmdline},
8510 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008511 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008512 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008513 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008514 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008515 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008516 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {"getfsize", 1, 1, f_getfsize},
8518 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008519 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008520 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008521 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008522 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008523 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008524 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008525 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008526 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008528 {"gettabvar", 2, 3, f_gettabvar},
8529 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 {"getwinposx", 0, 0, f_getwinposx},
8531 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008532 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008533 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008534 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008535 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008537 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008538 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008539 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8541 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8542 {"histadd", 2, 2, f_histadd},
8543 {"histdel", 1, 2, f_histdel},
8544 {"histget", 1, 2, f_histget},
8545 {"histnr", 1, 1, f_histnr},
8546 {"hlID", 1, 1, f_hlID},
8547 {"hlexists", 1, 1, f_hlexists},
8548 {"hostname", 0, 0, f_hostname},
8549 {"iconv", 3, 3, f_iconv},
8550 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008551 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008552 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008554 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 {"inputrestore", 0, 0, f_inputrestore},
8556 {"inputsave", 0, 0, f_inputsave},
8557 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008558 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008559 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008561 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008562#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8563 {"isnan", 1, 1, f_isnan},
8564#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008565 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008566#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008567 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008568 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008569 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008570 {"job_start", 1, 2, f_job_start},
8571 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008572 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008573#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008574 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008575 {"js_decode", 1, 1, f_js_decode},
8576 {"js_encode", 1, 1, f_js_encode},
8577 {"json_decode", 1, 1, f_json_decode},
8578 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008579 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {"libcall", 3, 3, f_libcall},
8583 {"libcallnr", 3, 3, f_libcallnr},
8584 {"line", 1, 1, f_line},
8585 {"line2byte", 1, 1, f_line2byte},
8586 {"lispindent", 1, 1, f_lispindent},
8587 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008588#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008589 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008590 {"log10", 1, 1, f_log10},
8591#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008592#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008593 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008594#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008595 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008596 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008597 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008598 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008599 {"matchadd", 2, 5, f_matchadd},
8600 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008601 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008602 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008603 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008604 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008605 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008606 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008607 {"max", 1, 1, f_max},
8608 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008609#ifdef vim_mkdir
8610 {"mkdir", 1, 3, f_mkdir},
8611#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008612 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008613#ifdef FEAT_MZSCHEME
8614 {"mzeval", 1, 1, f_mzeval},
8615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008617 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008618 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008619 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008620#ifdef FEAT_PERL
8621 {"perleval", 1, 1, f_perleval},
8622#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008623#ifdef FEAT_FLOAT
8624 {"pow", 2, 2, f_pow},
8625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008627 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008628 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008629#ifdef FEAT_PYTHON3
8630 {"py3eval", 1, 1, f_py3eval},
8631#endif
8632#ifdef FEAT_PYTHON
8633 {"pyeval", 1, 1, f_pyeval},
8634#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008635 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008636 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008637 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008638#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008639 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008640#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008641 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 {"remote_expr", 2, 3, f_remote_expr},
8643 {"remote_foreground", 1, 1, f_remote_foreground},
8644 {"remote_peek", 1, 2, f_remote_peek},
8645 {"remote_read", 1, 1, f_remote_read},
8646 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008647 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008649 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008651 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008652#ifdef FEAT_FLOAT
8653 {"round", 1, 1, f_round},
8654#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008655 {"screenattr", 2, 2, f_screenattr},
8656 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008657 {"screencol", 0, 0, f_screencol},
8658 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008659 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008660 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008661 {"searchpair", 3, 7, f_searchpair},
8662 {"searchpairpos", 3, 7, f_searchpairpos},
8663 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 {"server2client", 2, 2, f_server2client},
8665 {"serverlist", 0, 0, f_serverlist},
8666 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008667 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008669 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008671 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008672 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008673 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008674 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008676 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008677 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008679#ifdef FEAT_CRYPT
8680 {"sha256", 1, 1, f_sha256},
8681#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008682 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008683 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008685#ifdef FEAT_FLOAT
8686 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008687 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008688#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008689 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008690 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008691 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008692 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008693 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008694#ifdef FEAT_FLOAT
8695 {"sqrt", 1, 1, f_sqrt},
8696 {"str2float", 1, 1, f_str2float},
8697#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008698 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008699 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008700 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008701 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702#ifdef HAVE_STRFTIME
8703 {"strftime", 1, 2, f_strftime},
8704#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008705 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {"strlen", 1, 1, f_strlen},
8709 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008710 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008712 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008713 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 {"substitute", 4, 4, f_substitute},
8715 {"synID", 3, 3, f_synID},
8716 {"synIDattr", 2, 3, f_synIDattr},
8717 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008718 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008719 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008720 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008721 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008722 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008723 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008724 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008725 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008726 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008727#ifdef FEAT_FLOAT
8728 {"tan", 1, 1, f_tan},
8729 {"tanh", 1, 1, f_tanh},
8730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008732 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8733 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008734 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8735#ifdef FEAT_JOB_CHANNEL
8736 {"test_null_channel", 0, 0, f_test_null_channel},
8737#endif
8738 {"test_null_dict", 0, 0, f_test_null_dict},
8739#ifdef FEAT_JOB_CHANNEL
8740 {"test_null_job", 0, 0, f_test_null_job},
8741#endif
8742 {"test_null_list", 0, 0, f_test_null_list},
8743 {"test_null_partial", 0, 0, f_test_null_partial},
8744 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008745#ifdef FEAT_TIMERS
8746 {"timer_start", 2, 3, f_timer_start},
8747 {"timer_stop", 1, 1, f_timer_stop},
8748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {"tolower", 1, 1, f_tolower},
8750 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008751 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008752#ifdef FEAT_FLOAT
8753 {"trunc", 1, 1, f_trunc},
8754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008756 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008757 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008758 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008759 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 {"virtcol", 1, 1, f_virtcol},
8761 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008762 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008763 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008764 {"win_getid", 0, 2, f_win_getid},
8765 {"win_gotoid", 1, 1, f_win_gotoid},
8766 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8767 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 {"winbufnr", 1, 1, f_winbufnr},
8769 {"wincol", 0, 0, f_wincol},
8770 {"winheight", 1, 1, f_winheight},
8771 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008772 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008774 {"winrestview", 1, 1, f_winrestview},
8775 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008777 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008778 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008779 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780};
8781
8782#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8783
8784/*
8785 * Function given to ExpandGeneric() to obtain the list of internal
8786 * or user defined function names.
8787 */
8788 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008789get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
8791 static int intidx = -1;
8792 char_u *name;
8793
8794 if (idx == 0)
8795 intidx = -1;
8796 if (intidx < 0)
8797 {
8798 name = get_user_func_name(xp, idx);
8799 if (name != NULL)
8800 return name;
8801 }
8802 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8803 {
8804 STRCPY(IObuff, functions[intidx].f_name);
8805 STRCAT(IObuff, "(");
8806 if (functions[intidx].f_max_argc == 0)
8807 STRCAT(IObuff, ")");
8808 return IObuff;
8809 }
8810
8811 return NULL;
8812}
8813
8814/*
8815 * Function given to ExpandGeneric() to obtain the list of internal or
8816 * user defined variable or function names.
8817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008819get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 static int intidx = -1;
8822 char_u *name;
8823
8824 if (idx == 0)
8825 intidx = -1;
8826 if (intidx < 0)
8827 {
8828 name = get_function_name(xp, idx);
8829 if (name != NULL)
8830 return name;
8831 }
8832 return get_user_var_name(xp, ++intidx);
8833}
8834
8835#endif /* FEAT_CMDL_COMPL */
8836
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008837#if defined(EBCDIC) || defined(PROTO)
8838/*
8839 * Compare struct fst by function name.
8840 */
8841 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008842compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008843{
8844 struct fst *p1 = (struct fst *)s1;
8845 struct fst *p2 = (struct fst *)s2;
8846
8847 return STRCMP(p1->f_name, p2->f_name);
8848}
8849
8850/*
8851 * Sort the function table by function name.
8852 * The sorting of the table above is ASCII dependant.
8853 * On machines using EBCDIC we have to sort it.
8854 */
8855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008856sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008857{
8858 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8859
8860 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8861}
8862#endif
8863
8864
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865/*
8866 * Find internal function in table above.
8867 * Return index, or -1 if not found
8868 */
8869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008870find_internal_func(
8871 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
8873 int first = 0;
8874 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8875 int cmp;
8876 int x;
8877
8878 /*
8879 * Find the function name in the table. Binary search.
8880 */
8881 while (first <= last)
8882 {
8883 x = first + ((unsigned)(last - first) >> 1);
8884 cmp = STRCMP(name, functions[x].f_name);
8885 if (cmp < 0)
8886 last = x - 1;
8887 else if (cmp > 0)
8888 first = x + 1;
8889 else
8890 return x;
8891 }
8892 return -1;
8893}
8894
8895/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008896 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8897 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008898 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8899 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008900 */
8901 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008902deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008903{
Bram Moolenaar33570922005-01-25 22:26:29 +00008904 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008905 int cc;
8906
Bram Moolenaar65639032016-03-16 21:40:30 +01008907 if (partialp != NULL)
8908 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008909
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008910 cc = name[*lenp];
8911 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008912 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008913 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008915 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008917 {
8918 *lenp = 0;
8919 return (char_u *)""; /* just in case */
8920 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008921 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008922 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008923 }
8924
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008925 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8926 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008927 partial_T *pt = v->di_tv.vval.v_partial;
8928
8929 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008930 {
8931 *lenp = 0;
8932 return (char_u *)""; /* just in case */
8933 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008934 if (partialp != NULL)
8935 *partialp = pt;
8936 *lenp = (int)STRLEN(pt->pt_name);
8937 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008938 }
8939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008940 return name;
8941}
8942
8943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 * Allocate a variable for the result of a function.
8945 * Return OK or FAIL.
8946 */
8947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008948get_func_tv(
8949 char_u *name, /* name of the function */
8950 int len, /* length of "name" */
8951 typval_T *rettv,
8952 char_u **arg, /* argument, pointing to the '(' */
8953 linenr_T firstline, /* first line of range */
8954 linenr_T lastline, /* last line of range */
8955 int *doesrange, /* return: function handled range */
8956 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008957 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008958 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959{
8960 char_u *argp;
8961 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008962 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 int argcount = 0; /* number of arguments found */
8964
8965 /*
8966 * Get the arguments.
8967 */
8968 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008969 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 {
8971 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8972 if (*argp == ')' || *argp == ',' || *argp == NUL)
8973 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8975 {
8976 ret = FAIL;
8977 break;
8978 }
8979 ++argcount;
8980 if (*argp != ',')
8981 break;
8982 }
8983 if (*argp == ')')
8984 ++argp;
8985 else
8986 ret = FAIL;
8987
8988 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008989 {
8990 int i = 0;
8991
8992 if (get_vim_var_nr(VV_TESTING))
8993 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008994 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008995 * what variables are used on the call stack. */
8996 if (funcargs.ga_itemsize == 0)
8997 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8998 for (i = 0; i < argcount; ++i)
8999 if (ga_grow(&funcargs, 1) == OK)
9000 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9001 &argvars[i];
9002 }
9003
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009005 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009006
9007 funcargs.ga_len -= i;
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 {
9011 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009012 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009014 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016
9017 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
9020 *arg = skipwhite(argp);
9021 return ret;
9022}
9023
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009024#define ERROR_UNKNOWN 0
9025#define ERROR_TOOMANY 1
9026#define ERROR_TOOFEW 2
9027#define ERROR_SCRIPT 3
9028#define ERROR_DICT 4
9029#define ERROR_NONE 5
9030#define ERROR_OTHER 6
9031#define FLEN_FIXED 40
9032
9033/*
9034 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9035 * Change <SNR>123_name() to K_SNR 123_name().
9036 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9037 * (slow).
9038 */
9039 static char_u *
9040fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9041{
9042 int llen;
9043 char_u *fname;
9044 int i;
9045
9046 llen = eval_fname_script(name);
9047 if (llen > 0)
9048 {
9049 fname_buf[0] = K_SPECIAL;
9050 fname_buf[1] = KS_EXTRA;
9051 fname_buf[2] = (int)KE_SNR;
9052 i = 3;
9053 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9054 {
9055 if (current_SID <= 0)
9056 *error = ERROR_SCRIPT;
9057 else
9058 {
9059 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9060 i = (int)STRLEN(fname_buf);
9061 }
9062 }
9063 if (i + STRLEN(name + llen) < FLEN_FIXED)
9064 {
9065 STRCPY(fname_buf + i, name + llen);
9066 fname = fname_buf;
9067 }
9068 else
9069 {
9070 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9071 if (fname == NULL)
9072 *error = ERROR_OTHER;
9073 else
9074 {
9075 *tofree = fname;
9076 mch_memmove(fname, fname_buf, (size_t)i);
9077 STRCPY(fname + i, name + llen);
9078 }
9079 }
9080 }
9081 else
9082 fname = name;
9083 return fname;
9084}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
9086/*
9087 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009088 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009089 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009092call_func(
9093 char_u *funcname, /* name of the function */
9094 int len, /* length of "name" */
9095 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009096 int argcount_in, /* number of "argvars" */
9097 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009098 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009099 linenr_T firstline, /* first line of range */
9100 linenr_T lastline, /* last line of range */
9101 int *doesrange, /* return: function handled range */
9102 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009103 partial_T *partial, /* optional, can be NULL */
9104 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105{
9106 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 int error = ERROR_NONE;
9108 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009111 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009113 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009114 int argcount = argcount_in;
9115 typval_T *argvars = argvars_in;
9116 dict_T *selfdict = selfdict_in;
9117 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9118 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009119
9120 /* Make a copy of the name, if it comes from a funcref variable it could
9121 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009122 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009123 if (name == NULL)
9124 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009126 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127
9128 *doesrange = FALSE;
9129
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009130 if (partial != NULL)
9131 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009132 /* When the function has a partial with a dict and there is a dict
9133 * argument, use the dict argument. That is backwards compatible.
9134 * When the dict was bound explicitly use the one from the partial. */
9135 if (partial->pt_dict != NULL
9136 && (selfdict_in == NULL || !partial->pt_auto))
9137 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009138 if (error == ERROR_NONE && partial->pt_argc > 0)
9139 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009140 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9141 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9142 for (i = 0; i < argcount_in; ++i)
9143 argv[i + argv_clear] = argvars_in[i];
9144 argvars = argv;
9145 argcount = partial->pt_argc + argcount_in;
9146 }
9147 }
9148
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149
9150 /* execute the function if no errors detected and executing */
9151 if (evaluate && error == ERROR_NONE)
9152 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009153 char_u *rfname = fname;
9154
9155 /* Ignore "g:" before a function name. */
9156 if (fname[0] == 'g' && fname[1] == ':')
9157 rfname = fname + 2;
9158
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009159 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9160 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 error = ERROR_UNKNOWN;
9162
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009163 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 {
9165 /*
9166 * User defined function.
9167 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009168 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009169
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009171 /* Trigger FuncUndefined event, may load the function. */
9172 if (fp == NULL
9173 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009174 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009175 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009177 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009178 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 }
9180#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009181 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009182 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009183 {
9184 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009185 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009186 }
9187
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 if (fp != NULL)
9189 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009190 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009192 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009194 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009196 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009197 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 else
9199 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009200 int did_save_redo = FALSE;
9201
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 /*
9203 * Call the user function.
9204 * Save and restore search patterns, script variables and
9205 * redo buffer.
9206 */
9207 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009208#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009209 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009210#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009211 {
9212 saveRedobuff();
9213 did_save_redo = TRUE;
9214 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009215 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009217 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009218 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9219 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9220 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009221 /* Function was unreferenced while being used, free it
9222 * now. */
9223 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009224 if (did_save_redo)
9225 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 restore_search_patterns();
9227 error = ERROR_NONE;
9228 }
9229 }
9230 }
9231 else
9232 {
9233 /*
9234 * Find the function name in the table, call its implementation.
9235 */
9236 i = find_internal_func(fname);
9237 if (i >= 0)
9238 {
9239 if (argcount < functions[i].f_min_argc)
9240 error = ERROR_TOOFEW;
9241 else if (argcount > functions[i].f_max_argc)
9242 error = ERROR_TOOMANY;
9243 else
9244 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009245 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009246 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 error = ERROR_NONE;
9248 }
9249 }
9250 }
9251 /*
9252 * The function call (or "FuncUndefined" autocommand sequence) might
9253 * have been aborted by an error, an interrupt, or an explicitly thrown
9254 * exception that has not been caught so far. This situation can be
9255 * tested for by calling aborting(). For an error in an internal
9256 * function or for the "E132" error in call_user_func(), however, the
9257 * throw point at which the "force_abort" flag (temporarily reset by
9258 * emsg()) is normally updated has not been reached yet. We need to
9259 * update that flag first to make aborting() reliable.
9260 */
9261 update_force_abort();
9262 }
9263 if (error == ERROR_NONE)
9264 ret = OK;
9265
9266 /*
9267 * Report an error unless the argument evaluation or function call has been
9268 * cancelled due to an aborting error, an interrupt, or an exception.
9269 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009270 if (!aborting())
9271 {
9272 switch (error)
9273 {
9274 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009275 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009276 break;
9277 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009278 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009279 break;
9280 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009281 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009282 name);
9283 break;
9284 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009285 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009286 name);
9287 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009289 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 name);
9291 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009292 }
9293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009295 while (argv_clear > 0)
9296 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009297 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009298 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299
9300 return ret;
9301}
9302
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009303/*
9304 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009305 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009306 */
9307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009308emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009309{
9310 char_u *p;
9311
9312 if (*name == K_SPECIAL)
9313 p = concat_str((char_u *)"<SNR>", name + 3);
9314 else
9315 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009316 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009317 if (p != name)
9318 vim_free(p);
9319}
9320
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009321/*
9322 * Return TRUE for a non-zero Number and a non-empty String.
9323 */
9324 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009325non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009326{
9327 return ((argvars[0].v_type == VAR_NUMBER
9328 && argvars[0].vval.v_number != 0)
9329 || (argvars[0].v_type == VAR_STRING
9330 && argvars[0].vval.v_string != NULL
9331 && *argvars[0].vval.v_string != NUL));
9332}
9333
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334/*********************************************
9335 * Implementation of the built-in functions
9336 */
9337
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009338#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009339static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009340
9341/*
9342 * Get the float value of "argvars[0]" into "f".
9343 * Returns FAIL when the argument is not a Number or Float.
9344 */
9345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009346get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009347{
9348 if (argvars[0].v_type == VAR_FLOAT)
9349 {
9350 *f = argvars[0].vval.v_float;
9351 return OK;
9352 }
9353 if (argvars[0].v_type == VAR_NUMBER)
9354 {
9355 *f = (float_T)argvars[0].vval.v_number;
9356 return OK;
9357 }
9358 EMSG(_("E808: Number or Float required"));
9359 return FAIL;
9360}
9361
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009362/*
9363 * "abs(expr)" function
9364 */
9365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009366f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009367{
9368 if (argvars[0].v_type == VAR_FLOAT)
9369 {
9370 rettv->v_type = VAR_FLOAT;
9371 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9372 }
9373 else
9374 {
9375 varnumber_T n;
9376 int error = FALSE;
9377
9378 n = get_tv_number_chk(&argvars[0], &error);
9379 if (error)
9380 rettv->vval.v_number = -1;
9381 else if (n > 0)
9382 rettv->vval.v_number = n;
9383 else
9384 rettv->vval.v_number = -n;
9385 }
9386}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009387
9388/*
9389 * "acos()" function
9390 */
9391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009393{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009394 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009395
9396 rettv->v_type = VAR_FLOAT;
9397 if (get_float_arg(argvars, &f) == OK)
9398 rettv->vval.v_float = acos(f);
9399 else
9400 rettv->vval.v_float = 0.0;
9401}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009402#endif
9403
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009405 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 */
9407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009408f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
Bram Moolenaar33570922005-01-25 22:26:29 +00009410 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009413 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009415 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009416 && !tv_check_lock(l->lv_lock,
9417 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009418 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009420 }
9421 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009422 EMSG(_(e_listreq));
9423}
9424
9425/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009426 * "and(expr, expr)" function
9427 */
9428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009429f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009430{
9431 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9432 & get_tv_number_chk(&argvars[1], NULL);
9433}
9434
9435/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009436 * "append(lnum, string/list)" function
9437 */
9438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009439f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009440{
9441 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009442 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009443 list_T *l = NULL;
9444 listitem_T *li = NULL;
9445 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009446 long added = 0;
9447
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009448 /* When coming here from Insert mode, sync undo, so that this can be
9449 * undone separately from what was previously inserted. */
9450 if (u_sync_once == 2)
9451 {
9452 u_sync_once = 1; /* notify that u_sync() was called */
9453 u_sync(TRUE);
9454 }
9455
Bram Moolenaar0d660222005-01-07 21:51:51 +00009456 lnum = get_tv_lnum(argvars);
9457 if (lnum >= 0
9458 && lnum <= curbuf->b_ml.ml_line_count
9459 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009460 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009461 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009462 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009463 l = argvars[1].vval.v_list;
9464 if (l == NULL)
9465 return;
9466 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009467 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009468 for (;;)
9469 {
9470 if (l == NULL)
9471 tv = &argvars[1]; /* append a string */
9472 else if (li == NULL)
9473 break; /* end of list */
9474 else
9475 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 line = get_tv_string_chk(tv);
9477 if (line == NULL) /* type error */
9478 {
9479 rettv->vval.v_number = 1; /* Failed */
9480 break;
9481 }
9482 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009483 ++added;
9484 if (l == NULL)
9485 break;
9486 li = li->li_next;
9487 }
9488
9489 appended_lines_mark(lnum, added);
9490 if (curwin->w_cursor.lnum > lnum)
9491 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009493 else
9494 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495}
9496
9497/*
9498 * "argc()" function
9499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504}
9505
9506/*
9507 * "argidx()" function
9508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009510f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513}
9514
9515/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009516 * "arglistid()" function
9517 */
9518 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009519f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009520{
9521 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009522
9523 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009524 wp = find_tabwin(&argvars[0], &argvars[1]);
9525 if (wp != NULL)
9526 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009527}
9528
9529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 * "argv(nr)" function
9531 */
9532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009533f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535 int idx;
9536
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009537 if (argvars[0].v_type != VAR_UNKNOWN)
9538 {
9539 idx = get_tv_number_chk(&argvars[0], NULL);
9540 if (idx >= 0 && idx < ARGCOUNT)
9541 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9542 else
9543 rettv->vval.v_string = NULL;
9544 rettv->v_type = VAR_STRING;
9545 }
9546 else if (rettv_list_alloc(rettv) == OK)
9547 for (idx = 0; idx < ARGCOUNT; ++idx)
9548 list_append_string(rettv->vval.v_list,
9549 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550}
9551
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009552typedef enum
9553{
9554 ASSERT_EQUAL,
9555 ASSERT_NOTEQUAL,
9556 ASSERT_MATCH,
9557 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009558 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009559} assert_type_T;
9560
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009561static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009562static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009563static void assert_error(garray_T *gap);
9564static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009565
9566/*
9567 * Prepare "gap" for an assert error and add the sourcing position.
9568 */
9569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009570prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009571{
9572 char buf[NUMBUFLEN];
9573
9574 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009575 if (sourcing_name != NULL)
9576 {
9577 ga_concat(gap, sourcing_name);
9578 if (sourcing_lnum > 0)
9579 ga_concat(gap, (char_u *)" ");
9580 }
9581 if (sourcing_lnum > 0)
9582 {
9583 sprintf(buf, "line %ld", (long)sourcing_lnum);
9584 ga_concat(gap, (char_u *)buf);
9585 }
9586 if (sourcing_name != NULL || sourcing_lnum > 0)
9587 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009588}
9589
9590/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009591 * Append "str" to "gap", escaping unprintable characters.
9592 * Changes NL to \n, CR to \r, etc.
9593 */
9594 static void
9595ga_concat_esc(garray_T *gap, char_u *str)
9596{
9597 char_u *p;
9598 char_u buf[NUMBUFLEN];
9599
Bram Moolenaarf1551962016-03-15 12:55:58 +01009600 if (str == NULL)
9601 {
9602 ga_concat(gap, (char_u *)"NULL");
9603 return;
9604 }
9605
Bram Moolenaar23689172016-02-15 22:37:37 +01009606 for (p = str; *p != NUL; ++p)
9607 switch (*p)
9608 {
9609 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9610 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9611 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9612 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9613 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9614 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9615 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9616 default:
9617 if (*p < ' ')
9618 {
9619 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9620 ga_concat(gap, buf);
9621 }
9622 else
9623 ga_append(gap, *p);
9624 break;
9625 }
9626}
9627
9628/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009629 * Fill "gap" with information about an assert error.
9630 */
9631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632fill_assert_error(
9633 garray_T *gap,
9634 typval_T *opt_msg_tv,
9635 char_u *exp_str,
9636 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009637 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009638 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009639{
9640 char_u numbuf[NUMBUFLEN];
9641 char_u *tofree;
9642
9643 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9644 {
9645 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9646 vim_free(tofree);
9647 }
9648 else
9649 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009650 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009651 ga_concat(gap, (char_u *)"Pattern ");
9652 else
9653 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009654 if (exp_str == NULL)
9655 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009656 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009657 vim_free(tofree);
9658 }
9659 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009660 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009661 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009662 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009663 else if (atype == ASSERT_NOTMATCH)
9664 ga_concat(gap, (char_u *)" does match ");
9665 else if (atype == ASSERT_NOTEQUAL)
9666 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009667 else
9668 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009669 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009670 vim_free(tofree);
9671 }
9672}
Bram Moolenaar43345542015-11-29 17:35:35 +01009673
9674/*
9675 * Add an assert error to v:errors.
9676 */
9677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009678assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009679{
9680 struct vimvar *vp = &vimvars[VV_ERRORS];
9681
9682 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9683 /* Make sure v:errors is a list. */
9684 set_vim_var_list(VV_ERRORS, list_alloc());
9685 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9686}
9687
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009688 static void
9689assert_equal_common(typval_T *argvars, assert_type_T atype)
9690{
9691 garray_T ga;
9692
9693 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9694 != (atype == ASSERT_EQUAL))
9695 {
9696 prepare_assert_error(&ga);
9697 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9698 atype);
9699 assert_error(&ga);
9700 ga_clear(&ga);
9701 }
9702}
9703
Bram Moolenaar43345542015-11-29 17:35:35 +01009704/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009705 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009706 */
9707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009708f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009709{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009710 assert_equal_common(argvars, ASSERT_EQUAL);
9711}
Bram Moolenaar43345542015-11-29 17:35:35 +01009712
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009713/*
9714 * "assert_notequal(expected, actual[, msg])" function
9715 */
9716 static void
9717f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9718{
9719 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009720}
9721
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009722/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009723 * "assert_exception(string[, msg])" function
9724 */
9725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009726f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009727{
9728 garray_T ga;
9729 char *error;
9730
9731 error = (char *)get_tv_string_chk(&argvars[0]);
9732 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9733 {
9734 prepare_assert_error(&ga);
9735 ga_concat(&ga, (char_u *)"v:exception is not set");
9736 assert_error(&ga);
9737 ga_clear(&ga);
9738 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009739 else if (error != NULL
9740 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009741 {
9742 prepare_assert_error(&ga);
9743 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009744 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009745 assert_error(&ga);
9746 ga_clear(&ga);
9747 }
9748}
9749
9750/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009751 * "assert_fails(cmd [, error])" function
9752 */
9753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009754f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009755{
9756 char_u *cmd = get_tv_string_chk(&argvars[0]);
9757 garray_T ga;
9758
9759 called_emsg = FALSE;
9760 suppress_errthrow = TRUE;
9761 emsg_silent = TRUE;
9762 do_cmdline_cmd(cmd);
9763 if (!called_emsg)
9764 {
9765 prepare_assert_error(&ga);
9766 ga_concat(&ga, (char_u *)"command did not fail: ");
9767 ga_concat(&ga, cmd);
9768 assert_error(&ga);
9769 ga_clear(&ga);
9770 }
9771 else if (argvars[1].v_type != VAR_UNKNOWN)
9772 {
9773 char_u buf[NUMBUFLEN];
9774 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9775
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009776 if (error == NULL
9777 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009778 {
9779 prepare_assert_error(&ga);
9780 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009781 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009782 assert_error(&ga);
9783 ga_clear(&ga);
9784 }
9785 }
9786
9787 called_emsg = FALSE;
9788 suppress_errthrow = FALSE;
9789 emsg_silent = FALSE;
9790 emsg_on_display = FALSE;
9791 set_vim_var_string(VV_ERRMSG, NULL, 0);
9792}
9793
9794/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009795 * Common for assert_true() and assert_false().
9796 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009798assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009799{
9800 int error = FALSE;
9801 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009802
Bram Moolenaar37127922016-02-06 20:29:28 +01009803 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009804 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009805 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009806 if (argvars[0].v_type != VAR_NUMBER
9807 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9808 || error)
9809 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009810 prepare_assert_error(&ga);
9811 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009812 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009813 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009814 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009815 ga_clear(&ga);
9816 }
9817}
9818
9819/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009820 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009821 */
9822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009823f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009824{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009825 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009826}
9827
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009828 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009829assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009830{
9831 garray_T ga;
9832 char_u buf1[NUMBUFLEN];
9833 char_u buf2[NUMBUFLEN];
9834 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9835 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9836
Bram Moolenaar72188e92016-03-28 22:48:29 +02009837 if (pat == NULL || text == NULL)
9838 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009839 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009840 {
9841 prepare_assert_error(&ga);
9842 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009843 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009844 assert_error(&ga);
9845 ga_clear(&ga);
9846 }
9847}
9848
9849/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009850 * "assert_match(pattern, actual[, msg])" function
9851 */
9852 static void
9853f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9854{
9855 assert_match_common(argvars, ASSERT_MATCH);
9856}
9857
9858/*
9859 * "assert_notmatch(pattern, actual[, msg])" function
9860 */
9861 static void
9862f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9863{
9864 assert_match_common(argvars, ASSERT_NOTMATCH);
9865}
9866
9867/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009868 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009869 */
9870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009871f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009872{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009873 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009874}
9875
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009876#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009877/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009878 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009879 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009881f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009882{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009883 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009884
9885 rettv->v_type = VAR_FLOAT;
9886 if (get_float_arg(argvars, &f) == OK)
9887 rettv->vval.v_float = asin(f);
9888 else
9889 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009890}
9891
9892/*
9893 * "atan()" function
9894 */
9895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009896f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009897{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009898 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009899
9900 rettv->v_type = VAR_FLOAT;
9901 if (get_float_arg(argvars, &f) == OK)
9902 rettv->vval.v_float = atan(f);
9903 else
9904 rettv->vval.v_float = 0.0;
9905}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009906
9907/*
9908 * "atan2()" function
9909 */
9910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009911f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009912{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009913 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009914
9915 rettv->v_type = VAR_FLOAT;
9916 if (get_float_arg(argvars, &fx) == OK
9917 && get_float_arg(&argvars[1], &fy) == OK)
9918 rettv->vval.v_float = atan2(fx, fy);
9919 else
9920 rettv->vval.v_float = 0.0;
9921}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009922#endif
9923
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924/*
9925 * "browse(save, title, initdir, default)" function
9926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009928f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929{
9930#ifdef FEAT_BROWSE
9931 int save;
9932 char_u *title;
9933 char_u *initdir;
9934 char_u *defname;
9935 char_u buf[NUMBUFLEN];
9936 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009937 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009939 save = get_tv_number_chk(&argvars[0], &error);
9940 title = get_tv_string_chk(&argvars[1]);
9941 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9942 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009944 if (error || title == NULL || initdir == NULL || defname == NULL)
9945 rettv->vval.v_string = NULL;
9946 else
9947 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009948 do_browse(save ? BROWSE_SAVE : 0,
9949 title, defname, NULL, initdir, NULL, curbuf);
9950#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009952#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009954}
9955
9956/*
9957 * "browsedir(title, initdir)" function
9958 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009960f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009961{
9962#ifdef FEAT_BROWSE
9963 char_u *title;
9964 char_u *initdir;
9965 char_u buf[NUMBUFLEN];
9966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009967 title = get_tv_string_chk(&argvars[0]);
9968 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009970 if (title == NULL || initdir == NULL)
9971 rettv->vval.v_string = NULL;
9972 else
9973 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009974 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979}
9980
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009981static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009982
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983/*
9984 * Find a buffer by number or exact name.
9985 */
9986 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009987find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
9989 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009991 if (avar->v_type == VAR_NUMBER)
9992 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009993 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009995 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009996 if (buf == NULL)
9997 {
9998 /* No full path name match, try a match with a URL or a "nofile"
9999 * buffer, these don't use the full path. */
10000 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10001 if (buf->b_fname != NULL
10002 && (path_with_url(buf->b_fname)
10003#ifdef FEAT_QUICKFIX
10004 || bt_nofile(buf)
10005#endif
10006 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010007 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010008 break;
10009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 }
10011 return buf;
10012}
10013
10014/*
10015 * "bufexists(expr)" function
10016 */
10017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010018f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021}
10022
10023/*
10024 * "buflisted(expr)" function
10025 */
10026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010027f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
10029 buf_T *buf;
10030
10031 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033}
10034
10035/*
10036 * "bufloaded(expr)" function
10037 */
10038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010039f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040{
10041 buf_T *buf;
10042
10043 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045}
10046
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010047 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010048buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 int save_magic;
10051 char_u *save_cpo;
10052 buf_T *buf;
10053
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10055 save_magic = p_magic;
10056 p_magic = TRUE;
10057 save_cpo = p_cpo;
10058 p_cpo = (char_u *)"";
10059
10060 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010061 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062
10063 p_magic = save_magic;
10064 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010065 return buf;
10066}
10067
10068/*
10069 * Get buffer by number or pattern.
10070 */
10071 static buf_T *
10072get_buf_tv(typval_T *tv, int curtab_only)
10073{
10074 char_u *name = tv->vval.v_string;
10075 buf_T *buf;
10076
10077 if (tv->v_type == VAR_NUMBER)
10078 return buflist_findnr((int)tv->vval.v_number);
10079 if (tv->v_type != VAR_STRING)
10080 return NULL;
10081 if (name == NULL || *name == NUL)
10082 return curbuf;
10083 if (name[0] == '$' && name[1] == NUL)
10084 return lastbuf;
10085
10086 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
10088 /* If not found, try expanding the name, like done for bufexists(). */
10089 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091
10092 return buf;
10093}
10094
10095/*
10096 * "bufname(expr)" function
10097 */
10098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010099f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101 buf_T *buf;
10102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010103 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010105 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 --emsg_off;
10112}
10113
10114/*
10115 * "bufnr(expr)" function
10116 */
10117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010118f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
10120 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010121 int error = FALSE;
10122 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010124 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010126 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010127 --emsg_off;
10128
10129 /* If the buffer isn't found and the second argument is not zero create a
10130 * new buffer. */
10131 if (buf == NULL
10132 && argvars[1].v_type != VAR_UNKNOWN
10133 && get_tv_number_chk(&argvars[1], &error) != 0
10134 && !error
10135 && (name = get_tv_string_chk(&argvars[0])) != NULL
10136 && !error)
10137 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10138
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143}
10144
10145/*
10146 * "bufwinnr(nr)" function
10147 */
10148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010149f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150{
10151#ifdef FEAT_WINDOWS
10152 win_T *wp;
10153 int winnr = 0;
10154#endif
10155 buf_T *buf;
10156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010157 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010159 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160#ifdef FEAT_WINDOWS
10161 for (wp = firstwin; wp; wp = wp->w_next)
10162 {
10163 ++winnr;
10164 if (wp->w_buffer == buf)
10165 break;
10166 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010167 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170#endif
10171 --emsg_off;
10172}
10173
10174/*
10175 * "byte2line(byte)" function
10176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010178f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182#else
10183 long boff = 0;
10184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 (linenr_T)0, &boff);
10191#endif
10192}
10193
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010195byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010196{
10197#ifdef FEAT_MBYTE
10198 char_u *t;
10199#endif
10200 char_u *str;
10201 long idx;
10202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010203 str = get_tv_string_chk(&argvars[0]);
10204 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010206 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010207 return;
10208
10209#ifdef FEAT_MBYTE
10210 t = str;
10211 for ( ; idx > 0; idx--)
10212 {
10213 if (*t == NUL) /* EOL reached */
10214 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010215 if (enc_utf8 && comp)
10216 t += utf_ptr2len(t);
10217 else
10218 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010219 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010220 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010221#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010222 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010224#endif
10225}
10226
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010227/*
10228 * "byteidx()" function
10229 */
10230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010231f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010232{
10233 byteidx(argvars, rettv, FALSE);
10234}
10235
10236/*
10237 * "byteidxcomp()" function
10238 */
10239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010240f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010241{
10242 byteidx(argvars, rettv, TRUE);
10243}
10244
Bram Moolenaardb913952012-06-29 12:54:53 +020010245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010246func_call(
10247 char_u *name,
10248 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010249 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010250 dict_T *selfdict,
10251 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010252{
10253 listitem_T *item;
10254 typval_T argv[MAX_FUNC_ARGS + 1];
10255 int argc = 0;
10256 int dummy;
10257 int r = 0;
10258
10259 for (item = args->vval.v_list->lv_first; item != NULL;
10260 item = item->li_next)
10261 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010262 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010263 {
10264 EMSG(_("E699: Too many arguments"));
10265 break;
10266 }
10267 /* Make a copy of each argument. This is needed to be able to set
10268 * v_lock to VAR_FIXED in the copy without changing the original list.
10269 */
10270 copy_tv(&item->li_tv, &argv[argc++]);
10271 }
10272
10273 if (item == NULL)
10274 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10275 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010276 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010277
10278 /* Free the arguments. */
10279 while (argc > 0)
10280 clear_tv(&argv[--argc]);
10281
10282 return r;
10283}
10284
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010285/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010286 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010287 */
10288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010289f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010290{
10291 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010292 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010294
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010295 if (argvars[1].v_type != VAR_LIST)
10296 {
10297 EMSG(_(e_listreq));
10298 return;
10299 }
10300 if (argvars[1].vval.v_list == NULL)
10301 return;
10302
10303 if (argvars[0].v_type == VAR_FUNC)
10304 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010305 else if (argvars[0].v_type == VAR_PARTIAL)
10306 {
10307 partial = argvars[0].vval.v_partial;
10308 func = partial->pt_name;
10309 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010310 else
10311 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010312 if (*func == NUL)
10313 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010314
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 if (argvars[2].v_type != VAR_UNKNOWN)
10316 {
10317 if (argvars[2].v_type != VAR_DICT)
10318 {
10319 EMSG(_(e_dictreq));
10320 return;
10321 }
10322 selfdict = argvars[2].vval.v_dict;
10323 }
10324
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010325 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010326}
10327
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010328#ifdef FEAT_FLOAT
10329/*
10330 * "ceil({float})" function
10331 */
10332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010333f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010334{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010335 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010336
10337 rettv->v_type = VAR_FLOAT;
10338 if (get_float_arg(argvars, &f) == OK)
10339 rettv->vval.v_float = ceil(f);
10340 else
10341 rettv->vval.v_float = 0.0;
10342}
10343#endif
10344
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010345#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010346/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010347 * "ch_close()" function
10348 */
10349 static void
10350f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10351{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010352 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010353
10354 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010355 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010356 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010357 channel_clear(channel);
10358 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010359}
10360
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010361/*
10362 * "ch_getbufnr()" function
10363 */
10364 static void
10365f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10366{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010367 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010368
10369 rettv->vval.v_number = -1;
10370 if (channel != NULL)
10371 {
10372 char_u *what = get_tv_string(&argvars[1]);
10373 int part;
10374
10375 if (STRCMP(what, "err") == 0)
10376 part = PART_ERR;
10377 else if (STRCMP(what, "out") == 0)
10378 part = PART_OUT;
10379 else if (STRCMP(what, "in") == 0)
10380 part = PART_IN;
10381 else
10382 part = PART_SOCK;
10383 if (channel->ch_part[part].ch_buffer != NULL)
10384 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10385 }
10386}
10387
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010388/*
10389 * "ch_getjob()" function
10390 */
10391 static void
10392f_ch_getjob(typval_T *argvars, typval_T *rettv)
10393{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010394 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010395
10396 if (channel != NULL)
10397 {
10398 rettv->v_type = VAR_JOB;
10399 rettv->vval.v_job = channel->ch_job;
10400 if (channel->ch_job != NULL)
10401 ++channel->ch_job->jv_refcount;
10402 }
10403}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010404
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010405/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010406 * "ch_info()" function
10407 */
10408 static void
10409f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10410{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010411 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010412
10413 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10414 channel_info(channel, rettv->vval.v_dict);
10415}
10416
10417/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010418 * "ch_log()" function
10419 */
10420 static void
10421f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10422{
10423 char_u *msg = get_tv_string(&argvars[0]);
10424 channel_T *channel = NULL;
10425
10426 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010427 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010428
10429 ch_log(channel, (char *)msg);
10430}
10431
10432/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010433 * "ch_logfile()" function
10434 */
10435 static void
10436f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10437{
10438 char_u *fname;
10439 char_u *opt = (char_u *)"";
10440 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010441
10442 fname = get_tv_string(&argvars[0]);
10443 if (argvars[1].v_type == VAR_STRING)
10444 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010445 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010446}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010447
10448/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010449 * "ch_open()" function
10450 */
10451 static void
10452f_ch_open(typval_T *argvars, typval_T *rettv)
10453{
Bram Moolenaar77073442016-02-13 23:23:53 +010010454 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010455 if (check_restricted() || check_secure())
10456 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010457 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010458}
10459
10460/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010461 * "ch_read()" function
10462 */
10463 static void
10464f_ch_read(typval_T *argvars, typval_T *rettv)
10465{
10466 common_channel_read(argvars, rettv, FALSE);
10467}
10468
10469/*
10470 * "ch_readraw()" function
10471 */
10472 static void
10473f_ch_readraw(typval_T *argvars, typval_T *rettv)
10474{
10475 common_channel_read(argvars, rettv, TRUE);
10476}
10477
10478/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010479 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010480 */
10481 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010482f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10483{
10484 ch_expr_common(argvars, rettv, TRUE);
10485}
10486
10487/*
10488 * "ch_sendexpr()" function
10489 */
10490 static void
10491f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10492{
10493 ch_expr_common(argvars, rettv, FALSE);
10494}
10495
10496/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010497 * "ch_evalraw()" function
10498 */
10499 static void
10500f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10501{
10502 ch_raw_common(argvars, rettv, TRUE);
10503}
10504
10505/*
10506 * "ch_sendraw()" function
10507 */
10508 static void
10509f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10510{
10511 ch_raw_common(argvars, rettv, FALSE);
10512}
10513
10514/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010515 * "ch_setoptions()" function
10516 */
10517 static void
10518f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10519{
10520 channel_T *channel;
10521 jobopt_T opt;
10522
Bram Moolenaar437905c2016-04-26 19:01:05 +020010523 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010524 if (channel == NULL)
10525 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010526 clear_job_options(&opt);
10527 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010528 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10529 channel_set_options(channel, &opt);
10530 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010531}
10532
10533/*
10534 * "ch_status()" function
10535 */
10536 static void
10537f_ch_status(typval_T *argvars, typval_T *rettv)
10538{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010539 channel_T *channel;
10540
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010541 /* return an empty string by default */
10542 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010543 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010544
Bram Moolenaar437905c2016-04-26 19:01:05 +020010545 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010546 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010547}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010548#endif
10549
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010550/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010551 * "changenr()" function
10552 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010554f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010555{
10556 rettv->vval.v_number = curbuf->b_u_seq_cur;
10557}
10558
10559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 * "char2nr(string)" function
10561 */
10562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010563f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564{
10565#ifdef FEAT_MBYTE
10566 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010567 {
10568 int utf8 = 0;
10569
10570 if (argvars[1].v_type != VAR_UNKNOWN)
10571 utf8 = get_tv_number_chk(&argvars[1], NULL);
10572
10573 if (utf8)
10574 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10575 else
10576 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 else
10579#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581}
10582
10583/*
10584 * "cindent(lnum)" function
10585 */
10586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010587f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
10589#ifdef FEAT_CINDENT
10590 pos_T pos;
10591 linenr_T lnum;
10592
10593 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010594 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10596 {
10597 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 curwin->w_cursor = pos;
10600 }
10601 else
10602#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604}
10605
10606/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010607 * "clearmatches()" function
10608 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010610f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010611{
10612#ifdef FEAT_SEARCH_EXTRA
10613 clear_matches(curwin);
10614#endif
10615}
10616
10617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 * "col(string)" function
10619 */
10620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010621f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
10623 colnr_T col = 0;
10624 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010625 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010627 fp = var2fpos(&argvars[0], FALSE, &fnum);
10628 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 {
10630 if (fp->col == MAXCOL)
10631 {
10632 /* '> can be MAXCOL, get the length of the line then */
10633 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010634 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 else
10636 col = MAXCOL;
10637 }
10638 else
10639 {
10640 col = fp->col + 1;
10641#ifdef FEAT_VIRTUALEDIT
10642 /* col(".") when the cursor is on the NUL at the end of the line
10643 * because of "coladd" can be seen as an extra column. */
10644 if (virtual_active() && fp == &curwin->w_cursor)
10645 {
10646 char_u *p = ml_get_cursor();
10647
10648 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10649 curwin->w_virtcol - curwin->w_cursor.coladd))
10650 {
10651# ifdef FEAT_MBYTE
10652 int l;
10653
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010654 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 col += l;
10656# else
10657 if (*p != NUL && p[1] == NUL)
10658 ++col;
10659# endif
10660 }
10661 }
10662#endif
10663 }
10664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666}
10667
Bram Moolenaar572cb562005-08-05 21:35:02 +000010668#if defined(FEAT_INS_EXPAND)
10669/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010670 * "complete()" function
10671 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010673f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010674{
10675 int startcol;
10676
10677 if ((State & INSERT) == 0)
10678 {
10679 EMSG(_("E785: complete() can only be used in Insert mode"));
10680 return;
10681 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010682
10683 /* Check for undo allowed here, because if something was already inserted
10684 * the line was already saved for undo and this check isn't done. */
10685 if (!undo_allowed())
10686 return;
10687
Bram Moolenaarade00832006-03-10 21:46:58 +000010688 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10689 {
10690 EMSG(_(e_invarg));
10691 return;
10692 }
10693
10694 startcol = get_tv_number_chk(&argvars[0], NULL);
10695 if (startcol <= 0)
10696 return;
10697
10698 set_completion(startcol - 1, argvars[1].vval.v_list);
10699}
10700
10701/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010702 * "complete_add()" function
10703 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010705f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010706{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010707 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010708}
10709
10710/*
10711 * "complete_check()" function
10712 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010714f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010715{
10716 int saved = RedrawingDisabled;
10717
10718 RedrawingDisabled = 0;
10719 ins_compl_check_keys(0);
10720 rettv->vval.v_number = compl_interrupted;
10721 RedrawingDisabled = saved;
10722}
10723#endif
10724
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725/*
10726 * "confirm(message, buttons[, default [, type]])" function
10727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010729f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730{
10731#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10732 char_u *message;
10733 char_u *buttons = NULL;
10734 char_u buf[NUMBUFLEN];
10735 char_u buf2[NUMBUFLEN];
10736 int def = 1;
10737 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010738 char_u *typestr;
10739 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 message = get_tv_string_chk(&argvars[0]);
10742 if (message == NULL)
10743 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010744 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10747 if (buttons == NULL)
10748 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010749 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010751 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010752 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10755 if (typestr == NULL)
10756 error = TRUE;
10757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 switch (TOUPPER_ASC(*typestr))
10760 {
10761 case 'E': type = VIM_ERROR; break;
10762 case 'Q': type = VIM_QUESTION; break;
10763 case 'I': type = VIM_INFO; break;
10764 case 'W': type = VIM_WARNING; break;
10765 case 'G': type = VIM_GENERIC; break;
10766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 }
10768 }
10769 }
10770 }
10771
10772 if (buttons == NULL || *buttons == NUL)
10773 buttons = (char_u *)_("&Ok");
10774
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010775 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010776 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010777 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778#endif
10779}
10780
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010781/*
10782 * "copy()" function
10783 */
10784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010785f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010786{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010787 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010788}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010790#ifdef FEAT_FLOAT
10791/*
10792 * "cos()" function
10793 */
10794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010795f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010796{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010797 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010798
10799 rettv->v_type = VAR_FLOAT;
10800 if (get_float_arg(argvars, &f) == OK)
10801 rettv->vval.v_float = cos(f);
10802 else
10803 rettv->vval.v_float = 0.0;
10804}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010805
10806/*
10807 * "cosh()" function
10808 */
10809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010810f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010811{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010812 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010813
10814 rettv->v_type = VAR_FLOAT;
10815 if (get_float_arg(argvars, &f) == OK)
10816 rettv->vval.v_float = cosh(f);
10817 else
10818 rettv->vval.v_float = 0.0;
10819}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010820#endif
10821
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010823 * "count()" function
10824 */
10825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010826f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010827{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010828 long n = 0;
10829 int ic = FALSE;
10830
Bram Moolenaare9a41262005-01-15 22:18:47 +000010831 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010832 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010833 listitem_T *li;
10834 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010836
Bram Moolenaare9a41262005-01-15 22:18:47 +000010837 if ((l = argvars[0].vval.v_list) != NULL)
10838 {
10839 li = l->lv_first;
10840 if (argvars[2].v_type != VAR_UNKNOWN)
10841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 int error = FALSE;
10843
10844 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 if (argvars[3].v_type != VAR_UNKNOWN)
10846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010847 idx = get_tv_number_chk(&argvars[3], &error);
10848 if (!error)
10849 {
10850 li = list_find(l, idx);
10851 if (li == NULL)
10852 EMSGN(_(e_listidx), idx);
10853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010854 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010855 if (error)
10856 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010857 }
10858
10859 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010860 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861 ++n;
10862 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864 else if (argvars[0].v_type == VAR_DICT)
10865 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010866 int todo;
10867 dict_T *d;
10868 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010869
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010870 if ((d = argvars[0].vval.v_dict) != NULL)
10871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 int error = FALSE;
10873
Bram Moolenaare9a41262005-01-15 22:18:47 +000010874 if (argvars[2].v_type != VAR_UNKNOWN)
10875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010876 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010877 if (argvars[3].v_type != VAR_UNKNOWN)
10878 EMSG(_(e_invarg));
10879 }
10880
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010881 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010882 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010883 {
10884 if (!HASHITEM_EMPTY(hi))
10885 {
10886 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010887 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010888 ++n;
10889 }
10890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010891 }
10892 }
10893 else
10894 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010895 rettv->vval.v_number = n;
10896}
10897
10898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10900 *
10901 * Checks the existence of a cscope connection.
10902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010904f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905{
10906#ifdef FEAT_CSCOPE
10907 int num = 0;
10908 char_u *dbpath = NULL;
10909 char_u *prepend = NULL;
10910 char_u buf[NUMBUFLEN];
10911
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010912 if (argvars[0].v_type != VAR_UNKNOWN
10913 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 num = (int)get_tv_number(&argvars[0]);
10916 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010917 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 }
10920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922#endif
10923}
10924
10925/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010926 * "cursor(lnum, col)" function, or
10927 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010929 * Moves the cursor to the specified line and column.
10930 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010933f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934{
10935 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010936#ifdef FEAT_VIRTUALEDIT
10937 long coladd = 0;
10938#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010939 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010941 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010942 if (argvars[1].v_type == VAR_UNKNOWN)
10943 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010944 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010945 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010946
Bram Moolenaar493c1782014-05-28 14:34:46 +020010947 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010948 {
10949 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010950 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010951 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010952 line = pos.lnum;
10953 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010954#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010955 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010956#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010957 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010958 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010959 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010960 set_curswant = FALSE;
10961 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010962 }
10963 else
10964 {
10965 line = get_tv_lnum(argvars);
10966 col = get_tv_number_chk(&argvars[1], NULL);
10967#ifdef FEAT_VIRTUALEDIT
10968 if (argvars[2].v_type != VAR_UNKNOWN)
10969 coladd = get_tv_number_chk(&argvars[2], NULL);
10970#endif
10971 }
10972 if (line < 0 || col < 0
10973#ifdef FEAT_VIRTUALEDIT
10974 || coladd < 0
10975#endif
10976 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010977 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 if (line > 0)
10979 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 if (col > 0)
10981 curwin->w_cursor.col = col - 1;
10982#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010983 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984#endif
10985
10986 /* Make sure the cursor is in a valid position. */
10987 check_cursor();
10988#ifdef FEAT_MBYTE
10989 /* Correct cursor for multi-byte character. */
10990 if (has_mbyte)
10991 mb_adjust_cursor();
10992#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010993
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010994 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010995 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996}
10997
10998/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010999 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 */
11001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011002f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011004 int noref = 0;
11005
11006 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011007 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011008 if (noref < 0 || noref > 1)
11009 EMSG(_(e_invarg));
11010 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011011 {
11012 current_copyID += COPYID_INC;
11013 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015}
11016
11017/*
11018 * "delete()" function
11019 */
11020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011021f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011023 char_u nbuf[NUMBUFLEN];
11024 char_u *name;
11025 char_u *flags;
11026
11027 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011029 return;
11030
11031 name = get_tv_string(&argvars[0]);
11032 if (name == NULL || *name == NUL)
11033 {
11034 EMSG(_(e_invarg));
11035 return;
11036 }
11037
11038 if (argvars[1].v_type != VAR_UNKNOWN)
11039 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011041 flags = (char_u *)"";
11042
11043 if (*flags == NUL)
11044 /* delete a file */
11045 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11046 else if (STRCMP(flags, "d") == 0)
11047 /* delete an empty directory */
11048 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11049 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011050 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011051 rettv->vval.v_number = delete_recursive(name);
11052 else
11053 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054}
11055
11056/*
11057 * "did_filetype()" function
11058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011060f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061{
11062#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064#endif
11065}
11066
11067/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011068 * "diff_filler()" function
11069 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011071f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011072{
11073#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011075#endif
11076}
11077
11078/*
11079 * "diff_hlID()" function
11080 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011082f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011083{
11084#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011086 static linenr_T prev_lnum = 0;
11087 static int changedtick = 0;
11088 static int fnum = 0;
11089 static int change_start = 0;
11090 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011091 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011092 int filler_lines;
11093 int col;
11094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 if (lnum < 0) /* ignore type error in {lnum} arg */
11096 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011097 if (lnum != prev_lnum
11098 || changedtick != curbuf->b_changedtick
11099 || fnum != curbuf->b_fnum)
11100 {
11101 /* New line, buffer, change: need to get the values. */
11102 filler_lines = diff_check(curwin, lnum);
11103 if (filler_lines < 0)
11104 {
11105 if (filler_lines == -1)
11106 {
11107 change_start = MAXCOL;
11108 change_end = -1;
11109 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11110 hlID = HLF_ADD; /* added line */
11111 else
11112 hlID = HLF_CHD; /* changed line */
11113 }
11114 else
11115 hlID = HLF_ADD; /* added line */
11116 }
11117 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011118 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011119 prev_lnum = lnum;
11120 changedtick = curbuf->b_changedtick;
11121 fnum = curbuf->b_fnum;
11122 }
11123
11124 if (hlID == HLF_CHD || hlID == HLF_TXD)
11125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011127 if (col >= change_start && col <= change_end)
11128 hlID = HLF_TXD; /* changed text */
11129 else
11130 hlID = HLF_CHD; /* changed line */
11131 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011132 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011133#endif
11134}
11135
11136/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011137 * "empty({expr})" function
11138 */
11139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011140f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011141{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011142 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011143
11144 switch (argvars[0].v_type)
11145 {
11146 case VAR_STRING:
11147 case VAR_FUNC:
11148 n = argvars[0].vval.v_string == NULL
11149 || *argvars[0].vval.v_string == NUL;
11150 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011151 case VAR_PARTIAL:
11152 n = FALSE;
11153 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011154 case VAR_NUMBER:
11155 n = argvars[0].vval.v_number == 0;
11156 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011157 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011158#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011159 n = argvars[0].vval.v_float == 0.0;
11160 break;
11161#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011162 case VAR_LIST:
11163 n = argvars[0].vval.v_list == NULL
11164 || argvars[0].vval.v_list->lv_first == NULL;
11165 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011166 case VAR_DICT:
11167 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011168 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011169 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011170 case VAR_SPECIAL:
11171 n = argvars[0].vval.v_number != VVAL_TRUE;
11172 break;
11173
Bram Moolenaar835dc632016-02-07 14:27:38 +010011174 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011175#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011176 n = argvars[0].vval.v_job == NULL
11177 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11178 break;
11179#endif
11180 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011181#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011182 n = argvars[0].vval.v_channel == NULL
11183 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011184 break;
11185#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011186 case VAR_UNKNOWN:
11187 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11188 n = TRUE;
11189 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011190 }
11191
11192 rettv->vval.v_number = n;
11193}
11194
11195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 * "escape({string}, {chars})" function
11197 */
11198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011199f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200{
11201 char_u buf[NUMBUFLEN];
11202
Bram Moolenaar758711c2005-02-02 23:11:38 +000011203 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11204 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206}
11207
11208/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011209 * "eval()" function
11210 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011212f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011213{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011214 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 s = get_tv_string_chk(&argvars[0]);
11217 if (s != NULL)
11218 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011219
Bram Moolenaar615b9972015-01-14 17:15:05 +010011220 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011221 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11222 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011223 if (p != NULL && !aborting())
11224 EMSG2(_(e_invexpr2), p);
11225 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011227 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011229 else if (*s != NUL)
11230 EMSG(_(e_trailing));
11231}
11232
11233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 * "eventhandler()" function
11235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011237f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240}
11241
11242/*
11243 * "executable()" function
11244 */
11245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011246f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011248 char_u *name = get_tv_string(&argvars[0]);
11249
11250 /* Check in $PATH and also check directly if there is a directory name. */
11251 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11252 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011253}
11254
11255/*
11256 * "exepath()" function
11257 */
11258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011259f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011260{
11261 char_u *p = NULL;
11262
Bram Moolenaarb5971142015-03-21 17:32:19 +010011263 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011264 rettv->v_type = VAR_STRING;
11265 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266}
11267
11268/*
11269 * "exists()" function
11270 */
11271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011272f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273{
11274 char_u *p;
11275 char_u *name;
11276 int n = FALSE;
11277 int len = 0;
11278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 if (*p == '$') /* environment variable */
11281 {
11282 /* first try "normal" environment variables (fast) */
11283 if (mch_getenv(p + 1) != NULL)
11284 n = TRUE;
11285 else
11286 {
11287 /* try expanding things like $VIM and ${HOME} */
11288 p = expand_env_save(p);
11289 if (p != NULL && *p != '$')
11290 n = TRUE;
11291 vim_free(p);
11292 }
11293 }
11294 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011297 if (*skipwhite(p) != NUL)
11298 n = FALSE; /* trailing garbage */
11299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 else if (*p == '*') /* internal or user defined function */
11301 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011302 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 }
11304 else if (*p == ':')
11305 {
11306 n = cmd_exists(p + 1);
11307 }
11308 else if (*p == '#')
11309 {
11310#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011311 if (p[1] == '#')
11312 n = autocmd_supported(p + 2);
11313 else
11314 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315#endif
11316 }
11317 else /* internal variable */
11318 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011319 char_u *tofree;
11320 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011322 /* get_name_len() takes care of expanding curly braces */
11323 name = p;
11324 len = get_name_len(&p, &tofree, TRUE, FALSE);
11325 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011327 if (tofree != NULL)
11328 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011329 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011330 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011332 /* handle d.key, l[idx], f(expr) */
11333 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11334 if (n)
11335 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 }
11337 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011338 if (*p != NUL)
11339 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011341 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 }
11343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345}
11346
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011347#ifdef FEAT_FLOAT
11348/*
11349 * "exp()" function
11350 */
11351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011352f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011353{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011354 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011355
11356 rettv->v_type = VAR_FLOAT;
11357 if (get_float_arg(argvars, &f) == OK)
11358 rettv->vval.v_float = exp(f);
11359 else
11360 rettv->vval.v_float = 0.0;
11361}
11362#endif
11363
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364/*
11365 * "expand()" function
11366 */
11367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011368f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369{
11370 char_u *s;
11371 int len;
11372 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011373 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011376 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011379 if (argvars[1].v_type != VAR_UNKNOWN
11380 && argvars[2].v_type != VAR_UNKNOWN
11381 && get_tv_number_chk(&argvars[2], &error)
11382 && !error)
11383 {
11384 rettv->v_type = VAR_LIST;
11385 rettv->vval.v_list = NULL;
11386 }
11387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 if (*s == '%' || *s == '#' || *s == '<')
11390 {
11391 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011392 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011394 if (rettv->v_type == VAR_LIST)
11395 {
11396 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11397 list_append_string(rettv->vval.v_list, result, -1);
11398 else
11399 vim_free(result);
11400 }
11401 else
11402 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 }
11404 else
11405 {
11406 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011407 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011408 if (argvars[1].v_type != VAR_UNKNOWN
11409 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011410 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 if (!error)
11412 {
11413 ExpandInit(&xpc);
11414 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011415 if (p_wic)
11416 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011417 if (rettv->v_type == VAR_STRING)
11418 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11419 options, WILD_ALL);
11420 else if (rettv_list_alloc(rettv) != FAIL)
11421 {
11422 int i;
11423
11424 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11425 for (i = 0; i < xpc.xp_numfiles; i++)
11426 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11427 ExpandCleanup(&xpc);
11428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011429 }
11430 else
11431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 }
11433}
11434
11435/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011436 * Go over all entries in "d2" and add them to "d1".
11437 * When "action" is "error" then a duplicate key is an error.
11438 * When "action" is "force" then a duplicate key is overwritten.
11439 * Otherwise duplicate keys are ignored ("action" is "keep").
11440 */
11441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011442dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011443{
11444 dictitem_T *di1;
11445 hashitem_T *hi2;
11446 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011447 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011448
11449 todo = (int)d2->dv_hashtab.ht_used;
11450 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11451 {
11452 if (!HASHITEM_EMPTY(hi2))
11453 {
11454 --todo;
11455 di1 = dict_find(d1, hi2->hi_key, -1);
11456 if (d1->dv_scope != 0)
11457 {
11458 /* Disallow replacing a builtin function in l: and g:.
11459 * Check the key to be valid when adding to any
11460 * scope. */
11461 if (d1->dv_scope == VAR_DEF_SCOPE
11462 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11463 && var_check_func_name(hi2->hi_key,
11464 di1 == NULL))
11465 break;
11466 if (!valid_varname(hi2->hi_key))
11467 break;
11468 }
11469 if (di1 == NULL)
11470 {
11471 di1 = dictitem_copy(HI2DI(hi2));
11472 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11473 dictitem_free(di1);
11474 }
11475 else if (*action == 'e')
11476 {
11477 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11478 break;
11479 }
11480 else if (*action == 'f' && HI2DI(hi2) != di1)
11481 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011482 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11483 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011484 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011485 clear_tv(&di1->di_tv);
11486 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11487 }
11488 }
11489 }
11490}
11491
11492/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011493 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011494 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011495 */
11496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011497f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011498{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011499 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011500
Bram Moolenaare9a41262005-01-15 22:18:47 +000011501 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011502 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011503 list_T *l1, *l2;
11504 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011505 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011507
Bram Moolenaare9a41262005-01-15 22:18:47 +000011508 l1 = argvars[0].vval.v_list;
11509 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011510 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011511 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 {
11513 if (argvars[2].v_type != VAR_UNKNOWN)
11514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011515 before = get_tv_number_chk(&argvars[2], &error);
11516 if (error)
11517 return; /* type error; errmsg already given */
11518
Bram Moolenaar758711c2005-02-02 23:11:38 +000011519 if (before == l1->lv_len)
11520 item = NULL;
11521 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011522 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011523 item = list_find(l1, before);
11524 if (item == NULL)
11525 {
11526 EMSGN(_(e_listidx), before);
11527 return;
11528 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011529 }
11530 }
11531 else
11532 item = NULL;
11533 list_extend(l1, l2, item);
11534
Bram Moolenaare9a41262005-01-15 22:18:47 +000011535 copy_tv(&argvars[0], rettv);
11536 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011538 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11539 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011540 dict_T *d1, *d2;
11541 char_u *action;
11542 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011543
11544 d1 = argvars[0].vval.v_dict;
11545 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011546 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011547 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548 {
11549 /* Check the third argument. */
11550 if (argvars[2].v_type != VAR_UNKNOWN)
11551 {
11552 static char *(av[]) = {"keep", "force", "error"};
11553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 action = get_tv_string_chk(&argvars[2]);
11555 if (action == NULL)
11556 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011557 for (i = 0; i < 3; ++i)
11558 if (STRCMP(action, av[i]) == 0)
11559 break;
11560 if (i == 3)
11561 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011562 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 return;
11564 }
11565 }
11566 else
11567 action = (char_u *)"force";
11568
Bram Moolenaara9922d62013-05-30 13:01:18 +020011569 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011570
Bram Moolenaare9a41262005-01-15 22:18:47 +000011571 copy_tv(&argvars[0], rettv);
11572 }
11573 }
11574 else
11575 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011576}
11577
11578/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011579 * "feedkeys()" function
11580 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011582f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011583{
11584 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011585 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011586 char_u *keys, *flags;
11587 char_u nbuf[NUMBUFLEN];
11588 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011589 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011590 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011591 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011592
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011593 /* This is not allowed in the sandbox. If the commands would still be
11594 * executed in the sandbox it would be OK, but it probably happens later,
11595 * when "sandbox" is no longer set. */
11596 if (check_secure())
11597 return;
11598
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011599 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011600
11601 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011602 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011603 flags = get_tv_string_buf(&argvars[1], nbuf);
11604 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011605 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011606 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011607 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011608 case 'n': remap = FALSE; break;
11609 case 'm': remap = TRUE; break;
11610 case 't': typed = TRUE; break;
11611 case 'i': insert = TRUE; break;
11612 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011613 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011614 }
11615 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011616 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011617
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011618 if (*keys != NUL || execute)
11619 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011620 /* Need to escape K_SPECIAL and CSI before putting the string in the
11621 * typeahead buffer. */
11622 keys_esc = vim_strsave_escape_csi(keys);
11623 if (keys_esc != NULL)
11624 {
11625 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011626 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011627 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011628 if (vgetc_busy)
11629 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011630 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011631 {
11632 int save_msg_scroll = msg_scroll;
11633
11634 /* Avoid a 1 second delay when the keys start Insert mode. */
11635 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011636
Bram Moolenaar245c4102016-04-20 17:37:41 +020011637 if (!dangerous)
11638 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011639 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011640 if (!dangerous)
11641 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011642 msg_scroll |= save_msg_scroll;
11643 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011644 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011645 }
11646}
11647
11648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 * "filereadable()" function
11650 */
11651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011652f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011654 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 char_u *p;
11656 int n;
11657
Bram Moolenaarc236c162008-07-13 17:41:49 +000011658#ifndef O_NONBLOCK
11659# define O_NONBLOCK 0
11660#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011662 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11663 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 {
11665 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011666 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 }
11668 else
11669 n = FALSE;
11670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672}
11673
11674/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011675 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 * rights to write into.
11677 */
11678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011679f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011681 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682}
11683
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011684 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011685findfilendir(
11686 typval_T *argvars UNUSED,
11687 typval_T *rettv,
11688 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011689{
11690#ifdef FEAT_SEARCHPATH
11691 char_u *fname;
11692 char_u *fresult = NULL;
11693 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11694 char_u *p;
11695 char_u pathbuf[NUMBUFLEN];
11696 int count = 1;
11697 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011698 int error = FALSE;
11699#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011700
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011701 rettv->vval.v_string = NULL;
11702 rettv->v_type = VAR_STRING;
11703
11704#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11710 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011711 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011712 else
11713 {
11714 if (*p != NUL)
11715 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011718 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011719 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011720 }
11721
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011722 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11723 error = TRUE;
11724
11725 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011727 do
11728 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011729 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011730 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011731 fresult = find_file_in_path_option(first ? fname : NULL,
11732 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011733 0, first, path,
11734 find_what,
11735 curbuf->b_ffname,
11736 find_what == FINDFILE_DIR
11737 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011738 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011739
11740 if (fresult != NULL && rettv->v_type == VAR_LIST)
11741 list_append_string(rettv->vval.v_list, fresult, -1);
11742
11743 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011745
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011746 if (rettv->v_type == VAR_STRING)
11747 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011748#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011749}
11750
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011751static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11752static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011753
11754/*
11755 * Implementation of map() and filter().
11756 */
11757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011758filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011759{
11760 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011761 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011762 listitem_T *li, *nli;
11763 list_T *l = NULL;
11764 dictitem_T *di;
11765 hashtab_T *ht;
11766 hashitem_T *hi;
11767 dict_T *d = NULL;
11768 typval_T save_val;
11769 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011770 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011771 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011772 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011773 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011774 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011775 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011776 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011777
Bram Moolenaare9a41262005-01-15 22:18:47 +000011778 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011779 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011780 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011781 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011782 return;
11783 }
11784 else if (argvars[0].v_type == VAR_DICT)
11785 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011786 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011787 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011788 return;
11789 }
11790 else
11791 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011792 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011793 return;
11794 }
11795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011796 expr = get_tv_string_buf_chk(&argvars[1], buf);
11797 /* On type errors, the preceding call has already displayed an error
11798 * message. Avoid a misleading error message for an empty string that
11799 * was not passed as argument. */
11800 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 prepare_vimvar(VV_VAL, &save_val);
11803 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011804
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011805 /* We reset "did_emsg" to be able to detect whether an error
11806 * occurred during evaluation of the expression. */
11807 save_did_emsg = did_emsg;
11808 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011809
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011810 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011811 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 vimvars[VV_KEY].vv_type = VAR_STRING;
11814
11815 ht = &d->dv_hashtab;
11816 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011817 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 if (!HASHITEM_EMPTY(hi))
11821 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011822 int r;
11823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824 --todo;
11825 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011826 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011827 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11828 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011829 break;
11830 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011831 r = filter_map_one(&di->di_tv, expr, map, &rem);
11832 clear_tv(&vimvars[VV_KEY].vv_tv);
11833 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011834 break;
11835 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011836 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011837 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11838 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011839 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011840 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011841 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011842 }
11843 }
11844 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011845 }
11846 else
11847 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011848 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850 for (li = l->lv_first; li != NULL; li = nli)
11851 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011852 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011853 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011854 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011855 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011856 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011857 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011858 break;
11859 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011860 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011861 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011862 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011863 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011864
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011865 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011867
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011868 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011869 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870
11871 copy_tv(&argvars[0], rettv);
11872}
11873
11874 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011875filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011876{
Bram Moolenaar33570922005-01-25 22:26:29 +000011877 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011878 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011879 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011880
Bram Moolenaar33570922005-01-25 22:26:29 +000011881 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011882 s = expr;
11883 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011884 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011885 if (*s != NUL) /* check for trailing chars after expr */
11886 {
11887 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011888 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011889 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011890 }
11891 if (map)
11892 {
11893 /* map(): replace the list item value */
11894 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011895 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011896 *tv = rettv;
11897 }
11898 else
11899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011900 int error = FALSE;
11901
Bram Moolenaare9a41262005-01-15 22:18:47 +000011902 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011904 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011905 /* On type error, nothing has been removed; return FAIL to stop the
11906 * loop. The error message was given by get_tv_number_chk(). */
11907 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011908 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011909 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011910 retval = OK;
11911theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011912 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011913 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011914}
11915
11916/*
11917 * "filter()" function
11918 */
11919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011920f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011921{
11922 filter_map(argvars, rettv, FALSE);
11923}
11924
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011925/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011926 * "finddir({fname}[, {path}[, {count}]])" function
11927 */
11928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011929f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011930{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011931 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011932}
11933
11934/*
11935 * "findfile({fname}[, {path}[, {count}]])" function
11936 */
11937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011938f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011939{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011940 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011941}
11942
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011943#ifdef FEAT_FLOAT
11944/*
11945 * "float2nr({float})" function
11946 */
11947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011948f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011949{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011950 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011951
11952 if (get_float_arg(argvars, &f) == OK)
11953 {
11954 if (f < -0x7fffffff)
11955 rettv->vval.v_number = -0x7fffffff;
11956 else if (f > 0x7fffffff)
11957 rettv->vval.v_number = 0x7fffffff;
11958 else
11959 rettv->vval.v_number = (varnumber_T)f;
11960 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011961}
11962
11963/*
11964 * "floor({float})" function
11965 */
11966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011967f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011968{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011969 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011970
11971 rettv->v_type = VAR_FLOAT;
11972 if (get_float_arg(argvars, &f) == OK)
11973 rettv->vval.v_float = floor(f);
11974 else
11975 rettv->vval.v_float = 0.0;
11976}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011977
11978/*
11979 * "fmod()" function
11980 */
11981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011982f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011983{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011984 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011985
11986 rettv->v_type = VAR_FLOAT;
11987 if (get_float_arg(argvars, &fx) == OK
11988 && get_float_arg(&argvars[1], &fy) == OK)
11989 rettv->vval.v_float = fmod(fx, fy);
11990 else
11991 rettv->vval.v_float = 0.0;
11992}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011993#endif
11994
Bram Moolenaar0d660222005-01-07 21:51:51 +000011995/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011996 * "fnameescape({string})" function
11997 */
11998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011999f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012000{
12001 rettv->vval.v_string = vim_strsave_fnameescape(
12002 get_tv_string(&argvars[0]), FALSE);
12003 rettv->v_type = VAR_STRING;
12004}
12005
12006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 * "fnamemodify({fname}, {mods})" function
12008 */
12009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012010f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011{
12012 char_u *fname;
12013 char_u *mods;
12014 int usedlen = 0;
12015 int len;
12016 char_u *fbuf = NULL;
12017 char_u buf[NUMBUFLEN];
12018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012019 fname = get_tv_string_chk(&argvars[0]);
12020 mods = get_tv_string_buf_chk(&argvars[1], buf);
12021 if (fname == NULL || mods == NULL)
12022 fname = NULL;
12023 else
12024 {
12025 len = (int)STRLEN(fname);
12026 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012029 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 vim_free(fbuf);
12035}
12036
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012037static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038
12039/*
12040 * "foldclosed()" function
12041 */
12042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012043foldclosed_both(
12044 typval_T *argvars UNUSED,
12045 typval_T *rettv,
12046 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047{
12048#ifdef FEAT_FOLDING
12049 linenr_T lnum;
12050 linenr_T first, last;
12051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12054 {
12055 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12056 {
12057 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 return;
12062 }
12063 }
12064#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066}
12067
12068/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012069 * "foldclosed()" function
12070 */
12071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012072f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073{
12074 foldclosed_both(argvars, rettv, FALSE);
12075}
12076
12077/*
12078 * "foldclosedend()" function
12079 */
12080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012081f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012082{
12083 foldclosed_both(argvars, rettv, TRUE);
12084}
12085
12086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 * "foldlevel()" function
12088 */
12089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012090f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091{
12092#ifdef FEAT_FOLDING
12093 linenr_T lnum;
12094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099}
12100
12101/*
12102 * "foldtext()" function
12103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012105f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106{
12107#ifdef FEAT_FOLDING
12108 linenr_T lnum;
12109 char_u *s;
12110 char_u *r;
12111 int len;
12112 char *txt;
12113#endif
12114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012115 rettv->v_type = VAR_STRING;
12116 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012118 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12119 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12120 <= curbuf->b_ml.ml_line_count
12121 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 {
12123 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012124 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12125 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 {
12127 if (!linewhite(lnum))
12128 break;
12129 ++lnum;
12130 }
12131
12132 /* Find interesting text in this line. */
12133 s = skipwhite(ml_get(lnum));
12134 /* skip C comment-start */
12135 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012138 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012139 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012140 {
12141 s = skipwhite(ml_get(lnum + 1));
12142 if (*s == '*')
12143 s = skipwhite(s + 1);
12144 }
12145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 txt = _("+-%s%3ld lines: ");
12147 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012148 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149 + 20 /* for %3ld */
12150 + STRLEN(s))); /* concatenated */
12151 if (r != NULL)
12152 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012153 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12154 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12155 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 len = (int)STRLEN(r);
12157 STRCAT(r, s);
12158 /* remove 'foldmarker' and 'commentstring' */
12159 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 }
12162 }
12163#endif
12164}
12165
12166/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012167 * "foldtextresult(lnum)" function
12168 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012170f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012171{
12172#ifdef FEAT_FOLDING
12173 linenr_T lnum;
12174 char_u *text;
12175 char_u buf[51];
12176 foldinfo_T foldinfo;
12177 int fold_count;
12178#endif
12179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->v_type = VAR_STRING;
12181 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012182#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012184 /* treat illegal types and illegal string values for {lnum} the same */
12185 if (lnum < 0)
12186 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012187 fold_count = foldedCount(curwin, lnum, &foldinfo);
12188 if (fold_count > 0)
12189 {
12190 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12191 &foldinfo, buf);
12192 if (text == buf)
12193 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012195 }
12196#endif
12197}
12198
12199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 * "foreground()" function
12201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012203f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205#ifdef FEAT_GUI
12206 if (gui.in_use)
12207 gui_mch_set_foreground();
12208#else
12209# ifdef WIN32
12210 win32_set_foreground();
12211# endif
12212#endif
12213}
12214
12215/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012216 * "function()" function
12217 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012219f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012220{
12221 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012222 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012223 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012224 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012225
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012226 if (argvars[0].v_type == VAR_FUNC)
12227 {
12228 /* function(MyFunc, [arg], dict) */
12229 s = argvars[0].vval.v_string;
12230 }
12231 else if (argvars[0].v_type == VAR_PARTIAL
12232 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012233 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012234 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012235 arg_pt = argvars[0].vval.v_partial;
12236 s = arg_pt->pt_name;
12237 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012238 else
12239 {
12240 /* function('MyFunc', [arg], dict) */
12241 s = get_tv_string(&argvars[0]);
12242 use_string = TRUE;
12243 }
12244
12245 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012246 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012247 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012248 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12249 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012250 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012251 else
12252 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012253 int dict_idx = 0;
12254 int arg_idx = 0;
12255 list_T *list = NULL;
12256
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012257 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012258 {
12259 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012260 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012261
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012262 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12263 * also be called from another script. Using trans_function_name()
12264 * would also work, but some plugins depend on the name being
12265 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012266 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012267 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12268 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012269 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012270 STRCPY(name, sid_buf);
12271 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012272 }
12273 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012274 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012275 name = vim_strsave(s);
12276
12277 if (argvars[1].v_type != VAR_UNKNOWN)
12278 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012279 if (argvars[2].v_type != VAR_UNKNOWN)
12280 {
12281 /* function(name, [args], dict) */
12282 arg_idx = 1;
12283 dict_idx = 2;
12284 }
12285 else if (argvars[1].v_type == VAR_DICT)
12286 /* function(name, dict) */
12287 dict_idx = 1;
12288 else
12289 /* function(name, [args]) */
12290 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012291 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012292 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012293 if (argvars[dict_idx].v_type != VAR_DICT)
12294 {
12295 EMSG(_("E922: expected a dict"));
12296 vim_free(name);
12297 return;
12298 }
12299 if (argvars[dict_idx].vval.v_dict == NULL)
12300 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012301 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012302 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012303 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012304 if (argvars[arg_idx].v_type != VAR_LIST)
12305 {
12306 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12307 vim_free(name);
12308 return;
12309 }
12310 list = argvars[arg_idx].vval.v_list;
12311 if (list == NULL || list->lv_len == 0)
12312 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012313 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012314 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012315 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012316 {
12317 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012318
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012319 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012320 if (pt == NULL)
12321 vim_free(name);
12322 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012323 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012324 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012325 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012326 listitem_T *li;
12327 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012328 int arg_len = 0;
12329 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012330
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012331 if (arg_pt != NULL)
12332 arg_len = arg_pt->pt_argc;
12333 if (list != NULL)
12334 lv_len = list->lv_len;
12335 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012336 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012337 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012338 if (pt->pt_argv == NULL)
12339 {
12340 vim_free(pt);
12341 vim_free(name);
12342 return;
12343 }
12344 else
12345 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012346 for (i = 0; i < arg_len; i++)
12347 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12348 if (lv_len > 0)
12349 for (li = list->lv_first; li != NULL;
12350 li = li->li_next)
12351 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012352 }
12353 }
12354
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012355 /* For "function(dict.func, [], dict)" and "func" is a partial
12356 * use "dict". That is backwards compatible. */
12357 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012358 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012359 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012360 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12361 ++pt->pt_dict->dv_refcount;
12362 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012363 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012364 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012365 /* If the dict was bound automatically the result is also
12366 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012367 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012368 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012369 if (pt->pt_dict != NULL)
12370 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012371 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012372
12373 pt->pt_refcount = 1;
12374 pt->pt_name = name;
12375 func_ref(pt->pt_name);
12376 }
12377 rettv->v_type = VAR_PARTIAL;
12378 rettv->vval.v_partial = pt;
12379 }
12380 else
12381 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012382 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012383 rettv->v_type = VAR_FUNC;
12384 rettv->vval.v_string = name;
12385 func_ref(name);
12386 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012387 }
12388}
12389
12390/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012391 * "garbagecollect()" function
12392 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012394f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012395{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012396 /* This is postponed until we are back at the toplevel, because we may be
12397 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12398 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012399
12400 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12401 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012402}
12403
12404/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012405 * "get()" function
12406 */
12407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012408f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012409{
Bram Moolenaar33570922005-01-25 22:26:29 +000012410 listitem_T *li;
12411 list_T *l;
12412 dictitem_T *di;
12413 dict_T *d;
12414 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012415
Bram Moolenaare9a41262005-01-15 22:18:47 +000012416 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012417 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012418 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012420 int error = FALSE;
12421
12422 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12423 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012424 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012425 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012427 else if (argvars[0].v_type == VAR_DICT)
12428 {
12429 if ((d = argvars[0].vval.v_dict) != NULL)
12430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012431 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012432 if (di != NULL)
12433 tv = &di->di_tv;
12434 }
12435 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012436 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012437 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012438 partial_T *pt;
12439 partial_T fref_pt;
12440
12441 if (argvars[0].v_type == VAR_PARTIAL)
12442 pt = argvars[0].vval.v_partial;
12443 else
12444 {
12445 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12446 fref_pt.pt_name = argvars[0].vval.v_string;
12447 pt = &fref_pt;
12448 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012449
12450 if (pt != NULL)
12451 {
12452 char_u *what = get_tv_string(&argvars[1]);
12453
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012454 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012455 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012456 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012457 if (pt->pt_name == NULL)
12458 rettv->vval.v_string = NULL;
12459 else
12460 rettv->vval.v_string = vim_strsave(pt->pt_name);
12461 }
12462 else if (STRCMP(what, "dict") == 0)
12463 {
12464 rettv->v_type = VAR_DICT;
12465 rettv->vval.v_dict = pt->pt_dict;
12466 if (pt->pt_dict != NULL)
12467 ++pt->pt_dict->dv_refcount;
12468 }
12469 else if (STRCMP(what, "args") == 0)
12470 {
12471 rettv->v_type = VAR_LIST;
12472 if (rettv_list_alloc(rettv) == OK)
12473 {
12474 int i;
12475
12476 for (i = 0; i < pt->pt_argc; ++i)
12477 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12478 }
12479 }
12480 else
12481 EMSG2(_(e_invarg2), what);
12482 return;
12483 }
12484 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012485 else
12486 EMSG2(_(e_listdictarg), "get()");
12487
12488 if (tv == NULL)
12489 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012490 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012491 copy_tv(&argvars[2], rettv);
12492 }
12493 else
12494 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495}
12496
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012497static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012498
12499/*
12500 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012501 * Return a range (from start to end) of lines in rettv from the specified
12502 * buffer.
12503 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012504 */
12505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012506get_buffer_lines(
12507 buf_T *buf,
12508 linenr_T start,
12509 linenr_T end,
12510 int retlist,
12511 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012512{
12513 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012514
Bram Moolenaar959a1432013-12-14 12:17:38 +010012515 rettv->v_type = VAR_STRING;
12516 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012517 if (retlist && rettv_list_alloc(rettv) == FAIL)
12518 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012519
12520 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12521 return;
12522
12523 if (!retlist)
12524 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012525 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12526 p = ml_get_buf(buf, start, FALSE);
12527 else
12528 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012529 rettv->vval.v_string = vim_strsave(p);
12530 }
12531 else
12532 {
12533 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012534 return;
12535
12536 if (start < 1)
12537 start = 1;
12538 if (end > buf->b_ml.ml_line_count)
12539 end = buf->b_ml.ml_line_count;
12540 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012541 if (list_append_string(rettv->vval.v_list,
12542 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012543 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012544 }
12545}
12546
12547/*
12548 * "getbufline()" function
12549 */
12550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012551f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012552{
12553 linenr_T lnum;
12554 linenr_T end;
12555 buf_T *buf;
12556
12557 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12558 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012559 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012560 --emsg_off;
12561
Bram Moolenaar661b1822005-07-28 22:36:45 +000012562 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012563 if (argvars[2].v_type == VAR_UNKNOWN)
12564 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012565 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012566 end = get_tv_lnum_buf(&argvars[2], buf);
12567
Bram Moolenaar342337a2005-07-21 21:11:17 +000012568 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012569}
12570
Bram Moolenaar0d660222005-01-07 21:51:51 +000012571/*
12572 * "getbufvar()" function
12573 */
12574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012575f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012576{
12577 buf_T *buf;
12578 buf_T *save_curbuf;
12579 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012580 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012581 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012583 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12584 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012585 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012586 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012588 rettv->v_type = VAR_STRING;
12589 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012590
12591 if (buf != NULL && varname != NULL)
12592 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012593 /* set curbuf to be our buf, temporarily */
12594 save_curbuf = curbuf;
12595 curbuf = buf;
12596
Bram Moolenaar0d660222005-01-07 21:51:51 +000012597 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012598 {
12599 if (get_option_tv(&varname, rettv, TRUE) == OK)
12600 done = TRUE;
12601 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012602 else if (STRCMP(varname, "changedtick") == 0)
12603 {
12604 rettv->v_type = VAR_NUMBER;
12605 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012606 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012607 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012608 else
12609 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012610 /* Look up the variable. */
12611 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12612 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12613 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012614 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012615 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012616 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012617 done = TRUE;
12618 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012620
12621 /* restore previous notion of curbuf */
12622 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012623 }
12624
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012625 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12626 /* use the default value */
12627 copy_tv(&argvars[2], rettv);
12628
Bram Moolenaar0d660222005-01-07 21:51:51 +000012629 --emsg_off;
12630}
12631
12632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 * "getchar()" function
12634 */
12635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012636f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637{
12638 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012639 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012641 /* Position the cursor. Needed after a message that ends in a space. */
12642 windgoto(msg_row, msg_col);
12643
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 ++no_mapping;
12645 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012646 for (;;)
12647 {
12648 if (argvars[0].v_type == VAR_UNKNOWN)
12649 /* getchar(): blocking wait. */
12650 n = safe_vgetc();
12651 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12652 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012653 n = vpeekc_any();
12654 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012655 /* illegal argument or getchar(0) and no char avail: return zero */
12656 n = 0;
12657 else
12658 /* getchar(0) and char avail: return char */
12659 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012660
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012661 if (n == K_IGNORE)
12662 continue;
12663 break;
12664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 --no_mapping;
12666 --allow_keys;
12667
Bram Moolenaar219b8702006-11-01 14:32:36 +000012668 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12669 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12670 vimvars[VV_MOUSE_COL].vv_nr = 0;
12671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012672 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673 if (IS_SPECIAL(n) || mod_mask != 0)
12674 {
12675 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12676 int i = 0;
12677
12678 /* Turn a special key into three bytes, plus modifier. */
12679 if (mod_mask != 0)
12680 {
12681 temp[i++] = K_SPECIAL;
12682 temp[i++] = KS_MODIFIER;
12683 temp[i++] = mod_mask;
12684 }
12685 if (IS_SPECIAL(n))
12686 {
12687 temp[i++] = K_SPECIAL;
12688 temp[i++] = K_SECOND(n);
12689 temp[i++] = K_THIRD(n);
12690 }
12691#ifdef FEAT_MBYTE
12692 else if (has_mbyte)
12693 i += (*mb_char2bytes)(n, temp + i);
12694#endif
12695 else
12696 temp[i++] = n;
12697 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012698 rettv->v_type = VAR_STRING;
12699 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012700
12701#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012702 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012703 {
12704 int row = mouse_row;
12705 int col = mouse_col;
12706 win_T *win;
12707 linenr_T lnum;
12708# ifdef FEAT_WINDOWS
12709 win_T *wp;
12710# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012711 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012712
12713 if (row >= 0 && col >= 0)
12714 {
12715 /* Find the window at the mouse coordinates and compute the
12716 * text position. */
12717 win = mouse_find_win(&row, &col);
12718 (void)mouse_comp_pos(win, &row, &col, &lnum);
12719# ifdef FEAT_WINDOWS
12720 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012721 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012722# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012723 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012724 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12725 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12726 }
12727 }
12728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 }
12730}
12731
12732/*
12733 * "getcharmod()" function
12734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012736f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739}
12740
12741/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012742 * "getcharsearch()" function
12743 */
12744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012745f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012746{
12747 if (rettv_dict_alloc(rettv) != FAIL)
12748 {
12749 dict_T *dict = rettv->vval.v_dict;
12750
12751 dict_add_nr_str(dict, "char", 0L, last_csearch());
12752 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12753 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12754 }
12755}
12756
12757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 * "getcmdline()" function
12759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012761f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 rettv->v_type = VAR_STRING;
12764 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765}
12766
12767/*
12768 * "getcmdpos()" function
12769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012771f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774}
12775
12776/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012777 * "getcmdtype()" function
12778 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012780f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012781{
12782 rettv->v_type = VAR_STRING;
12783 rettv->vval.v_string = alloc(2);
12784 if (rettv->vval.v_string != NULL)
12785 {
12786 rettv->vval.v_string[0] = get_cmdline_type();
12787 rettv->vval.v_string[1] = NUL;
12788 }
12789}
12790
12791/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012792 * "getcmdwintype()" function
12793 */
12794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012795f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012796{
12797 rettv->v_type = VAR_STRING;
12798 rettv->vval.v_string = NULL;
12799#ifdef FEAT_CMDWIN
12800 rettv->vval.v_string = alloc(2);
12801 if (rettv->vval.v_string != NULL)
12802 {
12803 rettv->vval.v_string[0] = cmdwin_type;
12804 rettv->vval.v_string[1] = NUL;
12805 }
12806#endif
12807}
12808
12809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 * "getcwd()" function
12811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012813f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012815 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012816 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012819 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012820
12821 wp = find_tabwin(&argvars[0], &argvars[1]);
12822 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012824 if (wp->w_localdir != NULL)
12825 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12826 else if(globaldir != NULL)
12827 rettv->vval.v_string = vim_strsave(globaldir);
12828 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012829 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012830 cwd = alloc(MAXPATHL);
12831 if (cwd != NULL)
12832 {
12833 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12834 rettv->vval.v_string = vim_strsave(cwd);
12835 vim_free(cwd);
12836 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012837 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012838#ifdef BACKSLASH_IN_FILENAME
12839 if (rettv->vval.v_string != NULL)
12840 slash_adjust(rettv->vval.v_string);
12841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 }
12843}
12844
12845/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012846 * "getfontname()" function
12847 */
12848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012849f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012850{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 rettv->v_type = VAR_STRING;
12852 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012853#ifdef FEAT_GUI
12854 if (gui.in_use)
12855 {
12856 GuiFont font;
12857 char_u *name = NULL;
12858
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012859 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012860 {
12861 /* Get the "Normal" font. Either the name saved by
12862 * hl_set_font_name() or from the font ID. */
12863 font = gui.norm_font;
12864 name = hl_get_font_name();
12865 }
12866 else
12867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012869 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12870 return;
12871 font = gui_mch_get_font(name, FALSE);
12872 if (font == NOFONT)
12873 return; /* Invalid font name, return empty string. */
12874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012876 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012877 gui_mch_free_font(font);
12878 }
12879#endif
12880}
12881
12882/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012883 * "getfperm({fname})" function
12884 */
12885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012886f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012887{
12888 char_u *fname;
12889 struct stat st;
12890 char_u *perm = NULL;
12891 char_u flags[] = "rwx";
12892 int i;
12893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012897 if (mch_stat((char *)fname, &st) >= 0)
12898 {
12899 perm = vim_strsave((char_u *)"---------");
12900 if (perm != NULL)
12901 {
12902 for (i = 0; i < 9; i++)
12903 {
12904 if (st.st_mode & (1 << (8 - i)))
12905 perm[i] = flags[i % 3];
12906 }
12907 }
12908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012910}
12911
12912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 * "getfsize({fname})" function
12914 */
12915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012916f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917{
12918 char_u *fname;
12919 struct stat st;
12920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012923 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924
12925 if (mch_stat((char *)fname, &st) >= 0)
12926 {
12927 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012932
12933 /* non-perfect check for overflow */
12934 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12935 rettv->vval.v_number = -2;
12936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 }
12938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940}
12941
12942/*
12943 * "getftime({fname})" function
12944 */
12945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012946f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947{
12948 char_u *fname;
12949 struct stat st;
12950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952
12953 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012956 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957}
12958
12959/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012960 * "getftype({fname})" function
12961 */
12962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012963f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012964{
12965 char_u *fname;
12966 struct stat st;
12967 char_u *type = NULL;
12968 char *t;
12969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012970 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012972 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012973 if (mch_lstat((char *)fname, &st) >= 0)
12974 {
12975#ifdef S_ISREG
12976 if (S_ISREG(st.st_mode))
12977 t = "file";
12978 else if (S_ISDIR(st.st_mode))
12979 t = "dir";
12980# ifdef S_ISLNK
12981 else if (S_ISLNK(st.st_mode))
12982 t = "link";
12983# endif
12984# ifdef S_ISBLK
12985 else if (S_ISBLK(st.st_mode))
12986 t = "bdev";
12987# endif
12988# ifdef S_ISCHR
12989 else if (S_ISCHR(st.st_mode))
12990 t = "cdev";
12991# endif
12992# ifdef S_ISFIFO
12993 else if (S_ISFIFO(st.st_mode))
12994 t = "fifo";
12995# endif
12996# ifdef S_ISSOCK
12997 else if (S_ISSOCK(st.st_mode))
12998 t = "fifo";
12999# endif
13000 else
13001 t = "other";
13002#else
13003# ifdef S_IFMT
13004 switch (st.st_mode & S_IFMT)
13005 {
13006 case S_IFREG: t = "file"; break;
13007 case S_IFDIR: t = "dir"; break;
13008# ifdef S_IFLNK
13009 case S_IFLNK: t = "link"; break;
13010# endif
13011# ifdef S_IFBLK
13012 case S_IFBLK: t = "bdev"; break;
13013# endif
13014# ifdef S_IFCHR
13015 case S_IFCHR: t = "cdev"; break;
13016# endif
13017# ifdef S_IFIFO
13018 case S_IFIFO: t = "fifo"; break;
13019# endif
13020# ifdef S_IFSOCK
13021 case S_IFSOCK: t = "socket"; break;
13022# endif
13023 default: t = "other";
13024 }
13025# else
13026 if (mch_isdir(fname))
13027 t = "dir";
13028 else
13029 t = "file";
13030# endif
13031#endif
13032 type = vim_strsave((char_u *)t);
13033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013034 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013035}
13036
13037/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013038 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013039 */
13040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013041f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013042{
13043 linenr_T lnum;
13044 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013045 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013046
13047 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013048 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013049 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013050 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013051 retlist = FALSE;
13052 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013053 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013054 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013055 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013056 retlist = TRUE;
13057 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013058
Bram Moolenaar342337a2005-07-21 21:11:17 +000013059 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013060}
13061
13062/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013063 * "getmatches()" function
13064 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013066f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013067{
13068#ifdef FEAT_SEARCH_EXTRA
13069 dict_T *dict;
13070 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013071 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013072
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013073 if (rettv_list_alloc(rettv) == OK)
13074 {
13075 while (cur != NULL)
13076 {
13077 dict = dict_alloc();
13078 if (dict == NULL)
13079 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013080 if (cur->match.regprog == NULL)
13081 {
13082 /* match added with matchaddpos() */
13083 for (i = 0; i < MAXPOSMATCH; ++i)
13084 {
13085 llpos_T *llpos;
13086 char buf[6];
13087 list_T *l;
13088
13089 llpos = &cur->pos.pos[i];
13090 if (llpos->lnum == 0)
13091 break;
13092 l = list_alloc();
13093 if (l == NULL)
13094 break;
13095 list_append_number(l, (varnumber_T)llpos->lnum);
13096 if (llpos->col > 0)
13097 {
13098 list_append_number(l, (varnumber_T)llpos->col);
13099 list_append_number(l, (varnumber_T)llpos->len);
13100 }
13101 sprintf(buf, "pos%d", i + 1);
13102 dict_add_list(dict, buf, l);
13103 }
13104 }
13105 else
13106 {
13107 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13108 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013109 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013110 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13111 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013112# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013113 if (cur->conceal_char)
13114 {
13115 char_u buf[MB_MAXBYTES + 1];
13116
13117 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13118 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13119 }
13120# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013121 list_append_dict(rettv->vval.v_list, dict);
13122 cur = cur->next;
13123 }
13124 }
13125#endif
13126}
13127
13128/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013129 * "getpid()" function
13130 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013132f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013133{
13134 rettv->vval.v_number = mch_get_pid();
13135}
13136
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013138getpos_both(
13139 typval_T *argvars,
13140 typval_T *rettv,
13141 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013142{
Bram Moolenaara5525202006-03-02 22:52:09 +000013143 pos_T *fp;
13144 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013145 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013146
13147 if (rettv_list_alloc(rettv) == OK)
13148 {
13149 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013150 if (getcurpos)
13151 fp = &curwin->w_cursor;
13152 else
13153 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013154 if (fnum != -1)
13155 list_append_number(l, (varnumber_T)fnum);
13156 else
13157 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013158 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13159 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013160 list_append_number(l, (fp != NULL)
13161 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013162 : (varnumber_T)0);
13163 list_append_number(l,
13164#ifdef FEAT_VIRTUALEDIT
13165 (fp != NULL) ? (varnumber_T)fp->coladd :
13166#endif
13167 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013168 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013169 {
13170 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013171 list_append_number(l, curwin->w_curswant == MAXCOL ?
13172 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013173 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013174 }
13175 else
13176 rettv->vval.v_number = FALSE;
13177}
13178
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013179
13180/*
13181 * "getcurpos()" function
13182 */
13183 static void
13184f_getcurpos(typval_T *argvars, typval_T *rettv)
13185{
13186 getpos_both(argvars, rettv, TRUE);
13187}
13188
13189/*
13190 * "getpos(string)" function
13191 */
13192 static void
13193f_getpos(typval_T *argvars, typval_T *rettv)
13194{
13195 getpos_both(argvars, rettv, FALSE);
13196}
13197
Bram Moolenaara5525202006-03-02 22:52:09 +000013198/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013199 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013200 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013202f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013203{
13204#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013205 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013206#endif
13207
Bram Moolenaar2641f772005-03-25 21:58:17 +000013208#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013209 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013210 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013211 wp = NULL;
13212 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13213 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013214 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013215 if (wp == NULL)
13216 return;
13217 }
13218
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013219 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013220 }
13221#endif
13222}
13223
13224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 * "getreg()" function
13226 */
13227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013228f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229{
13230 char_u *strregname;
13231 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013232 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013233 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013234 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013236 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013238 strregname = get_tv_string_chk(&argvars[0]);
13239 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013240 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013242 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013243 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13244 return_list = get_tv_number_chk(&argvars[2], &error);
13245 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013248 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013249
13250 if (error)
13251 return;
13252
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 regname = (strregname == NULL ? '"' : *strregname);
13254 if (regname == 0)
13255 regname = '"';
13256
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013257 if (return_list)
13258 {
13259 rettv->v_type = VAR_LIST;
13260 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13261 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013262 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013263 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013264 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013265 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013266 }
13267 else
13268 {
13269 rettv->v_type = VAR_STRING;
13270 rettv->vval.v_string = get_reg_contents(regname,
13271 arg2 ? GREG_EXPR_SRC : 0);
13272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273}
13274
13275/*
13276 * "getregtype()" function
13277 */
13278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013279f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280{
13281 char_u *strregname;
13282 int regname;
13283 char_u buf[NUMBUFLEN + 2];
13284 long reglen = 0;
13285
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013286 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 {
13288 strregname = get_tv_string_chk(&argvars[0]);
13289 if (strregname == NULL) /* type error; errmsg already given */
13290 {
13291 rettv->v_type = VAR_STRING;
13292 rettv->vval.v_string = NULL;
13293 return;
13294 }
13295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 else
13297 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013298 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299
13300 regname = (strregname == NULL ? '"' : *strregname);
13301 if (regname == 0)
13302 regname = '"';
13303
13304 buf[0] = NUL;
13305 buf[1] = NUL;
13306 switch (get_reg_type(regname, &reglen))
13307 {
13308 case MLINE: buf[0] = 'V'; break;
13309 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310 case MBLOCK:
13311 buf[0] = Ctrl_V;
13312 sprintf((char *)buf + 1, "%ld", reglen + 1);
13313 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315 rettv->v_type = VAR_STRING;
13316 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317}
13318
13319/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013320 * "gettabvar()" function
13321 */
13322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013323f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013324{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013325 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013326 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013327 dictitem_T *v;
13328 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013329 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013330
13331 rettv->v_type = VAR_STRING;
13332 rettv->vval.v_string = NULL;
13333
13334 varname = get_tv_string_chk(&argvars[1]);
13335 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13336 if (tp != NULL && varname != NULL)
13337 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013338 /* Set tp to be our tabpage, temporarily. Also set the window to the
13339 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013340 if (switch_win(&oldcurwin, &oldtabpage,
13341 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013342 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013343 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013344 /* look up the variable */
13345 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13346 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13347 if (v != NULL)
13348 {
13349 copy_tv(&v->di_tv, rettv);
13350 done = TRUE;
13351 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013352 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013353
13354 /* restore previous notion of curwin */
13355 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013356 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013357
13358 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013359 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013360}
13361
13362/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013363 * "gettabwinvar()" function
13364 */
13365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013366f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013367{
13368 getwinvar(argvars, rettv, 1);
13369}
13370
13371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 * "getwinposx()" function
13373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013375f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378#ifdef FEAT_GUI
13379 if (gui.in_use)
13380 {
13381 int x, y;
13382
13383 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013384 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 }
13386#endif
13387}
13388
13389/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013390 * "win_findbuf()" function
13391 */
13392 static void
13393f_win_findbuf(typval_T *argvars, typval_T *rettv)
13394{
13395 if (rettv_list_alloc(rettv) != FAIL)
13396 win_findbuf(argvars, rettv->vval.v_list);
13397}
13398
13399/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013400 * "win_getid()" function
13401 */
13402 static void
13403f_win_getid(typval_T *argvars, typval_T *rettv)
13404{
13405 rettv->vval.v_number = win_getid(argvars);
13406}
13407
13408/*
13409 * "win_gotoid()" function
13410 */
13411 static void
13412f_win_gotoid(typval_T *argvars, typval_T *rettv)
13413{
13414 rettv->vval.v_number = win_gotoid(argvars);
13415}
13416
13417/*
13418 * "win_id2tabwin()" function
13419 */
13420 static void
13421f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13422{
13423 if (rettv_list_alloc(rettv) != FAIL)
13424 win_id2tabwin(argvars, rettv->vval.v_list);
13425}
13426
13427/*
13428 * "win_id2win()" function
13429 */
13430 static void
13431f_win_id2win(typval_T *argvars, typval_T *rettv)
13432{
13433 rettv->vval.v_number = win_id2win(argvars);
13434}
13435
13436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 * "getwinposy()" function
13438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013440f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013442 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443#ifdef FEAT_GUI
13444 if (gui.in_use)
13445 {
13446 int x, y;
13447
13448 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 }
13451#endif
13452}
13453
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013454/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013455 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013456 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013457 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013458find_win_by_nr(
13459 typval_T *vp,
13460 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013461{
13462#ifdef FEAT_WINDOWS
13463 win_T *wp;
13464#endif
13465 int nr;
13466
13467 nr = get_tv_number_chk(vp, NULL);
13468
13469#ifdef FEAT_WINDOWS
13470 if (nr < 0)
13471 return NULL;
13472 if (nr == 0)
13473 return curwin;
13474
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013475 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13476 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013477 if (--nr <= 0)
13478 break;
13479 return wp;
13480#else
13481 if (nr == 0 || nr == 1)
13482 return curwin;
13483 return NULL;
13484#endif
13485}
13486
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013488 * Find window specified by "wvp" in tabpage "tvp".
13489 */
13490 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013491find_tabwin(
13492 typval_T *wvp, /* VAR_UNKNOWN for current window */
13493 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013494{
13495 win_T *wp = NULL;
13496 tabpage_T *tp = NULL;
13497 long n;
13498
13499 if (wvp->v_type != VAR_UNKNOWN)
13500 {
13501 if (tvp->v_type != VAR_UNKNOWN)
13502 {
13503 n = get_tv_number(tvp);
13504 if (n >= 0)
13505 tp = find_tabpage(n);
13506 }
13507 else
13508 tp = curtab;
13509
13510 if (tp != NULL)
13511 wp = find_win_by_nr(wvp, tp);
13512 }
13513 else
13514 wp = curwin;
13515
13516 return wp;
13517}
13518
13519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 * "getwinvar()" function
13521 */
13522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013523f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013525 getwinvar(argvars, rettv, 0);
13526}
13527
13528/*
13529 * getwinvar() and gettabwinvar()
13530 */
13531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013532getwinvar(
13533 typval_T *argvars,
13534 typval_T *rettv,
13535 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013536{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013537 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013539 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013540 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013541 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013542#ifdef FEAT_WINDOWS
13543 win_T *oldcurwin;
13544 tabpage_T *oldtabpage;
13545 int need_switch_win;
13546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013548#ifdef FEAT_WINDOWS
13549 if (off == 1)
13550 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13551 else
13552 tp = curtab;
13553#endif
13554 win = find_win_by_nr(&argvars[off], tp);
13555 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013556 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013558 rettv->v_type = VAR_STRING;
13559 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560
13561 if (win != NULL && varname != NULL)
13562 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013563#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013564 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013565 * otherwise the window is not valid. Only do this when needed,
13566 * autocommands get blocked. */
13567 need_switch_win = !(tp == curtab && win == curwin);
13568 if (!need_switch_win
13569 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13570#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013571 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013572 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013573 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013574 if (get_option_tv(&varname, rettv, 1) == OK)
13575 done = TRUE;
13576 }
13577 else
13578 {
13579 /* Look up the variable. */
13580 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13581 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13582 varname, FALSE);
13583 if (v != NULL)
13584 {
13585 copy_tv(&v->di_tv, rettv);
13586 done = TRUE;
13587 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013590
Bram Moolenaarba117c22015-09-29 16:53:22 +020013591#ifdef FEAT_WINDOWS
13592 if (need_switch_win)
13593 /* restore previous notion of curwin */
13594 restore_win(oldcurwin, oldtabpage, TRUE);
13595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 }
13597
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013598 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13599 /* use the default return value */
13600 copy_tv(&argvars[off + 2], rettv);
13601
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 --emsg_off;
13603}
13604
13605/*
13606 * "glob()" function
13607 */
13608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013609f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013611 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013613 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013615 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013616 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013618 if (argvars[1].v_type != VAR_UNKNOWN)
13619 {
13620 if (get_tv_number_chk(&argvars[1], &error))
13621 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013622 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013623 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013624 if (get_tv_number_chk(&argvars[2], &error))
13625 {
13626 rettv->v_type = VAR_LIST;
13627 rettv->vval.v_list = NULL;
13628 }
13629 if (argvars[3].v_type != VAR_UNKNOWN
13630 && get_tv_number_chk(&argvars[3], &error))
13631 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013632 }
13633 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013634 if (!error)
13635 {
13636 ExpandInit(&xpc);
13637 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013638 if (p_wic)
13639 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013640 if (rettv->v_type == VAR_STRING)
13641 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013642 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013643 else if (rettv_list_alloc(rettv) != FAIL)
13644 {
13645 int i;
13646
13647 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13648 NULL, options, WILD_ALL_KEEP);
13649 for (i = 0; i < xpc.xp_numfiles; i++)
13650 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13651
13652 ExpandCleanup(&xpc);
13653 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013654 }
13655 else
13656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657}
13658
13659/*
13660 * "globpath()" function
13661 */
13662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013663f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013665 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013668 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013669 garray_T ga;
13670 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013672 /* When the optional second argument is non-zero, don't remove matches
13673 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013675 if (argvars[2].v_type != VAR_UNKNOWN)
13676 {
13677 if (get_tv_number_chk(&argvars[2], &error))
13678 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013679 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013680 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013681 if (get_tv_number_chk(&argvars[3], &error))
13682 {
13683 rettv->v_type = VAR_LIST;
13684 rettv->vval.v_list = NULL;
13685 }
13686 if (argvars[4].v_type != VAR_UNKNOWN
13687 && get_tv_number_chk(&argvars[4], &error))
13688 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013689 }
13690 }
13691 if (file != NULL && !error)
13692 {
13693 ga_init2(&ga, (int)sizeof(char_u *), 10);
13694 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13695 if (rettv->v_type == VAR_STRING)
13696 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13697 else if (rettv_list_alloc(rettv) != FAIL)
13698 for (i = 0; i < ga.ga_len; ++i)
13699 list_append_string(rettv->vval.v_list,
13700 ((char_u **)(ga.ga_data))[i], -1);
13701 ga_clear_strings(&ga);
13702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013703 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013704 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705}
13706
13707/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013708 * "glob2regpat()" function
13709 */
13710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013711f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013712{
13713 char_u *pat = get_tv_string_chk(&argvars[0]);
13714
13715 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013716 rettv->vval.v_string = (pat == NULL)
13717 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013718}
13719
13720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 * "has()" function
13722 */
13723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013724f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725{
13726 int i;
13727 char_u *name;
13728 int n = FALSE;
13729 static char *(has_list[]) =
13730 {
13731#ifdef AMIGA
13732 "amiga",
13733# ifdef FEAT_ARP
13734 "arp",
13735# endif
13736#endif
13737#ifdef __BEOS__
13738 "beos",
13739#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013740#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 "mac",
13742#endif
13743#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013744 "macunix", /* built with 'darwin' enabled */
13745#endif
13746#if defined(__APPLE__) && __APPLE__ == 1
13747 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#ifdef __QNX__
13750 "qnx",
13751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752#ifdef UNIX
13753 "unix",
13754#endif
13755#ifdef VMS
13756 "vms",
13757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758#ifdef WIN32
13759 "win32",
13760#endif
13761#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13762 "win32unix",
13763#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013764#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 "win64",
13766#endif
13767#ifdef EBCDIC
13768 "ebcdic",
13769#endif
13770#ifndef CASE_INSENSITIVE_FILENAME
13771 "fname_case",
13772#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013773#ifdef HAVE_ACL
13774 "acl",
13775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776#ifdef FEAT_ARABIC
13777 "arabic",
13778#endif
13779#ifdef FEAT_AUTOCMD
13780 "autocmd",
13781#endif
13782#ifdef FEAT_BEVAL
13783 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013784# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13785 "balloon_multiline",
13786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787#endif
13788#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13789 "builtin_terms",
13790# ifdef ALL_BUILTIN_TCAPS
13791 "all_builtin_terms",
13792# endif
13793#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013794#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13795 || defined(FEAT_GUI_W32) \
13796 || defined(FEAT_GUI_MOTIF))
13797 "browsefilter",
13798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799#ifdef FEAT_BYTEOFF
13800 "byte_offset",
13801#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013802#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013803 "channel",
13804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805#ifdef FEAT_CINDENT
13806 "cindent",
13807#endif
13808#ifdef FEAT_CLIENTSERVER
13809 "clientserver",
13810#endif
13811#ifdef FEAT_CLIPBOARD
13812 "clipboard",
13813#endif
13814#ifdef FEAT_CMDL_COMPL
13815 "cmdline_compl",
13816#endif
13817#ifdef FEAT_CMDHIST
13818 "cmdline_hist",
13819#endif
13820#ifdef FEAT_COMMENTS
13821 "comments",
13822#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013823#ifdef FEAT_CONCEAL
13824 "conceal",
13825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826#ifdef FEAT_CRYPT
13827 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013828 "crypt-blowfish",
13829 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830#endif
13831#ifdef FEAT_CSCOPE
13832 "cscope",
13833#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013834#ifdef FEAT_CURSORBIND
13835 "cursorbind",
13836#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013837#ifdef CURSOR_SHAPE
13838 "cursorshape",
13839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840#ifdef DEBUG
13841 "debug",
13842#endif
13843#ifdef FEAT_CON_DIALOG
13844 "dialog_con",
13845#endif
13846#ifdef FEAT_GUI_DIALOG
13847 "dialog_gui",
13848#endif
13849#ifdef FEAT_DIFF
13850 "diff",
13851#endif
13852#ifdef FEAT_DIGRAPHS
13853 "digraphs",
13854#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013855#ifdef FEAT_DIRECTX
13856 "directx",
13857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858#ifdef FEAT_DND
13859 "dnd",
13860#endif
13861#ifdef FEAT_EMACS_TAGS
13862 "emacs_tags",
13863#endif
13864 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013865 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866#ifdef FEAT_SEARCH_EXTRA
13867 "extra_search",
13868#endif
13869#ifdef FEAT_FKMAP
13870 "farsi",
13871#endif
13872#ifdef FEAT_SEARCHPATH
13873 "file_in_path",
13874#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013875#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013876 "filterpipe",
13877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878#ifdef FEAT_FIND_ID
13879 "find_in_path",
13880#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013881#ifdef FEAT_FLOAT
13882 "float",
13883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884#ifdef FEAT_FOLDING
13885 "folding",
13886#endif
13887#ifdef FEAT_FOOTER
13888 "footer",
13889#endif
13890#if !defined(USE_SYSTEM) && defined(UNIX)
13891 "fork",
13892#endif
13893#ifdef FEAT_GETTEXT
13894 "gettext",
13895#endif
13896#ifdef FEAT_GUI
13897 "gui",
13898#endif
13899#ifdef FEAT_GUI_ATHENA
13900# ifdef FEAT_GUI_NEXTAW
13901 "gui_neXtaw",
13902# else
13903 "gui_athena",
13904# endif
13905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906#ifdef FEAT_GUI_GTK
13907 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013908# ifdef USE_GTK3
13909 "gui_gtk3",
13910# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013914#ifdef FEAT_GUI_GNOME
13915 "gui_gnome",
13916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917#ifdef FEAT_GUI_MAC
13918 "gui_mac",
13919#endif
13920#ifdef FEAT_GUI_MOTIF
13921 "gui_motif",
13922#endif
13923#ifdef FEAT_GUI_PHOTON
13924 "gui_photon",
13925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926#ifdef FEAT_GUI_W32
13927 "gui_win32",
13928#endif
13929#ifdef FEAT_HANGULIN
13930 "hangul_input",
13931#endif
13932#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13933 "iconv",
13934#endif
13935#ifdef FEAT_INS_EXPAND
13936 "insert_expand",
13937#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013938#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013939 "job",
13940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941#ifdef FEAT_JUMPLIST
13942 "jumplist",
13943#endif
13944#ifdef FEAT_KEYMAP
13945 "keymap",
13946#endif
13947#ifdef FEAT_LANGMAP
13948 "langmap",
13949#endif
13950#ifdef FEAT_LIBCALL
13951 "libcall",
13952#endif
13953#ifdef FEAT_LINEBREAK
13954 "linebreak",
13955#endif
13956#ifdef FEAT_LISP
13957 "lispindent",
13958#endif
13959#ifdef FEAT_LISTCMDS
13960 "listcmds",
13961#endif
13962#ifdef FEAT_LOCALMAP
13963 "localmap",
13964#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013965#ifdef FEAT_LUA
13966# ifndef DYNAMIC_LUA
13967 "lua",
13968# endif
13969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970#ifdef FEAT_MENU
13971 "menu",
13972#endif
13973#ifdef FEAT_SESSION
13974 "mksession",
13975#endif
13976#ifdef FEAT_MODIFY_FNAME
13977 "modify_fname",
13978#endif
13979#ifdef FEAT_MOUSE
13980 "mouse",
13981#endif
13982#ifdef FEAT_MOUSESHAPE
13983 "mouseshape",
13984#endif
13985#if defined(UNIX) || defined(VMS)
13986# ifdef FEAT_MOUSE_DEC
13987 "mouse_dec",
13988# endif
13989# ifdef FEAT_MOUSE_GPM
13990 "mouse_gpm",
13991# endif
13992# ifdef FEAT_MOUSE_JSB
13993 "mouse_jsbterm",
13994# endif
13995# ifdef FEAT_MOUSE_NET
13996 "mouse_netterm",
13997# endif
13998# ifdef FEAT_MOUSE_PTERM
13999 "mouse_pterm",
14000# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014001# ifdef FEAT_MOUSE_SGR
14002 "mouse_sgr",
14003# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014004# ifdef FEAT_SYSMOUSE
14005 "mouse_sysmouse",
14006# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014007# ifdef FEAT_MOUSE_URXVT
14008 "mouse_urxvt",
14009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010# ifdef FEAT_MOUSE_XTERM
14011 "mouse_xterm",
14012# endif
14013#endif
14014#ifdef FEAT_MBYTE
14015 "multi_byte",
14016#endif
14017#ifdef FEAT_MBYTE_IME
14018 "multi_byte_ime",
14019#endif
14020#ifdef FEAT_MULTI_LANG
14021 "multi_lang",
14022#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014023#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014024#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014025 "mzscheme",
14026#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028#ifdef FEAT_OLE
14029 "ole",
14030#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014031 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032#ifdef FEAT_PATH_EXTRA
14033 "path_extra",
14034#endif
14035#ifdef FEAT_PERL
14036#ifndef DYNAMIC_PERL
14037 "perl",
14038#endif
14039#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014040#ifdef FEAT_PERSISTENT_UNDO
14041 "persistent_undo",
14042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043#ifdef FEAT_PYTHON
14044#ifndef DYNAMIC_PYTHON
14045 "python",
14046#endif
14047#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014048#ifdef FEAT_PYTHON3
14049#ifndef DYNAMIC_PYTHON3
14050 "python3",
14051#endif
14052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053#ifdef FEAT_POSTSCRIPT
14054 "postscript",
14055#endif
14056#ifdef FEAT_PRINTER
14057 "printer",
14058#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014059#ifdef FEAT_PROFILE
14060 "profile",
14061#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014062#ifdef FEAT_RELTIME
14063 "reltime",
14064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065#ifdef FEAT_QUICKFIX
14066 "quickfix",
14067#endif
14068#ifdef FEAT_RIGHTLEFT
14069 "rightleft",
14070#endif
14071#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14072 "ruby",
14073#endif
14074#ifdef FEAT_SCROLLBIND
14075 "scrollbind",
14076#endif
14077#ifdef FEAT_CMDL_INFO
14078 "showcmd",
14079 "cmdline_info",
14080#endif
14081#ifdef FEAT_SIGNS
14082 "signs",
14083#endif
14084#ifdef FEAT_SMARTINDENT
14085 "smartindent",
14086#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014087#ifdef STARTUPTIME
14088 "startuptime",
14089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090#ifdef FEAT_STL_OPT
14091 "statusline",
14092#endif
14093#ifdef FEAT_SUN_WORKSHOP
14094 "sun_workshop",
14095#endif
14096#ifdef FEAT_NETBEANS_INTG
14097 "netbeans_intg",
14098#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014099#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014100 "spell",
14101#endif
14102#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 "syntax",
14104#endif
14105#if defined(USE_SYSTEM) || !defined(UNIX)
14106 "system",
14107#endif
14108#ifdef FEAT_TAG_BINS
14109 "tag_binary",
14110#endif
14111#ifdef FEAT_TAG_OLDSTATIC
14112 "tag_old_static",
14113#endif
14114#ifdef FEAT_TAG_ANYWHITE
14115 "tag_any_white",
14116#endif
14117#ifdef FEAT_TCL
14118# ifndef DYNAMIC_TCL
14119 "tcl",
14120# endif
14121#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014122#ifdef FEAT_TERMGUICOLORS
14123 "termguicolors",
14124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125#ifdef TERMINFO
14126 "terminfo",
14127#endif
14128#ifdef FEAT_TERMRESPONSE
14129 "termresponse",
14130#endif
14131#ifdef FEAT_TEXTOBJ
14132 "textobjects",
14133#endif
14134#ifdef HAVE_TGETENT
14135 "tgetent",
14136#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014137#ifdef FEAT_TIMERS
14138 "timers",
14139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140#ifdef FEAT_TITLE
14141 "title",
14142#endif
14143#ifdef FEAT_TOOLBAR
14144 "toolbar",
14145#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014146#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14147 "unnamedplus",
14148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149#ifdef FEAT_USR_CMDS
14150 "user-commands", /* was accidentally included in 5.4 */
14151 "user_commands",
14152#endif
14153#ifdef FEAT_VIMINFO
14154 "viminfo",
14155#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014156#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 "vertsplit",
14158#endif
14159#ifdef FEAT_VIRTUALEDIT
14160 "virtualedit",
14161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163#ifdef FEAT_VISUALEXTRA
14164 "visualextra",
14165#endif
14166#ifdef FEAT_VREPLACE
14167 "vreplace",
14168#endif
14169#ifdef FEAT_WILDIGN
14170 "wildignore",
14171#endif
14172#ifdef FEAT_WILDMENU
14173 "wildmenu",
14174#endif
14175#ifdef FEAT_WINDOWS
14176 "windows",
14177#endif
14178#ifdef FEAT_WAK
14179 "winaltkeys",
14180#endif
14181#ifdef FEAT_WRITEBACKUP
14182 "writebackup",
14183#endif
14184#ifdef FEAT_XIM
14185 "xim",
14186#endif
14187#ifdef FEAT_XFONTSET
14188 "xfontset",
14189#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014190#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014191 "xpm",
14192 "xpm_w32", /* for backward compatibility */
14193#else
14194# if defined(HAVE_XPM)
14195 "xpm",
14196# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198#ifdef USE_XSMP
14199 "xsmp",
14200#endif
14201#ifdef USE_XSMP_INTERACT
14202 "xsmp_interact",
14203#endif
14204#ifdef FEAT_XCLIPBOARD
14205 "xterm_clipboard",
14206#endif
14207#ifdef FEAT_XTERM_SAVE
14208 "xterm_save",
14209#endif
14210#if defined(UNIX) && defined(FEAT_X11)
14211 "X11",
14212#endif
14213 NULL
14214 };
14215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014216 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 for (i = 0; has_list[i] != NULL; ++i)
14218 if (STRICMP(name, has_list[i]) == 0)
14219 {
14220 n = TRUE;
14221 break;
14222 }
14223
14224 if (n == FALSE)
14225 {
14226 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014227 {
14228 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014229 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014230 && vim_isdigit(name[6])
14231 && vim_isdigit(name[8])
14232 && vim_isdigit(name[10]))
14233 {
14234 int major = atoi((char *)name + 6);
14235 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014236
14237 /* Expect "patch-9.9.01234". */
14238 n = (major < VIM_VERSION_MAJOR
14239 || (major == VIM_VERSION_MAJOR
14240 && (minor < VIM_VERSION_MINOR
14241 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014242 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014243 }
14244 else
14245 n = has_patch(atoi((char *)name + 5));
14246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 else if (STRICMP(name, "vim_starting") == 0)
14248 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014249#ifdef FEAT_MBYTE
14250 else if (STRICMP(name, "multi_byte_encoding") == 0)
14251 n = has_mbyte;
14252#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014253#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14254 else if (STRICMP(name, "balloon_multiline") == 0)
14255 n = multiline_balloon_available();
14256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257#ifdef DYNAMIC_TCL
14258 else if (STRICMP(name, "tcl") == 0)
14259 n = tcl_enabled(FALSE);
14260#endif
14261#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14262 else if (STRICMP(name, "iconv") == 0)
14263 n = iconv_enabled(FALSE);
14264#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014265#ifdef DYNAMIC_LUA
14266 else if (STRICMP(name, "lua") == 0)
14267 n = lua_enabled(FALSE);
14268#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014269#ifdef DYNAMIC_MZSCHEME
14270 else if (STRICMP(name, "mzscheme") == 0)
14271 n = mzscheme_enabled(FALSE);
14272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273#ifdef DYNAMIC_RUBY
14274 else if (STRICMP(name, "ruby") == 0)
14275 n = ruby_enabled(FALSE);
14276#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014277#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278#ifdef DYNAMIC_PYTHON
14279 else if (STRICMP(name, "python") == 0)
14280 n = python_enabled(FALSE);
14281#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014282#endif
14283#ifdef FEAT_PYTHON3
14284#ifdef DYNAMIC_PYTHON3
14285 else if (STRICMP(name, "python3") == 0)
14286 n = python3_enabled(FALSE);
14287#endif
14288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289#ifdef DYNAMIC_PERL
14290 else if (STRICMP(name, "perl") == 0)
14291 n = perl_enabled(FALSE);
14292#endif
14293#ifdef FEAT_GUI
14294 else if (STRICMP(name, "gui_running") == 0)
14295 n = (gui.in_use || gui.starting);
14296# ifdef FEAT_GUI_W32
14297 else if (STRICMP(name, "gui_win32s") == 0)
14298 n = gui_is_win32s();
14299# endif
14300# ifdef FEAT_BROWSE
14301 else if (STRICMP(name, "browse") == 0)
14302 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14303# endif
14304#endif
14305#ifdef FEAT_SYN_HL
14306 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014307 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308#endif
14309#if defined(WIN3264)
14310 else if (STRICMP(name, "win95") == 0)
14311 n = mch_windows95();
14312#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014313#ifdef FEAT_NETBEANS_INTG
14314 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014315 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 }
14318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014319 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320}
14321
14322/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014323 * "has_key()" function
14324 */
14325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014326f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014327{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014328 if (argvars[0].v_type != VAR_DICT)
14329 {
14330 EMSG(_(e_dictreq));
14331 return;
14332 }
14333 if (argvars[0].vval.v_dict == NULL)
14334 return;
14335
14336 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014337 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014338}
14339
14340/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014341 * "haslocaldir()" function
14342 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014344f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014345{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014346 win_T *wp = NULL;
14347
14348 wp = find_tabwin(&argvars[0], &argvars[1]);
14349 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014350}
14351
14352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 * "hasmapto()" function
14354 */
14355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014356f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357{
14358 char_u *name;
14359 char_u *mode;
14360 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014361 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014364 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 mode = (char_u *)"nvo";
14366 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014369 if (argvars[2].v_type != VAR_UNKNOWN)
14370 abbr = get_tv_number(&argvars[2]);
14371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372
Bram Moolenaar2c932302006-03-18 21:42:09 +000014373 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377}
14378
14379/*
14380 * "histadd()" function
14381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014383f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384{
14385#ifdef FEAT_CMDHIST
14386 int histype;
14387 char_u *str;
14388 char_u buf[NUMBUFLEN];
14389#endif
14390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 if (check_restricted() || check_secure())
14393 return;
14394#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014395 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14396 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 if (histype >= 0)
14398 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 if (*str != NUL)
14401 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014402 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014404 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 return;
14406 }
14407 }
14408#endif
14409}
14410
14411/*
14412 * "histdel()" function
14413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014415f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416{
14417#ifdef FEAT_CMDHIST
14418 int n;
14419 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14423 if (str == NULL)
14424 n = 0;
14425 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014427 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014428 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014430 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014431 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 else
14433 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014434 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014435 get_tv_string_buf(&argvars[1], buf));
14436 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437#endif
14438}
14439
14440/*
14441 * "histget()" function
14442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014444f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445{
14446#ifdef FEAT_CMDHIST
14447 int type;
14448 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14452 if (str == NULL)
14453 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014455 {
14456 type = get_histtype(str);
14457 if (argvars[1].v_type == VAR_UNKNOWN)
14458 idx = get_history_idx(type);
14459 else
14460 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14461 /* -1 on type error */
14462 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468}
14469
14470/*
14471 * "histnr()" function
14472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014474f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475{
14476 int i;
14477
14478#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014479 char_u *history = get_tv_string_chk(&argvars[0]);
14480
14481 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 if (i >= HIST_CMD && i < HIST_COUNT)
14483 i = get_history_idx(i);
14484 else
14485#endif
14486 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014487 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488}
14489
14490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 * "highlightID(name)" function
14492 */
14493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014494f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497}
14498
14499/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014500 * "highlight_exists()" function
14501 */
14502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014503f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014504{
14505 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14506}
14507
14508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 * "hostname()" function
14510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014512f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513{
14514 char_u hostname[256];
14515
14516 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517 rettv->v_type = VAR_STRING;
14518 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519}
14520
14521/*
14522 * iconv() function
14523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014525f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526{
14527#ifdef FEAT_MBYTE
14528 char_u buf1[NUMBUFLEN];
14529 char_u buf2[NUMBUFLEN];
14530 char_u *from, *to, *str;
14531 vimconv_T vimconv;
14532#endif
14533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534 rettv->v_type = VAR_STRING;
14535 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536
14537#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014538 str = get_tv_string(&argvars[0]);
14539 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14540 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 vimconv.vc_type = CONV_NONE;
14542 convert_setup(&vimconv, from, to);
14543
14544 /* If the encodings are equal, no conversion needed. */
14545 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014548 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549
14550 convert_setup(&vimconv, NULL, NULL);
14551 vim_free(from);
14552 vim_free(to);
14553#endif
14554}
14555
14556/*
14557 * "indent()" function
14558 */
14559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014560f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561{
14562 linenr_T lnum;
14563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014564 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014568 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569}
14570
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014571/*
14572 * "index()" function
14573 */
14574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014575f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014576{
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 list_T *l;
14578 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014579 long idx = 0;
14580 int ic = FALSE;
14581
14582 rettv->vval.v_number = -1;
14583 if (argvars[0].v_type != VAR_LIST)
14584 {
14585 EMSG(_(e_listreq));
14586 return;
14587 }
14588 l = argvars[0].vval.v_list;
14589 if (l != NULL)
14590 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014591 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014592 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014594 int error = FALSE;
14595
Bram Moolenaar758711c2005-02-02 23:11:38 +000014596 /* Start at specified item. Use the cached index that list_find()
14597 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014599 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014600 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014601 ic = get_tv_number_chk(&argvars[3], &error);
14602 if (error)
14603 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014604 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014605
Bram Moolenaar758711c2005-02-02 23:11:38 +000014606 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014607 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014608 {
14609 rettv->vval.v_number = idx;
14610 break;
14611 }
14612 }
14613}
14614
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615static int inputsecret_flag = 0;
14616
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014617static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014618
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014620 * This function is used by f_input() and f_inputdialog() functions. The third
14621 * argument to f_input() specifies the type of completion to use at the
14622 * prompt. The third argument to f_inputdialog() specifies the value to return
14623 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 */
14625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014626get_user_input(
14627 typval_T *argvars,
14628 typval_T *rettv,
14629 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014631 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 char_u *p = NULL;
14633 int c;
14634 char_u buf[NUMBUFLEN];
14635 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014637 int xp_type = EXPAND_NOTHING;
14638 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014640 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014641 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642
14643#ifdef NO_CONSOLE_INPUT
14644 /* While starting up, there is no place to enter text. */
14645 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647#endif
14648
14649 cmd_silent = FALSE; /* Want to see the prompt. */
14650 if (prompt != NULL)
14651 {
14652 /* Only the part of the message after the last NL is considered as
14653 * prompt for the command line */
14654 p = vim_strrchr(prompt, '\n');
14655 if (p == NULL)
14656 p = prompt;
14657 else
14658 {
14659 ++p;
14660 c = *p;
14661 *p = NUL;
14662 msg_start();
14663 msg_clr_eos();
14664 msg_puts_attr(prompt, echo_attr);
14665 msg_didout = FALSE;
14666 msg_starthere();
14667 *p = c;
14668 }
14669 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 if (argvars[1].v_type != VAR_UNKNOWN)
14672 {
14673 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14674 if (defstr != NULL)
14675 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014677 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014678 {
14679 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014680 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014681 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014682
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014683 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014684 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014685
Bram Moolenaar4463f292005-09-25 22:20:24 +000014686 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14687 if (xp_name == NULL)
14688 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014689
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014690 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014691
Bram Moolenaar4463f292005-09-25 22:20:24 +000014692 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14693 &xp_arg) == FAIL)
14694 return;
14695 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014696 }
14697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014698 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014699 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014700 int save_ex_normal_busy = ex_normal_busy;
14701 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014703 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14704 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014705 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014706 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014707 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014708 && argvars[1].v_type != VAR_UNKNOWN
14709 && argvars[2].v_type != VAR_UNKNOWN)
14710 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14711 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014712
14713 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014715 /* since the user typed this, no need to wait for return */
14716 need_wait_return = FALSE;
14717 msg_didout = FALSE;
14718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719 cmd_silent = cmd_silent_save;
14720}
14721
14722/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014723 * "input()" function
14724 * Also handles inputsecret() when inputsecret is set.
14725 */
14726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014727f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014728{
14729 get_user_input(argvars, rettv, FALSE);
14730}
14731
14732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 * "inputdialog()" function
14734 */
14735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014736f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737{
14738#if defined(FEAT_GUI_TEXTDIALOG)
14739 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14740 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14741 {
14742 char_u *message;
14743 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014746 message = get_tv_string_chk(&argvars[0]);
14747 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014748 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014749 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 else
14751 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014752 if (message != NULL && defstr != NULL
14753 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014754 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 else
14757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014758 if (message != NULL && defstr != NULL
14759 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014760 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014761 rettv->vval.v_string = vim_strsave(
14762 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014766 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 }
14768 else
14769#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014770 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771}
14772
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014773/*
14774 * "inputlist()" function
14775 */
14776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014777f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014778{
14779 listitem_T *li;
14780 int selected;
14781 int mouse_used;
14782
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014783#ifdef NO_CONSOLE_INPUT
14784 /* While starting up, there is no place to enter text. */
14785 if (no_console_input())
14786 return;
14787#endif
14788 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14789 {
14790 EMSG2(_(e_listarg), "inputlist()");
14791 return;
14792 }
14793
14794 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014795 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014796 lines_left = Rows; /* avoid more prompt */
14797 msg_scroll = TRUE;
14798 msg_clr_eos();
14799
14800 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14801 {
14802 msg_puts(get_tv_string(&li->li_tv));
14803 msg_putchar('\n');
14804 }
14805
14806 /* Ask for choice. */
14807 selected = prompt_for_number(&mouse_used);
14808 if (mouse_used)
14809 selected -= lines_left;
14810
14811 rettv->vval.v_number = selected;
14812}
14813
14814
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14816
14817/*
14818 * "inputrestore()" function
14819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014821f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822{
14823 if (ga_userinput.ga_len > 0)
14824 {
14825 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14827 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014828 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829 }
14830 else if (p_verbose > 1)
14831 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014832 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 }
14835}
14836
14837/*
14838 * "inputsave()" function
14839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014841f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014843 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 if (ga_grow(&ga_userinput, 1) == OK)
14845 {
14846 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14847 + ga_userinput.ga_len);
14848 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014849 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850 }
14851 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014852 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853}
14854
14855/*
14856 * "inputsecret()" function
14857 */
14858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014859f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860{
14861 ++cmdline_star;
14862 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014863 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 --cmdline_star;
14865 --inputsecret_flag;
14866}
14867
14868/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014869 * "insert()" function
14870 */
14871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014872f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014873{
14874 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014875 listitem_T *item;
14876 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014877 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014878
14879 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014880 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014881 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014882 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014883 {
14884 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014885 before = get_tv_number_chk(&argvars[2], &error);
14886 if (error)
14887 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014888
Bram Moolenaar758711c2005-02-02 23:11:38 +000014889 if (before == l->lv_len)
14890 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014891 else
14892 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014893 item = list_find(l, before);
14894 if (item == NULL)
14895 {
14896 EMSGN(_(e_listidx), before);
14897 l = NULL;
14898 }
14899 }
14900 if (l != NULL)
14901 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014902 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014903 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014904 }
14905 }
14906}
14907
14908/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014909 * "invert(expr)" function
14910 */
14911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014912f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014913{
14914 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14915}
14916
14917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918 * "isdirectory()" function
14919 */
14920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014921f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924}
14925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014926/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014927 * "islocked()" function
14928 */
14929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014930f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014931{
14932 lval_T lv;
14933 char_u *end;
14934 dictitem_T *di;
14935
14936 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014937 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14938 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014939 if (end != NULL && lv.ll_name != NULL)
14940 {
14941 if (*end != NUL)
14942 EMSG(_(e_trailing));
14943 else
14944 {
14945 if (lv.ll_tv == NULL)
14946 {
14947 if (check_changedtick(lv.ll_name))
14948 rettv->vval.v_number = 1; /* always locked */
14949 else
14950 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014951 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014952 if (di != NULL)
14953 {
14954 /* Consider a variable locked when:
14955 * 1. the variable itself is locked
14956 * 2. the value of the variable is locked.
14957 * 3. the List or Dict value is locked.
14958 */
14959 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14960 || tv_islocked(&di->di_tv));
14961 }
14962 }
14963 }
14964 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014965 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014966 else if (lv.ll_newkey != NULL)
14967 EMSG2(_(e_dictkey), lv.ll_newkey);
14968 else if (lv.ll_list != NULL)
14969 /* List item. */
14970 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14971 else
14972 /* Dictionary item. */
14973 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14974 }
14975 }
14976
14977 clear_lval(&lv);
14978}
14979
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014980#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14981/*
14982 * "isnan()" function
14983 */
14984 static void
14985f_isnan(typval_T *argvars, typval_T *rettv)
14986{
14987 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14988 && isnan(argvars[0].vval.v_float);
14989}
14990#endif
14991
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014992static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014993
14994/*
14995 * Turn a dict into a list:
14996 * "what" == 0: list of keys
14997 * "what" == 1: list of values
14998 * "what" == 2: list of items
14999 */
15000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015001dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015002{
Bram Moolenaar33570922005-01-25 22:26:29 +000015003 list_T *l2;
15004 dictitem_T *di;
15005 hashitem_T *hi;
15006 listitem_T *li;
15007 listitem_T *li2;
15008 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015009 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015010
Bram Moolenaar8c711452005-01-14 21:53:12 +000015011 if (argvars[0].v_type != VAR_DICT)
15012 {
15013 EMSG(_(e_dictreq));
15014 return;
15015 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015016 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015017 return;
15018
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015019 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015020 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015021
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015022 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015024 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015025 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015026 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015027 --todo;
15028 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015029
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015030 li = listitem_alloc();
15031 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015032 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015033 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015034
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015035 if (what == 0)
15036 {
15037 /* keys() */
15038 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015039 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015040 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15041 }
15042 else if (what == 1)
15043 {
15044 /* values() */
15045 copy_tv(&di->di_tv, &li->li_tv);
15046 }
15047 else
15048 {
15049 /* items() */
15050 l2 = list_alloc();
15051 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015052 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015053 li->li_tv.vval.v_list = l2;
15054 if (l2 == NULL)
15055 break;
15056 ++l2->lv_refcount;
15057
15058 li2 = listitem_alloc();
15059 if (li2 == NULL)
15060 break;
15061 list_append(l2, li2);
15062 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015063 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015064 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15065
15066 li2 = listitem_alloc();
15067 if (li2 == NULL)
15068 break;
15069 list_append(l2, li2);
15070 copy_tv(&di->di_tv, &li2->li_tv);
15071 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015072 }
15073 }
15074}
15075
15076/*
15077 * "items(dict)" function
15078 */
15079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015080f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015081{
15082 dict_list(argvars, rettv, 2);
15083}
15084
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015085#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015086/*
15087 * Get the job from the argument.
15088 * Returns NULL if the job is invalid.
15089 */
15090 static job_T *
15091get_job_arg(typval_T *tv)
15092{
15093 job_T *job;
15094
15095 if (tv->v_type != VAR_JOB)
15096 {
15097 EMSG2(_(e_invarg2), get_tv_string(tv));
15098 return NULL;
15099 }
15100 job = tv->vval.v_job;
15101
15102 if (job == NULL)
15103 EMSG(_("E916: not a valid job"));
15104 return job;
15105}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015106
Bram Moolenaar835dc632016-02-07 14:27:38 +010015107/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015108 * "job_getchannel()" function
15109 */
15110 static void
15111f_job_getchannel(typval_T *argvars, typval_T *rettv)
15112{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015113 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015114
Bram Moolenaar65edff82016-02-21 16:40:11 +010015115 if (job != NULL)
15116 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015117 rettv->v_type = VAR_CHANNEL;
15118 rettv->vval.v_channel = job->jv_channel;
15119 if (job->jv_channel != NULL)
15120 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015121 }
15122}
15123
15124/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015125 * "job_info()" function
15126 */
15127 static void
15128f_job_info(typval_T *argvars, typval_T *rettv)
15129{
15130 job_T *job = get_job_arg(&argvars[0]);
15131
15132 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15133 job_info(job, rettv->vval.v_dict);
15134}
15135
15136/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015137 * "job_setoptions()" function
15138 */
15139 static void
15140f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15141{
15142 job_T *job = get_job_arg(&argvars[0]);
15143 jobopt_T opt;
15144
15145 if (job == NULL)
15146 return;
15147 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015148 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15149 job_set_options(job, &opt);
15150 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015151}
15152
15153/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015154 * "job_start()" function
15155 */
15156 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015157f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015158{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015159 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015160 if (check_restricted() || check_secure())
15161 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015162 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015163}
15164
15165/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015166 * "job_status()" function
15167 */
15168 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015169f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015170{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015171 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015172
Bram Moolenaar65edff82016-02-21 16:40:11 +010015173 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015174 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015175 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015176 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015177 }
15178}
15179
15180/*
15181 * "job_stop()" function
15182 */
15183 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015184f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015185{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015186 job_T *job = get_job_arg(&argvars[0]);
15187
15188 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015189 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015190}
15191#endif
15192
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015194 * "join()" function
15195 */
15196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015197f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015198{
15199 garray_T ga;
15200 char_u *sep;
15201
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015202 if (argvars[0].v_type != VAR_LIST)
15203 {
15204 EMSG(_(e_listreq));
15205 return;
15206 }
15207 if (argvars[0].vval.v_list == NULL)
15208 return;
15209 if (argvars[1].v_type == VAR_UNKNOWN)
15210 sep = (char_u *)" ";
15211 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015213
15214 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015215
15216 if (sep != NULL)
15217 {
15218 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015219 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015220 ga_append(&ga, NUL);
15221 rettv->vval.v_string = (char_u *)ga.ga_data;
15222 }
15223 else
15224 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015225}
15226
15227/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015228 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015229 */
15230 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015231f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015232{
15233 js_read_T reader;
15234
15235 reader.js_buf = get_tv_string(&argvars[0]);
15236 reader.js_fill = NULL;
15237 reader.js_used = 0;
15238 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15239 EMSG(_(e_invarg));
15240}
15241
15242/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015243 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015244 */
15245 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015246f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015247{
15248 rettv->v_type = VAR_STRING;
15249 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15250}
15251
15252/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015253 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015254 */
15255 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015256f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015257{
15258 js_read_T reader;
15259
15260 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015261 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015262 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015263 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015264 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015265}
15266
15267/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015268 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015269 */
15270 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015271f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015272{
15273 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015274 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015275}
15276
15277/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015278 * "keys()" function
15279 */
15280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015281f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015282{
15283 dict_list(argvars, rettv, 0);
15284}
15285
15286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 * "last_buffer_nr()" function.
15288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015290f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291{
15292 int n = 0;
15293 buf_T *buf;
15294
15295 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15296 if (n < buf->b_fnum)
15297 n = buf->b_fnum;
15298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015299 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015300}
15301
15302/*
15303 * "len()" function
15304 */
15305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015306f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015307{
15308 switch (argvars[0].v_type)
15309 {
15310 case VAR_STRING:
15311 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015312 rettv->vval.v_number = (varnumber_T)STRLEN(
15313 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015314 break;
15315 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015317 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015318 case VAR_DICT:
15319 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15320 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015321 case VAR_UNKNOWN:
15322 case VAR_SPECIAL:
15323 case VAR_FLOAT:
15324 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015325 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015326 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015327 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015328 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015329 break;
15330 }
15331}
15332
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015333static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015334
15335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015336libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015337{
15338#ifdef FEAT_LIBCALL
15339 char_u *string_in;
15340 char_u **string_result;
15341 int nr_result;
15342#endif
15343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015344 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015345 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015346 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015347
15348 if (check_restricted() || check_secure())
15349 return;
15350
15351#ifdef FEAT_LIBCALL
15352 /* The first two args must be strings, otherwise its meaningless */
15353 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15354 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015355 string_in = NULL;
15356 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015357 string_in = argvars[2].vval.v_string;
15358 if (type == VAR_NUMBER)
15359 string_result = NULL;
15360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015361 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015362 if (mch_libcall(argvars[0].vval.v_string,
15363 argvars[1].vval.v_string,
15364 string_in,
15365 argvars[2].vval.v_number,
15366 string_result,
15367 &nr_result) == OK
15368 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015369 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015370 }
15371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372}
15373
15374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015375 * "libcall()" function
15376 */
15377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015378f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015379{
15380 libcall_common(argvars, rettv, VAR_STRING);
15381}
15382
15383/*
15384 * "libcallnr()" function
15385 */
15386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015387f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015388{
15389 libcall_common(argvars, rettv, VAR_NUMBER);
15390}
15391
15392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 * "line(string)" function
15394 */
15395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015396f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397{
15398 linenr_T lnum = 0;
15399 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015400 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015402 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 if (fp != NULL)
15404 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406}
15407
15408/*
15409 * "line2byte(lnum)" function
15410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015412f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413{
15414#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416#else
15417 linenr_T lnum;
15418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015421 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15424 if (rettv->vval.v_number >= 0)
15425 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426#endif
15427}
15428
15429/*
15430 * "lispindent(lnum)" function
15431 */
15432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015433f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434{
15435#ifdef FEAT_LISP
15436 pos_T pos;
15437 linenr_T lnum;
15438
15439 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15442 {
15443 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 curwin->w_cursor = pos;
15446 }
15447 else
15448#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450}
15451
15452/*
15453 * "localtime()" function
15454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015456f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459}
15460
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015461static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462
15463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015464get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465{
15466 char_u *keys;
15467 char_u *which;
15468 char_u buf[NUMBUFLEN];
15469 char_u *keys_buf = NULL;
15470 char_u *rhs;
15471 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015472 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015473 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015474 mapblock_T *mp;
15475 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476
15477 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478 rettv->v_type = VAR_STRING;
15479 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015481 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 if (*keys == NUL)
15483 return;
15484
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015485 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015487 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015489 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015490 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015491 if (argvars[3].v_type != VAR_UNKNOWN)
15492 get_dict = get_tv_number(&argvars[3]);
15493 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 else
15496 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015497 if (which == NULL)
15498 return;
15499
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500 mode = get_map_mode(&which, 0);
15501
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015502 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015503 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015505
15506 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015508 /* Return a string. */
15509 if (rhs != NULL)
15510 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511
Bram Moolenaarbd743252010-10-20 21:23:33 +020015512 }
15513 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15514 {
15515 /* Return a dictionary. */
15516 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15517 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15518 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519
Bram Moolenaarbd743252010-10-20 21:23:33 +020015520 dict_add_nr_str(dict, "lhs", 0L, lhs);
15521 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15522 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15523 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15524 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15525 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15526 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015527 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015528 dict_add_nr_str(dict, "mode", 0L, mapmode);
15529
15530 vim_free(lhs);
15531 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 }
15533}
15534
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015535#ifdef FEAT_FLOAT
15536/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015537 * "log()" function
15538 */
15539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015540f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015541{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015542 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015543
15544 rettv->v_type = VAR_FLOAT;
15545 if (get_float_arg(argvars, &f) == OK)
15546 rettv->vval.v_float = log(f);
15547 else
15548 rettv->vval.v_float = 0.0;
15549}
15550
15551/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015552 * "log10()" function
15553 */
15554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015555f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015556{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015557 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015558
15559 rettv->v_type = VAR_FLOAT;
15560 if (get_float_arg(argvars, &f) == OK)
15561 rettv->vval.v_float = log10(f);
15562 else
15563 rettv->vval.v_float = 0.0;
15564}
15565#endif
15566
Bram Moolenaar1dced572012-04-05 16:54:08 +020015567#ifdef FEAT_LUA
15568/*
15569 * "luaeval()" function
15570 */
15571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015572f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015573{
15574 char_u *str;
15575 char_u buf[NUMBUFLEN];
15576
15577 str = get_tv_string_buf(&argvars[0], buf);
15578 do_luaeval(str, argvars + 1, rettv);
15579}
15580#endif
15581
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015583 * "map()" function
15584 */
15585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015586f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015587{
15588 filter_map(argvars, rettv, TRUE);
15589}
15590
15591/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 */
15594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015595f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015597 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598}
15599
15600/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015601 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 */
15603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015604f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607}
15608
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015609static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610
15611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015612find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015614 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015615 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015616 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 char_u *pat;
15618 regmatch_T regmatch;
15619 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015620 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 char_u *save_cpo;
15622 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015623 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015624 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015625 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015626 list_T *l = NULL;
15627 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015628 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015629 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630
15631 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15632 save_cpo = p_cpo;
15633 p_cpo = (char_u *)"";
15634
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015635 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015636 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015637 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015638 /* type 3: return empty list when there are no matches.
15639 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015640 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015641 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015642 if (type == 4
15643 && (list_append_string(rettv->vval.v_list,
15644 (char_u *)"", 0) == FAIL
15645 || list_append_number(rettv->vval.v_list,
15646 (varnumber_T)-1) == FAIL
15647 || list_append_number(rettv->vval.v_list,
15648 (varnumber_T)-1) == FAIL
15649 || list_append_number(rettv->vval.v_list,
15650 (varnumber_T)-1) == FAIL))
15651 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015652 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015653 rettv->vval.v_list = NULL;
15654 goto theend;
15655 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015656 }
15657 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015659 rettv->v_type = VAR_STRING;
15660 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015663 if (argvars[0].v_type == VAR_LIST)
15664 {
15665 if ((l = argvars[0].vval.v_list) == NULL)
15666 goto theend;
15667 li = l->lv_first;
15668 }
15669 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015670 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015671 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015672 len = (long)STRLEN(str);
15673 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015675 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15676 if (pat == NULL)
15677 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015678
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015679 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015681 int error = FALSE;
15682
15683 start = get_tv_number_chk(&argvars[2], &error);
15684 if (error)
15685 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015686 if (l != NULL)
15687 {
15688 li = list_find(l, start);
15689 if (li == NULL)
15690 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015691 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015692 }
15693 else
15694 {
15695 if (start < 0)
15696 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015697 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015698 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015699 /* When "count" argument is there ignore matches before "start",
15700 * otherwise skip part of the string. Differs when pattern is "^"
15701 * or "\<". */
15702 if (argvars[3].v_type != VAR_UNKNOWN)
15703 startcol = start;
15704 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015705 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015706 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015707 len -= start;
15708 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015709 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015710
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015711 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015712 nth = get_tv_number_chk(&argvars[3], &error);
15713 if (error)
15714 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 }
15716
15717 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15718 if (regmatch.regprog != NULL)
15719 {
15720 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015721
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015722 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015723 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015724 if (l != NULL)
15725 {
15726 if (li == NULL)
15727 {
15728 match = FALSE;
15729 break;
15730 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015731 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015732 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015733 if (str == NULL)
15734 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015735 }
15736
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015737 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015738
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015739 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015740 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015741 if (l == NULL && !match)
15742 break;
15743
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015744 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015745 if (l != NULL)
15746 {
15747 li = li->li_next;
15748 ++idx;
15749 }
15750 else
15751 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015752#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015753 startcol = (colnr_T)(regmatch.startp[0]
15754 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015755#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015756 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015757#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015758 if (startcol > (colnr_T)len
15759 || str + startcol <= regmatch.startp[0])
15760 {
15761 match = FALSE;
15762 break;
15763 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015764 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015765 }
15766
15767 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015769 if (type == 4)
15770 {
15771 listitem_T *li1 = rettv->vval.v_list->lv_first;
15772 listitem_T *li2 = li1->li_next;
15773 listitem_T *li3 = li2->li_next;
15774 listitem_T *li4 = li3->li_next;
15775
Bram Moolenaar3c809342016-06-01 22:34:48 +020015776 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015777 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15778 (int)(regmatch.endp[0] - regmatch.startp[0]));
15779 li3->li_tv.vval.v_number =
15780 (varnumber_T)(regmatch.startp[0] - expr);
15781 li4->li_tv.vval.v_number =
15782 (varnumber_T)(regmatch.endp[0] - expr);
15783 if (l != NULL)
15784 li2->li_tv.vval.v_number = (varnumber_T)idx;
15785 }
15786 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015788 int i;
15789
15790 /* return list with matched string and submatches */
15791 for (i = 0; i < NSUBEXP; ++i)
15792 {
15793 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015794 {
15795 if (list_append_string(rettv->vval.v_list,
15796 (char_u *)"", 0) == FAIL)
15797 break;
15798 }
15799 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015800 regmatch.startp[i],
15801 (int)(regmatch.endp[i] - regmatch.startp[i]))
15802 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015803 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015804 }
15805 }
15806 else if (type == 2)
15807 {
15808 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015809 if (l != NULL)
15810 copy_tv(&li->li_tv, rettv);
15811 else
15812 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015814 }
15815 else if (l != NULL)
15816 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817 else
15818 {
15819 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015820 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821 (varnumber_T)(regmatch.startp[0] - str);
15822 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015823 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015825 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826 }
15827 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015828 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 }
15830
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015831 if (type == 4 && l == NULL)
15832 /* matchstrpos() without a list: drop the second item. */
15833 listitem_remove(rettv->vval.v_list,
15834 rettv->vval.v_list->lv_first->li_next);
15835
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015837 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 p_cpo = save_cpo;
15839}
15840
15841/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015842 * "match()" function
15843 */
15844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015845f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015846{
15847 find_some_match(argvars, rettv, 1);
15848}
15849
15850/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015851 * "matchadd()" function
15852 */
15853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015854f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015855{
15856#ifdef FEAT_SEARCH_EXTRA
15857 char_u buf[NUMBUFLEN];
15858 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15859 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15860 int prio = 10; /* default priority */
15861 int id = -1;
15862 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015863 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015864
15865 rettv->vval.v_number = -1;
15866
15867 if (grp == NULL || pat == NULL)
15868 return;
15869 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015870 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015871 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015872 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015873 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015874 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015875 if (argvars[4].v_type != VAR_UNKNOWN)
15876 {
15877 if (argvars[4].v_type != VAR_DICT)
15878 {
15879 EMSG(_(e_dictreq));
15880 return;
15881 }
15882 if (dict_find(argvars[4].vval.v_dict,
15883 (char_u *)"conceal", -1) != NULL)
15884 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15885 (char_u *)"conceal", FALSE);
15886 }
15887 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015888 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015889 if (error == TRUE)
15890 return;
15891 if (id >= 1 && id <= 3)
15892 {
15893 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15894 return;
15895 }
15896
Bram Moolenaar6561d522015-07-21 15:48:27 +020015897 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15898 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015899#endif
15900}
15901
15902/*
15903 * "matchaddpos()" function
15904 */
15905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015906f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015907{
15908#ifdef FEAT_SEARCH_EXTRA
15909 char_u buf[NUMBUFLEN];
15910 char_u *group;
15911 int prio = 10;
15912 int id = -1;
15913 int error = FALSE;
15914 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015915 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015916
15917 rettv->vval.v_number = -1;
15918
15919 group = get_tv_string_buf_chk(&argvars[0], buf);
15920 if (group == NULL)
15921 return;
15922
15923 if (argvars[1].v_type != VAR_LIST)
15924 {
15925 EMSG2(_(e_listarg), "matchaddpos()");
15926 return;
15927 }
15928 l = argvars[1].vval.v_list;
15929 if (l == NULL)
15930 return;
15931
15932 if (argvars[2].v_type != VAR_UNKNOWN)
15933 {
15934 prio = get_tv_number_chk(&argvars[2], &error);
15935 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015936 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015937 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015938 if (argvars[4].v_type != VAR_UNKNOWN)
15939 {
15940 if (argvars[4].v_type != VAR_DICT)
15941 {
15942 EMSG(_(e_dictreq));
15943 return;
15944 }
15945 if (dict_find(argvars[4].vval.v_dict,
15946 (char_u *)"conceal", -1) != NULL)
15947 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15948 (char_u *)"conceal", FALSE);
15949 }
15950 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015951 }
15952 if (error == TRUE)
15953 return;
15954
15955 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15956 if (id == 1 || id == 2)
15957 {
15958 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15959 return;
15960 }
15961
Bram Moolenaar6561d522015-07-21 15:48:27 +020015962 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15963 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015964#endif
15965}
15966
15967/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015968 * "matcharg()" function
15969 */
15970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015971f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015972{
15973 if (rettv_list_alloc(rettv) == OK)
15974 {
15975#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015976 int id = get_tv_number(&argvars[0]);
15977 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015978
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015979 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015980 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015981 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15982 {
15983 list_append_string(rettv->vval.v_list,
15984 syn_id2name(m->hlg_id), -1);
15985 list_append_string(rettv->vval.v_list, m->pattern, -1);
15986 }
15987 else
15988 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015989 list_append_string(rettv->vval.v_list, NULL, -1);
15990 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015991 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015992 }
15993#endif
15994 }
15995}
15996
15997/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015998 * "matchdelete()" function
15999 */
16000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016001f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016002{
16003#ifdef FEAT_SEARCH_EXTRA
16004 rettv->vval.v_number = match_delete(curwin,
16005 (int)get_tv_number(&argvars[0]), TRUE);
16006#endif
16007}
16008
16009/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016010 * "matchend()" function
16011 */
16012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016013f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016014{
16015 find_some_match(argvars, rettv, 0);
16016}
16017
16018/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016019 * "matchlist()" function
16020 */
16021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016022f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016023{
16024 find_some_match(argvars, rettv, 3);
16025}
16026
16027/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016028 * "matchstr()" function
16029 */
16030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016031f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016032{
16033 find_some_match(argvars, rettv, 2);
16034}
16035
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016036/*
16037 * "matchstrpos()" function
16038 */
16039 static void
16040f_matchstrpos(typval_T *argvars, typval_T *rettv)
16041{
16042 find_some_match(argvars, rettv, 4);
16043}
16044
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016045static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016046
16047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016048max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016049{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016050 long n = 0;
16051 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016052 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016053
16054 if (argvars[0].v_type == VAR_LIST)
16055 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016056 list_T *l;
16057 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016058
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016059 l = argvars[0].vval.v_list;
16060 if (l != NULL)
16061 {
16062 li = l->lv_first;
16063 if (li != NULL)
16064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016065 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016066 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016067 {
16068 li = li->li_next;
16069 if (li == NULL)
16070 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016071 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016072 if (domax ? i > n : i < n)
16073 n = i;
16074 }
16075 }
16076 }
16077 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016078 else if (argvars[0].v_type == VAR_DICT)
16079 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016080 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016081 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016082 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016083 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016084
16085 d = argvars[0].vval.v_dict;
16086 if (d != NULL)
16087 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016088 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016089 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016091 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016092 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016093 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016094 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016095 if (first)
16096 {
16097 n = i;
16098 first = FALSE;
16099 }
16100 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016101 n = i;
16102 }
16103 }
16104 }
16105 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016106 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016107 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016108 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016109}
16110
16111/*
16112 * "max()" function
16113 */
16114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016115f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016116{
16117 max_min(argvars, rettv, TRUE);
16118}
16119
16120/*
16121 * "min()" function
16122 */
16123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016124f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016125{
16126 max_min(argvars, rettv, FALSE);
16127}
16128
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016129static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016130
16131/*
16132 * Create the directory in which "dir" is located, and higher levels when
16133 * needed.
16134 */
16135 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016136mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016137{
16138 char_u *p;
16139 char_u *updir;
16140 int r = FAIL;
16141
16142 /* Get end of directory name in "dir".
16143 * We're done when it's "/" or "c:/". */
16144 p = gettail_sep(dir);
16145 if (p <= get_past_head(dir))
16146 return OK;
16147
16148 /* If the directory exists we're done. Otherwise: create it.*/
16149 updir = vim_strnsave(dir, (int)(p - dir));
16150 if (updir == NULL)
16151 return FAIL;
16152 if (mch_isdir(updir))
16153 r = OK;
16154 else if (mkdir_recurse(updir, prot) == OK)
16155 r = vim_mkdir_emsg(updir, prot);
16156 vim_free(updir);
16157 return r;
16158}
16159
16160#ifdef vim_mkdir
16161/*
16162 * "mkdir()" function
16163 */
16164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016165f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016166{
16167 char_u *dir;
16168 char_u buf[NUMBUFLEN];
16169 int prot = 0755;
16170
16171 rettv->vval.v_number = FAIL;
16172 if (check_restricted() || check_secure())
16173 return;
16174
16175 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016176 if (*dir == NUL)
16177 rettv->vval.v_number = FAIL;
16178 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016179 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016180 if (*gettail(dir) == NUL)
16181 /* remove trailing slashes */
16182 *gettail_sep(dir) = NUL;
16183
16184 if (argvars[1].v_type != VAR_UNKNOWN)
16185 {
16186 if (argvars[2].v_type != VAR_UNKNOWN)
16187 prot = get_tv_number_chk(&argvars[2], NULL);
16188 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16189 mkdir_recurse(dir, prot);
16190 }
16191 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016192 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016193}
16194#endif
16195
Bram Moolenaar0d660222005-01-07 21:51:51 +000016196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 * "mode()" function
16198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016200f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016202 char_u buf[3];
16203
16204 buf[1] = NUL;
16205 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 if (VIsual_active)
16208 {
16209 if (VIsual_select)
16210 buf[0] = VIsual_mode + 's' - 'v';
16211 else
16212 buf[0] = VIsual_mode;
16213 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016214 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016215 || State == CONFIRM)
16216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016218 if (State == ASKMORE)
16219 buf[1] = 'm';
16220 else if (State == CONFIRM)
16221 buf[1] = '?';
16222 }
16223 else if (State == EXTERNCMD)
16224 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225 else if (State & INSERT)
16226 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016227#ifdef FEAT_VREPLACE
16228 if (State & VREPLACE_FLAG)
16229 {
16230 buf[0] = 'R';
16231 buf[1] = 'v';
16232 }
16233 else
16234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235 if (State & REPLACE_FLAG)
16236 buf[0] = 'R';
16237 else
16238 buf[0] = 'i';
16239 }
16240 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016243 if (exmode_active)
16244 buf[1] = 'v';
16245 }
16246 else if (exmode_active)
16247 {
16248 buf[0] = 'c';
16249 buf[1] = 'e';
16250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016254 if (finish_op)
16255 buf[1] = 'o';
16256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016258 /* Clear out the minor mode when the argument is not a non-zero number or
16259 * non-empty string. */
16260 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016261 buf[1] = NUL;
16262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016263 rettv->vval.v_string = vim_strsave(buf);
16264 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265}
16266
Bram Moolenaar429fa852013-04-15 12:27:36 +020016267#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016268/*
16269 * "mzeval()" function
16270 */
16271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016272f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016273{
16274 char_u *str;
16275 char_u buf[NUMBUFLEN];
16276
16277 str = get_tv_string_buf(&argvars[0], buf);
16278 do_mzeval(str, rettv);
16279}
Bram Moolenaar75676462013-01-30 14:55:42 +010016280
16281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016282mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016283{
16284 typval_T argvars[3];
16285
16286 argvars[0].v_type = VAR_STRING;
16287 argvars[0].vval.v_string = name;
16288 copy_tv(args, &argvars[1]);
16289 argvars[2].v_type = VAR_UNKNOWN;
16290 f_call(argvars, rettv);
16291 clear_tv(&argvars[1]);
16292}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016293#endif
16294
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016296 * "nextnonblank()" function
16297 */
16298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016299f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016300{
16301 linenr_T lnum;
16302
16303 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016305 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016306 {
16307 lnum = 0;
16308 break;
16309 }
16310 if (*skipwhite(ml_get(lnum)) != NUL)
16311 break;
16312 }
16313 rettv->vval.v_number = lnum;
16314}
16315
16316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 * "nr2char()" function
16318 */
16319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016320f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321{
16322 char_u buf[NUMBUFLEN];
16323
16324#ifdef FEAT_MBYTE
16325 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016326 {
16327 int utf8 = 0;
16328
16329 if (argvars[1].v_type != VAR_UNKNOWN)
16330 utf8 = get_tv_number_chk(&argvars[1], NULL);
16331 if (utf8)
16332 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16333 else
16334 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 else
16337#endif
16338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016339 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 buf[1] = NUL;
16341 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016342 rettv->v_type = VAR_STRING;
16343 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016344}
16345
16346/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016347 * "or(expr, expr)" function
16348 */
16349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016350f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016351{
16352 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16353 | get_tv_number_chk(&argvars[1], NULL);
16354}
16355
16356/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016357 * "pathshorten()" function
16358 */
16359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016360f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016361{
16362 char_u *p;
16363
16364 rettv->v_type = VAR_STRING;
16365 p = get_tv_string_chk(&argvars[0]);
16366 if (p == NULL)
16367 rettv->vval.v_string = NULL;
16368 else
16369 {
16370 p = vim_strsave(p);
16371 rettv->vval.v_string = p;
16372 if (p != NULL)
16373 shorten_dir(p);
16374 }
16375}
16376
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016377#ifdef FEAT_PERL
16378/*
16379 * "perleval()" function
16380 */
16381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016382f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016383{
16384 char_u *str;
16385 char_u buf[NUMBUFLEN];
16386
16387 str = get_tv_string_buf(&argvars[0], buf);
16388 do_perleval(str, rettv);
16389}
16390#endif
16391
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016392#ifdef FEAT_FLOAT
16393/*
16394 * "pow()" function
16395 */
16396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016397f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016398{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016399 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016400
16401 rettv->v_type = VAR_FLOAT;
16402 if (get_float_arg(argvars, &fx) == OK
16403 && get_float_arg(&argvars[1], &fy) == OK)
16404 rettv->vval.v_float = pow(fx, fy);
16405 else
16406 rettv->vval.v_float = 0.0;
16407}
16408#endif
16409
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016410/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016411 * "prevnonblank()" function
16412 */
16413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016414f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016415{
16416 linenr_T lnum;
16417
16418 lnum = get_tv_lnum(argvars);
16419 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16420 lnum = 0;
16421 else
16422 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16423 --lnum;
16424 rettv->vval.v_number = lnum;
16425}
16426
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016427/* This dummy va_list is here because:
16428 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16429 * - locally in the function results in a "used before set" warning
16430 * - using va_start() to initialize it gives "function with fixed args" error */
16431static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016432
Bram Moolenaar8c711452005-01-14 21:53:12 +000016433/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016434 * "printf()" function
16435 */
16436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016437f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016438{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016439 char_u buf[NUMBUFLEN];
16440 int len;
16441 char_u *s;
16442 int saved_did_emsg = did_emsg;
16443 char *fmt;
16444
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016445 rettv->v_type = VAR_STRING;
16446 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016447
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016448 /* Get the required length, allocate the buffer and do it for real. */
16449 did_emsg = FALSE;
16450 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16451 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16452 if (!did_emsg)
16453 {
16454 s = alloc(len + 1);
16455 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016456 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016457 rettv->vval.v_string = s;
16458 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016459 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016460 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016461 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016462}
16463
16464/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016465 * "pumvisible()" function
16466 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016468f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016469{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016470#ifdef FEAT_INS_EXPAND
16471 if (pum_visible())
16472 rettv->vval.v_number = 1;
16473#endif
16474}
16475
Bram Moolenaardb913952012-06-29 12:54:53 +020016476#ifdef FEAT_PYTHON3
16477/*
16478 * "py3eval()" function
16479 */
16480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016481f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016482{
16483 char_u *str;
16484 char_u buf[NUMBUFLEN];
16485
16486 str = get_tv_string_buf(&argvars[0], buf);
16487 do_py3eval(str, rettv);
16488}
16489#endif
16490
16491#ifdef FEAT_PYTHON
16492/*
16493 * "pyeval()" function
16494 */
16495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016496f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016497{
16498 char_u *str;
16499 char_u buf[NUMBUFLEN];
16500
16501 str = get_tv_string_buf(&argvars[0], buf);
16502 do_pyeval(str, rettv);
16503}
16504#endif
16505
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016506/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016507 * "range()" function
16508 */
16509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016510f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016511{
16512 long start;
16513 long end;
16514 long stride = 1;
16515 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016516 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016518 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016519 if (argvars[1].v_type == VAR_UNKNOWN)
16520 {
16521 end = start - 1;
16522 start = 0;
16523 }
16524 else
16525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016526 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016528 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016529 }
16530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 if (error)
16532 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016533 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016534 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016535 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016536 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016537 else
16538 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016539 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016540 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016541 if (list_append_number(rettv->vval.v_list,
16542 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016543 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016544 }
16545}
16546
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016547/*
16548 * "readfile()" function
16549 */
16550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016551f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016552{
16553 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016554 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016555 char_u *fname;
16556 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016557 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16558 int io_size = sizeof(buf);
16559 int readlen; /* size of last fread() */
16560 char_u *prev = NULL; /* previously read bytes, if any */
16561 long prevlen = 0; /* length of data in prev */
16562 long prevsize = 0; /* size of prev buffer */
16563 long maxline = MAXLNUM;
16564 long cnt = 0;
16565 char_u *p; /* position in buf */
16566 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016567
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016568 if (argvars[1].v_type != VAR_UNKNOWN)
16569 {
16570 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16571 binary = TRUE;
16572 if (argvars[2].v_type != VAR_UNKNOWN)
16573 maxline = get_tv_number(&argvars[2]);
16574 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016575
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016576 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016577 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016578
16579 /* Always open the file in binary mode, library functions have a mind of
16580 * their own about CR-LF conversion. */
16581 fname = get_tv_string(&argvars[0]);
16582 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16583 {
16584 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16585 return;
16586 }
16587
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016588 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016589 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016590 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016591
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016592 /* This for loop processes what was read, but is also entered at end
16593 * of file so that either:
16594 * - an incomplete line gets written
16595 * - a "binary" file gets an empty line at the end if it ends in a
16596 * newline. */
16597 for (p = buf, start = buf;
16598 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16599 ++p)
16600 {
16601 if (*p == '\n' || readlen <= 0)
16602 {
16603 listitem_T *li;
16604 char_u *s = NULL;
16605 long_u len = p - start;
16606
16607 /* Finished a line. Remove CRs before NL. */
16608 if (readlen > 0 && !binary)
16609 {
16610 while (len > 0 && start[len - 1] == '\r')
16611 --len;
16612 /* removal may cross back to the "prev" string */
16613 if (len == 0)
16614 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16615 --prevlen;
16616 }
16617 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016618 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016619 else
16620 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016621 /* Change "prev" buffer to be the right size. This way
16622 * the bytes are only copied once, and very long lines are
16623 * allocated only once. */
16624 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016625 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016626 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016627 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016628 prev = NULL; /* the list will own the string */
16629 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016630 }
16631 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016632 if (s == NULL)
16633 {
16634 do_outofmem_msg((long_u) prevlen + len + 1);
16635 failed = TRUE;
16636 break;
16637 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016638
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016639 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016640 {
16641 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016642 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016643 break;
16644 }
16645 li->li_tv.v_type = VAR_STRING;
16646 li->li_tv.v_lock = 0;
16647 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016648 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016649
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016650 start = p + 1; /* step over newline */
16651 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016652 break;
16653 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016654 else if (*p == NUL)
16655 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016656#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016657 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16658 * when finding the BF and check the previous two bytes. */
16659 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016660 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016661 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16662 * + 1, these may be in the "prev" string. */
16663 char_u back1 = p >= buf + 1 ? p[-1]
16664 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16665 char_u back2 = p >= buf + 2 ? p[-2]
16666 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16667 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016668
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016669 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016670 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016671 char_u *dest = p - 2;
16672
16673 /* Usually a BOM is at the beginning of a file, and so at
16674 * the beginning of a line; then we can just step over it.
16675 */
16676 if (start == dest)
16677 start = p + 1;
16678 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016679 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016680 /* have to shuffle buf to close gap */
16681 int adjust_prevlen = 0;
16682
16683 if (dest < buf)
16684 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016685 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016686 dest = buf;
16687 }
16688 if (readlen > p - buf + 1)
16689 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16690 readlen -= 3 - adjust_prevlen;
16691 prevlen -= adjust_prevlen;
16692 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016693 }
16694 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016695 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016696#endif
16697 } /* for */
16698
16699 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16700 break;
16701 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016702 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016703 /* There's part of a line in buf, store it in "prev". */
16704 if (p - start + prevlen >= prevsize)
16705 {
16706 /* need bigger "prev" buffer */
16707 char_u *newprev;
16708
16709 /* A common use case is ordinary text files and "prev" gets a
16710 * fragment of a line, so the first allocation is made
16711 * small, to avoid repeatedly 'allocing' large and
16712 * 'reallocing' small. */
16713 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016714 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016715 else
16716 {
16717 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016718 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016719 prevsize = grow50pc > growmin ? grow50pc : growmin;
16720 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016721 newprev = prev == NULL ? alloc(prevsize)
16722 : vim_realloc(prev, prevsize);
16723 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016724 {
16725 do_outofmem_msg((long_u)prevsize);
16726 failed = TRUE;
16727 break;
16728 }
16729 prev = newprev;
16730 }
16731 /* Add the line part to end of "prev". */
16732 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016733 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016734 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016735 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016736
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016737 /*
16738 * For a negative line count use only the lines at the end of the file,
16739 * free the rest.
16740 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016741 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016742 while (cnt > -maxline)
16743 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016744 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016745 --cnt;
16746 }
16747
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016748 if (failed)
16749 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016750 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016751 /* readfile doc says an empty list is returned on error */
16752 rettv->vval.v_list = list_alloc();
16753 }
16754
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016755 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016756 fclose(fd);
16757}
16758
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016759#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016760static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016761
16762/*
16763 * Convert a List to proftime_T.
16764 * Return FAIL when there is something wrong.
16765 */
16766 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016767list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016768{
16769 long n1, n2;
16770 int error = FALSE;
16771
16772 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16773 || arg->vval.v_list->lv_len != 2)
16774 return FAIL;
16775 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16776 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16777# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016778 tm->HighPart = n1;
16779 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016780# else
16781 tm->tv_sec = n1;
16782 tm->tv_usec = n2;
16783# endif
16784 return error ? FAIL : OK;
16785}
16786#endif /* FEAT_RELTIME */
16787
16788/*
16789 * "reltime()" function
16790 */
16791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016792f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016793{
16794#ifdef FEAT_RELTIME
16795 proftime_T res;
16796 proftime_T start;
16797
16798 if (argvars[0].v_type == VAR_UNKNOWN)
16799 {
16800 /* No arguments: get current time. */
16801 profile_start(&res);
16802 }
16803 else if (argvars[1].v_type == VAR_UNKNOWN)
16804 {
16805 if (list2proftime(&argvars[0], &res) == FAIL)
16806 return;
16807 profile_end(&res);
16808 }
16809 else
16810 {
16811 /* Two arguments: compute the difference. */
16812 if (list2proftime(&argvars[0], &start) == FAIL
16813 || list2proftime(&argvars[1], &res) == FAIL)
16814 return;
16815 profile_sub(&res, &start);
16816 }
16817
16818 if (rettv_list_alloc(rettv) == OK)
16819 {
16820 long n1, n2;
16821
16822# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016823 n1 = res.HighPart;
16824 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016825# else
16826 n1 = res.tv_sec;
16827 n2 = res.tv_usec;
16828# endif
16829 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16830 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16831 }
16832#endif
16833}
16834
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016835#ifdef FEAT_FLOAT
16836/*
16837 * "reltimefloat()" function
16838 */
16839 static void
16840f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16841{
16842# ifdef FEAT_RELTIME
16843 proftime_T tm;
16844# endif
16845
16846 rettv->v_type = VAR_FLOAT;
16847 rettv->vval.v_float = 0;
16848# ifdef FEAT_RELTIME
16849 if (list2proftime(&argvars[0], &tm) == OK)
16850 rettv->vval.v_float = profile_float(&tm);
16851# endif
16852}
16853#endif
16854
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016855/*
16856 * "reltimestr()" function
16857 */
16858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016859f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016860{
16861#ifdef FEAT_RELTIME
16862 proftime_T tm;
16863#endif
16864
16865 rettv->v_type = VAR_STRING;
16866 rettv->vval.v_string = NULL;
16867#ifdef FEAT_RELTIME
16868 if (list2proftime(&argvars[0], &tm) == OK)
16869 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16870#endif
16871}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016872
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016874static void make_connection(void);
16875static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016876
16877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016878make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016879{
16880 if (X_DISPLAY == NULL
16881# ifdef FEAT_GUI
16882 && !gui.in_use
16883# endif
16884 )
16885 {
16886 x_force_connect = TRUE;
16887 setup_term_clip();
16888 x_force_connect = FALSE;
16889 }
16890}
16891
16892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016893check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016894{
16895 make_connection();
16896 if (X_DISPLAY == NULL)
16897 {
16898 EMSG(_("E240: No connection to Vim server"));
16899 return FAIL;
16900 }
16901 return OK;
16902}
16903#endif
16904
16905#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016907remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016908{
16909 char_u *server_name;
16910 char_u *keys;
16911 char_u *r = NULL;
16912 char_u buf[NUMBUFLEN];
16913# ifdef WIN32
16914 HWND w;
16915# else
16916 Window w;
16917# endif
16918
16919 if (check_restricted() || check_secure())
16920 return;
16921
16922# ifdef FEAT_X11
16923 if (check_connection() == FAIL)
16924 return;
16925# endif
16926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016927 server_name = get_tv_string_chk(&argvars[0]);
16928 if (server_name == NULL)
16929 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 keys = get_tv_string_buf(&argvars[1], buf);
16931# ifdef WIN32
16932 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16933# else
16934 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16935 < 0)
16936# endif
16937 {
16938 if (r != NULL)
16939 EMSG(r); /* sending worked but evaluation failed */
16940 else
16941 EMSG2(_("E241: Unable to send to %s"), server_name);
16942 return;
16943 }
16944
16945 rettv->vval.v_string = r;
16946
16947 if (argvars[2].v_type != VAR_UNKNOWN)
16948 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016949 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016950 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016951 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016953 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016954 v.di_tv.v_type = VAR_STRING;
16955 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016956 idvar = get_tv_string_chk(&argvars[2]);
16957 if (idvar != NULL)
16958 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016959 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 }
16961}
16962#endif
16963
16964/*
16965 * "remote_expr()" function
16966 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016968f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969{
16970 rettv->v_type = VAR_STRING;
16971 rettv->vval.v_string = NULL;
16972#ifdef FEAT_CLIENTSERVER
16973 remote_common(argvars, rettv, TRUE);
16974#endif
16975}
16976
16977/*
16978 * "remote_foreground()" function
16979 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016981f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983#ifdef FEAT_CLIENTSERVER
16984# ifdef WIN32
16985 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016986 {
16987 char_u *server_name = get_tv_string_chk(&argvars[0]);
16988
16989 if (server_name != NULL)
16990 serverForeground(server_name);
16991 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992# else
16993 /* Send a foreground() expression to the server. */
16994 argvars[1].v_type = VAR_STRING;
16995 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16996 argvars[2].v_type = VAR_UNKNOWN;
16997 remote_common(argvars, rettv, TRUE);
16998 vim_free(argvars[1].vval.v_string);
16999# endif
17000#endif
17001}
17002
Bram Moolenaar0d660222005-01-07 21:51:51 +000017003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017004f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017005{
17006#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017007 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017008 char_u *s = NULL;
17009# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017010 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017012 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017013
17014 if (check_restricted() || check_secure())
17015 {
17016 rettv->vval.v_number = -1;
17017 return;
17018 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017019 serverid = get_tv_string_chk(&argvars[0]);
17020 if (serverid == NULL)
17021 {
17022 rettv->vval.v_number = -1;
17023 return; /* type error; errmsg already given */
17024 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017026 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017027 if (n == 0)
17028 rettv->vval.v_number = -1;
17029 else
17030 {
17031 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17032 rettv->vval.v_number = (s != NULL);
17033 }
17034# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017035 if (check_connection() == FAIL)
17036 return;
17037
17038 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017039 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040# endif
17041
17042 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017044 char_u *retvar;
17045
Bram Moolenaar33570922005-01-25 22:26:29 +000017046 v.di_tv.v_type = VAR_STRING;
17047 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017048 retvar = get_tv_string_chk(&argvars[1]);
17049 if (retvar != NULL)
17050 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017051 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017052 }
17053#else
17054 rettv->vval.v_number = -1;
17055#endif
17056}
17057
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017059f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060{
17061 char_u *r = NULL;
17062
17063#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017064 char_u *serverid = get_tv_string_chk(&argvars[0]);
17065
17066 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017067 {
17068# ifdef WIN32
17069 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017070 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017072 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017073 if (n != 0)
17074 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17075 if (r == NULL)
17076# else
17077 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079# endif
17080 EMSG(_("E277: Unable to read a server reply"));
17081 }
17082#endif
17083 rettv->v_type = VAR_STRING;
17084 rettv->vval.v_string = r;
17085}
17086
17087/*
17088 * "remote_send()" function
17089 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017091f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017092{
17093 rettv->v_type = VAR_STRING;
17094 rettv->vval.v_string = NULL;
17095#ifdef FEAT_CLIENTSERVER
17096 remote_common(argvars, rettv, FALSE);
17097#endif
17098}
17099
17100/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017101 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017102 */
17103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017104f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017105{
Bram Moolenaar33570922005-01-25 22:26:29 +000017106 list_T *l;
17107 listitem_T *item, *item2;
17108 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017109 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017110 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017111 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017112 dict_T *d;
17113 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017114 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017115
Bram Moolenaar8c711452005-01-14 21:53:12 +000017116 if (argvars[0].v_type == VAR_DICT)
17117 {
17118 if (argvars[2].v_type != VAR_UNKNOWN)
17119 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017120 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017121 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017123 key = get_tv_string_chk(&argvars[1]);
17124 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017126 di = dict_find(d, key, -1);
17127 if (di == NULL)
17128 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017129 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17130 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017131 {
17132 *rettv = di->di_tv;
17133 init_tv(&di->di_tv);
17134 dictitem_remove(d, di);
17135 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017136 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017137 }
17138 }
17139 else if (argvars[0].v_type != VAR_LIST)
17140 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017141 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017142 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017144 int error = FALSE;
17145
17146 idx = get_tv_number_chk(&argvars[1], &error);
17147 if (error)
17148 ; /* type error: do nothing, errmsg already given */
17149 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017150 EMSGN(_(e_listidx), idx);
17151 else
17152 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017153 if (argvars[2].v_type == VAR_UNKNOWN)
17154 {
17155 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017156 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017157 *rettv = item->li_tv;
17158 vim_free(item);
17159 }
17160 else
17161 {
17162 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017163 end = get_tv_number_chk(&argvars[2], &error);
17164 if (error)
17165 ; /* type error: do nothing */
17166 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017167 EMSGN(_(e_listidx), end);
17168 else
17169 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017170 int cnt = 0;
17171
17172 for (li = item; li != NULL; li = li->li_next)
17173 {
17174 ++cnt;
17175 if (li == item2)
17176 break;
17177 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017178 if (li == NULL) /* didn't find "item2" after "item" */
17179 EMSG(_(e_invrange));
17180 else
17181 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017182 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017183 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017184 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017185 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017186 l->lv_first = item;
17187 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017188 item->li_prev = NULL;
17189 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017190 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017191 }
17192 }
17193 }
17194 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017195 }
17196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197}
17198
17199/*
17200 * "rename({from}, {to})" function
17201 */
17202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017203f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204{
17205 char_u buf[NUMBUFLEN];
17206
17207 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017210 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17211 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212}
17213
17214/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017215 * "repeat()" function
17216 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017218f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017219{
17220 char_u *p;
17221 int n;
17222 int slen;
17223 int len;
17224 char_u *r;
17225 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017226
17227 n = get_tv_number(&argvars[1]);
17228 if (argvars[0].v_type == VAR_LIST)
17229 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017230 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017231 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017232 if (list_extend(rettv->vval.v_list,
17233 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017234 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017235 }
17236 else
17237 {
17238 p = get_tv_string(&argvars[0]);
17239 rettv->v_type = VAR_STRING;
17240 rettv->vval.v_string = NULL;
17241
17242 slen = (int)STRLEN(p);
17243 len = slen * n;
17244 if (len <= 0)
17245 return;
17246
17247 r = alloc(len + 1);
17248 if (r != NULL)
17249 {
17250 for (i = 0; i < n; i++)
17251 mch_memmove(r + i * slen, p, (size_t)slen);
17252 r[len] = NUL;
17253 }
17254
17255 rettv->vval.v_string = r;
17256 }
17257}
17258
17259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 * "resolve()" function
17261 */
17262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017263f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264{
17265 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017266#ifdef HAVE_READLINK
17267 char_u *buf = NULL;
17268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017270 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271#ifdef FEAT_SHORTCUT
17272 {
17273 char_u *v = NULL;
17274
17275 v = mch_resolve_shortcut(p);
17276 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017277 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017279 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 }
17281#else
17282# ifdef HAVE_READLINK
17283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 char_u *cpy;
17285 int len;
17286 char_u *remain = NULL;
17287 char_u *q;
17288 int is_relative_to_current = FALSE;
17289 int has_trailing_pathsep = FALSE;
17290 int limit = 100;
17291
17292 p = vim_strsave(p);
17293
17294 if (p[0] == '.' && (vim_ispathsep(p[1])
17295 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17296 is_relative_to_current = TRUE;
17297
17298 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017299 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017300 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017302 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304
17305 q = getnextcomp(p);
17306 if (*q != NUL)
17307 {
17308 /* Separate the first path component in "p", and keep the
17309 * remainder (beginning with the path separator). */
17310 remain = vim_strsave(q - 1);
17311 q[-1] = NUL;
17312 }
17313
Bram Moolenaard9462e32011-04-11 21:35:11 +020017314 buf = alloc(MAXPATHL + 1);
17315 if (buf == NULL)
17316 goto fail;
17317
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 for (;;)
17319 {
17320 for (;;)
17321 {
17322 len = readlink((char *)p, (char *)buf, MAXPATHL);
17323 if (len <= 0)
17324 break;
17325 buf[len] = NUL;
17326
17327 if (limit-- == 0)
17328 {
17329 vim_free(p);
17330 vim_free(remain);
17331 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017332 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 goto fail;
17334 }
17335
17336 /* Ensure that the result will have a trailing path separator
17337 * if the argument has one. */
17338 if (remain == NULL && has_trailing_pathsep)
17339 add_pathsep(buf);
17340
17341 /* Separate the first path component in the link value and
17342 * concatenate the remainders. */
17343 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17344 if (*q != NUL)
17345 {
17346 if (remain == NULL)
17347 remain = vim_strsave(q - 1);
17348 else
17349 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017350 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 if (cpy != NULL)
17352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 vim_free(remain);
17354 remain = cpy;
17355 }
17356 }
17357 q[-1] = NUL;
17358 }
17359
17360 q = gettail(p);
17361 if (q > p && *q == NUL)
17362 {
17363 /* Ignore trailing path separator. */
17364 q[-1] = NUL;
17365 q = gettail(p);
17366 }
17367 if (q > p && !mch_isFullName(buf))
17368 {
17369 /* symlink is relative to directory of argument */
17370 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17371 if (cpy != NULL)
17372 {
17373 STRCPY(cpy, p);
17374 STRCPY(gettail(cpy), buf);
17375 vim_free(p);
17376 p = cpy;
17377 }
17378 }
17379 else
17380 {
17381 vim_free(p);
17382 p = vim_strsave(buf);
17383 }
17384 }
17385
17386 if (remain == NULL)
17387 break;
17388
17389 /* Append the first path component of "remain" to "p". */
17390 q = getnextcomp(remain + 1);
17391 len = q - remain - (*q != NUL);
17392 cpy = vim_strnsave(p, STRLEN(p) + len);
17393 if (cpy != NULL)
17394 {
17395 STRNCAT(cpy, remain, len);
17396 vim_free(p);
17397 p = cpy;
17398 }
17399 /* Shorten "remain". */
17400 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017401 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 else
17403 {
17404 vim_free(remain);
17405 remain = NULL;
17406 }
17407 }
17408
17409 /* If the result is a relative path name, make it explicitly relative to
17410 * the current directory if and only if the argument had this form. */
17411 if (!vim_ispathsep(*p))
17412 {
17413 if (is_relative_to_current
17414 && *p != NUL
17415 && !(p[0] == '.'
17416 && (p[1] == NUL
17417 || vim_ispathsep(p[1])
17418 || (p[1] == '.'
17419 && (p[2] == NUL
17420 || vim_ispathsep(p[2]))))))
17421 {
17422 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017423 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 if (cpy != NULL)
17425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 vim_free(p);
17427 p = cpy;
17428 }
17429 }
17430 else if (!is_relative_to_current)
17431 {
17432 /* Strip leading "./". */
17433 q = p;
17434 while (q[0] == '.' && vim_ispathsep(q[1]))
17435 q += 2;
17436 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017437 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438 }
17439 }
17440
17441 /* Ensure that the result will have no trailing path separator
17442 * if the argument had none. But keep "/" or "//". */
17443 if (!has_trailing_pathsep)
17444 {
17445 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017446 if (after_pathsep(p, q))
17447 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 }
17449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017450 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 }
17452# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017453 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454# endif
17455#endif
17456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017457 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458
17459#ifdef HAVE_READLINK
17460fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017461 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017463 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464}
17465
17466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017467 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 */
17469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017470f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471{
Bram Moolenaar33570922005-01-25 22:26:29 +000017472 list_T *l;
17473 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474
Bram Moolenaar0d660222005-01-07 21:51:51 +000017475 if (argvars[0].v_type != VAR_LIST)
17476 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017477 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017478 && !tv_check_lock(l->lv_lock,
17479 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017480 {
17481 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017482 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017483 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017484 while (li != NULL)
17485 {
17486 ni = li->li_prev;
17487 list_append(l, li);
17488 li = ni;
17489 }
17490 rettv->vval.v_list = l;
17491 rettv->v_type = VAR_LIST;
17492 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017493 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495}
17496
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017497#define SP_NOMOVE 0x01 /* don't move cursor */
17498#define SP_REPEAT 0x02 /* repeat to find outer pair */
17499#define SP_RETCOUNT 0x04 /* return matchcount */
17500#define SP_SETPCMARK 0x08 /* set previous context mark */
17501#define SP_START 0x10 /* accept match at start position */
17502#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17503#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017504#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017505
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017506static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017507
17508/*
17509 * Get flags for a search function.
17510 * Possibly sets "p_ws".
17511 * Returns BACKWARD, FORWARD or zero (for an error).
17512 */
17513 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017514get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017515{
17516 int dir = FORWARD;
17517 char_u *flags;
17518 char_u nbuf[NUMBUFLEN];
17519 int mask;
17520
17521 if (varp->v_type != VAR_UNKNOWN)
17522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017523 flags = get_tv_string_buf_chk(varp, nbuf);
17524 if (flags == NULL)
17525 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526 while (*flags != NUL)
17527 {
17528 switch (*flags)
17529 {
17530 case 'b': dir = BACKWARD; break;
17531 case 'w': p_ws = TRUE; break;
17532 case 'W': p_ws = FALSE; break;
17533 default: mask = 0;
17534 if (flagsp != NULL)
17535 switch (*flags)
17536 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017537 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017538 case 'e': mask = SP_END; break;
17539 case 'm': mask = SP_RETCOUNT; break;
17540 case 'n': mask = SP_NOMOVE; break;
17541 case 'p': mask = SP_SUBPAT; break;
17542 case 'r': mask = SP_REPEAT; break;
17543 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017544 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017545 }
17546 if (mask == 0)
17547 {
17548 EMSG2(_(e_invarg2), flags);
17549 dir = 0;
17550 }
17551 else
17552 *flagsp |= mask;
17553 }
17554 if (dir == 0)
17555 break;
17556 ++flags;
17557 }
17558 }
17559 return dir;
17560}
17561
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017563 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017565 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017566search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017568 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 char_u *pat;
17570 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017571 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 int save_p_ws = p_ws;
17573 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017574 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017575 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017576 proftime_T tm;
17577#ifdef FEAT_RELTIME
17578 long time_limit = 0;
17579#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017580 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017581 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017583 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017584 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017585 if (dir == 0)
17586 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017587 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017588 if (flags & SP_START)
17589 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017590 if (flags & SP_END)
17591 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017592 if (flags & SP_COLUMN)
17593 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017594
Bram Moolenaar76929292008-01-06 19:07:36 +000017595 /* Optional arguments: line number to stop searching and timeout. */
17596 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017597 {
17598 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17599 if (lnum_stop < 0)
17600 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017601#ifdef FEAT_RELTIME
17602 if (argvars[3].v_type != VAR_UNKNOWN)
17603 {
17604 time_limit = get_tv_number_chk(&argvars[3], NULL);
17605 if (time_limit < 0)
17606 goto theend;
17607 }
17608#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017609 }
17610
Bram Moolenaar76929292008-01-06 19:07:36 +000017611#ifdef FEAT_RELTIME
17612 /* Set the time limit, if there is one. */
17613 profile_setlimit(time_limit, &tm);
17614#endif
17615
Bram Moolenaar231334e2005-07-25 20:46:57 +000017616 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017617 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017618 * Check to make sure only those flags are set.
17619 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17620 * flags cannot be set. Check for that condition also.
17621 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017622 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017623 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017625 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017626 goto theend;
17627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017629 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017630 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017631 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017632 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017634 if (flags & SP_SUBPAT)
17635 retval = subpatnum;
17636 else
17637 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017638 if (flags & SP_SETPCMARK)
17639 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017641 if (match_pos != NULL)
17642 {
17643 /* Store the match cursor position */
17644 match_pos->lnum = pos.lnum;
17645 match_pos->col = pos.col + 1;
17646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 /* "/$" will put the cursor after the end of the line, may need to
17648 * correct that here */
17649 check_cursor();
17650 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017651
17652 /* If 'n' flag is used: restore cursor position. */
17653 if (flags & SP_NOMOVE)
17654 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017655 else
17656 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017657theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017659
17660 return retval;
17661}
17662
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017663#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017664
17665/*
17666 * round() is not in C90, use ceil() or floor() instead.
17667 */
17668 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017669vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017670{
17671 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17672}
17673
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017674/*
17675 * "round({float})" function
17676 */
17677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017678f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017679{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017680 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017681
17682 rettv->v_type = VAR_FLOAT;
17683 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017684 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017685 else
17686 rettv->vval.v_float = 0.0;
17687}
17688#endif
17689
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017690/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017691 * "screenattr()" function
17692 */
17693 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017694f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017695{
17696 int row;
17697 int col;
17698 int c;
17699
17700 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17701 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17702 if (row < 0 || row >= screen_Rows
17703 || col < 0 || col >= screen_Columns)
17704 c = -1;
17705 else
17706 c = ScreenAttrs[LineOffset[row] + col];
17707 rettv->vval.v_number = c;
17708}
17709
17710/*
17711 * "screenchar()" function
17712 */
17713 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017714f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017715{
17716 int row;
17717 int col;
17718 int off;
17719 int c;
17720
17721 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17722 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17723 if (row < 0 || row >= screen_Rows
17724 || col < 0 || col >= screen_Columns)
17725 c = -1;
17726 else
17727 {
17728 off = LineOffset[row] + col;
17729#ifdef FEAT_MBYTE
17730 if (enc_utf8 && ScreenLinesUC[off] != 0)
17731 c = ScreenLinesUC[off];
17732 else
17733#endif
17734 c = ScreenLines[off];
17735 }
17736 rettv->vval.v_number = c;
17737}
17738
17739/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017740 * "screencol()" function
17741 *
17742 * First column is 1 to be consistent with virtcol().
17743 */
17744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017745f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017746{
17747 rettv->vval.v_number = screen_screencol() + 1;
17748}
17749
17750/*
17751 * "screenrow()" function
17752 */
17753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017754f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017755{
17756 rettv->vval.v_number = screen_screenrow() + 1;
17757}
17758
17759/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017760 * "search()" function
17761 */
17762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017763f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017764{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017765 int flags = 0;
17766
17767 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768}
17769
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017771 * "searchdecl()" function
17772 */
17773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017774f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017775{
17776 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017777 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017778 int error = FALSE;
17779 char_u *name;
17780
17781 rettv->vval.v_number = 1; /* default: FAIL */
17782
17783 name = get_tv_string_chk(&argvars[0]);
17784 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017785 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017786 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017787 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17788 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17789 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017790 if (!error && name != NULL)
17791 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017792 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017793}
17794
17795/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017796 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017799searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800{
17801 char_u *spat, *mpat, *epat;
17802 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804 int dir;
17805 int flags = 0;
17806 char_u nbuf1[NUMBUFLEN];
17807 char_u nbuf2[NUMBUFLEN];
17808 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017809 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017810 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017811 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017814 spat = get_tv_string_chk(&argvars[0]);
17815 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17816 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17817 if (spat == NULL || mpat == NULL || epat == NULL)
17818 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820 /* Handle the optional fourth argument: flags */
17821 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017822 if (dir == 0)
17823 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017824
17825 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017826 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17827 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017828 if ((flags & (SP_END | SP_SUBPAT)) != 0
17829 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017830 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017831 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017832 goto theend;
17833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017835 /* Using 'r' implies 'W', otherwise it doesn't work. */
17836 if (flags & SP_REPEAT)
17837 p_ws = FALSE;
17838
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017839 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017840 if (argvars[3].v_type == VAR_UNKNOWN
17841 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 skip = (char_u *)"";
17843 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017845 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017846 if (argvars[5].v_type != VAR_UNKNOWN)
17847 {
17848 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17849 if (lnum_stop < 0)
17850 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017851#ifdef FEAT_RELTIME
17852 if (argvars[6].v_type != VAR_UNKNOWN)
17853 {
17854 time_limit = get_tv_number_chk(&argvars[6], NULL);
17855 if (time_limit < 0)
17856 goto theend;
17857 }
17858#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017859 }
17860 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017861 if (skip == NULL)
17862 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017864 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017865 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017866
17867theend:
17868 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017869
17870 return retval;
17871}
17872
17873/*
17874 * "searchpair()" function
17875 */
17876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017877f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017878{
17879 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17880}
17881
17882/*
17883 * "searchpairpos()" function
17884 */
17885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017886f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017887{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017888 pos_T match_pos;
17889 int lnum = 0;
17890 int col = 0;
17891
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017892 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017893 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017894
17895 if (searchpair_cmn(argvars, &match_pos) > 0)
17896 {
17897 lnum = match_pos.lnum;
17898 col = match_pos.col;
17899 }
17900
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017901 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17902 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017903}
17904
17905/*
17906 * Search for a start/middle/end thing.
17907 * Used by searchpair(), see its documentation for the details.
17908 * Returns 0 or -1 for no match,
17909 */
17910 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017911do_searchpair(
17912 char_u *spat, /* start pattern */
17913 char_u *mpat, /* middle pattern */
17914 char_u *epat, /* end pattern */
17915 int dir, /* BACKWARD or FORWARD */
17916 char_u *skip, /* skip expression */
17917 int flags, /* SP_SETPCMARK and other SP_ values */
17918 pos_T *match_pos,
17919 linenr_T lnum_stop, /* stop at this line if not zero */
17920 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017921{
17922 char_u *save_cpo;
17923 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17924 long retval = 0;
17925 pos_T pos;
17926 pos_T firstpos;
17927 pos_T foundpos;
17928 pos_T save_cursor;
17929 pos_T save_pos;
17930 int n;
17931 int r;
17932 int nest = 1;
17933 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017934 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017935 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017936
17937 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17938 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017939 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017940
Bram Moolenaar76929292008-01-06 19:07:36 +000017941#ifdef FEAT_RELTIME
17942 /* Set the time limit, if there is one. */
17943 profile_setlimit(time_limit, &tm);
17944#endif
17945
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017946 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17947 * start/middle/end (pat3, for the top pair). */
17948 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17949 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17950 if (pat2 == NULL || pat3 == NULL)
17951 goto theend;
17952 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17953 if (*mpat == NUL)
17954 STRCPY(pat3, pat2);
17955 else
17956 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17957 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017958 if (flags & SP_START)
17959 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017960
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 save_cursor = curwin->w_cursor;
17962 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017963 clearpos(&firstpos);
17964 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965 pat = pat3;
17966 for (;;)
17967 {
17968 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017969 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17971 /* didn't find it or found the first match again: FAIL */
17972 break;
17973
17974 if (firstpos.lnum == 0)
17975 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017976 if (equalpos(pos, foundpos))
17977 {
17978 /* Found the same position again. Can happen with a pattern that
17979 * has "\zs" at the end and searching backwards. Advance one
17980 * character and try again. */
17981 if (dir == BACKWARD)
17982 decl(&pos);
17983 else
17984 incl(&pos);
17985 }
17986 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017988 /* clear the start flag to avoid getting stuck here */
17989 options &= ~SEARCH_START;
17990
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 /* If the skip pattern matches, ignore this match. */
17992 if (*skip != NUL)
17993 {
17994 save_pos = curwin->w_cursor;
17995 curwin->w_cursor = pos;
17996 r = eval_to_bool(skip, &err, NULL, FALSE);
17997 curwin->w_cursor = save_pos;
17998 if (err)
17999 {
18000 /* Evaluating {skip} caused an error, break here. */
18001 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018002 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003 break;
18004 }
18005 if (r)
18006 continue;
18007 }
18008
18009 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18010 {
18011 /* Found end when searching backwards or start when searching
18012 * forward: nested pair. */
18013 ++nest;
18014 pat = pat2; /* nested, don't search for middle */
18015 }
18016 else
18017 {
18018 /* Found end when searching forward or start when searching
18019 * backward: end of (nested) pair; or found middle in outer pair. */
18020 if (--nest == 1)
18021 pat = pat3; /* outer level, search for middle */
18022 }
18023
18024 if (nest == 0)
18025 {
18026 /* Found the match: return matchcount or line number. */
18027 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018028 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018030 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018031 if (flags & SP_SETPCMARK)
18032 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 curwin->w_cursor = pos;
18034 if (!(flags & SP_REPEAT))
18035 break;
18036 nest = 1; /* search for next unmatched */
18037 }
18038 }
18039
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018040 if (match_pos != NULL)
18041 {
18042 /* Store the match cursor position */
18043 match_pos->lnum = curwin->w_cursor.lnum;
18044 match_pos->col = curwin->w_cursor.col + 1;
18045 }
18046
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018048 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 curwin->w_cursor = save_cursor;
18050
18051theend:
18052 vim_free(pat2);
18053 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018054 if (p_cpo == empty_option)
18055 p_cpo = save_cpo;
18056 else
18057 /* Darn, evaluating the {skip} expression changed the value. */
18058 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018059
18060 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061}
18062
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018063/*
18064 * "searchpos()" function
18065 */
18066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018067f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018068{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018069 pos_T match_pos;
18070 int lnum = 0;
18071 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018072 int n;
18073 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018074
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018075 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018076 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018077
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018078 n = search_cmn(argvars, &match_pos, &flags);
18079 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018080 {
18081 lnum = match_pos.lnum;
18082 col = match_pos.col;
18083 }
18084
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018085 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18086 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018087 if (flags & SP_SUBPAT)
18088 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018089}
18090
Bram Moolenaar0d660222005-01-07 21:51:51 +000018091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018092f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018094#ifdef FEAT_CLIENTSERVER
18095 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018096 char_u *server = get_tv_string_chk(&argvars[0]);
18097 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018100 if (server == NULL || reply == NULL)
18101 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018102 if (check_restricted() || check_secure())
18103 return;
18104# ifdef FEAT_X11
18105 if (check_connection() == FAIL)
18106 return;
18107# endif
18108
18109 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018111 EMSG(_("E258: Unable to send to client"));
18112 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018114 rettv->vval.v_number = 0;
18115#else
18116 rettv->vval.v_number = -1;
18117#endif
18118}
18119
Bram Moolenaar0d660222005-01-07 21:51:51 +000018120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018121f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018122{
18123 char_u *r = NULL;
18124
18125#ifdef FEAT_CLIENTSERVER
18126# ifdef WIN32
18127 r = serverGetVimNames();
18128# else
18129 make_connection();
18130 if (X_DISPLAY != NULL)
18131 r = serverGetVimNames(X_DISPLAY);
18132# endif
18133#endif
18134 rettv->v_type = VAR_STRING;
18135 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136}
18137
18138/*
18139 * "setbufvar()" function
18140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018142f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143{
18144 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018147 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 char_u nbuf[NUMBUFLEN];
18149
18150 if (check_restricted() || check_secure())
18151 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018152 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18153 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018154 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155 varp = &argvars[2];
18156
18157 if (buf != NULL && varname != NULL && varp != NULL)
18158 {
18159 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161
18162 if (*varname == '&')
18163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018164 long numval;
18165 char_u *strval;
18166 int error = FALSE;
18167
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018169 numval = get_tv_number_chk(varp, &error);
18170 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018171 if (!error && strval != NULL)
18172 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 }
18174 else
18175 {
18176 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18177 if (bufvarname != NULL)
18178 {
18179 STRCPY(bufvarname, "b:");
18180 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018181 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182 vim_free(bufvarname);
18183 }
18184 }
18185
18186 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189}
18190
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018192f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018193{
18194 dict_T *d;
18195 dictitem_T *di;
18196 char_u *csearch;
18197
18198 if (argvars[0].v_type != VAR_DICT)
18199 {
18200 EMSG(_(e_dictreq));
18201 return;
18202 }
18203
18204 if ((d = argvars[0].vval.v_dict) != NULL)
18205 {
18206 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18207 if (csearch != NULL)
18208 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018209#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018210 if (enc_utf8)
18211 {
18212 int pcc[MAX_MCO];
18213 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018214
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018215 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18216 }
18217 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018218#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018219 set_last_csearch(PTR2CHAR(csearch),
18220 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018221 }
18222
18223 di = dict_find(d, (char_u *)"forward", -1);
18224 if (di != NULL)
18225 set_csearch_direction(get_tv_number(&di->di_tv)
18226 ? FORWARD : BACKWARD);
18227
18228 di = dict_find(d, (char_u *)"until", -1);
18229 if (di != NULL)
18230 set_csearch_until(!!get_tv_number(&di->di_tv));
18231 }
18232}
18233
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234/*
18235 * "setcmdpos()" function
18236 */
18237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018238f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018240 int pos = (int)get_tv_number(&argvars[0]) - 1;
18241
18242 if (pos >= 0)
18243 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244}
18245
18246/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018247 * "setfperm({fname}, {mode})" function
18248 */
18249 static void
18250f_setfperm(typval_T *argvars, typval_T *rettv)
18251{
18252 char_u *fname;
18253 char_u modebuf[NUMBUFLEN];
18254 char_u *mode_str;
18255 int i;
18256 int mask;
18257 int mode = 0;
18258
18259 rettv->vval.v_number = 0;
18260 fname = get_tv_string_chk(&argvars[0]);
18261 if (fname == NULL)
18262 return;
18263 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18264 if (mode_str == NULL)
18265 return;
18266 if (STRLEN(mode_str) != 9)
18267 {
18268 EMSG2(_(e_invarg2), mode_str);
18269 return;
18270 }
18271
18272 mask = 1;
18273 for (i = 8; i >= 0; --i)
18274 {
18275 if (mode_str[i] != '-')
18276 mode |= mask;
18277 mask = mask << 1;
18278 }
18279 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18280}
18281
18282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 * "setline()" function
18284 */
18285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018286f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287{
18288 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018289 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018290 list_T *l = NULL;
18291 listitem_T *li = NULL;
18292 long added = 0;
18293 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018295 lnum = get_tv_lnum(&argvars[0]);
18296 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018298 l = argvars[1].vval.v_list;
18299 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018301 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018302 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018303
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018304 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018305 for (;;)
18306 {
18307 if (l != NULL)
18308 {
18309 /* list argument, get next string */
18310 if (li == NULL)
18311 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018312 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018313 li = li->li_next;
18314 }
18315
18316 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018317 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018318 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018319
18320 /* When coming here from Insert mode, sync undo, so that this can be
18321 * undone separately from what was previously inserted. */
18322 if (u_sync_once == 2)
18323 {
18324 u_sync_once = 1; /* notify that u_sync() was called */
18325 u_sync(TRUE);
18326 }
18327
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018328 if (lnum <= curbuf->b_ml.ml_line_count)
18329 {
18330 /* existing line, replace it */
18331 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18332 {
18333 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018334 if (lnum == curwin->w_cursor.lnum)
18335 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018336 rettv->vval.v_number = 0; /* OK */
18337 }
18338 }
18339 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18340 {
18341 /* lnum is one past the last line, append the line */
18342 ++added;
18343 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18344 rettv->vval.v_number = 0; /* OK */
18345 }
18346
18347 if (l == NULL) /* only one string argument */
18348 break;
18349 ++lnum;
18350 }
18351
18352 if (added > 0)
18353 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354}
18355
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018356static 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 +000018357
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018359 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018360 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018362set_qf_ll_list(
18363 win_T *wp UNUSED,
18364 typval_T *list_arg UNUSED,
18365 typval_T *action_arg UNUSED,
18366 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018367{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018368#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018369 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018370 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018371 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018372#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018373
Bram Moolenaar2641f772005-03-25 21:58:17 +000018374 rettv->vval.v_number = -1;
18375
18376#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018377 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018378 EMSG(_(e_listreq));
18379 else
18380 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018381 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018382
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018383 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018384 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018385 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018386 if (act == NULL)
18387 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018388 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018389 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018390 else
18391 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018392 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018393 else if (action_arg->v_type == VAR_UNKNOWN)
18394 action = ' ';
18395 else
18396 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018397
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018398 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018399 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018400 rettv->vval.v_number = 0;
18401 }
18402#endif
18403}
18404
18405/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018406 * "setloclist()" function
18407 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018409f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018410{
18411 win_T *win;
18412
18413 rettv->vval.v_number = -1;
18414
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018415 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018416 if (win != NULL)
18417 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18418}
18419
18420/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018421 * "setmatches()" function
18422 */
18423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018424f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018425{
18426#ifdef FEAT_SEARCH_EXTRA
18427 list_T *l;
18428 listitem_T *li;
18429 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018430 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018431
18432 rettv->vval.v_number = -1;
18433 if (argvars[0].v_type != VAR_LIST)
18434 {
18435 EMSG(_(e_listreq));
18436 return;
18437 }
18438 if ((l = argvars[0].vval.v_list) != NULL)
18439 {
18440
18441 /* To some extent make sure that we are dealing with a list from
18442 * "getmatches()". */
18443 li = l->lv_first;
18444 while (li != NULL)
18445 {
18446 if (li->li_tv.v_type != VAR_DICT
18447 || (d = li->li_tv.vval.v_dict) == NULL)
18448 {
18449 EMSG(_(e_invarg));
18450 return;
18451 }
18452 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018453 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18454 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018455 && dict_find(d, (char_u *)"priority", -1) != NULL
18456 && dict_find(d, (char_u *)"id", -1) != NULL))
18457 {
18458 EMSG(_(e_invarg));
18459 return;
18460 }
18461 li = li->li_next;
18462 }
18463
18464 clear_matches(curwin);
18465 li = l->lv_first;
18466 while (li != NULL)
18467 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018468 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018469 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018470 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018471 char_u *group;
18472 int priority;
18473 int id;
18474 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018475
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018476 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018477 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18478 {
18479 if (s == NULL)
18480 {
18481 s = list_alloc();
18482 if (s == NULL)
18483 return;
18484 }
18485
18486 /* match from matchaddpos() */
18487 for (i = 1; i < 9; i++)
18488 {
18489 sprintf((char *)buf, (char *)"pos%d", i);
18490 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18491 {
18492 if (di->di_tv.v_type != VAR_LIST)
18493 return;
18494
18495 list_append_tv(s, &di->di_tv);
18496 s->lv_refcount++;
18497 }
18498 else
18499 break;
18500 }
18501 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018502
18503 group = get_dict_string(d, (char_u *)"group", FALSE);
18504 priority = (int)get_dict_number(d, (char_u *)"priority");
18505 id = (int)get_dict_number(d, (char_u *)"id");
18506 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18507 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18508 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018509 if (i == 0)
18510 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018511 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018512 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018513 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018514 }
18515 else
18516 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018517 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018518 list_unref(s);
18519 s = NULL;
18520 }
18521
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018522 li = li->li_next;
18523 }
18524 rettv->vval.v_number = 0;
18525 }
18526#endif
18527}
18528
18529/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018530 * "setpos()" function
18531 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018533f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018534{
18535 pos_T pos;
18536 int fnum;
18537 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018538 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018539
Bram Moolenaar08250432008-02-13 11:42:46 +000018540 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018541 name = get_tv_string_chk(argvars);
18542 if (name != NULL)
18543 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018544 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018545 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018546 if (--pos.col < 0)
18547 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018548 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018549 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018550 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018551 if (fnum == curbuf->b_fnum)
18552 {
18553 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018554 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018555 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018556 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018557 curwin->w_set_curswant = FALSE;
18558 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018559 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018560 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018561 }
18562 else
18563 EMSG(_(e_invarg));
18564 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018565 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18566 {
18567 /* set mark */
18568 if (setmark_pos(name[1], &pos, fnum) == OK)
18569 rettv->vval.v_number = 0;
18570 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018571 else
18572 EMSG(_(e_invarg));
18573 }
18574 }
18575}
18576
18577/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018578 * "setqflist()" function
18579 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018581f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018582{
18583 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18584}
18585
18586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 * "setreg()" function
18588 */
18589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018590f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591{
18592 int regname;
18593 char_u *strregname;
18594 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018595 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 int append;
18597 char_u yank_type;
18598 long block_len;
18599
18600 block_len = -1;
18601 yank_type = MAUTO;
18602 append = FALSE;
18603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018604 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018605 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018607 if (strregname == NULL)
18608 return; /* type error; errmsg already given */
18609 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610 if (regname == 0 || regname == '@')
18611 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018613 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018615 stropt = get_tv_string_chk(&argvars[2]);
18616 if (stropt == NULL)
18617 return; /* type error */
18618 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 switch (*stropt)
18620 {
18621 case 'a': case 'A': /* append */
18622 append = TRUE;
18623 break;
18624 case 'v': case 'c': /* character-wise selection */
18625 yank_type = MCHAR;
18626 break;
18627 case 'V': case 'l': /* line-wise selection */
18628 yank_type = MLINE;
18629 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 case 'b': case Ctrl_V: /* block-wise selection */
18631 yank_type = MBLOCK;
18632 if (VIM_ISDIGIT(stropt[1]))
18633 {
18634 ++stropt;
18635 block_len = getdigits(&stropt) - 1;
18636 --stropt;
18637 }
18638 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 }
18640 }
18641
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018642 if (argvars[1].v_type == VAR_LIST)
18643 {
18644 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018645 char_u **allocval;
18646 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018647 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018648 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018649 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018650 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018651 int len;
18652
18653 /* If the list is NULL handle like an empty list. */
18654 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018655
Bram Moolenaar7d647822014-04-05 21:28:56 +020018656 /* First half: use for pointers to result lines; second half: use for
18657 * pointers to allocated copies. */
18658 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018659 if (lstval == NULL)
18660 return;
18661 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018662 allocval = lstval + len + 2;
18663 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018664
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018665 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018666 li = li->li_next)
18667 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018668 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018669 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018670 goto free_lstval;
18671 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018672 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018673 /* Need to make a copy, next get_tv_string_buf_chk() will
18674 * overwrite the string. */
18675 strval = vim_strsave(buf);
18676 if (strval == NULL)
18677 goto free_lstval;
18678 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018679 }
18680 *curval++ = strval;
18681 }
18682 *curval++ = NULL;
18683
18684 write_reg_contents_lst(regname, lstval, -1,
18685 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018686free_lstval:
18687 while (curallocval > allocval)
18688 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018689 vim_free(lstval);
18690 }
18691 else
18692 {
18693 strval = get_tv_string_chk(&argvars[1]);
18694 if (strval == NULL)
18695 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018696 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018698 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018699 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700}
18701
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018702/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018703 * "settabvar()" function
18704 */
18705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018706f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018707{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018708#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018709 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018710 tabpage_T *tp;
18711#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018712 char_u *varname, *tabvarname;
18713 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018714
18715 rettv->vval.v_number = 0;
18716
18717 if (check_restricted() || check_secure())
18718 return;
18719
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018720#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018721 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018722#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018723 varname = get_tv_string_chk(&argvars[1]);
18724 varp = &argvars[2];
18725
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018726 if (varname != NULL && varp != NULL
18727#ifdef FEAT_WINDOWS
18728 && tp != NULL
18729#endif
18730 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018731 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018732#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018733 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018734 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018735#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018736
18737 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18738 if (tabvarname != NULL)
18739 {
18740 STRCPY(tabvarname, "t:");
18741 STRCPY(tabvarname + 2, varname);
18742 set_var(tabvarname, varp, TRUE);
18743 vim_free(tabvarname);
18744 }
18745
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018746#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018747 /* Restore current tabpage */
18748 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018749 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018750#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018751 }
18752}
18753
18754/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018755 * "settabwinvar()" function
18756 */
18757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018758f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018759{
18760 setwinvar(argvars, rettv, 1);
18761}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762
18763/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018764 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018767f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018769 setwinvar(argvars, rettv, 0);
18770}
18771
18772/*
18773 * "setwinvar()" and "settabwinvar()" functions
18774 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018775
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018777setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018778{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 win_T *win;
18780#ifdef FEAT_WINDOWS
18781 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018782 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018783 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784#endif
18785 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018786 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018788 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789
18790 if (check_restricted() || check_secure())
18791 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018792
18793#ifdef FEAT_WINDOWS
18794 if (off == 1)
18795 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18796 else
18797 tp = curtab;
18798#endif
18799 win = find_win_by_nr(&argvars[off], tp);
18800 varname = get_tv_string_chk(&argvars[off + 1]);
18801 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802
18803 if (win != NULL && varname != NULL && varp != NULL)
18804 {
18805#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018806 need_switch_win = !(tp == curtab && win == curwin);
18807 if (!need_switch_win
18808 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018811 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018813 long numval;
18814 char_u *strval;
18815 int error = FALSE;
18816
18817 ++varname;
18818 numval = get_tv_number_chk(varp, &error);
18819 strval = get_tv_string_buf_chk(varp, nbuf);
18820 if (!error && strval != NULL)
18821 set_option_value(varname, numval, strval, OPT_LOCAL);
18822 }
18823 else
18824 {
18825 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18826 if (winvarname != NULL)
18827 {
18828 STRCPY(winvarname, "w:");
18829 STRCPY(winvarname + 2, varname);
18830 set_var(winvarname, varp, TRUE);
18831 vim_free(winvarname);
18832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 }
18834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018836 if (need_switch_win)
18837 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838#endif
18839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840}
18841
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018842#ifdef FEAT_CRYPT
18843/*
18844 * "sha256({string})" function
18845 */
18846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018847f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018848{
18849 char_u *p;
18850
18851 p = get_tv_string(&argvars[0]);
18852 rettv->vval.v_string = vim_strsave(
18853 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18854 rettv->v_type = VAR_STRING;
18855}
18856#endif /* FEAT_CRYPT */
18857
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018859 * "shellescape({string})" function
18860 */
18861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018862f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018863{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018864 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018865 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018866 rettv->v_type = VAR_STRING;
18867}
18868
18869/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018870 * shiftwidth() function
18871 */
18872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018873f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018874{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018875 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018876}
18877
18878/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018879 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 */
18881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018882f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018884 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885
Bram Moolenaar0d660222005-01-07 21:51:51 +000018886 p = get_tv_string(&argvars[0]);
18887 rettv->vval.v_string = vim_strsave(p);
18888 simplify_filename(rettv->vval.v_string); /* simplify in place */
18889 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890}
18891
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018892#ifdef FEAT_FLOAT
18893/*
18894 * "sin()" function
18895 */
18896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018897f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018898{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018899 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018900
18901 rettv->v_type = VAR_FLOAT;
18902 if (get_float_arg(argvars, &f) == OK)
18903 rettv->vval.v_float = sin(f);
18904 else
18905 rettv->vval.v_float = 0.0;
18906}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018907
18908/*
18909 * "sinh()" function
18910 */
18911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018912f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018913{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018914 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018915
18916 rettv->v_type = VAR_FLOAT;
18917 if (get_float_arg(argvars, &f) == OK)
18918 rettv->vval.v_float = sinh(f);
18919 else
18920 rettv->vval.v_float = 0.0;
18921}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018922#endif
18923
Bram Moolenaar0d660222005-01-07 21:51:51 +000018924static int
18925#ifdef __BORLANDC__
18926 _RTLENTRYF
18927#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018928 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929static int
18930#ifdef __BORLANDC__
18931 _RTLENTRYF
18932#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018933 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018934
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018935/* struct used in the array that's given to qsort() */
18936typedef struct
18937{
18938 listitem_T *item;
18939 int idx;
18940} sortItem_T;
18941
Bram Moolenaar0b962472016-02-22 22:51:33 +010018942/* struct storing information about current sort */
18943typedef struct
18944{
18945 int item_compare_ic;
18946 int item_compare_numeric;
18947 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018948#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018949 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018950#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018951 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018952 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018953 dict_T *item_compare_selfdict;
18954 int item_compare_func_err;
18955 int item_compare_keep_zero;
18956} sortinfo_T;
18957static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018958static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018959#define ITEM_COMPARE_FAIL 999
18960
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018962 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018964 static int
18965#ifdef __BORLANDC__
18966_RTLENTRYF
18967#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018968item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018970 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018971 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018973 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018974 int res;
18975 char_u numbuf1[NUMBUFLEN];
18976 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018978 si1 = (sortItem_T *)s1;
18979 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018980 tv1 = &si1->item->li_tv;
18981 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018982
Bram Moolenaar0b962472016-02-22 22:51:33 +010018983 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018984 {
18985 long v1 = get_tv_number(tv1);
18986 long v2 = get_tv_number(tv2);
18987
18988 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18989 }
18990
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018991#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018992 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018993 {
18994 float_T v1 = get_tv_float(tv1);
18995 float_T v2 = get_tv_float(tv2);
18996
18997 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18998 }
18999#endif
19000
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019001 /* tv2string() puts quotes around a string and allocates memory. Don't do
19002 * that for string variables. Use a single quote when comparing with a
19003 * non-string to do what the docs promise. */
19004 if (tv1->v_type == VAR_STRING)
19005 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019006 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019007 p1 = (char_u *)"'";
19008 else
19009 p1 = tv1->vval.v_string;
19010 }
19011 else
19012 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19013 if (tv2->v_type == VAR_STRING)
19014 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019015 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019016 p2 = (char_u *)"'";
19017 else
19018 p2 = tv2->vval.v_string;
19019 }
19020 else
19021 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019022 if (p1 == NULL)
19023 p1 = (char_u *)"";
19024 if (p2 == NULL)
19025 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019026 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019027 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019028 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019029 res = STRICMP(p1, p2);
19030 else
19031 res = STRCMP(p1, p2);
19032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019034 {
19035 double n1, n2;
19036 n1 = strtod((char *)p1, (char **)&p1);
19037 n2 = strtod((char *)p2, (char **)&p2);
19038 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19039 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019040
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019041 /* When the result would be zero, compare the item indexes. Makes the
19042 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019043 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019044 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019045
Bram Moolenaar0d660222005-01-07 21:51:51 +000019046 vim_free(tofree1);
19047 vim_free(tofree2);
19048 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049}
19050
19051 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019052#ifdef __BORLANDC__
19053_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019055item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019056{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019057 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019058 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019059 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019060 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019061 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019062 char_u *func_name;
19063 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019065 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019066 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019067 return 0;
19068
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019069 si1 = (sortItem_T *)s1;
19070 si2 = (sortItem_T *)s2;
19071
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019072 if (partial == NULL)
19073 func_name = sortinfo->item_compare_func;
19074 else
19075 func_name = partial->pt_name;
19076
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019077 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019078 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019079 copy_tv(&si1->item->li_tv, &argv[0]);
19080 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081
19082 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019083 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019084 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019085 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019086 clear_tv(&argv[0]);
19087 clear_tv(&argv[1]);
19088
19089 if (res == FAIL)
19090 res = ITEM_COMPARE_FAIL;
19091 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019092 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19093 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019094 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019095 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019096
19097 /* When the result would be zero, compare the pointers themselves. Makes
19098 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019099 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019100 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019101
Bram Moolenaar0d660222005-01-07 21:51:51 +000019102 return res;
19103}
19104
19105/*
19106 * "sort({list})" function
19107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019109do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110{
Bram Moolenaar33570922005-01-25 22:26:29 +000019111 list_T *l;
19112 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019113 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 sortinfo_T *old_sortinfo;
19115 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019116 long len;
19117 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118
Bram Moolenaar0b962472016-02-22 22:51:33 +010019119 /* Pointer to current info struct used in compare function. Save and
19120 * restore the current one for nested calls. */
19121 old_sortinfo = sortinfo;
19122 sortinfo = &info;
19123
Bram Moolenaar0d660222005-01-07 21:51:51 +000019124 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019125 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 else
19127 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019128 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019129 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019130 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19131 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019132 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019133 rettv->vval.v_list = l;
19134 rettv->v_type = VAR_LIST;
19135 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136
Bram Moolenaar0d660222005-01-07 21:51:51 +000019137 len = list_len(l);
19138 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019139 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140
Bram Moolenaar0b962472016-02-22 22:51:33 +010019141 info.item_compare_ic = FALSE;
19142 info.item_compare_numeric = FALSE;
19143 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019144#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019145 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019146#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019147 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019148 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019149 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019150 if (argvars[1].v_type != VAR_UNKNOWN)
19151 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019152 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019153 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019154 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019155 else if (argvars[1].v_type == VAR_PARTIAL)
19156 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019157 else
19158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019159 int error = FALSE;
19160
19161 i = get_tv_number_chk(&argvars[1], &error);
19162 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019163 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019164 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019165 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019166 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019167 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019168 else if (i != 0)
19169 {
19170 EMSG(_(e_invarg));
19171 goto theend;
19172 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019173 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019174 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019175 if (*info.item_compare_func == NUL)
19176 {
19177 /* empty string means default sort */
19178 info.item_compare_func = NULL;
19179 }
19180 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019181 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019182 info.item_compare_func = NULL;
19183 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019184 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019185 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019186 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019187 info.item_compare_func = NULL;
19188 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019189 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019190#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019191 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019192 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019193 info.item_compare_func = NULL;
19194 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019195 }
19196#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019197 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019198 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019199 info.item_compare_func = NULL;
19200 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019201 }
19202 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019203 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019204
19205 if (argvars[2].v_type != VAR_UNKNOWN)
19206 {
19207 /* optional third argument: {dict} */
19208 if (argvars[2].v_type != VAR_DICT)
19209 {
19210 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019211 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019212 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019213 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019214 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216
Bram Moolenaar0d660222005-01-07 21:51:51 +000019217 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019218 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019219 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019220 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221
Bram Moolenaar327aa022014-03-25 18:24:23 +010019222 i = 0;
19223 if (sort)
19224 {
19225 /* sort(): ptrs will be the list to sort */
19226 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019227 {
19228 ptrs[i].item = li;
19229 ptrs[i].idx = i;
19230 ++i;
19231 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019232
Bram Moolenaar0b962472016-02-22 22:51:33 +010019233 info.item_compare_func_err = FALSE;
19234 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019235 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019236 if ((info.item_compare_func != NULL
19237 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019238 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019239 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019240 EMSG(_("E702: Sort compare function failed"));
19241 else
19242 {
19243 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019244 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019245 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019246 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019247 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019248
Bram Moolenaar0b962472016-02-22 22:51:33 +010019249 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019250 {
19251 /* Clear the List and append the items in sorted order. */
19252 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19253 l->lv_len = 0;
19254 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019255 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019256 }
19257 }
19258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019260 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019261 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019262
19263 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019264 info.item_compare_func_err = FALSE;
19265 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019266 item_compare_func_ptr = info.item_compare_func != NULL
19267 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019268 ? item_compare2 : item_compare;
19269
19270 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19271 li = li->li_next)
19272 {
19273 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19274 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019275 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019276 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019277 {
19278 EMSG(_("E882: Uniq compare function failed"));
19279 break;
19280 }
19281 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019282
Bram Moolenaar0b962472016-02-22 22:51:33 +010019283 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019284 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019285 while (--i >= 0)
19286 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019287 li = ptrs[i].item->li_next;
19288 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019289 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019290 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019291 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019292 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019293 list_fix_watch(l, li);
19294 listitem_free(li);
19295 l->lv_len--;
19296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019297 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019298 }
19299
19300 vim_free(ptrs);
19301 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019302theend:
19303 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019304}
19305
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019306/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019307 * "sort({list})" function
19308 */
19309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019310f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019311{
19312 do_sort_uniq(argvars, rettv, TRUE);
19313}
19314
19315/*
19316 * "uniq({list})" function
19317 */
19318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019319f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019320{
19321 do_sort_uniq(argvars, rettv, FALSE);
19322}
19323
19324/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019325 * "soundfold({word})" function
19326 */
19327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019328f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019329{
19330 char_u *s;
19331
19332 rettv->v_type = VAR_STRING;
19333 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019334#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019335 rettv->vval.v_string = eval_soundfold(s);
19336#else
19337 rettv->vval.v_string = vim_strsave(s);
19338#endif
19339}
19340
19341/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019342 * "spellbadword()" function
19343 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019345f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019346{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019347 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019348 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019349 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019350
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019351 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019352 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019353
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019354#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019355 if (argvars[0].v_type == VAR_UNKNOWN)
19356 {
19357 /* Find the start and length of the badly spelled word. */
19358 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19359 if (len != 0)
19360 word = ml_get_cursor();
19361 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019362 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019363 {
19364 char_u *str = get_tv_string_chk(&argvars[0]);
19365 int capcol = -1;
19366
19367 if (str != NULL)
19368 {
19369 /* Check the argument for spelling. */
19370 while (*str != NUL)
19371 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019372 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019373 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019374 {
19375 word = str;
19376 break;
19377 }
19378 str += len;
19379 }
19380 }
19381 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019382#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019383
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019384 list_append_string(rettv->vval.v_list, word, len);
19385 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019386 attr == HLF_SPB ? "bad" :
19387 attr == HLF_SPR ? "rare" :
19388 attr == HLF_SPL ? "local" :
19389 attr == HLF_SPC ? "caps" :
19390 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019391}
19392
19393/*
19394 * "spellsuggest()" function
19395 */
19396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019397f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019398{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019399#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019400 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019401 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019402 int maxcount;
19403 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019404 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019405 listitem_T *li;
19406 int need_capital = FALSE;
19407#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019408
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019409 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019410 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019411
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019412#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019413 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019414 {
19415 str = get_tv_string(&argvars[0]);
19416 if (argvars[1].v_type != VAR_UNKNOWN)
19417 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019418 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019419 if (maxcount <= 0)
19420 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019421 if (argvars[2].v_type != VAR_UNKNOWN)
19422 {
19423 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19424 if (typeerr)
19425 return;
19426 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019427 }
19428 else
19429 maxcount = 25;
19430
Bram Moolenaar4770d092006-01-12 23:22:24 +000019431 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019432
19433 for (i = 0; i < ga.ga_len; ++i)
19434 {
19435 str = ((char_u **)ga.ga_data)[i];
19436
19437 li = listitem_alloc();
19438 if (li == NULL)
19439 vim_free(str);
19440 else
19441 {
19442 li->li_tv.v_type = VAR_STRING;
19443 li->li_tv.v_lock = 0;
19444 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019445 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019446 }
19447 }
19448 ga_clear(&ga);
19449 }
19450#endif
19451}
19452
Bram Moolenaar0d660222005-01-07 21:51:51 +000019453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019454f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019455{
19456 char_u *str;
19457 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019458 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019459 regmatch_T regmatch;
19460 char_u patbuf[NUMBUFLEN];
19461 char_u *save_cpo;
19462 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019463 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019464 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019465 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019466
19467 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19468 save_cpo = p_cpo;
19469 p_cpo = (char_u *)"";
19470
19471 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019472 if (argvars[1].v_type != VAR_UNKNOWN)
19473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019474 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19475 if (pat == NULL)
19476 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019477 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019478 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019479 }
19480 if (pat == NULL || *pat == NUL)
19481 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019482
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019483 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019485 if (typeerr)
19486 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487
Bram Moolenaar0d660222005-01-07 21:51:51 +000019488 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19489 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019491 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019492 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019493 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019494 if (*str == NUL)
19495 match = FALSE; /* empty item at the end */
19496 else
19497 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019498 if (match)
19499 end = regmatch.startp[0];
19500 else
19501 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019502 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19503 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019504 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019505 if (list_append_string(rettv->vval.v_list, str,
19506 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019507 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019508 }
19509 if (!match)
19510 break;
19511 /* Advance to just after the match. */
19512 if (regmatch.endp[0] > str)
19513 col = 0;
19514 else
19515 {
19516 /* Don't get stuck at the same match. */
19517#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019518 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019519#else
19520 col = 1;
19521#endif
19522 }
19523 str = regmatch.endp[0];
19524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525
Bram Moolenaar473de612013-06-08 18:19:48 +020019526 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528
Bram Moolenaar0d660222005-01-07 21:51:51 +000019529 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530}
19531
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019532#ifdef FEAT_FLOAT
19533/*
19534 * "sqrt()" function
19535 */
19536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019537f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019538{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019539 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019540
19541 rettv->v_type = VAR_FLOAT;
19542 if (get_float_arg(argvars, &f) == OK)
19543 rettv->vval.v_float = sqrt(f);
19544 else
19545 rettv->vval.v_float = 0.0;
19546}
19547
19548/*
19549 * "str2float()" function
19550 */
19551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019552f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019553{
19554 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19555
19556 if (*p == '+')
19557 p = skipwhite(p + 1);
19558 (void)string2float(p, &rettv->vval.v_float);
19559 rettv->v_type = VAR_FLOAT;
19560}
19561#endif
19562
Bram Moolenaar2c932302006-03-18 21:42:09 +000019563/*
19564 * "str2nr()" function
19565 */
19566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019567f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019568{
19569 int base = 10;
19570 char_u *p;
19571 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019572 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019573
19574 if (argvars[1].v_type != VAR_UNKNOWN)
19575 {
19576 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019577 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019578 {
19579 EMSG(_(e_invarg));
19580 return;
19581 }
19582 }
19583
19584 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019585 if (*p == '+')
19586 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019587 switch (base)
19588 {
19589 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19590 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19591 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19592 default: what = 0;
19593 }
19594 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019595 rettv->vval.v_number = n;
19596}
19597
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598#ifdef HAVE_STRFTIME
19599/*
19600 * "strftime({format}[, {time}])" function
19601 */
19602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019603f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604{
19605 char_u result_buf[256];
19606 struct tm *curtime;
19607 time_t seconds;
19608 char_u *p;
19609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019610 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019612 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019613 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 seconds = time(NULL);
19615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019616 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 curtime = localtime(&seconds);
19618 /* MSVC returns NULL for an invalid value of seconds. */
19619 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019620 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 else
19622 {
19623# ifdef FEAT_MBYTE
19624 vimconv_T conv;
19625 char_u *enc;
19626
19627 conv.vc_type = CONV_NONE;
19628 enc = enc_locale();
19629 convert_setup(&conv, p_enc, enc);
19630 if (conv.vc_type != CONV_NONE)
19631 p = string_convert(&conv, p, NULL);
19632# endif
19633 if (p != NULL)
19634 (void)strftime((char *)result_buf, sizeof(result_buf),
19635 (char *)p, curtime);
19636 else
19637 result_buf[0] = NUL;
19638
19639# ifdef FEAT_MBYTE
19640 if (conv.vc_type != CONV_NONE)
19641 vim_free(p);
19642 convert_setup(&conv, enc, p_enc);
19643 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019644 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 else
19646# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019647 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648
19649# ifdef FEAT_MBYTE
19650 /* Release conversion descriptors */
19651 convert_setup(&conv, NULL, NULL);
19652 vim_free(enc);
19653# endif
19654 }
19655}
19656#endif
19657
19658/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019659 * "strgetchar()" function
19660 */
19661 static void
19662f_strgetchar(typval_T *argvars, typval_T *rettv)
19663{
19664 char_u *str;
19665 int len;
19666 int error = FALSE;
19667 int charidx;
19668
19669 rettv->vval.v_number = -1;
19670 str = get_tv_string_chk(&argvars[0]);
19671 if (str == NULL)
19672 return;
19673 len = (int)STRLEN(str);
19674 charidx = get_tv_number_chk(&argvars[1], &error);
19675 if (error)
19676 return;
19677#ifdef FEAT_MBYTE
19678 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019679 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019680
19681 while (charidx >= 0 && byteidx < len)
19682 {
19683 if (charidx == 0)
19684 {
19685 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19686 break;
19687 }
19688 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019689 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019690 }
19691 }
19692#else
19693 if (charidx < len)
19694 rettv->vval.v_number = str[charidx];
19695#endif
19696}
19697
19698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 * "stridx()" function
19700 */
19701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019702f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703{
19704 char_u buf[NUMBUFLEN];
19705 char_u *needle;
19706 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019707 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019709 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019711 needle = get_tv_string_chk(&argvars[1]);
19712 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019713 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019714 if (needle == NULL || haystack == NULL)
19715 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716
Bram Moolenaar33570922005-01-25 22:26:29 +000019717 if (argvars[2].v_type != VAR_UNKNOWN)
19718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019719 int error = FALSE;
19720
19721 start_idx = get_tv_number_chk(&argvars[2], &error);
19722 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019723 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019724 if (start_idx >= 0)
19725 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019726 }
19727
19728 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19729 if (pos != NULL)
19730 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731}
19732
19733/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019734 * "string()" function
19735 */
19736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019737f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019738{
19739 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019740 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019743 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19744 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019745 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019746 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019747 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748}
19749
19750/*
19751 * "strlen()" function
19752 */
19753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019754f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019756 rettv->vval.v_number = (varnumber_T)(STRLEN(
19757 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758}
19759
19760/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019761 * "strchars()" function
19762 */
19763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019764f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019765{
19766 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019767 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019768#ifdef FEAT_MBYTE
19769 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019770 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019771#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019772
19773 if (argvars[1].v_type != VAR_UNKNOWN)
19774 skipcc = get_tv_number_chk(&argvars[1], NULL);
19775 if (skipcc < 0 || skipcc > 1)
19776 EMSG(_(e_invarg));
19777 else
19778 {
19779#ifdef FEAT_MBYTE
19780 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19781 while (*s != NUL)
19782 {
19783 func_mb_ptr2char_adv(&s);
19784 ++len;
19785 }
19786 rettv->vval.v_number = len;
19787#else
19788 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19789#endif
19790 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019791}
19792
19793/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019794 * "strdisplaywidth()" function
19795 */
19796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019797f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019798{
19799 char_u *s = get_tv_string(&argvars[0]);
19800 int col = 0;
19801
19802 if (argvars[1].v_type != VAR_UNKNOWN)
19803 col = get_tv_number(&argvars[1]);
19804
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019805 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019806}
19807
19808/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019809 * "strwidth()" function
19810 */
19811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019812f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019813{
19814 char_u *s = get_tv_string(&argvars[0]);
19815
19816 rettv->vval.v_number = (varnumber_T)(
19817#ifdef FEAT_MBYTE
19818 mb_string2cells(s, -1)
19819#else
19820 STRLEN(s)
19821#endif
19822 );
19823}
19824
19825/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019826 * "strcharpart()" function
19827 */
19828 static void
19829f_strcharpart(typval_T *argvars, typval_T *rettv)
19830{
19831#ifdef FEAT_MBYTE
19832 char_u *p;
19833 int nchar;
19834 int nbyte = 0;
19835 int charlen;
19836 int len = 0;
19837 int slen;
19838 int error = FALSE;
19839
19840 p = get_tv_string(&argvars[0]);
19841 slen = (int)STRLEN(p);
19842
19843 nchar = get_tv_number_chk(&argvars[1], &error);
19844 if (!error)
19845 {
19846 if (nchar > 0)
19847 while (nchar > 0 && nbyte < slen)
19848 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019849 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019850 --nchar;
19851 }
19852 else
19853 nbyte = nchar;
19854 if (argvars[2].v_type != VAR_UNKNOWN)
19855 {
19856 charlen = get_tv_number(&argvars[2]);
19857 while (charlen > 0 && nbyte + len < slen)
19858 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019859 int off = nbyte + len;
19860
19861 if (off < 0)
19862 len += 1;
19863 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019864 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019865 --charlen;
19866 }
19867 }
19868 else
19869 len = slen - nbyte; /* default: all bytes that are available. */
19870 }
19871
19872 /*
19873 * Only return the overlap between the specified part and the actual
19874 * string.
19875 */
19876 if (nbyte < 0)
19877 {
19878 len += nbyte;
19879 nbyte = 0;
19880 }
19881 else if (nbyte > slen)
19882 nbyte = slen;
19883 if (len < 0)
19884 len = 0;
19885 else if (nbyte + len > slen)
19886 len = slen - nbyte;
19887
19888 rettv->v_type = VAR_STRING;
19889 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19890#else
19891 f_strpart(argvars, rettv);
19892#endif
19893}
19894
19895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896 * "strpart()" function
19897 */
19898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019899f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900{
19901 char_u *p;
19902 int n;
19903 int len;
19904 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019905 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019907 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 slen = (int)STRLEN(p);
19909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019910 n = get_tv_number_chk(&argvars[1], &error);
19911 if (error)
19912 len = 0;
19913 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019914 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 else
19916 len = slen - n; /* default len: all bytes that are available. */
19917
19918 /*
19919 * Only return the overlap between the specified part and the actual
19920 * string.
19921 */
19922 if (n < 0)
19923 {
19924 len += n;
19925 n = 0;
19926 }
19927 else if (n > slen)
19928 n = slen;
19929 if (len < 0)
19930 len = 0;
19931 else if (n + len > slen)
19932 len = slen - n;
19933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019934 rettv->v_type = VAR_STRING;
19935 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936}
19937
19938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019939 * "strridx()" function
19940 */
19941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019942f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019943{
19944 char_u buf[NUMBUFLEN];
19945 char_u *needle;
19946 char_u *haystack;
19947 char_u *rest;
19948 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019949 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019951 needle = get_tv_string_chk(&argvars[1]);
19952 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019953
19954 rettv->vval.v_number = -1;
19955 if (needle == NULL || haystack == NULL)
19956 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019957
19958 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019959 if (argvars[2].v_type != VAR_UNKNOWN)
19960 {
19961 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019962 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019963 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019964 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019965 }
19966 else
19967 end_idx = haystack_len;
19968
Bram Moolenaar0d660222005-01-07 21:51:51 +000019969 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019970 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019971 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019972 lastmatch = haystack + end_idx;
19973 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019974 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019975 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019976 for (rest = haystack; *rest != '\0'; ++rest)
19977 {
19978 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019979 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019980 break;
19981 lastmatch = rest;
19982 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019983 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019984
19985 if (lastmatch == NULL)
19986 rettv->vval.v_number = -1;
19987 else
19988 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19989}
19990
19991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 * "strtrans()" function
19993 */
19994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019995f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019997 rettv->v_type = VAR_STRING;
19998 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999}
20000
20001/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020002 * "submatch()" function
20003 */
20004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020005f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020006{
Bram Moolenaar41571762014-04-02 19:00:58 +020020007 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020008 int no;
20009 int retList = 0;
20010
20011 no = (int)get_tv_number_chk(&argvars[0], &error);
20012 if (error)
20013 return;
20014 error = FALSE;
20015 if (argvars[1].v_type != VAR_UNKNOWN)
20016 retList = get_tv_number_chk(&argvars[1], &error);
20017 if (error)
20018 return;
20019
20020 if (retList == 0)
20021 {
20022 rettv->v_type = VAR_STRING;
20023 rettv->vval.v_string = reg_submatch(no);
20024 }
20025 else
20026 {
20027 rettv->v_type = VAR_LIST;
20028 rettv->vval.v_list = reg_submatch_list(no);
20029 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020030}
20031
20032/*
20033 * "substitute()" function
20034 */
20035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020036f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020037{
20038 char_u patbuf[NUMBUFLEN];
20039 char_u subbuf[NUMBUFLEN];
20040 char_u flagsbuf[NUMBUFLEN];
20041
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020042 char_u *str = get_tv_string_chk(&argvars[0]);
20043 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20044 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20045 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20046
Bram Moolenaar0d660222005-01-07 21:51:51 +000020047 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020048 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20049 rettv->vval.v_string = NULL;
20050 else
20051 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020052}
20053
20054/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020055 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020058f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059{
20060 int id = 0;
20061#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020062 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 long col;
20064 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020065 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020067 lnum = get_tv_lnum(argvars); /* -1 on type error */
20068 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20069 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020071 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020072 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020073 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074#endif
20075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020076 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077}
20078
20079/*
20080 * "synIDattr(id, what [, mode])" function
20081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020083f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084{
20085 char_u *p = NULL;
20086#ifdef FEAT_SYN_HL
20087 int id;
20088 char_u *what;
20089 char_u *mode;
20090 char_u modebuf[NUMBUFLEN];
20091 int modec;
20092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020093 id = get_tv_number(&argvars[0]);
20094 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020095 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020097 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020099 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 modec = 0; /* replace invalid with current */
20101 }
20102 else
20103 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020104#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020105 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 modec = 'g';
20107 else
20108#endif
20109 if (t_colors > 1)
20110 modec = 'c';
20111 else
20112 modec = 't';
20113 }
20114
20115
20116 switch (TOLOWER_ASC(what[0]))
20117 {
20118 case 'b':
20119 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20120 p = highlight_color(id, what, modec);
20121 else /* bold */
20122 p = highlight_has_attr(id, HL_BOLD, modec);
20123 break;
20124
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020125 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 p = highlight_color(id, what, modec);
20127 break;
20128
20129 case 'i':
20130 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20131 p = highlight_has_attr(id, HL_INVERSE, modec);
20132 else /* italic */
20133 p = highlight_has_attr(id, HL_ITALIC, modec);
20134 break;
20135
20136 case 'n': /* name */
20137 p = get_highlight_name(NULL, id - 1);
20138 break;
20139
20140 case 'r': /* reverse */
20141 p = highlight_has_attr(id, HL_INVERSE, modec);
20142 break;
20143
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020144 case 's':
20145 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20146 p = highlight_color(id, what, modec);
20147 else /* standout */
20148 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 break;
20150
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020151 case 'u':
20152 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20153 /* underline */
20154 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20155 else
20156 /* undercurl */
20157 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158 break;
20159 }
20160
20161 if (p != NULL)
20162 p = vim_strsave(p);
20163#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020164 rettv->v_type = VAR_STRING;
20165 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166}
20167
20168/*
20169 * "synIDtrans(id)" function
20170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020172f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173{
20174 int id;
20175
20176#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020177 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178
20179 if (id > 0)
20180 id = syn_get_final_id(id);
20181 else
20182#endif
20183 id = 0;
20184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020185 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186}
20187
20188/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020189 * "synconcealed(lnum, col)" function
20190 */
20191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020192f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020193{
20194#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20195 long lnum;
20196 long col;
20197 int syntax_flags = 0;
20198 int cchar;
20199 int matchid = 0;
20200 char_u str[NUMBUFLEN];
20201#endif
20202
20203 rettv->v_type = VAR_LIST;
20204 rettv->vval.v_list = NULL;
20205
20206#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20207 lnum = get_tv_lnum(argvars); /* -1 on type error */
20208 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20209
20210 vim_memset(str, NUL, sizeof(str));
20211
20212 if (rettv_list_alloc(rettv) != FAIL)
20213 {
20214 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20215 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20216 && curwin->w_p_cole > 0)
20217 {
20218 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20219 syntax_flags = get_syntax_info(&matchid);
20220
20221 /* get the conceal character */
20222 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20223 {
20224 cchar = syn_get_sub_char();
20225 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20226 cchar = lcs_conceal;
20227 if (cchar != NUL)
20228 {
20229# ifdef FEAT_MBYTE
20230 if (has_mbyte)
20231 (*mb_char2bytes)(cchar, str);
20232 else
20233# endif
20234 str[0] = cchar;
20235 }
20236 }
20237 }
20238
20239 list_append_number(rettv->vval.v_list,
20240 (syntax_flags & HL_CONCEAL) != 0);
20241 /* -1 to auto-determine strlen */
20242 list_append_string(rettv->vval.v_list, str, -1);
20243 list_append_number(rettv->vval.v_list, matchid);
20244 }
20245#endif
20246}
20247
20248/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020249 * "synstack(lnum, col)" function
20250 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020252f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020253{
20254#ifdef FEAT_SYN_HL
20255 long lnum;
20256 long col;
20257 int i;
20258 int id;
20259#endif
20260
20261 rettv->v_type = VAR_LIST;
20262 rettv->vval.v_list = NULL;
20263
20264#ifdef FEAT_SYN_HL
20265 lnum = get_tv_lnum(argvars); /* -1 on type error */
20266 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20267
20268 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020269 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020270 && rettv_list_alloc(rettv) != FAIL)
20271 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020272 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020273 for (i = 0; ; ++i)
20274 {
20275 id = syn_get_stack_item(i);
20276 if (id < 0)
20277 break;
20278 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20279 break;
20280 }
20281 }
20282#endif
20283}
20284
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020286get_cmd_output_as_rettv(
20287 typval_T *argvars,
20288 typval_T *rettv,
20289 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020291 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020293 char_u *infile = NULL;
20294 char_u buf[NUMBUFLEN];
20295 int err = FALSE;
20296 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020297 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020298 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020300 rettv->v_type = VAR_STRING;
20301 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020302 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020303 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020304
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020305 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020306 {
20307 /*
20308 * Write the string to a temp file, to be used for input of the shell
20309 * command.
20310 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020311 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020312 {
20313 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020314 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020315 }
20316
20317 fd = mch_fopen((char *)infile, WRITEBIN);
20318 if (fd == NULL)
20319 {
20320 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020321 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020322 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020323 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020324 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020325 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20326 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020327 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020328 else
20329 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020330 size_t len;
20331
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020332 p = get_tv_string_buf_chk(&argvars[1], buf);
20333 if (p == NULL)
20334 {
20335 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020336 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020337 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020338 len = STRLEN(p);
20339 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020340 err = TRUE;
20341 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020342 if (fclose(fd) != 0)
20343 err = TRUE;
20344 if (err)
20345 {
20346 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020347 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020348 }
20349 }
20350
Bram Moolenaar52a72462014-08-29 15:53:52 +020020351 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20352 * echoes typeahead, that messes up the display. */
20353 if (!msg_silent)
20354 flags += SHELL_COOKED;
20355
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020356 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020358 int len;
20359 listitem_T *li;
20360 char_u *s = NULL;
20361 char_u *start;
20362 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020363 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364
Bram Moolenaar52a72462014-08-29 15:53:52 +020020365 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020366 if (res == NULL)
20367 goto errret;
20368
20369 list = list_alloc();
20370 if (list == NULL)
20371 goto errret;
20372
20373 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020375 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020376 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020377 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020378 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020379
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020380 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020381 if (s == NULL)
20382 goto errret;
20383
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020384 for (p = s; start < end; ++p, ++start)
20385 *p = *start == NUL ? NL : *start;
20386 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020387
20388 li = listitem_alloc();
20389 if (li == NULL)
20390 {
20391 vim_free(s);
20392 goto errret;
20393 }
20394 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020395 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020396 li->li_tv.vval.v_string = s;
20397 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020399
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020400 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020401 rettv->v_type = VAR_LIST;
20402 rettv->vval.v_list = list;
20403 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020405 else
20406 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020407 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020408#ifdef USE_CR
20409 /* translate <CR> into <NL> */
20410 if (res != NULL)
20411 {
20412 char_u *s;
20413
20414 for (s = res; *s; ++s)
20415 {
20416 if (*s == CAR)
20417 *s = NL;
20418 }
20419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420#else
20421# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020422 /* translate <CR><NL> into <NL> */
20423 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020425 char_u *s, *d;
20426
20427 d = res;
20428 for (s = res; *s; ++s)
20429 {
20430 if (s[0] == CAR && s[1] == NL)
20431 ++s;
20432 *d++ = *s;
20433 }
20434 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436# endif
20437#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020438 rettv->vval.v_string = res;
20439 res = NULL;
20440 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020441
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020442errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020443 if (infile != NULL)
20444 {
20445 mch_remove(infile);
20446 vim_free(infile);
20447 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020448 if (res != NULL)
20449 vim_free(res);
20450 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020451 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020452}
20453
20454/*
20455 * "system()" function
20456 */
20457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020458f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020459{
20460 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20461}
20462
20463/*
20464 * "systemlist()" function
20465 */
20466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020467f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020468{
20469 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470}
20471
20472/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020473 * "tabpagebuflist()" function
20474 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020476f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020477{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020478#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020479 tabpage_T *tp;
20480 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020481
20482 if (argvars[0].v_type == VAR_UNKNOWN)
20483 wp = firstwin;
20484 else
20485 {
20486 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20487 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020488 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020489 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020490 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020491 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020492 for (; wp != NULL; wp = wp->w_next)
20493 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020494 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020495 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020496 }
20497#endif
20498}
20499
20500
20501/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020502 * "tabpagenr()" function
20503 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020505f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020506{
20507 int nr = 1;
20508#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020509 char_u *arg;
20510
20511 if (argvars[0].v_type != VAR_UNKNOWN)
20512 {
20513 arg = get_tv_string_chk(&argvars[0]);
20514 nr = 0;
20515 if (arg != NULL)
20516 {
20517 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020518 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020519 else
20520 EMSG2(_(e_invexpr2), arg);
20521 }
20522 }
20523 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020524 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020525#endif
20526 rettv->vval.v_number = nr;
20527}
20528
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020529
20530#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020531static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020532
20533/*
20534 * Common code for tabpagewinnr() and winnr().
20535 */
20536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020537get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020538{
20539 win_T *twin;
20540 int nr = 1;
20541 win_T *wp;
20542 char_u *arg;
20543
20544 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20545 if (argvar->v_type != VAR_UNKNOWN)
20546 {
20547 arg = get_tv_string_chk(argvar);
20548 if (arg == NULL)
20549 nr = 0; /* type error; errmsg already given */
20550 else if (STRCMP(arg, "$") == 0)
20551 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20552 else if (STRCMP(arg, "#") == 0)
20553 {
20554 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20555 if (twin == NULL)
20556 nr = 0;
20557 }
20558 else
20559 {
20560 EMSG2(_(e_invexpr2), arg);
20561 nr = 0;
20562 }
20563 }
20564
20565 if (nr > 0)
20566 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20567 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020568 {
20569 if (wp == NULL)
20570 {
20571 /* didn't find it in this tabpage */
20572 nr = 0;
20573 break;
20574 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020575 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020576 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020577 return nr;
20578}
20579#endif
20580
20581/*
20582 * "tabpagewinnr()" function
20583 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020585f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020586{
20587 int nr = 1;
20588#ifdef FEAT_WINDOWS
20589 tabpage_T *tp;
20590
20591 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20592 if (tp == NULL)
20593 nr = 0;
20594 else
20595 nr = get_winnr(tp, &argvars[1]);
20596#endif
20597 rettv->vval.v_number = nr;
20598}
20599
20600
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020601/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020602 * "tagfiles()" function
20603 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020605f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020606{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020607 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020608 tagname_T tn;
20609 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020610
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020611 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020612 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020613 fname = alloc(MAXPATHL);
20614 if (fname == NULL)
20615 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020616
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020617 for (first = TRUE; ; first = FALSE)
20618 if (get_tagfname(&tn, first, fname) == FAIL
20619 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020620 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020621 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020622 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020623}
20624
20625/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020626 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020627 */
20628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020629f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020630{
20631 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020632
20633 tag_pattern = get_tv_string(&argvars[0]);
20634
20635 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020636 if (*tag_pattern == NUL)
20637 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020638
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020639 if (rettv_list_alloc(rettv) == OK)
20640 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020641}
20642
20643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 * "tempname()" function
20645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020647f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648{
20649 static int x = 'A';
20650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020651 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020652 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653
20654 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20655 * names. Skip 'I' and 'O', they are used for shell redirection. */
20656 do
20657 {
20658 if (x == 'Z')
20659 x = '0';
20660 else if (x == '9')
20661 x = 'A';
20662 else
20663 {
20664#ifdef EBCDIC
20665 if (x == 'I')
20666 x = 'J';
20667 else if (x == 'R')
20668 x = 'S';
20669 else
20670#endif
20671 ++x;
20672 }
20673 } while (x == 'I' || x == 'O');
20674}
20675
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020676#ifdef FEAT_FLOAT
20677/*
20678 * "tan()" function
20679 */
20680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020681f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020682{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020683 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020684
20685 rettv->v_type = VAR_FLOAT;
20686 if (get_float_arg(argvars, &f) == OK)
20687 rettv->vval.v_float = tan(f);
20688 else
20689 rettv->vval.v_float = 0.0;
20690}
20691
20692/*
20693 * "tanh()" function
20694 */
20695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020696f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020697{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020698 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020699
20700 rettv->v_type = VAR_FLOAT;
20701 if (get_float_arg(argvars, &f) == OK)
20702 rettv->vval.v_float = tanh(f);
20703 else
20704 rettv->vval.v_float = 0.0;
20705}
20706#endif
20707
Bram Moolenaar574860b2016-05-24 17:33:34 +020020708/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020709 * "test_alloc_fail(id, countdown, repeat)" function
20710 */
20711 static void
20712f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20713{
20714 if (argvars[0].v_type != VAR_NUMBER
20715 || argvars[0].vval.v_number <= 0
20716 || argvars[1].v_type != VAR_NUMBER
20717 || argvars[1].vval.v_number < 0
20718 || argvars[2].v_type != VAR_NUMBER)
20719 EMSG(_(e_invarg));
20720 else
20721 {
20722 alloc_fail_id = argvars[0].vval.v_number;
20723 if (alloc_fail_id >= aid_last)
20724 EMSG(_(e_invarg));
20725 alloc_fail_countdown = argvars[1].vval.v_number;
20726 alloc_fail_repeat = argvars[2].vval.v_number;
20727 did_outofmem_msg = FALSE;
20728 }
20729}
20730
20731/*
20732 * "test_disable_char_avail({expr})" function
20733 */
20734 static void
20735f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20736{
20737 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20738}
20739
20740/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020741 * "test_garbagecollect_now()" function
20742 */
20743 static void
20744f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20745{
20746 /* This is dangerous, any Lists and Dicts used internally may be freed
20747 * while still in use. */
20748 garbage_collect(TRUE);
20749}
20750
20751#ifdef FEAT_JOB_CHANNEL
20752 static void
20753f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20754{
20755 rettv->v_type = VAR_CHANNEL;
20756 rettv->vval.v_channel = NULL;
20757}
20758#endif
20759
20760 static void
20761f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20762{
20763 rettv->v_type = VAR_DICT;
20764 rettv->vval.v_dict = NULL;
20765}
20766
20767#ifdef FEAT_JOB_CHANNEL
20768 static void
20769f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20770{
20771 rettv->v_type = VAR_JOB;
20772 rettv->vval.v_job = NULL;
20773}
20774#endif
20775
20776 static void
20777f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20778{
20779 rettv->v_type = VAR_LIST;
20780 rettv->vval.v_list = NULL;
20781}
20782
20783 static void
20784f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20785{
20786 rettv->v_type = VAR_PARTIAL;
20787 rettv->vval.v_partial = NULL;
20788}
20789
20790 static void
20791f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20792{
20793 rettv->v_type = VAR_STRING;
20794 rettv->vval.v_string = NULL;
20795}
20796
Bram Moolenaar975b5272016-03-15 23:10:59 +010020797#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20798/*
20799 * Get a callback from "arg". It can be a Funcref or a function name.
20800 * When "arg" is zero return an empty string.
20801 * Return NULL for an invalid argument.
20802 */
20803 char_u *
20804get_callback(typval_T *arg, partial_T **pp)
20805{
20806 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20807 {
20808 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020809 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020810 return (*pp)->pt_name;
20811 }
20812 *pp = NULL;
20813 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20814 return arg->vval.v_string;
20815 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20816 return (char_u *)"";
20817 EMSG(_("E921: Invalid callback argument"));
20818 return NULL;
20819}
20820#endif
20821
20822#ifdef FEAT_TIMERS
20823/*
20824 * "timer_start(time, callback [, options])" function
20825 */
20826 static void
20827f_timer_start(typval_T *argvars, typval_T *rettv)
20828{
20829 long msec = get_tv_number(&argvars[0]);
20830 timer_T *timer;
20831 int repeat = 0;
20832 char_u *callback;
20833 dict_T *dict;
20834
Bram Moolenaar38499922016-04-22 20:46:52 +020020835 if (check_secure())
20836 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020837 if (argvars[2].v_type != VAR_UNKNOWN)
20838 {
20839 if (argvars[2].v_type != VAR_DICT
20840 || (dict = argvars[2].vval.v_dict) == NULL)
20841 {
20842 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20843 return;
20844 }
20845 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20846 repeat = get_dict_number(dict, (char_u *)"repeat");
20847 }
20848
20849 timer = create_timer(msec, repeat);
20850 callback = get_callback(&argvars[1], &timer->tr_partial);
20851 if (callback == NULL)
20852 {
20853 stop_timer(timer);
20854 rettv->vval.v_number = -1;
20855 }
20856 else
20857 {
20858 timer->tr_callback = vim_strsave(callback);
20859 rettv->vval.v_number = timer->tr_id;
20860 }
20861}
20862
20863/*
20864 * "timer_stop(timer)" function
20865 */
20866 static void
20867f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20868{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020869 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020870
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020871 if (argvars[0].v_type != VAR_NUMBER)
20872 {
20873 EMSG(_(e_number_exp));
20874 return;
20875 }
20876 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020877 if (timer != NULL)
20878 stop_timer(timer);
20879}
20880#endif
20881
Bram Moolenaard52d9742005-08-21 22:20:28 +000020882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 * "tolower(string)" function
20884 */
20885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020886f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887{
20888 char_u *p;
20889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020890 p = vim_strsave(get_tv_string(&argvars[0]));
20891 rettv->v_type = VAR_STRING;
20892 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893
20894 if (p != NULL)
20895 while (*p != NUL)
20896 {
20897#ifdef FEAT_MBYTE
20898 int l;
20899
20900 if (enc_utf8)
20901 {
20902 int c, lc;
20903
20904 c = utf_ptr2char(p);
20905 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020906 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 /* TODO: reallocate string when byte count changes. */
20908 if (utf_char2len(lc) == l)
20909 utf_char2bytes(lc, p);
20910 p += l;
20911 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020912 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 p += l; /* skip multi-byte character */
20914 else
20915#endif
20916 {
20917 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20918 ++p;
20919 }
20920 }
20921}
20922
20923/*
20924 * "toupper(string)" function
20925 */
20926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020927f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020929 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020930 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931}
20932
20933/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020934 * "tr(string, fromstr, tostr)" function
20935 */
20936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020937f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020938{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020939 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020940 char_u *fromstr;
20941 char_u *tostr;
20942 char_u *p;
20943#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020944 int inlen;
20945 int fromlen;
20946 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020947 int idx;
20948 char_u *cpstr;
20949 int cplen;
20950 int first = TRUE;
20951#endif
20952 char_u buf[NUMBUFLEN];
20953 char_u buf2[NUMBUFLEN];
20954 garray_T ga;
20955
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020956 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020957 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20958 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020959
20960 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020961 rettv->v_type = VAR_STRING;
20962 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020963 if (fromstr == NULL || tostr == NULL)
20964 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020965 ga_init2(&ga, (int)sizeof(char), 80);
20966
20967#ifdef FEAT_MBYTE
20968 if (!has_mbyte)
20969#endif
20970 /* not multi-byte: fromstr and tostr must be the same length */
20971 if (STRLEN(fromstr) != STRLEN(tostr))
20972 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020973#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020974error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020975#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020976 EMSG2(_(e_invarg2), fromstr);
20977 ga_clear(&ga);
20978 return;
20979 }
20980
20981 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020982 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020983 {
20984#ifdef FEAT_MBYTE
20985 if (has_mbyte)
20986 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020987 inlen = (*mb_ptr2len)(in_str);
20988 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020989 cplen = inlen;
20990 idx = 0;
20991 for (p = fromstr; *p != NUL; p += fromlen)
20992 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020993 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020994 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020995 {
20996 for (p = tostr; *p != NUL; p += tolen)
20997 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020998 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020999 if (idx-- == 0)
21000 {
21001 cplen = tolen;
21002 cpstr = p;
21003 break;
21004 }
21005 }
21006 if (*p == NUL) /* tostr is shorter than fromstr */
21007 goto error;
21008 break;
21009 }
21010 ++idx;
21011 }
21012
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021013 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021014 {
21015 /* Check that fromstr and tostr have the same number of
21016 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021017 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021018 first = FALSE;
21019 for (p = tostr; *p != NUL; p += tolen)
21020 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021021 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021022 --idx;
21023 }
21024 if (idx != 0)
21025 goto error;
21026 }
21027
Bram Moolenaarcde88542015-08-11 19:14:00 +020021028 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021029 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021030 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021031
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021032 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021033 }
21034 else
21035#endif
21036 {
21037 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021038 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021039 if (p != NULL)
21040 ga_append(&ga, tostr[p - fromstr]);
21041 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021042 ga_append(&ga, *in_str);
21043 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021044 }
21045 }
21046
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021047 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021048 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021049 ga_append(&ga, NUL);
21050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021051 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021052}
21053
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021054#ifdef FEAT_FLOAT
21055/*
21056 * "trunc({float})" function
21057 */
21058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021059f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021060{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021061 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021062
21063 rettv->v_type = VAR_FLOAT;
21064 if (get_float_arg(argvars, &f) == OK)
21065 /* trunc() is not in C90, use floor() or ceil() instead. */
21066 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21067 else
21068 rettv->vval.v_float = 0.0;
21069}
21070#endif
21071
Bram Moolenaar8299df92004-07-10 09:47:34 +000021072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 * "type(expr)" function
21074 */
21075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021076f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021078 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021079
21080 switch (argvars[0].v_type)
21081 {
21082 case VAR_NUMBER: n = 0; break;
21083 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021084 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021085 case VAR_FUNC: n = 2; break;
21086 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021087 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021088 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021089 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021090 if (argvars[0].vval.v_number == VVAL_FALSE
21091 || argvars[0].vval.v_number == VVAL_TRUE)
21092 n = 6;
21093 else
21094 n = 7;
21095 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021096 case VAR_JOB: n = 8; break;
21097 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021098 case VAR_UNKNOWN:
21099 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21100 n = -1;
21101 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021102 }
21103 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104}
21105
21106/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021107 * "undofile(name)" function
21108 */
21109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021110f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021111{
21112 rettv->v_type = VAR_STRING;
21113#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021114 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021115 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021116
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021117 if (*fname == NUL)
21118 {
21119 /* If there is no file name there will be no undo file. */
21120 rettv->vval.v_string = NULL;
21121 }
21122 else
21123 {
21124 char_u *ffname = FullName_save(fname, FALSE);
21125
21126 if (ffname != NULL)
21127 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21128 vim_free(ffname);
21129 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021130 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021131#else
21132 rettv->vval.v_string = NULL;
21133#endif
21134}
21135
21136/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021137 * "undotree()" function
21138 */
21139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021140f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021141{
21142 if (rettv_dict_alloc(rettv) == OK)
21143 {
21144 dict_T *dict = rettv->vval.v_dict;
21145 list_T *list;
21146
Bram Moolenaar730cde92010-06-27 05:18:54 +020021147 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021148 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021149 dict_add_nr_str(dict, "save_last",
21150 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021151 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21152 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021153 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021154
21155 list = list_alloc();
21156 if (list != NULL)
21157 {
21158 u_eval_tree(curbuf->b_u_oldhead, list);
21159 dict_add_list(dict, "entries", list);
21160 }
21161 }
21162}
21163
21164/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021165 * "values(dict)" function
21166 */
21167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021168f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021169{
21170 dict_list(argvars, rettv, 1);
21171}
21172
21173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 * "virtcol(string)" function
21175 */
21176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021177f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178{
21179 colnr_T vcol = 0;
21180 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021181 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021183 fp = var2fpos(&argvars[0], FALSE, &fnum);
21184 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21185 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186 {
21187 getvvcol(curwin, fp, NULL, NULL, &vcol);
21188 ++vcol;
21189 }
21190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021191 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192}
21193
21194/*
21195 * "visualmode()" function
21196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021198f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200 char_u str[2];
21201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021202 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 str[0] = curbuf->b_visual_mode_eval;
21204 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021205 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206
21207 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021208 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210}
21211
21212/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021213 * "wildmenumode()" function
21214 */
21215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021216f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021217{
21218#ifdef FEAT_WILDMENU
21219 if (wild_menu_showing)
21220 rettv->vval.v_number = 1;
21221#endif
21222}
21223
21224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 * "winbufnr(nr)" function
21226 */
21227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021228f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229{
21230 win_T *wp;
21231
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021232 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021234 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021236 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237}
21238
21239/*
21240 * "wincol()" function
21241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021243f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244{
21245 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021246 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247}
21248
21249/*
21250 * "winheight(nr)" function
21251 */
21252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021253f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254{
21255 win_T *wp;
21256
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021257 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021259 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021261 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262}
21263
21264/*
21265 * "winline()" function
21266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021268f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269{
21270 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021271 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272}
21273
21274/*
21275 * "winnr()" function
21276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021278f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279{
21280 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021281
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021283 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021285 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286}
21287
21288/*
21289 * "winrestcmd()" function
21290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021292f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293{
21294#ifdef FEAT_WINDOWS
21295 win_T *wp;
21296 int winnr = 1;
21297 garray_T ga;
21298 char_u buf[50];
21299
21300 ga_init2(&ga, (int)sizeof(char), 70);
21301 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21302 {
21303 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21304 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21306 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 ++winnr;
21308 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021309 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021311 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021313 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021315 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316}
21317
21318/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021319 * "winrestview()" function
21320 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021322f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021323{
21324 dict_T *dict;
21325
21326 if (argvars[0].v_type != VAR_DICT
21327 || (dict = argvars[0].vval.v_dict) == NULL)
21328 EMSG(_(e_invarg));
21329 else
21330 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021331 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21332 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21333 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21334 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021335#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021336 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21337 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021338#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021339 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21340 {
21341 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21342 curwin->w_set_curswant = FALSE;
21343 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021344
Bram Moolenaar82c25852014-05-28 16:47:16 +020021345 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21346 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021347#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021348 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21349 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021350#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021351 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21352 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21353 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21354 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021355
21356 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021357 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021358# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021359 win_new_width(curwin, W_WIDTH(curwin));
21360# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021361 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021362
Bram Moolenaarb851a962014-10-31 15:45:52 +010021363 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021364 curwin->w_topline = 1;
21365 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21366 curwin->w_topline = curbuf->b_ml.ml_line_count;
21367#ifdef FEAT_DIFF
21368 check_topfill(curwin, TRUE);
21369#endif
21370 }
21371}
21372
21373/*
21374 * "winsaveview()" function
21375 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021377f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021378{
21379 dict_T *dict;
21380
Bram Moolenaara800b422010-06-27 01:15:55 +020021381 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021382 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021383 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021384
21385 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21386 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21387#ifdef FEAT_VIRTUALEDIT
21388 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21389#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021390 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021391 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21392
21393 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21394#ifdef FEAT_DIFF
21395 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21396#endif
21397 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21398 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21399}
21400
21401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 * "winwidth(nr)" function
21403 */
21404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021405f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406{
21407 win_T *wp;
21408
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021409 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021411 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021413#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021414 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021416 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417#endif
21418}
21419
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021421 * "wordcount()" function
21422 */
21423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021424f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021425{
21426 if (rettv_dict_alloc(rettv) == FAIL)
21427 return;
21428 cursor_pos_info(rettv->vval.v_dict);
21429}
21430
21431/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021432 * Write list of strings to file
21433 */
21434 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021435write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021436{
21437 listitem_T *li;
21438 int c;
21439 int ret = OK;
21440 char_u *s;
21441
21442 for (li = list->lv_first; li != NULL; li = li->li_next)
21443 {
21444 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21445 {
21446 if (*s == '\n')
21447 c = putc(NUL, fd);
21448 else
21449 c = putc(*s, fd);
21450 if (c == EOF)
21451 {
21452 ret = FAIL;
21453 break;
21454 }
21455 }
21456 if (!binary || li->li_next != NULL)
21457 if (putc('\n', fd) == EOF)
21458 {
21459 ret = FAIL;
21460 break;
21461 }
21462 if (ret == FAIL)
21463 {
21464 EMSG(_(e_write));
21465 break;
21466 }
21467 }
21468 return ret;
21469}
21470
21471/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021472 * "writefile()" function
21473 */
21474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021475f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021476{
21477 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021478 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021479 char_u *fname;
21480 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021481 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021482
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021483 if (check_restricted() || check_secure())
21484 return;
21485
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021486 if (argvars[0].v_type != VAR_LIST)
21487 {
21488 EMSG2(_(e_listarg), "writefile()");
21489 return;
21490 }
21491 if (argvars[0].vval.v_list == NULL)
21492 return;
21493
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021494 if (argvars[2].v_type != VAR_UNKNOWN)
21495 {
21496 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21497 binary = TRUE;
21498 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21499 append = TRUE;
21500 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021501
21502 /* Always open the file in binary mode, library functions have a mind of
21503 * their own about CR-LF conversion. */
21504 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021505 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21506 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021507 {
21508 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21509 ret = -1;
21510 }
21511 else
21512 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021513 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21514 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021515 fclose(fd);
21516 }
21517
21518 rettv->vval.v_number = ret;
21519}
21520
21521/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021522 * "xor(expr, expr)" function
21523 */
21524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021525f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021526{
21527 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21528 ^ get_tv_number_chk(&argvars[1], NULL);
21529}
21530
21531
21532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021534 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 */
21536 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021537var2fpos(
21538 typval_T *varp,
21539 int dollar_lnum, /* TRUE when $ is last line */
21540 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021542 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021544 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545
Bram Moolenaara5525202006-03-02 22:52:09 +000021546 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021547 if (varp->v_type == VAR_LIST)
21548 {
21549 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021550 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021551 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021552 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021553
21554 l = varp->vval.v_list;
21555 if (l == NULL)
21556 return NULL;
21557
21558 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021559 pos.lnum = list_find_nr(l, 0L, &error);
21560 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021561 return NULL; /* invalid line number */
21562
21563 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021564 pos.col = list_find_nr(l, 1L, &error);
21565 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021566 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021567 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021568
21569 /* We accept "$" for the column number: last column. */
21570 li = list_find(l, 1L);
21571 if (li != NULL && li->li_tv.v_type == VAR_STRING
21572 && li->li_tv.vval.v_string != NULL
21573 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21574 pos.col = len + 1;
21575
Bram Moolenaara5525202006-03-02 22:52:09 +000021576 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021577 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021578 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021579 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021580
Bram Moolenaara5525202006-03-02 22:52:09 +000021581#ifdef FEAT_VIRTUALEDIT
21582 /* Get the virtual offset. Defaults to zero. */
21583 pos.coladd = list_find_nr(l, 2L, &error);
21584 if (error)
21585 pos.coladd = 0;
21586#endif
21587
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021588 return &pos;
21589 }
21590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021591 name = get_tv_string_chk(varp);
21592 if (name == NULL)
21593 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021594 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021596 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21597 {
21598 if (VIsual_active)
21599 return &VIsual;
21600 return &curwin->w_cursor;
21601 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021602 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021604 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21606 return NULL;
21607 return pp;
21608 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021609
21610#ifdef FEAT_VIRTUALEDIT
21611 pos.coladd = 0;
21612#endif
21613
Bram Moolenaar477933c2007-07-17 14:32:23 +000021614 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021615 {
21616 pos.col = 0;
21617 if (name[1] == '0') /* "w0": first visible line */
21618 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021619 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021620 pos.lnum = curwin->w_topline;
21621 return &pos;
21622 }
21623 else if (name[1] == '$') /* "w$": last visible line */
21624 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021625 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021626 pos.lnum = curwin->w_botline - 1;
21627 return &pos;
21628 }
21629 }
21630 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021632 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 {
21634 pos.lnum = curbuf->b_ml.ml_line_count;
21635 pos.col = 0;
21636 }
21637 else
21638 {
21639 pos.lnum = curwin->w_cursor.lnum;
21640 pos.col = (colnr_T)STRLEN(ml_get_curline());
21641 }
21642 return &pos;
21643 }
21644 return NULL;
21645}
21646
21647/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021648 * Convert list in "arg" into a position and optional file number.
21649 * When "fnump" is NULL there is no file number, only 3 items.
21650 * Note that the column is passed on as-is, the caller may want to decrement
21651 * it to use 1 for the first column.
21652 * Return FAIL when conversion is not possible, doesn't check the position for
21653 * validity.
21654 */
21655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021656list2fpos(
21657 typval_T *arg,
21658 pos_T *posp,
21659 int *fnump,
21660 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021661{
21662 list_T *l = arg->vval.v_list;
21663 long i = 0;
21664 long n;
21665
Bram Moolenaar493c1782014-05-28 14:34:46 +020021666 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21667 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021668 if (arg->v_type != VAR_LIST
21669 || l == NULL
21670 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021671 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021672 return FAIL;
21673
21674 if (fnump != NULL)
21675 {
21676 n = list_find_nr(l, i++, NULL); /* fnum */
21677 if (n < 0)
21678 return FAIL;
21679 if (n == 0)
21680 n = curbuf->b_fnum; /* current buffer */
21681 *fnump = n;
21682 }
21683
21684 n = list_find_nr(l, i++, NULL); /* lnum */
21685 if (n < 0)
21686 return FAIL;
21687 posp->lnum = n;
21688
21689 n = list_find_nr(l, i++, NULL); /* col */
21690 if (n < 0)
21691 return FAIL;
21692 posp->col = n;
21693
21694#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021695 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021696 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021697 posp->coladd = 0;
21698 else
21699 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021700#endif
21701
Bram Moolenaar493c1782014-05-28 14:34:46 +020021702 if (curswantp != NULL)
21703 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21704
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021705 return OK;
21706}
21707
21708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 * Get the length of an environment variable name.
21710 * Advance "arg" to the first character after the name.
21711 * Return 0 for error.
21712 */
21713 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021714get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715{
21716 char_u *p;
21717 int len;
21718
21719 for (p = *arg; vim_isIDc(*p); ++p)
21720 ;
21721 if (p == *arg) /* no name found */
21722 return 0;
21723
21724 len = (int)(p - *arg);
21725 *arg = p;
21726 return len;
21727}
21728
21729/*
21730 * Get the length of the name of a function or internal variable.
21731 * "arg" is advanced to the first non-white character after the name.
21732 * Return 0 if something is wrong.
21733 */
21734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021735get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736{
21737 char_u *p;
21738 int len;
21739
21740 /* Find the end of the name. */
21741 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021742 {
21743 if (*p == ':')
21744 {
21745 /* "s:" is start of "s:var", but "n:" is not and can be used in
21746 * slice "[n:]". Also "xx:" is not a namespace. */
21747 len = (int)(p - *arg);
21748 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21749 || len > 1)
21750 break;
21751 }
21752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 if (p == *arg) /* no name found */
21754 return 0;
21755
21756 len = (int)(p - *arg);
21757 *arg = skipwhite(p);
21758
21759 return len;
21760}
21761
21762/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021763 * Get the length of the name of a variable or function.
21764 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021766 * Return -1 if curly braces expansion failed.
21767 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 * If the name contains 'magic' {}'s, expand them and return the
21769 * expanded name in an allocated string via 'alias' - caller must free.
21770 */
21771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021772get_name_len(
21773 char_u **arg,
21774 char_u **alias,
21775 int evaluate,
21776 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777{
21778 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779 char_u *p;
21780 char_u *expr_start;
21781 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782
21783 *alias = NULL; /* default to no alias */
21784
21785 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21786 && (*arg)[2] == (int)KE_SNR)
21787 {
21788 /* hard coded <SNR>, already translated */
21789 *arg += 3;
21790 return get_id_len(arg) + 3;
21791 }
21792 len = eval_fname_script(*arg);
21793 if (len > 0)
21794 {
21795 /* literal "<SID>", "s:" or "<SNR>" */
21796 *arg += len;
21797 }
21798
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021800 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021802 p = find_name_end(*arg, &expr_start, &expr_end,
21803 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 if (expr_start != NULL)
21805 {
21806 char_u *temp_string;
21807
21808 if (!evaluate)
21809 {
21810 len += (int)(p - *arg);
21811 *arg = skipwhite(p);
21812 return len;
21813 }
21814
21815 /*
21816 * Include any <SID> etc in the expanded string:
21817 * Thus the -len here.
21818 */
21819 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21820 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021821 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 *alias = temp_string;
21823 *arg = skipwhite(p);
21824 return (int)STRLEN(temp_string);
21825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826
21827 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021828 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 EMSG2(_(e_invexpr2), *arg);
21830
21831 return len;
21832}
21833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021834/*
21835 * Find the end of a variable or function name, taking care of magic braces.
21836 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21837 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021838 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021839 * Return a pointer to just after the name. Equal to "arg" if there is no
21840 * valid name.
21841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021843find_name_end(
21844 char_u *arg,
21845 char_u **expr_start,
21846 char_u **expr_end,
21847 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021849 int mb_nest = 0;
21850 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021852 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021854 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021856 *expr_start = NULL;
21857 *expr_end = NULL;
21858 }
21859
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021860 /* Quick check for valid starting character. */
21861 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21862 return arg;
21863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021864 for (p = arg; *p != NUL
21865 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021866 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021867 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021868 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021869 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021870 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021871 if (*p == '\'')
21872 {
21873 /* skip over 'string' to avoid counting [ and ] inside it. */
21874 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21875 ;
21876 if (*p == NUL)
21877 break;
21878 }
21879 else if (*p == '"')
21880 {
21881 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21882 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21883 if (*p == '\\' && p[1] != NUL)
21884 ++p;
21885 if (*p == NUL)
21886 break;
21887 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021888 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21889 {
21890 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021891 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021892 len = (int)(p - arg);
21893 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021894 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021895 break;
21896 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021898 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021900 if (*p == '[')
21901 ++br_nest;
21902 else if (*p == ']')
21903 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021906 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021908 if (*p == '{')
21909 {
21910 mb_nest++;
21911 if (expr_start != NULL && *expr_start == NULL)
21912 *expr_start = p;
21913 }
21914 else if (*p == '}')
21915 {
21916 mb_nest--;
21917 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21918 *expr_end = p;
21919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 }
21922
21923 return p;
21924}
21925
21926/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021927 * Expands out the 'magic' {}'s in a variable/function name.
21928 * Note that this can call itself recursively, to deal with
21929 * constructs like foo{bar}{baz}{bam}
21930 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21931 * "in_start" ^
21932 * "expr_start" ^
21933 * "expr_end" ^
21934 * "in_end" ^
21935 *
21936 * Returns a new allocated string, which the caller must free.
21937 * Returns NULL for failure.
21938 */
21939 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021940make_expanded_name(
21941 char_u *in_start,
21942 char_u *expr_start,
21943 char_u *expr_end,
21944 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021945{
21946 char_u c1;
21947 char_u *retval = NULL;
21948 char_u *temp_result;
21949 char_u *nextcmd = NULL;
21950
21951 if (expr_end == NULL || in_end == NULL)
21952 return NULL;
21953 *expr_start = NUL;
21954 *expr_end = NUL;
21955 c1 = *in_end;
21956 *in_end = NUL;
21957
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021958 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021959 if (temp_result != NULL && nextcmd == NULL)
21960 {
21961 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21962 + (in_end - expr_end) + 1));
21963 if (retval != NULL)
21964 {
21965 STRCPY(retval, in_start);
21966 STRCAT(retval, temp_result);
21967 STRCAT(retval, expr_end + 1);
21968 }
21969 }
21970 vim_free(temp_result);
21971
21972 *in_end = c1; /* put char back for error messages */
21973 *expr_start = '{';
21974 *expr_end = '}';
21975
21976 if (retval != NULL)
21977 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021978 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021979 if (expr_start != NULL)
21980 {
21981 /* Further expansion! */
21982 temp_result = make_expanded_name(retval, expr_start,
21983 expr_end, temp_result);
21984 vim_free(retval);
21985 retval = temp_result;
21986 }
21987 }
21988
21989 return retval;
21990}
21991
21992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021994 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 */
21996 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021997eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021999 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22000}
22001
22002/*
22003 * Return TRUE if character "c" can be used as the first character in a
22004 * variable or function name (excluding '{' and '}').
22005 */
22006 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022007eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022008{
22009 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010}
22011
22012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 * Set number v: variable to "val".
22014 */
22015 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022016set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022018 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019}
22020
22021/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022022 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 */
22024 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022025get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022027 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028}
22029
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022030/*
22031 * Get string v: variable value. Uses a static buffer, can only be used once.
22032 */
22033 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022034get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022035{
22036 return get_tv_string(&vimvars[idx].vv_tv);
22037}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022038
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022040 * Get List v: variable value. Caller must take care of reference count when
22041 * needed.
22042 */
22043 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022044get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022045{
22046 return vimvars[idx].vv_list;
22047}
22048
22049/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022050 * Set v:char to character "c".
22051 */
22052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022053set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022054{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022055 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022056
22057#ifdef FEAT_MBYTE
22058 if (has_mbyte)
22059 buf[(*mb_char2bytes)(c, buf)] = NUL;
22060 else
22061#endif
22062 {
22063 buf[0] = c;
22064 buf[1] = NUL;
22065 }
22066 set_vim_var_string(VV_CHAR, buf, -1);
22067}
22068
22069/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022070 * Set v:count to "count" and v:count1 to "count1".
22071 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 */
22073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022074set_vcount(
22075 long count,
22076 long count1,
22077 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022079 if (set_prevcount)
22080 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022081 vimvars[VV_COUNT].vv_nr = count;
22082 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083}
22084
22085/*
22086 * Set string v: variable to a copy of "val".
22087 */
22088 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022089set_vim_var_string(
22090 int idx,
22091 char_u *val,
22092 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093{
Bram Moolenaara542c682016-01-31 16:28:04 +010022094 clear_tv(&vimvars[idx].vv_di.di_tv);
22095 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022097 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022099 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022101 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102}
22103
22104/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022105 * Set List v: variable to "val".
22106 */
22107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022108set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022109{
Bram Moolenaara542c682016-01-31 16:28:04 +010022110 clear_tv(&vimvars[idx].vv_di.di_tv);
22111 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022112 vimvars[idx].vv_list = val;
22113 if (val != NULL)
22114 ++val->lv_refcount;
22115}
22116
22117/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022118 * Set Dictionary v: variable to "val".
22119 */
22120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022121set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022122{
22123 int todo;
22124 hashitem_T *hi;
22125
Bram Moolenaara542c682016-01-31 16:28:04 +010022126 clear_tv(&vimvars[idx].vv_di.di_tv);
22127 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022128 vimvars[idx].vv_dict = val;
22129 if (val != NULL)
22130 {
22131 ++val->dv_refcount;
22132
22133 /* Set readonly */
22134 todo = (int)val->dv_hashtab.ht_used;
22135 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22136 {
22137 if (HASHITEM_EMPTY(hi))
22138 continue;
22139 --todo;
22140 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22141 }
22142 }
22143}
22144
22145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 * Set v:register if needed.
22147 */
22148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022149set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150{
22151 char_u regname;
22152
22153 if (c == 0 || c == ' ')
22154 regname = '"';
22155 else
22156 regname = c;
22157 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022158 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 set_vim_var_string(VV_REG, &regname, 1);
22160}
22161
22162/*
22163 * Get or set v:exception. If "oldval" == NULL, return the current value.
22164 * Otherwise, restore the value to "oldval" and return NULL.
22165 * Must always be called in pairs to save and restore v:exception! Does not
22166 * take care of memory allocations.
22167 */
22168 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022169v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170{
22171 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022172 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173
Bram Moolenaare9a41262005-01-15 22:18:47 +000022174 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 return NULL;
22176}
22177
22178/*
22179 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22180 * Otherwise, restore the value to "oldval" and return NULL.
22181 * Must always be called in pairs to save and restore v:throwpoint! Does not
22182 * take care of memory allocations.
22183 */
22184 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022185v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186{
22187 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022188 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189
Bram Moolenaare9a41262005-01-15 22:18:47 +000022190 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 return NULL;
22192}
22193
22194#if defined(FEAT_AUTOCMD) || defined(PROTO)
22195/*
22196 * Set v:cmdarg.
22197 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22198 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22199 * Must always be called in pairs!
22200 */
22201 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022202set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203{
22204 char_u *oldval;
22205 char_u *newval;
22206 unsigned len;
22207
Bram Moolenaare9a41262005-01-15 22:18:47 +000022208 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022209 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022211 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022212 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022213 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214 }
22215
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022216 if (eap->force_bin == FORCE_BIN)
22217 len = 6;
22218 else if (eap->force_bin == FORCE_NOBIN)
22219 len = 8;
22220 else
22221 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022222
22223 if (eap->read_edit)
22224 len += 7;
22225
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022226 if (eap->force_ff != 0)
22227 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22228# ifdef FEAT_MBYTE
22229 if (eap->force_enc != 0)
22230 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022231 if (eap->bad_char != 0)
22232 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022233# endif
22234
22235 newval = alloc(len + 1);
22236 if (newval == NULL)
22237 return NULL;
22238
22239 if (eap->force_bin == FORCE_BIN)
22240 sprintf((char *)newval, " ++bin");
22241 else if (eap->force_bin == FORCE_NOBIN)
22242 sprintf((char *)newval, " ++nobin");
22243 else
22244 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022245
22246 if (eap->read_edit)
22247 STRCAT(newval, " ++edit");
22248
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022249 if (eap->force_ff != 0)
22250 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22251 eap->cmd + eap->force_ff);
22252# ifdef FEAT_MBYTE
22253 if (eap->force_enc != 0)
22254 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22255 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022256 if (eap->bad_char == BAD_KEEP)
22257 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22258 else if (eap->bad_char == BAD_DROP)
22259 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22260 else if (eap->bad_char != 0)
22261 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022262# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022263 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022264 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265}
22266#endif
22267
22268/*
22269 * Get the value of internal variable "name".
22270 * Return OK or FAIL.
22271 */
22272 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022273get_var_tv(
22274 char_u *name,
22275 int len, /* length of "name" */
22276 typval_T *rettv, /* NULL when only checking existence */
22277 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22278 int verbose, /* may give error message */
22279 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280{
22281 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022282 typval_T *tv = NULL;
22283 typval_T atv;
22284 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286
22287 /* truncate the name, so that we can use strcmp() */
22288 cc = name[len];
22289 name[len] = NUL;
22290
22291 /*
22292 * Check for "b:changedtick".
22293 */
22294 if (STRCMP(name, "b:changedtick") == 0)
22295 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022296 atv.v_type = VAR_NUMBER;
22297 atv.vval.v_number = curbuf->b_changedtick;
22298 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299 }
22300
22301 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302 * Check for user-defined variables.
22303 */
22304 else
22305 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022306 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022308 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022309 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022310 if (dip != NULL)
22311 *dip = v;
22312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 }
22314
Bram Moolenaare9a41262005-01-15 22:18:47 +000022315 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022317 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022318 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 ret = FAIL;
22320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022321 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022322 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323
22324 name[len] = cc;
22325
22326 return ret;
22327}
22328
22329/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022330 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22331 * Also handle function call with Funcref variable: func(expr)
22332 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22333 */
22334 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022335handle_subscript(
22336 char_u **arg,
22337 typval_T *rettv,
22338 int evaluate, /* do more than finding the end */
22339 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022340{
22341 int ret = OK;
22342 dict_T *selfdict = NULL;
22343 char_u *s;
22344 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022345 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022346
22347 while (ret == OK
22348 && (**arg == '['
22349 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022350 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22351 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022352 && !vim_iswhite(*(*arg - 1)))
22353 {
22354 if (**arg == '(')
22355 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022356 partial_T *pt = NULL;
22357
Bram Moolenaard9fba312005-06-26 22:34:35 +000022358 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022359 if (evaluate)
22360 {
22361 functv = *rettv;
22362 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022363
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022364 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022365 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022366 {
22367 pt = functv.vval.v_partial;
22368 s = pt->pt_name;
22369 }
22370 else
22371 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022372 }
22373 else
22374 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022375 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022376 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022377 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022378
22379 /* Clear the funcref afterwards, so that deleting it while
22380 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022381 if (evaluate)
22382 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022383
22384 /* Stop the expression evaluation when immediately aborting on
22385 * error, or when an interrupt occurred or an exception was thrown
22386 * but not caught. */
22387 if (aborting())
22388 {
22389 if (ret == OK)
22390 clear_tv(rettv);
22391 ret = FAIL;
22392 }
22393 dict_unref(selfdict);
22394 selfdict = NULL;
22395 }
22396 else /* **arg == '[' || **arg == '.' */
22397 {
22398 dict_unref(selfdict);
22399 if (rettv->v_type == VAR_DICT)
22400 {
22401 selfdict = rettv->vval.v_dict;
22402 if (selfdict != NULL)
22403 ++selfdict->dv_refcount;
22404 }
22405 else
22406 selfdict = NULL;
22407 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22408 {
22409 clear_tv(rettv);
22410 ret = FAIL;
22411 }
22412 }
22413 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022414
Bram Moolenaar1d429612016-05-24 15:44:17 +020022415 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22416 * Don't do this when "Func" is already a partial that was bound
22417 * explicitly (pt_auto is FALSE). */
22418 if (selfdict != NULL
22419 && (rettv->v_type == VAR_FUNC
22420 || (rettv->v_type == VAR_PARTIAL
22421 && (rettv->vval.v_partial->pt_auto
22422 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022423 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022424 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22425 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022426 char_u *tofree = NULL;
22427 ufunc_T *fp;
22428 char_u fname_buf[FLEN_FIXED + 1];
22429 int error;
22430
22431 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022432 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022433 fp = find_func(fname);
22434 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022435
Bram Moolenaar65639032016-03-16 21:40:30 +010022436 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022437 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022438 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22439
22440 if (pt != NULL)
22441 {
22442 pt->pt_refcount = 1;
22443 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022444 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022445 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022446 if (rettv->v_type == VAR_FUNC)
22447 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022448 /* Just a function: Take over the function name and use
22449 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022450 pt->pt_name = rettv->vval.v_string;
22451 }
22452 else
22453 {
22454 partial_T *ret_pt = rettv->vval.v_partial;
22455 int i;
22456
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022457 /* Partial: copy the function name, use selfdict and copy
22458 * args. Can't take over name or args, the partial might
22459 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022460 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022461 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022462 if (ret_pt->pt_argc > 0)
22463 {
22464 pt->pt_argv = (typval_T *)alloc(
22465 sizeof(typval_T) * ret_pt->pt_argc);
22466 if (pt->pt_argv == NULL)
22467 /* out of memory: drop the arguments */
22468 pt->pt_argc = 0;
22469 else
22470 {
22471 pt->pt_argc = ret_pt->pt_argc;
22472 for (i = 0; i < pt->pt_argc; i++)
22473 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22474 }
22475 }
22476 partial_unref(ret_pt);
22477 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022478 rettv->v_type = VAR_PARTIAL;
22479 rettv->vval.v_partial = pt;
22480 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022481 }
22482 }
22483
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022484 dict_unref(selfdict);
22485 return ret;
22486}
22487
22488/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022489 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022490 * value).
22491 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022492 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022493alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022494{
Bram Moolenaar33570922005-01-25 22:26:29 +000022495 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022496}
22497
22498/*
22499 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 * The string "s" must have been allocated, it is consumed.
22501 * Return NULL for out of memory, the variable otherwise.
22502 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022503 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022504alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505{
Bram Moolenaar33570922005-01-25 22:26:29 +000022506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022508 rettv = alloc_tv();
22509 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022511 rettv->v_type = VAR_STRING;
22512 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 }
22514 else
22515 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022516 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517}
22518
22519/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022520 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022523free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524{
22525 if (varp != NULL)
22526 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022527 switch (varp->v_type)
22528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022529 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022530 func_unref(varp->vval.v_string);
22531 /*FALLTHROUGH*/
22532 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022533 vim_free(varp->vval.v_string);
22534 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022535 case VAR_PARTIAL:
22536 partial_unref(varp->vval.v_partial);
22537 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022538 case VAR_LIST:
22539 list_unref(varp->vval.v_list);
22540 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022541 case VAR_DICT:
22542 dict_unref(varp->vval.v_dict);
22543 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022544 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022545#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022546 job_unref(varp->vval.v_job);
22547 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022548#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022549 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022550#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022551 channel_unref(varp->vval.v_channel);
22552 break;
22553#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022554 case VAR_NUMBER:
22555 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022556 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022557 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022558 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 vim_free(varp);
22561 }
22562}
22563
22564/*
22565 * Free the memory for a variable value and set the value to NULL or 0.
22566 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022568clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569{
22570 if (varp != NULL)
22571 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022572 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022574 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022575 func_unref(varp->vval.v_string);
22576 /*FALLTHROUGH*/
22577 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022578 vim_free(varp->vval.v_string);
22579 varp->vval.v_string = NULL;
22580 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022581 case VAR_PARTIAL:
22582 partial_unref(varp->vval.v_partial);
22583 varp->vval.v_partial = NULL;
22584 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022585 case VAR_LIST:
22586 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022587 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022588 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022589 case VAR_DICT:
22590 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022591 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022592 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022593 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022594 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022595 varp->vval.v_number = 0;
22596 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022597 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022598#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022599 varp->vval.v_float = 0.0;
22600 break;
22601#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022602 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022603#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022604 job_unref(varp->vval.v_job);
22605 varp->vval.v_job = NULL;
22606#endif
22607 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022608 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022609#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022610 channel_unref(varp->vval.v_channel);
22611 varp->vval.v_channel = NULL;
22612#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022613 case VAR_UNKNOWN:
22614 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022616 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 }
22618}
22619
22620/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022621 * Set the value of a variable to NULL without freeing items.
22622 */
22623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022624init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022625{
22626 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022627 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022628}
22629
22630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 * Get the number value of a variable.
22632 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022633 * For incompatible types, return 0.
22634 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22635 * caller of incompatible types: it sets *denote to TRUE if "denote"
22636 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022638 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022639get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022641 int error = FALSE;
22642
22643 return get_tv_number_chk(varp, &error); /* return 0L on error */
22644}
22645
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022646 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022647get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022648{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022649 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022651 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022653 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022654 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022655 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022656#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022657 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022658 break;
22659#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022660 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022661 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022662 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022663 break;
22664 case VAR_STRING:
22665 if (varp->vval.v_string != NULL)
22666 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022667 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022668 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022669 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022670 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022671 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022672 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022673 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022674 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022675 case VAR_SPECIAL:
22676 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22677 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022678 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022679#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022680 EMSG(_("E910: Using a Job as a Number"));
22681 break;
22682#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022683 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022684#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022685 EMSG(_("E913: Using a Channel as a Number"));
22686 break;
22687#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022688 case VAR_UNKNOWN:
22689 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022690 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022692 if (denote == NULL) /* useful for values that must be unsigned */
22693 n = -1;
22694 else
22695 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022696 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697}
22698
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022699#ifdef FEAT_FLOAT
22700 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022701get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022702{
22703 switch (varp->v_type)
22704 {
22705 case VAR_NUMBER:
22706 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022707 case VAR_FLOAT:
22708 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022709 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022710 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022711 EMSG(_("E891: Using a Funcref as a Float"));
22712 break;
22713 case VAR_STRING:
22714 EMSG(_("E892: Using a String as a Float"));
22715 break;
22716 case VAR_LIST:
22717 EMSG(_("E893: Using a List as a Float"));
22718 break;
22719 case VAR_DICT:
22720 EMSG(_("E894: Using a Dictionary as a Float"));
22721 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022722 case VAR_SPECIAL:
22723 EMSG(_("E907: Using a special value as a Float"));
22724 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022725 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022726# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022727 EMSG(_("E911: Using a Job as a Float"));
22728 break;
22729# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022730 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022731# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022732 EMSG(_("E914: Using a Channel as a Float"));
22733 break;
22734# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022735 case VAR_UNKNOWN:
22736 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022737 break;
22738 }
22739 return 0;
22740}
22741#endif
22742
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022744 * Get the lnum from the first argument.
22745 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022746 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 */
22748 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022749get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750{
Bram Moolenaar33570922005-01-25 22:26:29 +000022751 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 linenr_T lnum;
22753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022754 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 if (lnum == 0) /* no valid number, try using line() */
22756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022757 rettv.v_type = VAR_NUMBER;
22758 f_line(argvars, &rettv);
22759 lnum = rettv.vval.v_number;
22760 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 }
22762 return lnum;
22763}
22764
22765/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022766 * Get the lnum from the first argument.
22767 * Also accepts "$", then "buf" is used.
22768 * Returns 0 on error.
22769 */
22770 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022771get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022772{
22773 if (argvars[0].v_type == VAR_STRING
22774 && argvars[0].vval.v_string != NULL
22775 && argvars[0].vval.v_string[0] == '$'
22776 && buf != NULL)
22777 return buf->b_ml.ml_line_count;
22778 return get_tv_number_chk(&argvars[0], NULL);
22779}
22780
22781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 * Get the string value of a variable.
22783 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022784 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22785 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 * If the String variable has never been set, return an empty string.
22787 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022788 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22789 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022791 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022792get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793{
22794 static char_u mybuf[NUMBUFLEN];
22795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022796 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797}
22798
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022799 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022800get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022802 char_u *res = get_tv_string_buf_chk(varp, buf);
22803
22804 return res != NULL ? res : (char_u *)"";
22805}
22806
Bram Moolenaar7d647822014-04-05 21:28:56 +020022807/*
22808 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22809 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022810 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022811get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022812{
22813 static char_u mybuf[NUMBUFLEN];
22814
22815 return get_tv_string_buf_chk(varp, mybuf);
22816}
22817
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022818 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022819get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022820{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022821 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022823 case VAR_NUMBER:
22824 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22825 return buf;
22826 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022827 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022828 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022829 break;
22830 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022831 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022832 break;
22833 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022834 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022835 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022836 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022837#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022838 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022839 break;
22840#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022841 case VAR_STRING:
22842 if (varp->vval.v_string != NULL)
22843 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022844 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022845 case VAR_SPECIAL:
22846 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22847 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022848 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022849#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022850 {
22851 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022852 char *status;
22853
22854 if (job == NULL)
22855 return (char_u *)"no process";
22856 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022857 : job->jv_status == JOB_ENDED ? "dead"
22858 : "run";
22859# ifdef UNIX
22860 vim_snprintf((char *)buf, NUMBUFLEN,
22861 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022862# elif defined(WIN32)
22863 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022864 "process %ld %s",
22865 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022866 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022867# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022868 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022869 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22870# endif
22871 return buf;
22872 }
22873#endif
22874 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022875 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022876#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022877 {
22878 channel_T *channel = varp->vval.v_channel;
22879 char *status = channel_status(channel);
22880
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022881 if (channel == NULL)
22882 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22883 else
22884 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022885 "channel %d %s", channel->ch_id, status);
22886 return buf;
22887 }
22888#endif
22889 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022890 case VAR_UNKNOWN:
22891 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022892 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022894 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895}
22896
22897/*
22898 * Find variable "name" in the list of variables.
22899 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022900 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022901 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022902 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022904 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022905find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022908 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909
Bram Moolenaara7043832005-01-21 11:56:39 +000022910 ht = find_var_ht(name, &varname);
22911 if (htp != NULL)
22912 *htp = ht;
22913 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022915 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916}
22917
22918/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022919 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022920 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022922 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022923find_var_in_ht(
22924 hashtab_T *ht,
22925 int htname,
22926 char_u *varname,
22927 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022928{
Bram Moolenaar33570922005-01-25 22:26:29 +000022929 hashitem_T *hi;
22930
22931 if (*varname == NUL)
22932 {
22933 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022934 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022935 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022936 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022937 case 'g': return &globvars_var;
22938 case 'v': return &vimvars_var;
22939 case 'b': return &curbuf->b_bufvar;
22940 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022941#ifdef FEAT_WINDOWS
22942 case 't': return &curtab->tp_winvar;
22943#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022944 case 'l': return current_funccal == NULL
22945 ? NULL : &current_funccal->l_vars_var;
22946 case 'a': return current_funccal == NULL
22947 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022948 }
22949 return NULL;
22950 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022951
22952 hi = hash_find(ht, varname);
22953 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022954 {
22955 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022956 * worked find the variable again. Don't auto-load a script if it was
22957 * loaded already, otherwise it would be loaded every time when
22958 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022959 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022960 {
22961 /* Note: script_autoload() may make "hi" invalid. It must either
22962 * be obtained again or not used. */
22963 if (!script_autoload(varname, FALSE) || aborting())
22964 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022965 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022966 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022967 if (HASHITEM_EMPTY(hi))
22968 return NULL;
22969 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022970 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022971}
22972
22973/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022974 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022975 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022976 * Set "varname" to the start of name without ':'.
22977 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022978 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022979find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022981 hashitem_T *hi;
22982
Bram Moolenaar73627d02015-08-11 15:46:09 +020022983 if (name[0] == NUL)
22984 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 if (name[1] != ':')
22986 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022987 /* The name must not start with a colon or #. */
22988 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 return NULL;
22990 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022991
22992 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022993 hi = hash_find(&compat_hashtab, name);
22994 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022995 return &compat_hashtab;
22996
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022998 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022999 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000 }
23001 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023002 if (*name == 'g') /* global variable */
23003 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023004 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23005 */
23006 if (vim_strchr(name + 2, ':') != NULL
23007 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023008 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023010 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023012 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023013#ifdef FEAT_WINDOWS
23014 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023015 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023016#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023017 if (*name == 'v') /* v: variable */
23018 return &vimvarht;
23019 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023020 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023022 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023 if (*name == 's' /* script variable */
23024 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23025 return &SCRIPT_VARS(current_SID);
23026 return NULL;
23027}
23028
23029/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023030 * Get function call environment based on bactrace debug level
23031 */
23032 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023033get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023034{
23035 int i;
23036 funccall_T *funccal;
23037 funccall_T *temp_funccal;
23038
23039 funccal = current_funccal;
23040 if (debug_backtrace_level > 0)
23041 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023042 for (i = 0; i < debug_backtrace_level; i++)
23043 {
23044 temp_funccal = funccal->caller;
23045 if (temp_funccal)
23046 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023047 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023048 /* backtrace level overflow. reset to max */
23049 debug_backtrace_level = i;
23050 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023051 }
23052 return funccal;
23053}
23054
23055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023057 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 * Returns NULL when it doesn't exist.
23059 */
23060 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023061get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062{
Bram Moolenaar33570922005-01-25 22:26:29 +000023063 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023065 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066 if (v == NULL)
23067 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023068 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069}
23070
23071/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023072 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 * sourcing this script and when executing functions defined in the script.
23074 */
23075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023076new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077{
Bram Moolenaara7043832005-01-21 11:56:39 +000023078 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023079 hashtab_T *ht;
23080 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023081
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23083 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023084 /* Re-allocating ga_data means that an ht_array pointing to
23085 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023086 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023087 for (i = 1; i <= ga_scripts.ga_len; ++i)
23088 {
23089 ht = &SCRIPT_VARS(i);
23090 if (ht->ht_mask == HT_INIT_SIZE - 1)
23091 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023092 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023093 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023094 }
23095
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 while (ga_scripts.ga_len < id)
23097 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023098 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023099 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023100 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 }
23103 }
23104}
23105
23106/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023107 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23108 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109 */
23110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023111init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112{
Bram Moolenaar33570922005-01-25 22:26:29 +000023113 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023114 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023115 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023116 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023117 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023118 dict_var->di_tv.vval.v_dict = dict;
23119 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023120 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023121 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23122 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123}
23124
23125/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023126 * Unreference a dictionary initialized by init_var_dict().
23127 */
23128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023129unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023130{
23131 /* Now the dict needs to be freed if no one else is using it, go back to
23132 * normal reference counting. */
23133 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23134 dict_unref(dict);
23135}
23136
23137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023139 * Frees all allocated variables and the value they contain.
23140 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 */
23142 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023143vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023144{
23145 vars_clear_ext(ht, TRUE);
23146}
23147
23148/*
23149 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23150 */
23151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023152vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153{
Bram Moolenaara7043832005-01-21 11:56:39 +000023154 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023155 hashitem_T *hi;
23156 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157
Bram Moolenaar33570922005-01-25 22:26:29 +000023158 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023159 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023160 for (hi = ht->ht_array; todo > 0; ++hi)
23161 {
23162 if (!HASHITEM_EMPTY(hi))
23163 {
23164 --todo;
23165
Bram Moolenaar33570922005-01-25 22:26:29 +000023166 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023167 * ht_array might change then. hash_clear() takes care of it
23168 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023169 v = HI2DI(hi);
23170 if (free_val)
23171 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023172 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023173 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023174 }
23175 }
23176 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023177 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178}
23179
Bram Moolenaara7043832005-01-21 11:56:39 +000023180/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023181 * Delete a variable from hashtab "ht" at item "hi".
23182 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023185delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186{
Bram Moolenaar33570922005-01-25 22:26:29 +000023187 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023188
23189 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023190 clear_tv(&di->di_tv);
23191 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192}
23193
23194/*
23195 * List the value of one internal variable.
23196 */
23197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023198list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023200 char_u *tofree;
23201 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023202 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023203
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023204 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023205 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023206 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023207 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208}
23209
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023211list_one_var_a(
23212 char_u *prefix,
23213 char_u *name,
23214 int type,
23215 char_u *string,
23216 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217{
Bram Moolenaar31859182007-08-14 20:41:13 +000023218 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23219 msg_start();
23220 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 if (name != NULL) /* "a:" vars don't have a name stored */
23222 msg_puts(name);
23223 msg_putchar(' ');
23224 msg_advance(22);
23225 if (type == VAR_NUMBER)
23226 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023227 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023228 msg_putchar('*');
23229 else if (type == VAR_LIST)
23230 {
23231 msg_putchar('[');
23232 if (*string == '[')
23233 ++string;
23234 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023235 else if (type == VAR_DICT)
23236 {
23237 msg_putchar('{');
23238 if (*string == '{')
23239 ++string;
23240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 else
23242 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023243
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023245
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023246 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023247 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023248 if (*first)
23249 {
23250 msg_clr_eos();
23251 *first = FALSE;
23252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253}
23254
23255/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023256 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 * If the variable already exists, the value is updated.
23258 * Otherwise the variable is created.
23259 */
23260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023261set_var(
23262 char_u *name,
23263 typval_T *tv,
23264 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265{
Bram Moolenaar33570922005-01-25 22:26:29 +000023266 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023268 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023270 ht = find_var_ht(name, &varname);
23271 if (ht == NULL || *varname == NUL)
23272 {
23273 EMSG2(_(e_illvar), name);
23274 return;
23275 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023276 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023277
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023278 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23279 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023280 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023281
Bram Moolenaar33570922005-01-25 22:26:29 +000023282 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023284 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023285 if (var_check_ro(v->di_flags, name, FALSE)
23286 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023287 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023288
23289 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023290 * Handle setting internal v: variables separately where needed to
23291 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023292 */
23293 if (ht == &vimvarht)
23294 {
23295 if (v->di_tv.v_type == VAR_STRING)
23296 {
23297 vim_free(v->di_tv.vval.v_string);
23298 if (copy || tv->v_type != VAR_STRING)
23299 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23300 else
23301 {
23302 /* Take over the string to avoid an extra alloc/free. */
23303 v->di_tv.vval.v_string = tv->vval.v_string;
23304 tv->vval.v_string = NULL;
23305 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023306 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023307 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023308 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023309 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023310 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023311 if (STRCMP(varname, "searchforward") == 0)
23312 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023313#ifdef FEAT_SEARCH_EXTRA
23314 else if (STRCMP(varname, "hlsearch") == 0)
23315 {
23316 no_hlsearch = !v->di_tv.vval.v_number;
23317 redraw_all_later(SOME_VALID);
23318 }
23319#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023320 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023321 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023322 else if (v->di_tv.v_type != tv->v_type)
23323 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023324 }
23325
23326 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 }
23328 else /* add a new variable */
23329 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023330 /* Can't add "v:" variable. */
23331 if (ht == &vimvarht)
23332 {
23333 EMSG2(_(e_illvar), name);
23334 return;
23335 }
23336
Bram Moolenaar92124a32005-06-17 22:03:40 +000023337 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023338 if (!valid_varname(varname))
23339 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023340
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023341 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23342 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023343 if (v == NULL)
23344 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023345 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023346 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023348 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023349 return;
23350 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023351 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023353
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023354 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023355 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023356 else
23357 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023358 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023359 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023360 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362}
23363
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023364/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023365 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023366 * Also give an error message.
23367 */
23368 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023369var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023370{
23371 if (flags & DI_FLAGS_RO)
23372 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023373 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023374 return TRUE;
23375 }
23376 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23377 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023378 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023379 return TRUE;
23380 }
23381 return FALSE;
23382}
23383
23384/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023385 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23386 * Also give an error message.
23387 */
23388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023389var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023390{
23391 if (flags & DI_FLAGS_FIX)
23392 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023393 EMSG2(_("E795: Cannot delete variable %s"),
23394 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023395 return TRUE;
23396 }
23397 return FALSE;
23398}
23399
23400/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023401 * Check if a funcref is assigned to a valid variable name.
23402 * Return TRUE and give an error if not.
23403 */
23404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023405var_check_func_name(
23406 char_u *name, /* points to start of variable name */
23407 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023408{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023409 /* Allow for w: b: s: and t:. */
23410 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023411 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23412 ? name[2] : name[0]))
23413 {
23414 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23415 name);
23416 return TRUE;
23417 }
23418 /* Don't allow hiding a function. When "v" is not NULL we might be
23419 * assigning another function to the same var, the type is checked
23420 * below. */
23421 if (new_var && function_exists(name))
23422 {
23423 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23424 name);
23425 return TRUE;
23426 }
23427 return FALSE;
23428}
23429
23430/*
23431 * Check if a variable name is valid.
23432 * Return FALSE and give an error if not.
23433 */
23434 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023435valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023436{
23437 char_u *p;
23438
23439 for (p = varname; *p != NUL; ++p)
23440 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23441 && *p != AUTOLOAD_CHAR)
23442 {
23443 EMSG2(_(e_illvar), varname);
23444 return FALSE;
23445 }
23446 return TRUE;
23447}
23448
23449/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023450 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023451 * Also give an error message, using "name" or _("name") when use_gettext is
23452 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023453 */
23454 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023455tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023456{
23457 if (lock & VAR_LOCKED)
23458 {
23459 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023460 name == NULL ? (char_u *)_("Unknown")
23461 : use_gettext ? (char_u *)_(name)
23462 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023463 return TRUE;
23464 }
23465 if (lock & VAR_FIXED)
23466 {
23467 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023468 name == NULL ? (char_u *)_("Unknown")
23469 : use_gettext ? (char_u *)_(name)
23470 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023471 return TRUE;
23472 }
23473 return FALSE;
23474}
23475
23476/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023477 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023478 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023479 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023480 * It is OK for "from" and "to" to point to the same item. This is used to
23481 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023482 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023484copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023486 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023487 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023488 switch (from->v_type)
23489 {
23490 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023491 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023492 to->vval.v_number = from->vval.v_number;
23493 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023494 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023495#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023496 to->vval.v_float = from->vval.v_float;
23497 break;
23498#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023499 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023500#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023501 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023502 if (to->vval.v_job != NULL)
23503 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023504 break;
23505#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023506 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023507#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023508 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023509 if (to->vval.v_channel != NULL)
23510 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023511 break;
23512#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023513 case VAR_STRING:
23514 case VAR_FUNC:
23515 if (from->vval.v_string == NULL)
23516 to->vval.v_string = NULL;
23517 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023518 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023519 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023520 if (from->v_type == VAR_FUNC)
23521 func_ref(to->vval.v_string);
23522 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023523 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023524 case VAR_PARTIAL:
23525 if (from->vval.v_partial == NULL)
23526 to->vval.v_partial = NULL;
23527 else
23528 {
23529 to->vval.v_partial = from->vval.v_partial;
23530 ++to->vval.v_partial->pt_refcount;
23531 }
23532 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023533 case VAR_LIST:
23534 if (from->vval.v_list == NULL)
23535 to->vval.v_list = NULL;
23536 else
23537 {
23538 to->vval.v_list = from->vval.v_list;
23539 ++to->vval.v_list->lv_refcount;
23540 }
23541 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023542 case VAR_DICT:
23543 if (from->vval.v_dict == NULL)
23544 to->vval.v_dict = NULL;
23545 else
23546 {
23547 to->vval.v_dict = from->vval.v_dict;
23548 ++to->vval.v_dict->dv_refcount;
23549 }
23550 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023551 case VAR_UNKNOWN:
23552 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023553 break;
23554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555}
23556
23557/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023558 * Make a copy of an item.
23559 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023560 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23561 * reference to an already copied list/dict can be used.
23562 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023563 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023565item_copy(
23566 typval_T *from,
23567 typval_T *to,
23568 int deep,
23569 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023570{
23571 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023572 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023573
Bram Moolenaar33570922005-01-25 22:26:29 +000023574 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023575 {
23576 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023577 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023578 }
23579 ++recurse;
23580
23581 switch (from->v_type)
23582 {
23583 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023584 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023585 case VAR_STRING:
23586 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023587 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023588 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023589 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023590 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023591 copy_tv(from, to);
23592 break;
23593 case VAR_LIST:
23594 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023595 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023596 if (from->vval.v_list == NULL)
23597 to->vval.v_list = NULL;
23598 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23599 {
23600 /* use the copy made earlier */
23601 to->vval.v_list = from->vval.v_list->lv_copylist;
23602 ++to->vval.v_list->lv_refcount;
23603 }
23604 else
23605 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23606 if (to->vval.v_list == NULL)
23607 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023608 break;
23609 case VAR_DICT:
23610 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023611 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023612 if (from->vval.v_dict == NULL)
23613 to->vval.v_dict = NULL;
23614 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23615 {
23616 /* use the copy made earlier */
23617 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23618 ++to->vval.v_dict->dv_refcount;
23619 }
23620 else
23621 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23622 if (to->vval.v_dict == NULL)
23623 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023624 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023625 case VAR_UNKNOWN:
23626 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023627 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023628 }
23629 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023630 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023631}
23632
23633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 * ":echo expr1 ..." print each argument separated with a space, add a
23635 * newline at the end.
23636 * ":echon expr1 ..." print each argument plain.
23637 */
23638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023639ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640{
23641 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023642 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023643 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 char_u *p;
23645 int needclr = TRUE;
23646 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023647 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648
23649 if (eap->skip)
23650 ++emsg_skip;
23651 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23652 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023653 /* If eval1() causes an error message the text from the command may
23654 * still need to be cleared. E.g., "echo 22,44". */
23655 need_clr_eos = needclr;
23656
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023658 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 {
23660 /*
23661 * Report the invalid expression unless the expression evaluation
23662 * has been cancelled due to an aborting error, an interrupt, or an
23663 * exception.
23664 */
23665 if (!aborting())
23666 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023667 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 break;
23669 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023670 need_clr_eos = FALSE;
23671
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 if (!eap->skip)
23673 {
23674 if (atstart)
23675 {
23676 atstart = FALSE;
23677 /* Call msg_start() after eval1(), evaluating the expression
23678 * may cause a message to appear. */
23679 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023680 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023681 /* Mark the saved text as finishing the line, so that what
23682 * follows is displayed on a new line when scrolling back
23683 * at the more prompt. */
23684 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687 }
23688 else if (eap->cmdidx == CMD_echo)
23689 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023690 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023691 if (p != NULL)
23692 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023694 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023696 if (*p != TAB && needclr)
23697 {
23698 /* remove any text still there from the command */
23699 msg_clr_eos();
23700 needclr = FALSE;
23701 }
23702 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 }
23704 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023705 {
23706#ifdef FEAT_MBYTE
23707 if (has_mbyte)
23708 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023709 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023710
23711 (void)msg_outtrans_len_attr(p, i, echo_attr);
23712 p += i - 1;
23713 }
23714 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023716 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023719 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023721 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 arg = skipwhite(arg);
23723 }
23724 eap->nextcmd = check_nextcmd(arg);
23725
23726 if (eap->skip)
23727 --emsg_skip;
23728 else
23729 {
23730 /* remove text that may still be there from the command */
23731 if (needclr)
23732 msg_clr_eos();
23733 if (eap->cmdidx == CMD_echo)
23734 msg_end();
23735 }
23736}
23737
23738/*
23739 * ":echohl {name}".
23740 */
23741 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023742ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743{
23744 int id;
23745
23746 id = syn_name2id(eap->arg);
23747 if (id == 0)
23748 echo_attr = 0;
23749 else
23750 echo_attr = syn_id2attr(id);
23751}
23752
23753/*
23754 * ":execute expr1 ..." execute the result of an expression.
23755 * ":echomsg expr1 ..." Print a message
23756 * ":echoerr expr1 ..." Print an error
23757 * Each gets spaces around each argument and a newline at the end for
23758 * echo commands
23759 */
23760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023761ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762{
23763 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023764 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 int ret = OK;
23766 char_u *p;
23767 garray_T ga;
23768 int len;
23769 int save_did_emsg;
23770
23771 ga_init2(&ga, 1, 80);
23772
23773 if (eap->skip)
23774 ++emsg_skip;
23775 while (*arg != NUL && *arg != '|' && *arg != '\n')
23776 {
23777 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023778 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 {
23780 /*
23781 * Report the invalid expression unless the expression evaluation
23782 * has been cancelled due to an aborting error, an interrupt, or an
23783 * exception.
23784 */
23785 if (!aborting())
23786 EMSG2(_(e_invexpr2), p);
23787 ret = FAIL;
23788 break;
23789 }
23790
23791 if (!eap->skip)
23792 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023793 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 len = (int)STRLEN(p);
23795 if (ga_grow(&ga, len + 2) == FAIL)
23796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023797 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 ret = FAIL;
23799 break;
23800 }
23801 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804 ga.ga_len += len;
23805 }
23806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023807 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808 arg = skipwhite(arg);
23809 }
23810
23811 if (ret != FAIL && ga.ga_data != NULL)
23812 {
23813 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023814 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023816 out_flush();
23817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 else if (eap->cmdidx == CMD_echoerr)
23819 {
23820 /* We don't want to abort following commands, restore did_emsg. */
23821 save_did_emsg = did_emsg;
23822 EMSG((char_u *)ga.ga_data);
23823 if (!force_abort)
23824 did_emsg = save_did_emsg;
23825 }
23826 else if (eap->cmdidx == CMD_execute)
23827 do_cmdline((char_u *)ga.ga_data,
23828 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23829 }
23830
23831 ga_clear(&ga);
23832
23833 if (eap->skip)
23834 --emsg_skip;
23835
23836 eap->nextcmd = check_nextcmd(arg);
23837}
23838
23839/*
23840 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23841 * "arg" points to the "&" or '+' when called, to "option" when returning.
23842 * Returns NULL when no option name found. Otherwise pointer to the char
23843 * after the option name.
23844 */
23845 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023846find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847{
23848 char_u *p = *arg;
23849
23850 ++p;
23851 if (*p == 'g' && p[1] == ':')
23852 {
23853 *opt_flags = OPT_GLOBAL;
23854 p += 2;
23855 }
23856 else if (*p == 'l' && p[1] == ':')
23857 {
23858 *opt_flags = OPT_LOCAL;
23859 p += 2;
23860 }
23861 else
23862 *opt_flags = 0;
23863
23864 if (!ASCII_ISALPHA(*p))
23865 return NULL;
23866 *arg = p;
23867
23868 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23869 p += 4; /* termcap option */
23870 else
23871 while (ASCII_ISALPHA(*p))
23872 ++p;
23873 return p;
23874}
23875
23876/*
23877 * ":function"
23878 */
23879 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023880ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881{
23882 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023883 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884 int j;
23885 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023887 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 char_u *name = NULL;
23889 char_u *p;
23890 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023891 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 garray_T newargs;
23893 garray_T newlines;
23894 int varargs = FALSE;
23895 int mustend = FALSE;
23896 int flags = 0;
23897 ufunc_T *fp;
23898 int indent;
23899 int nesting;
23900 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023901 dictitem_T *v;
23902 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023903 static int func_nr = 0; /* number for nameless function */
23904 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023905 hashtab_T *ht;
23906 int todo;
23907 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023908 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023909
23910 /*
23911 * ":function" without argument: list functions.
23912 */
23913 if (ends_excmd(*eap->arg))
23914 {
23915 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023916 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023917 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023918 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023919 {
23920 if (!HASHITEM_EMPTY(hi))
23921 {
23922 --todo;
23923 fp = HI2UF(hi);
23924 if (!isdigit(*fp->uf_name))
23925 list_func_head(fp, FALSE);
23926 }
23927 }
23928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 eap->nextcmd = check_nextcmd(eap->arg);
23930 return;
23931 }
23932
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023933 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023934 * ":function /pat": list functions matching pattern.
23935 */
23936 if (*eap->arg == '/')
23937 {
23938 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23939 if (!eap->skip)
23940 {
23941 regmatch_T regmatch;
23942
23943 c = *p;
23944 *p = NUL;
23945 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23946 *p = c;
23947 if (regmatch.regprog != NULL)
23948 {
23949 regmatch.rm_ic = p_ic;
23950
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023951 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023952 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23953 {
23954 if (!HASHITEM_EMPTY(hi))
23955 {
23956 --todo;
23957 fp = HI2UF(hi);
23958 if (!isdigit(*fp->uf_name)
23959 && vim_regexec(&regmatch, fp->uf_name, 0))
23960 list_func_head(fp, FALSE);
23961 }
23962 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023963 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023964 }
23965 }
23966 if (*p == '/')
23967 ++p;
23968 eap->nextcmd = check_nextcmd(p);
23969 return;
23970 }
23971
23972 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023973 * Get the function name. There are these situations:
23974 * func normal function name
23975 * "name" == func, "fudi.fd_dict" == NULL
23976 * dict.func new dictionary entry
23977 * "name" == NULL, "fudi.fd_dict" set,
23978 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23979 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023980 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023981 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23982 * dict.func existing dict entry that's not a Funcref
23983 * "name" == NULL, "fudi.fd_dict" set,
23984 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023985 * s:func script-local function name
23986 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023989 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023990 paren = (vim_strchr(p, '(') != NULL);
23991 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 {
23993 /*
23994 * Return on an invalid expression in braces, unless the expression
23995 * evaluation has been cancelled due to an aborting error, an
23996 * interrupt, or an exception.
23997 */
23998 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023999 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024000 if (!eap->skip && fudi.fd_newkey != NULL)
24001 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024002 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 else
24006 eap->skip = TRUE;
24007 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024008
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 /* An error in a function call during evaluation of an expression in magic
24010 * braces should not cause the function not to be defined. */
24011 saved_did_emsg = did_emsg;
24012 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013
24014 /*
24015 * ":function func" with only function name: list function.
24016 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024017 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 {
24019 if (!ends_excmd(*skipwhite(p)))
24020 {
24021 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024022 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 }
24024 eap->nextcmd = check_nextcmd(p);
24025 if (eap->nextcmd != NULL)
24026 *p = NUL;
24027 if (!eap->skip && !got_int)
24028 {
24029 fp = find_func(name);
24030 if (fp != NULL)
24031 {
24032 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024033 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024035 if (FUNCLINE(fp, j) == NULL)
24036 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 msg_putchar('\n');
24038 msg_outnum((long)(j + 1));
24039 if (j < 9)
24040 msg_putchar(' ');
24041 if (j < 99)
24042 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024043 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 out_flush(); /* show a line at a time */
24045 ui_breakcheck();
24046 }
24047 if (!got_int)
24048 {
24049 msg_putchar('\n');
24050 msg_puts((char_u *)" endfunction");
24051 }
24052 }
24053 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024054 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024056 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057 }
24058
24059 /*
24060 * ":function name(arg1, arg2)" Define function.
24061 */
24062 p = skipwhite(p);
24063 if (*p != '(')
24064 {
24065 if (!eap->skip)
24066 {
24067 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024068 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069 }
24070 /* attempt to continue by skipping some text */
24071 if (vim_strchr(p, '(') != NULL)
24072 p = vim_strchr(p, '(');
24073 }
24074 p = skipwhite(p + 1);
24075
24076 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24077 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24078
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024079 if (!eap->skip)
24080 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024081 /* Check the name of the function. Unless it's a dictionary function
24082 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024083 if (name != NULL)
24084 arg = name;
24085 else
24086 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024087 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024088 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24089 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024090 {
24091 if (*arg == K_SPECIAL)
24092 j = 3;
24093 else
24094 j = 0;
24095 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24096 : eval_isnamec(arg[j])))
24097 ++j;
24098 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024099 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024100 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024101 /* Disallow using the g: dict. */
24102 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24103 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024104 }
24105
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106 /*
24107 * Isolate the arguments: "arg1, arg2, ...)"
24108 */
24109 while (*p != ')')
24110 {
24111 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24112 {
24113 varargs = TRUE;
24114 p += 3;
24115 mustend = TRUE;
24116 }
24117 else
24118 {
24119 arg = p;
24120 while (ASCII_ISALNUM(*p) || *p == '_')
24121 ++p;
24122 if (arg == p || isdigit(*arg)
24123 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24124 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24125 {
24126 if (!eap->skip)
24127 EMSG2(_("E125: Illegal argument: %s"), arg);
24128 break;
24129 }
24130 if (ga_grow(&newargs, 1) == FAIL)
24131 goto erret;
24132 c = *p;
24133 *p = NUL;
24134 arg = vim_strsave(arg);
24135 if (arg == NULL)
24136 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024137
24138 /* Check for duplicate argument name. */
24139 for (i = 0; i < newargs.ga_len; ++i)
24140 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24141 {
24142 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024143 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024144 goto erret;
24145 }
24146
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24148 *p = c;
24149 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 if (*p == ',')
24151 ++p;
24152 else
24153 mustend = TRUE;
24154 }
24155 p = skipwhite(p);
24156 if (mustend && *p != ')')
24157 {
24158 if (!eap->skip)
24159 EMSG2(_(e_invarg2), eap->arg);
24160 break;
24161 }
24162 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024163 if (*p != ')')
24164 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165 ++p; /* skip the ')' */
24166
Bram Moolenaare9a41262005-01-15 22:18:47 +000024167 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168 for (;;)
24169 {
24170 p = skipwhite(p);
24171 if (STRNCMP(p, "range", 5) == 0)
24172 {
24173 flags |= FC_RANGE;
24174 p += 5;
24175 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024176 else if (STRNCMP(p, "dict", 4) == 0)
24177 {
24178 flags |= FC_DICT;
24179 p += 4;
24180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 else if (STRNCMP(p, "abort", 5) == 0)
24182 {
24183 flags |= FC_ABORT;
24184 p += 5;
24185 }
24186 else
24187 break;
24188 }
24189
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024190 /* When there is a line break use what follows for the function body.
24191 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24192 if (*p == '\n')
24193 line_arg = p + 1;
24194 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 EMSG(_(e_trailing));
24196
24197 /*
24198 * Read the body of the function, until ":endfunction" is found.
24199 */
24200 if (KeyTyped)
24201 {
24202 /* Check if the function already exists, don't let the user type the
24203 * whole function before telling him it doesn't work! For a script we
24204 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024205 if (!eap->skip && !eap->forceit)
24206 {
24207 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24208 EMSG(_(e_funcdict));
24209 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024210 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024213 if (!eap->skip && did_emsg)
24214 goto erret;
24215
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216 msg_putchar('\n'); /* don't overwrite the function name */
24217 cmdline_row = msg_row;
24218 }
24219
24220 indent = 2;
24221 nesting = 0;
24222 for (;;)
24223 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024224 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024225 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024226 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024227 saved_wait_return = FALSE;
24228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024230 sourcing_lnum_off = sourcing_lnum;
24231
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024232 if (line_arg != NULL)
24233 {
24234 /* Use eap->arg, split up in parts by line breaks. */
24235 theline = line_arg;
24236 p = vim_strchr(theline, '\n');
24237 if (p == NULL)
24238 line_arg += STRLEN(line_arg);
24239 else
24240 {
24241 *p = NUL;
24242 line_arg = p + 1;
24243 }
24244 }
24245 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 theline = getcmdline(':', 0L, indent);
24247 else
24248 theline = eap->getline(':', eap->cookie, indent);
24249 if (KeyTyped)
24250 lines_left = Rows - 1;
24251 if (theline == NULL)
24252 {
24253 EMSG(_("E126: Missing :endfunction"));
24254 goto erret;
24255 }
24256
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024257 /* Detect line continuation: sourcing_lnum increased more than one. */
24258 if (sourcing_lnum > sourcing_lnum_off + 1)
24259 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24260 else
24261 sourcing_lnum_off = 0;
24262
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 if (skip_until != NULL)
24264 {
24265 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24266 * don't check for ":endfunc". */
24267 if (STRCMP(theline, skip_until) == 0)
24268 {
24269 vim_free(skip_until);
24270 skip_until = NULL;
24271 }
24272 }
24273 else
24274 {
24275 /* skip ':' and blanks*/
24276 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24277 ;
24278
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024279 /* Check for "endfunction". */
24280 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024282 if (line_arg == NULL)
24283 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 break;
24285 }
24286
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024287 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288 * at "end". */
24289 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24290 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024291 else if (STRNCMP(p, "if", 2) == 0
24292 || STRNCMP(p, "wh", 2) == 0
24293 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294 || STRNCMP(p, "try", 3) == 0)
24295 indent += 2;
24296
24297 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024298 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024300 if (*p == '!')
24301 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024303 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024304 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024306 ++nesting;
24307 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 }
24309 }
24310
24311 /* Check for ":append" or ":insert". */
24312 p = skip_range(p, NULL);
24313 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24314 || (p[0] == 'i'
24315 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24316 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24317 skip_until = vim_strsave((char_u *)".");
24318
24319 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24320 arg = skipwhite(skiptowhite(p));
24321 if (arg[0] == '<' && arg[1] =='<'
24322 && ((p[0] == 'p' && p[1] == 'y'
24323 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24324 || (p[0] == 'p' && p[1] == 'e'
24325 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24326 || (p[0] == 't' && p[1] == 'c'
24327 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024328 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24329 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24331 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024332 || (p[0] == 'm' && p[1] == 'z'
24333 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334 ))
24335 {
24336 /* ":python <<" continues until a dot, like ":append" */
24337 p = skipwhite(arg + 2);
24338 if (*p == NUL)
24339 skip_until = vim_strsave((char_u *)".");
24340 else
24341 skip_until = vim_strsave(p);
24342 }
24343 }
24344
24345 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024346 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024347 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024348 if (line_arg == NULL)
24349 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024351 }
24352
24353 /* Copy the line to newly allocated memory. get_one_sourceline()
24354 * allocates 250 bytes per line, this saves 80% on average. The cost
24355 * is an extra alloc/free. */
24356 p = vim_strsave(theline);
24357 if (p != NULL)
24358 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024359 if (line_arg == NULL)
24360 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024361 theline = p;
24362 }
24363
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024364 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24365
24366 /* Add NULL lines for continuation lines, so that the line count is
24367 * equal to the index in the growarray. */
24368 while (sourcing_lnum_off-- > 0)
24369 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024370
24371 /* Check for end of eap->arg. */
24372 if (line_arg != NULL && *line_arg == NUL)
24373 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374 }
24375
24376 /* Don't define the function when skipping commands or when an error was
24377 * detected. */
24378 if (eap->skip || did_emsg)
24379 goto erret;
24380
24381 /*
24382 * If there are no errors, add the function
24383 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024384 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024385 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024386 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024387 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024388 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024389 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024390 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024391 goto erret;
24392 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024393
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024394 fp = find_func(name);
24395 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024397 if (!eap->forceit)
24398 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024399 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024400 goto erret;
24401 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024402 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024403 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024404 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024405 name);
24406 goto erret;
24407 }
24408 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024409 ga_clear_strings(&(fp->uf_args));
24410 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024411 vim_free(name);
24412 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 }
24415 else
24416 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024417 char numbuf[20];
24418
24419 fp = NULL;
24420 if (fudi.fd_newkey == NULL && !eap->forceit)
24421 {
24422 EMSG(_(e_funcdict));
24423 goto erret;
24424 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024425 if (fudi.fd_di == NULL)
24426 {
24427 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024428 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024429 goto erret;
24430 }
24431 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024432 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024433 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024434
24435 /* Give the function a sequential number. Can only be used with a
24436 * Funcref! */
24437 vim_free(name);
24438 sprintf(numbuf, "%d", ++func_nr);
24439 name = vim_strsave((char_u *)numbuf);
24440 if (name == NULL)
24441 goto erret;
24442 }
24443
24444 if (fp == NULL)
24445 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024446 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024447 {
24448 int slen, plen;
24449 char_u *scriptname;
24450
24451 /* Check that the autoload name matches the script name. */
24452 j = FAIL;
24453 if (sourcing_name != NULL)
24454 {
24455 scriptname = autoload_name(name);
24456 if (scriptname != NULL)
24457 {
24458 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024459 plen = (int)STRLEN(p);
24460 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024461 if (slen > plen && fnamecmp(p,
24462 sourcing_name + slen - plen) == 0)
24463 j = OK;
24464 vim_free(scriptname);
24465 }
24466 }
24467 if (j == FAIL)
24468 {
24469 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24470 goto erret;
24471 }
24472 }
24473
24474 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024475 if (fp == NULL)
24476 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024477
24478 if (fudi.fd_dict != NULL)
24479 {
24480 if (fudi.fd_di == NULL)
24481 {
24482 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024483 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024484 if (fudi.fd_di == NULL)
24485 {
24486 vim_free(fp);
24487 goto erret;
24488 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024489 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24490 {
24491 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024492 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024493 goto erret;
24494 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024495 }
24496 else
24497 /* overwrite existing dict entry */
24498 clear_tv(&fudi.fd_di->di_tv);
24499 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024500 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024501 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024502 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024503
24504 /* behave like "dict" was used */
24505 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024506 }
24507
Bram Moolenaar071d4272004-06-13 20:20:40 +000024508 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024509 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024510 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24511 {
24512 vim_free(fp);
24513 goto erret;
24514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024516 fp->uf_args = newargs;
24517 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024518#ifdef FEAT_PROFILE
24519 fp->uf_tml_count = NULL;
24520 fp->uf_tml_total = NULL;
24521 fp->uf_tml_self = NULL;
24522 fp->uf_profiling = FALSE;
24523 if (prof_def_func())
24524 func_do_profile(fp);
24525#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024526 fp->uf_varargs = varargs;
24527 fp->uf_flags = flags;
24528 fp->uf_calls = 0;
24529 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024530 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531
24532erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 ga_clear_strings(&newargs);
24534 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024535ret_free:
24536 vim_free(skip_until);
24537 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024540 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541}
24542
24543/*
24544 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024545 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024547 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024548 * TFN_INT: internal function name OK
24549 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024550 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024551 * Advances "pp" to just after the function name (if no error).
24552 */
24553 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024554trans_function_name(
24555 char_u **pp,
24556 int skip, /* only find the end, don't evaluate */
24557 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024558 funcdict_T *fdp, /* return: info about dictionary used */
24559 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024560{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024561 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562 char_u *start;
24563 char_u *end;
24564 int lead;
24565 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024567 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024568
24569 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024570 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024572
24573 /* Check for hard coded <SNR>: already translated function ID (from a user
24574 * command). */
24575 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24576 && (*pp)[2] == (int)KE_SNR)
24577 {
24578 *pp += 3;
24579 len = get_id_len(pp) + 3;
24580 return vim_strnsave(start, len);
24581 }
24582
24583 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24584 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024586 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024587 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024588
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024589 /* Note that TFN_ flags use the same values as GLV_ flags. */
24590 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024591 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024592 if (end == start)
24593 {
24594 if (!skip)
24595 EMSG(_("E129: Function name required"));
24596 goto theend;
24597 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024598 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024599 {
24600 /*
24601 * Report an invalid expression in braces, unless the expression
24602 * evaluation has been cancelled due to an aborting error, an
24603 * interrupt, or an exception.
24604 */
24605 if (!aborting())
24606 {
24607 if (end != NULL)
24608 EMSG2(_(e_invarg2), start);
24609 }
24610 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024611 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024612 goto theend;
24613 }
24614
24615 if (lv.ll_tv != NULL)
24616 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024617 if (fdp != NULL)
24618 {
24619 fdp->fd_dict = lv.ll_dict;
24620 fdp->fd_newkey = lv.ll_newkey;
24621 lv.ll_newkey = NULL;
24622 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024623 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024624 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24625 {
24626 name = vim_strsave(lv.ll_tv->vval.v_string);
24627 *pp = end;
24628 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024629 else if (lv.ll_tv->v_type == VAR_PARTIAL
24630 && lv.ll_tv->vval.v_partial != NULL)
24631 {
24632 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24633 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024634 if (partial != NULL)
24635 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024636 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024637 else
24638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024639 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24640 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024641 EMSG(_(e_funcref));
24642 else
24643 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024644 name = NULL;
24645 }
24646 goto theend;
24647 }
24648
24649 if (lv.ll_name == NULL)
24650 {
24651 /* Error found, but continue after the function name. */
24652 *pp = end;
24653 goto theend;
24654 }
24655
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024656 /* Check if the name is a Funcref. If so, use the value. */
24657 if (lv.ll_exp_name != NULL)
24658 {
24659 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024660 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024661 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024662 if (name == lv.ll_exp_name)
24663 name = NULL;
24664 }
24665 else
24666 {
24667 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024668 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024669 if (name == *pp)
24670 name = NULL;
24671 }
24672 if (name != NULL)
24673 {
24674 name = vim_strsave(name);
24675 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024676 if (STRNCMP(name, "<SNR>", 5) == 0)
24677 {
24678 /* Change "<SNR>" to the byte sequence. */
24679 name[0] = K_SPECIAL;
24680 name[1] = KS_EXTRA;
24681 name[2] = (int)KE_SNR;
24682 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24683 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024684 goto theend;
24685 }
24686
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024687 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024688 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024689 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024690 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24691 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24692 {
24693 /* When there was "s:" already or the name expanded to get a
24694 * leading "s:" then remove it. */
24695 lv.ll_name += 2;
24696 len -= 2;
24697 lead = 2;
24698 }
24699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024700 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024701 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024702 /* skip over "s:" and "g:" */
24703 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024704 lv.ll_name += 2;
24705 len = (int)(end - lv.ll_name);
24706 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024707
24708 /*
24709 * Copy the function name to allocated memory.
24710 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24711 * Accept <SNR>123_name() outside a script.
24712 */
24713 if (skip)
24714 lead = 0; /* do nothing */
24715 else if (lead > 0)
24716 {
24717 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024718 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24719 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024720 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024721 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024722 if (current_SID <= 0)
24723 {
24724 EMSG(_(e_usingsid));
24725 goto theend;
24726 }
24727 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24728 lead += (int)STRLEN(sid_buf);
24729 }
24730 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024731 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024732 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024733 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024734 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024735 goto theend;
24736 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024737 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024738 {
24739 char_u *cp = vim_strchr(lv.ll_name, ':');
24740
24741 if (cp != NULL && cp < end)
24742 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024743 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024744 goto theend;
24745 }
24746 }
24747
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024748 name = alloc((unsigned)(len + lead + 1));
24749 if (name != NULL)
24750 {
24751 if (lead > 0)
24752 {
24753 name[0] = K_SPECIAL;
24754 name[1] = KS_EXTRA;
24755 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024756 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024757 STRCPY(name + 3, sid_buf);
24758 }
24759 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024760 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024761 }
24762 *pp = end;
24763
24764theend:
24765 clear_lval(&lv);
24766 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024767}
24768
24769/*
24770 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24771 * Return 2 if "p" starts with "s:".
24772 * Return 0 otherwise.
24773 */
24774 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024775eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024776{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024777 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24778 * the standard library function. */
24779 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24780 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 return 5;
24782 if (p[0] == 's' && p[1] == ':')
24783 return 2;
24784 return 0;
24785}
24786
24787/*
24788 * Return TRUE if "p" starts with "<SID>" or "s:".
24789 * Only works if eval_fname_script() returned non-zero for "p"!
24790 */
24791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024792eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793{
24794 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24795}
24796
24797/*
24798 * List the head of the function: "name(arg1, arg2)".
24799 */
24800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024801list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802{
24803 int j;
24804
24805 msg_start();
24806 if (indent)
24807 MSG_PUTS(" ");
24808 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024809 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810 {
24811 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024812 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024813 }
24814 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024815 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024817 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024818 {
24819 if (j)
24820 MSG_PUTS(", ");
24821 msg_puts(FUNCARG(fp, j));
24822 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024823 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824 {
24825 if (j)
24826 MSG_PUTS(", ");
24827 MSG_PUTS("...");
24828 }
24829 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024830 if (fp->uf_flags & FC_ABORT)
24831 MSG_PUTS(" abort");
24832 if (fp->uf_flags & FC_RANGE)
24833 MSG_PUTS(" range");
24834 if (fp->uf_flags & FC_DICT)
24835 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024836 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024837 if (p_verbose > 0)
24838 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839}
24840
24841/*
24842 * Find a function by name, return pointer to it in ufuncs.
24843 * Return NULL for unknown function.
24844 */
24845 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024846find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024848 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024849
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024850 hi = hash_find(&func_hashtab, name);
24851 if (!HASHITEM_EMPTY(hi))
24852 return HI2UF(hi);
24853 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854}
24855
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024856#if defined(EXITFREE) || defined(PROTO)
24857 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024858free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024859{
24860 hashitem_T *hi;
24861
24862 /* Need to start all over every time, because func_free() may change the
24863 * hash table. */
24864 while (func_hashtab.ht_used > 0)
24865 for (hi = func_hashtab.ht_array; ; ++hi)
24866 if (!HASHITEM_EMPTY(hi))
24867 {
24868 func_free(HI2UF(hi));
24869 break;
24870 }
24871}
24872#endif
24873
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024875translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024876{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024877 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024878 return find_internal_func(name) >= 0;
24879 return find_func(name) != NULL;
24880}
24881
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024882/*
24883 * Return TRUE if a function "name" exists.
24884 */
24885 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024886function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024887{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024888 char_u *nm = name;
24889 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024890 int n = FALSE;
24891
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024892 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024893 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024894 nm = skipwhite(nm);
24895
24896 /* Only accept "funcname", "funcname ", "funcname (..." and
24897 * "funcname(...", not "funcname!...". */
24898 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024899 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024900 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024901 return n;
24902}
24903
Bram Moolenaara1544c02013-05-30 12:35:52 +020024904 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024905get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024906{
24907 char_u *nm = name;
24908 char_u *p;
24909
Bram Moolenaar65639032016-03-16 21:40:30 +010024910 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024911
24912 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024913 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024914 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024915
Bram Moolenaara1544c02013-05-30 12:35:52 +020024916 vim_free(p);
24917 return NULL;
24918}
24919
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024920/*
24921 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024922 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24923 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024924 */
24925 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024926builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024927{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024928 char_u *p;
24929
24930 if (!ASCII_ISLOWER(name[0]))
24931 return FALSE;
24932 p = vim_strchr(name, AUTOLOAD_CHAR);
24933 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024934}
24935
Bram Moolenaar05159a02005-02-26 23:04:13 +000024936#if defined(FEAT_PROFILE) || defined(PROTO)
24937/*
24938 * Start profiling function "fp".
24939 */
24940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024941func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024942{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024943 int len = fp->uf_lines.ga_len;
24944
24945 if (len == 0)
24946 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024947 fp->uf_tm_count = 0;
24948 profile_zero(&fp->uf_tm_self);
24949 profile_zero(&fp->uf_tm_total);
24950 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024951 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024952 if (fp->uf_tml_total == NULL)
24953 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024954 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024955 if (fp->uf_tml_self == NULL)
24956 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024957 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024958 fp->uf_tml_idx = -1;
24959 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24960 || fp->uf_tml_self == NULL)
24961 return; /* out of memory */
24962
24963 fp->uf_profiling = TRUE;
24964}
24965
24966/*
24967 * Dump the profiling results for all functions in file "fd".
24968 */
24969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024970func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024971{
24972 hashitem_T *hi;
24973 int todo;
24974 ufunc_T *fp;
24975 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024976 ufunc_T **sorttab;
24977 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024978
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024979 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024980 if (todo == 0)
24981 return; /* nothing to dump */
24982
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024983 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024984
Bram Moolenaar05159a02005-02-26 23:04:13 +000024985 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24986 {
24987 if (!HASHITEM_EMPTY(hi))
24988 {
24989 --todo;
24990 fp = HI2UF(hi);
24991 if (fp->uf_profiling)
24992 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024993 if (sorttab != NULL)
24994 sorttab[st_len++] = fp;
24995
Bram Moolenaar05159a02005-02-26 23:04:13 +000024996 if (fp->uf_name[0] == K_SPECIAL)
24997 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24998 else
24999 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25000 if (fp->uf_tm_count == 1)
25001 fprintf(fd, "Called 1 time\n");
25002 else
25003 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25004 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25005 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25006 fprintf(fd, "\n");
25007 fprintf(fd, "count total (s) self (s)\n");
25008
25009 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25010 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025011 if (FUNCLINE(fp, i) == NULL)
25012 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025013 prof_func_line(fd, fp->uf_tml_count[i],
25014 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025015 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25016 }
25017 fprintf(fd, "\n");
25018 }
25019 }
25020 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025021
25022 if (sorttab != NULL && st_len > 0)
25023 {
25024 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25025 prof_total_cmp);
25026 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25027 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25028 prof_self_cmp);
25029 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25030 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025031
25032 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025033}
Bram Moolenaar73830342005-02-28 22:48:19 +000025034
25035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025036prof_sort_list(
25037 FILE *fd,
25038 ufunc_T **sorttab,
25039 int st_len,
25040 char *title,
25041 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025042{
25043 int i;
25044 ufunc_T *fp;
25045
25046 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25047 fprintf(fd, "count total (s) self (s) function\n");
25048 for (i = 0; i < 20 && i < st_len; ++i)
25049 {
25050 fp = sorttab[i];
25051 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25052 prefer_self);
25053 if (fp->uf_name[0] == K_SPECIAL)
25054 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25055 else
25056 fprintf(fd, " %s()\n", fp->uf_name);
25057 }
25058 fprintf(fd, "\n");
25059}
25060
25061/*
25062 * Print the count and times for one function or function line.
25063 */
25064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025065prof_func_line(
25066 FILE *fd,
25067 int count,
25068 proftime_T *total,
25069 proftime_T *self,
25070 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025071{
25072 if (count > 0)
25073 {
25074 fprintf(fd, "%5d ", count);
25075 if (prefer_self && profile_equal(total, self))
25076 fprintf(fd, " ");
25077 else
25078 fprintf(fd, "%s ", profile_msg(total));
25079 if (!prefer_self && profile_equal(total, self))
25080 fprintf(fd, " ");
25081 else
25082 fprintf(fd, "%s ", profile_msg(self));
25083 }
25084 else
25085 fprintf(fd, " ");
25086}
25087
25088/*
25089 * Compare function for total time sorting.
25090 */
25091 static int
25092#ifdef __BORLANDC__
25093_RTLENTRYF
25094#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025095prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025096{
25097 ufunc_T *p1, *p2;
25098
25099 p1 = *(ufunc_T **)s1;
25100 p2 = *(ufunc_T **)s2;
25101 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25102}
25103
25104/*
25105 * Compare function for self time sorting.
25106 */
25107 static int
25108#ifdef __BORLANDC__
25109_RTLENTRYF
25110#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025111prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025112{
25113 ufunc_T *p1, *p2;
25114
25115 p1 = *(ufunc_T **)s1;
25116 p2 = *(ufunc_T **)s2;
25117 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25118}
25119
Bram Moolenaar05159a02005-02-26 23:04:13 +000025120#endif
25121
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025122/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025123 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025124 * Return TRUE if a package was loaded.
25125 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025127script_autoload(
25128 char_u *name,
25129 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025130{
25131 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025132 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025133 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025134 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025135
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025136 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025137 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025138 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025139 return FALSE;
25140
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025141 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025142
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025143 /* Find the name in the list of previously loaded package names. Skip
25144 * "autoload/", it's always the same. */
25145 for (i = 0; i < ga_loaded.ga_len; ++i)
25146 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25147 break;
25148 if (!reload && i < ga_loaded.ga_len)
25149 ret = FALSE; /* was loaded already */
25150 else
25151 {
25152 /* Remember the name if it wasn't loaded already. */
25153 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25154 {
25155 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25156 tofree = NULL;
25157 }
25158
25159 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025160 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025161 ret = TRUE;
25162 }
25163
25164 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025165 return ret;
25166}
25167
25168/*
25169 * Return the autoload script name for a function or variable name.
25170 * Returns NULL when out of memory.
25171 */
25172 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025173autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025174{
25175 char_u *p;
25176 char_u *scriptname;
25177
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025178 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025179 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25180 if (scriptname == NULL)
25181 return FALSE;
25182 STRCPY(scriptname, "autoload/");
25183 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025184 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025185 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025186 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025187 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025188 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025189}
25190
Bram Moolenaar071d4272004-06-13 20:20:40 +000025191#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25192
25193/*
25194 * Function given to ExpandGeneric() to obtain the list of user defined
25195 * function names.
25196 */
25197 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025198get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025200 static long_u done;
25201 static hashitem_T *hi;
25202 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203
25204 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025206 done = 0;
25207 hi = func_hashtab.ht_array;
25208 }
25209 if (done < func_hashtab.ht_used)
25210 {
25211 if (done++ > 0)
25212 ++hi;
25213 while (HASHITEM_EMPTY(hi))
25214 ++hi;
25215 fp = HI2UF(hi);
25216
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025217 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025218 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025219
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025220 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25221 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025222
25223 cat_func_name(IObuff, fp);
25224 if (xp->xp_context != EXPAND_USER_FUNC)
25225 {
25226 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025227 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228 STRCAT(IObuff, ")");
25229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230 return IObuff;
25231 }
25232 return NULL;
25233}
25234
25235#endif /* FEAT_CMDL_COMPL */
25236
25237/*
25238 * Copy the function name of "fp" to buffer "buf".
25239 * "buf" must be able to hold the function name plus three bytes.
25240 * Takes care of script-local function names.
25241 */
25242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025243cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025245 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246 {
25247 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025248 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 }
25250 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025251 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252}
25253
25254/*
25255 * ":delfunction {name}"
25256 */
25257 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025258ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025259{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025260 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261 char_u *p;
25262 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025263 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264
25265 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025266 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025267 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025269 {
25270 if (fudi.fd_dict != NULL && !eap->skip)
25271 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 if (!ends_excmd(*skipwhite(p)))
25275 {
25276 vim_free(name);
25277 EMSG(_(e_trailing));
25278 return;
25279 }
25280 eap->nextcmd = check_nextcmd(p);
25281 if (eap->nextcmd != NULL)
25282 *p = NUL;
25283
25284 if (!eap->skip)
25285 fp = find_func(name);
25286 vim_free(name);
25287
25288 if (!eap->skip)
25289 {
25290 if (fp == NULL)
25291 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025292 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 return;
25294 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025295 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 {
25297 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25298 return;
25299 }
25300
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025301 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025303 /* Delete the dict item that refers to the function, it will
25304 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025305 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025307 else
25308 func_free(fp);
25309 }
25310}
25311
25312/*
25313 * Free a function and remove it from the list of functions.
25314 */
25315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025316func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025317{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025318 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025319
25320 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025321 ga_clear_strings(&(fp->uf_args));
25322 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025323#ifdef FEAT_PROFILE
25324 vim_free(fp->uf_tml_count);
25325 vim_free(fp->uf_tml_total);
25326 vim_free(fp->uf_tml_self);
25327#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025328
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025329 /* remove the function from the function hashtable */
25330 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25331 if (HASHITEM_EMPTY(hi))
25332 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025333 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025334 hash_remove(&func_hashtab, hi);
25335
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025336 vim_free(fp);
25337}
25338
25339/*
25340 * Unreference a Function: decrement the reference count and free it when it
25341 * becomes zero. Only for numbered functions.
25342 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025344func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025345{
25346 ufunc_T *fp;
25347
25348 if (name != NULL && isdigit(*name))
25349 {
25350 fp = find_func(name);
25351 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025352 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025353#ifdef EXITFREE
25354 if (!entered_free_all_mem)
25355#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025356 EMSG2(_(e_intern2), "func_unref()");
25357 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025358 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025359 {
25360 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025361 * when "uf_calls" becomes zero. */
25362 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025363 func_free(fp);
25364 }
25365 }
25366}
25367
25368/*
25369 * Count a reference to a Function.
25370 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025371 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025372func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025373{
25374 ufunc_T *fp;
25375
25376 if (name != NULL && isdigit(*name))
25377 {
25378 fp = find_func(name);
25379 if (fp == NULL)
25380 EMSG2(_(e_intern2), "func_ref()");
25381 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025382 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383 }
25384}
25385
25386/*
25387 * Call a user function.
25388 */
25389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025390call_user_func(
25391 ufunc_T *fp, /* pointer to function */
25392 int argcount, /* nr of args */
25393 typval_T *argvars, /* arguments */
25394 typval_T *rettv, /* return value */
25395 linenr_T firstline, /* first line of range */
25396 linenr_T lastline, /* last line of range */
25397 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398{
Bram Moolenaar33570922005-01-25 22:26:29 +000025399 char_u *save_sourcing_name;
25400 linenr_T save_sourcing_lnum;
25401 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025402 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025403 int save_did_emsg;
25404 static int depth = 0;
25405 dictitem_T *v;
25406 int fixvar_idx = 0; /* index in fixvar[] */
25407 int i;
25408 int ai;
25409 char_u numbuf[NUMBUFLEN];
25410 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025411 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025412#ifdef FEAT_PROFILE
25413 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025414 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416
25417 /* If depth of calling is getting too high, don't execute the function */
25418 if (depth >= p_mfd)
25419 {
25420 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025421 rettv->v_type = VAR_NUMBER;
25422 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025423 return;
25424 }
25425 ++depth;
25426
25427 line_breakcheck(); /* check for CTRL-C hit */
25428
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025429 fc = (funccall_T *)alloc(sizeof(funccall_T));
25430 fc->caller = current_funccal;
25431 current_funccal = fc;
25432 fc->func = fp;
25433 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025434 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025435 fc->linenr = 0;
25436 fc->returned = FALSE;
25437 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025438 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025439 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25440 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441
Bram Moolenaar33570922005-01-25 22:26:29 +000025442 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025443 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025444 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25445 * each argument variable and saves a lot of time.
25446 */
25447 /*
25448 * Init l: variables.
25449 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025450 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025451 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025452 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025453 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25454 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025455 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025456 name = v->di_key;
25457 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025458 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025459 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025460 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025461 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025462 v->di_tv.vval.v_dict = selfdict;
25463 ++selfdict->dv_refcount;
25464 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025465
Bram Moolenaar33570922005-01-25 22:26:29 +000025466 /*
25467 * Init a: variables.
25468 * Set a:0 to "argcount".
25469 * Set a:000 to a list with room for the "..." arguments.
25470 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025471 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025472 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025473 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025474 /* Use "name" to avoid a warning from some compiler that checks the
25475 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025476 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025477 name = v->di_key;
25478 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025479 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025480 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025481 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025482 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025483 v->di_tv.vval.v_list = &fc->l_varlist;
25484 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25485 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25486 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025487
25488 /*
25489 * Set a:firstline to "firstline" and a:lastline to "lastline".
25490 * Set a:name to named arguments.
25491 * Set a:N to the "..." arguments.
25492 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025493 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025494 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025495 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025496 (varnumber_T)lastline);
25497 for (i = 0; i < argcount; ++i)
25498 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025499 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025500 if (ai < 0)
25501 /* named argument a:name */
25502 name = FUNCARG(fp, i);
25503 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025504 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025505 /* "..." argument a:1, a:2, etc. */
25506 sprintf((char *)numbuf, "%d", ai + 1);
25507 name = numbuf;
25508 }
25509 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25510 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025511 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025512 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25513 }
25514 else
25515 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025516 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25517 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025518 if (v == NULL)
25519 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025520 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025521 }
25522 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025523 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025524
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025525 /* Note: the values are copied directly to avoid alloc/free.
25526 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025527 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025528 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025529
25530 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25531 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025532 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25533 fc->l_listitems[ai].li_tv = argvars[i];
25534 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025535 }
25536 }
25537
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538 /* Don't redraw while executing the function. */
25539 ++RedrawingDisabled;
25540 save_sourcing_name = sourcing_name;
25541 save_sourcing_lnum = sourcing_lnum;
25542 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025543 /* need space for function name + ("function " + 3) or "[number]" */
25544 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25545 + STRLEN(fp->uf_name) + 20;
25546 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547 if (sourcing_name != NULL)
25548 {
25549 if (save_sourcing_name != NULL
25550 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025551 sprintf((char *)sourcing_name, "%s[%d]..",
25552 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553 else
25554 STRCPY(sourcing_name, "function ");
25555 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25556
25557 if (p_verbose >= 12)
25558 {
25559 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025560 verbose_enter_scroll();
25561
Bram Moolenaar555b2802005-05-19 21:08:39 +000025562 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 if (p_verbose >= 14)
25564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025566 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025567 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025568 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025569
25570 msg_puts((char_u *)"(");
25571 for (i = 0; i < argcount; ++i)
25572 {
25573 if (i > 0)
25574 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025575 if (argvars[i].v_type == VAR_NUMBER)
25576 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577 else
25578 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025579 /* Do not want errors such as E724 here. */
25580 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025581 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025582 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025583 if (s != NULL)
25584 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025585 if (vim_strsize(s) > MSG_BUF_CLEN)
25586 {
25587 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25588 s = buf;
25589 }
25590 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025591 vim_free(tofree);
25592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025593 }
25594 }
25595 msg_puts((char_u *)")");
25596 }
25597 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025598
25599 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025600 --no_wait_return;
25601 }
25602 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025603#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025604 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025605 {
25606 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25607 func_do_profile(fp);
25608 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025609 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025610 {
25611 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025612 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025613 profile_zero(&fp->uf_tm_children);
25614 }
25615 script_prof_save(&wait_start);
25616 }
25617#endif
25618
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025620 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025621 save_did_emsg = did_emsg;
25622 did_emsg = FALSE;
25623
25624 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025625 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025626 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25627
25628 --RedrawingDisabled;
25629
25630 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025631 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025633 clear_tv(rettv);
25634 rettv->v_type = VAR_NUMBER;
25635 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 }
25637
Bram Moolenaar05159a02005-02-26 23:04:13 +000025638#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025639 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025640 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025641 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025642 profile_end(&call_start);
25643 profile_sub_wait(&wait_start, &call_start);
25644 profile_add(&fp->uf_tm_total, &call_start);
25645 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025646 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025647 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025648 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25649 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025650 }
25651 }
25652#endif
25653
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654 /* when being verbose, mention the return value */
25655 if (p_verbose >= 12)
25656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025658 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025659
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025661 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025662 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025663 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025664 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025665 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025667 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025668 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025669 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025670 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025671
Bram Moolenaar555b2802005-05-19 21:08:39 +000025672 /* The value may be very long. Skip the middle part, so that we
25673 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025674 * truncate it at the end. Don't want errors such as E724 here. */
25675 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025676 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025677 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025678 if (s != NULL)
25679 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025680 if (vim_strsize(s) > MSG_BUF_CLEN)
25681 {
25682 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25683 s = buf;
25684 }
25685 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025686 vim_free(tofree);
25687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025688 }
25689 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025690
25691 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692 --no_wait_return;
25693 }
25694
25695 vim_free(sourcing_name);
25696 sourcing_name = save_sourcing_name;
25697 sourcing_lnum = save_sourcing_lnum;
25698 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025699#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025700 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025701 script_prof_restore(&wait_start);
25702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703
25704 if (p_verbose >= 12 && sourcing_name != NULL)
25705 {
25706 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025707 verbose_enter_scroll();
25708
Bram Moolenaar555b2802005-05-19 21:08:39 +000025709 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025710 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025711
25712 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025713 --no_wait_return;
25714 }
25715
25716 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025717 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025718 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025719
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025720 /* If the a:000 list and the l: and a: dicts are not referenced we can
25721 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025722 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25723 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25724 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25725 {
25726 free_funccal(fc, FALSE);
25727 }
25728 else
25729 {
25730 hashitem_T *hi;
25731 listitem_T *li;
25732 int todo;
25733
25734 /* "fc" is still in use. This can happen when returning "a:000" or
25735 * assigning "l:" to a global variable.
25736 * Link "fc" in the list for garbage collection later. */
25737 fc->caller = previous_funccal;
25738 previous_funccal = fc;
25739
25740 /* Make a copy of the a: variables, since we didn't do that above. */
25741 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25742 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25743 {
25744 if (!HASHITEM_EMPTY(hi))
25745 {
25746 --todo;
25747 v = HI2DI(hi);
25748 copy_tv(&v->di_tv, &v->di_tv);
25749 }
25750 }
25751
25752 /* Make a copy of the a:000 items, since we didn't do that above. */
25753 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25754 copy_tv(&li->li_tv, &li->li_tv);
25755 }
25756}
25757
25758/*
25759 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025760 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025761 */
25762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025763can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025764{
25765 return (fc->l_varlist.lv_copyID != copyID
25766 && fc->l_vars.dv_copyID != copyID
25767 && fc->l_avars.dv_copyID != copyID);
25768}
25769
25770/*
25771 * Free "fc" and what it contains.
25772 */
25773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025774free_funccal(
25775 funccall_T *fc,
25776 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025777{
25778 listitem_T *li;
25779
25780 /* The a: variables typevals may not have been allocated, only free the
25781 * allocated variables. */
25782 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25783
25784 /* free all l: variables */
25785 vars_clear(&fc->l_vars.dv_hashtab);
25786
25787 /* Free the a:000 variables if they were allocated. */
25788 if (free_val)
25789 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25790 clear_tv(&li->li_tv);
25791
25792 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793}
25794
25795/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025796 * Add a number variable "name" to dict "dp" with value "nr".
25797 */
25798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025799add_nr_var(
25800 dict_T *dp,
25801 dictitem_T *v,
25802 char *name,
25803 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025804{
25805 STRCPY(v->di_key, name);
25806 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25807 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25808 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025809 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025810 v->di_tv.vval.v_number = nr;
25811}
25812
25813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814 * ":return [expr]"
25815 */
25816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025817ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818{
25819 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025820 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025821 int returning = FALSE;
25822
25823 if (current_funccal == NULL)
25824 {
25825 EMSG(_("E133: :return not inside a function"));
25826 return;
25827 }
25828
25829 if (eap->skip)
25830 ++emsg_skip;
25831
25832 eap->nextcmd = NULL;
25833 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025834 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025835 {
25836 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025837 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025839 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025840 }
25841 /* It's safer to return also on error. */
25842 else if (!eap->skip)
25843 {
25844 /*
25845 * Return unless the expression evaluation has been cancelled due to an
25846 * aborting error, an interrupt, or an exception.
25847 */
25848 if (!aborting())
25849 returning = do_return(eap, FALSE, TRUE, NULL);
25850 }
25851
25852 /* When skipping or the return gets pending, advance to the next command
25853 * in this line (!returning). Otherwise, ignore the rest of the line.
25854 * Following lines will be ignored by get_func_line(). */
25855 if (returning)
25856 eap->nextcmd = NULL;
25857 else if (eap->nextcmd == NULL) /* no argument */
25858 eap->nextcmd = check_nextcmd(arg);
25859
25860 if (eap->skip)
25861 --emsg_skip;
25862}
25863
25864/*
25865 * Return from a function. Possibly makes the return pending. Also called
25866 * for a pending return at the ":endtry" or after returning from an extra
25867 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025868 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025869 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025870 * FALSE when the return gets pending.
25871 */
25872 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025873do_return(
25874 exarg_T *eap,
25875 int reanimate,
25876 int is_cmd,
25877 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878{
25879 int idx;
25880 struct condstack *cstack = eap->cstack;
25881
25882 if (reanimate)
25883 /* Undo the return. */
25884 current_funccal->returned = FALSE;
25885
25886 /*
25887 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25888 * not in its finally clause (which then is to be executed next) is found.
25889 * In this case, make the ":return" pending for execution at the ":endtry".
25890 * Otherwise, return normally.
25891 */
25892 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25893 if (idx >= 0)
25894 {
25895 cstack->cs_pending[idx] = CSTP_RETURN;
25896
25897 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025898 /* A pending return again gets pending. "rettv" points to an
25899 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025901 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902 else
25903 {
25904 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025905 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025906 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025907 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025909 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910 {
25911 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025912 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025913 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025914 else
25915 EMSG(_(e_outofmem));
25916 }
25917 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025918 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919
25920 if (reanimate)
25921 {
25922 /* The pending return value could be overwritten by a ":return"
25923 * without argument in a finally clause; reset the default
25924 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025925 current_funccal->rettv->v_type = VAR_NUMBER;
25926 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025927 }
25928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025929 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025930 }
25931 else
25932 {
25933 current_funccal->returned = TRUE;
25934
25935 /* If the return is carried out now, store the return value. For
25936 * a return immediately after reanimation, the value is already
25937 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025938 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025940 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025941 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025943 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025944 }
25945 }
25946
25947 return idx < 0;
25948}
25949
25950/*
25951 * Free the variable with a pending return value.
25952 */
25953 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025954discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025955{
Bram Moolenaar33570922005-01-25 22:26:29 +000025956 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025957}
25958
25959/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025960 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025961 * is an allocated string. Used by report_pending() for verbose messages.
25962 */
25963 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025964get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025965{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025966 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025967 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025968 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025970 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025971 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025972 if (s == NULL)
25973 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025974
25975 STRCPY(IObuff, ":return ");
25976 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25977 if (STRLEN(s) + 8 >= IOSIZE)
25978 STRCPY(IObuff + IOSIZE - 4, "...");
25979 vim_free(tofree);
25980 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025981}
25982
25983/*
25984 * Get next function line.
25985 * Called by do_cmdline() to get the next line.
25986 * Returns allocated string, or NULL for end of function.
25987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025988 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025989get_func_line(
25990 int c UNUSED,
25991 void *cookie,
25992 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993{
Bram Moolenaar33570922005-01-25 22:26:29 +000025994 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025995 ufunc_T *fp = fcp->func;
25996 char_u *retval;
25997 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998
25999 /* If breakpoints have been added/deleted need to check for it. */
26000 if (fcp->dbg_tick != debug_tick)
26001 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026002 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026003 sourcing_lnum);
26004 fcp->dbg_tick = debug_tick;
26005 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026006#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026007 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026008 func_line_end(cookie);
26009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026010
Bram Moolenaar05159a02005-02-26 23:04:13 +000026011 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026012 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26013 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014 retval = NULL;
26015 else
26016 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026017 /* Skip NULL lines (continuation lines). */
26018 while (fcp->linenr < gap->ga_len
26019 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26020 ++fcp->linenr;
26021 if (fcp->linenr >= gap->ga_len)
26022 retval = NULL;
26023 else
26024 {
26025 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26026 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026027#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026028 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026029 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026030#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026032 }
26033
26034 /* Did we encounter a breakpoint? */
26035 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26036 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026037 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026038 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026039 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026040 sourcing_lnum);
26041 fcp->dbg_tick = debug_tick;
26042 }
26043
26044 return retval;
26045}
26046
Bram Moolenaar05159a02005-02-26 23:04:13 +000026047#if defined(FEAT_PROFILE) || defined(PROTO)
26048/*
26049 * Called when starting to read a function line.
26050 * "sourcing_lnum" must be correct!
26051 * When skipping lines it may not actually be executed, but we won't find out
26052 * until later and we need to store the time now.
26053 */
26054 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026055func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026056{
26057 funccall_T *fcp = (funccall_T *)cookie;
26058 ufunc_T *fp = fcp->func;
26059
26060 if (fp->uf_profiling && sourcing_lnum >= 1
26061 && sourcing_lnum <= fp->uf_lines.ga_len)
26062 {
26063 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026064 /* Skip continuation lines. */
26065 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26066 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026067 fp->uf_tml_execed = FALSE;
26068 profile_start(&fp->uf_tml_start);
26069 profile_zero(&fp->uf_tml_children);
26070 profile_get_wait(&fp->uf_tml_wait);
26071 }
26072}
26073
26074/*
26075 * Called when actually executing a function line.
26076 */
26077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026078func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026079{
26080 funccall_T *fcp = (funccall_T *)cookie;
26081 ufunc_T *fp = fcp->func;
26082
26083 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26084 fp->uf_tml_execed = TRUE;
26085}
26086
26087/*
26088 * Called when done with a function line.
26089 */
26090 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026091func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026092{
26093 funccall_T *fcp = (funccall_T *)cookie;
26094 ufunc_T *fp = fcp->func;
26095
26096 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26097 {
26098 if (fp->uf_tml_execed)
26099 {
26100 ++fp->uf_tml_count[fp->uf_tml_idx];
26101 profile_end(&fp->uf_tml_start);
26102 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026103 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026104 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26105 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026106 }
26107 fp->uf_tml_idx = -1;
26108 }
26109}
26110#endif
26111
Bram Moolenaar071d4272004-06-13 20:20:40 +000026112/*
26113 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026114 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026115 */
26116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026117func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026118{
Bram Moolenaar33570922005-01-25 22:26:29 +000026119 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026120
26121 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26122 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026123 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026124 || fcp->returned);
26125}
26126
26127/*
26128 * return TRUE if cookie indicates a function which "abort"s on errors.
26129 */
26130 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026131func_has_abort(
26132 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026133{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026134 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026135}
26136
26137#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26138typedef enum
26139{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026140 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26141 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26142 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026143} var_flavour_T;
26144
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026145static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026146
26147 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026148var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026149{
26150 char_u *p = varname;
26151
26152 if (ASCII_ISUPPER(*p))
26153 {
26154 while (*(++p))
26155 if (ASCII_ISLOWER(*p))
26156 return VAR_FLAVOUR_SESSION;
26157 return VAR_FLAVOUR_VIMINFO;
26158 }
26159 else
26160 return VAR_FLAVOUR_DEFAULT;
26161}
26162#endif
26163
26164#if defined(FEAT_VIMINFO) || defined(PROTO)
26165/*
26166 * Restore global vars that start with a capital from the viminfo file
26167 */
26168 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026169read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026170{
26171 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026172 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026173 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026174 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026175
26176 if (!writing && (find_viminfo_parameter('!') != NULL))
26177 {
26178 tab = vim_strchr(virp->vir_line + 1, '\t');
26179 if (tab != NULL)
26180 {
26181 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026182 switch (*tab)
26183 {
26184 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026185#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026186 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026187#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026188 case 'D': type = VAR_DICT; break;
26189 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026190 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192
26193 tab = vim_strchr(tab, '\t');
26194 if (tab != NULL)
26195 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026196 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026197 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026198 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026199 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026200#ifdef FEAT_FLOAT
26201 else if (type == VAR_FLOAT)
26202 (void)string2float(tab + 1, &tv.vval.v_float);
26203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026204 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026205 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026206 if (type == VAR_DICT || type == VAR_LIST)
26207 {
26208 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26209
26210 if (etv == NULL)
26211 /* Failed to parse back the dict or list, use it as a
26212 * string. */
26213 tv.v_type = VAR_STRING;
26214 else
26215 {
26216 vim_free(tv.vval.v_string);
26217 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026218 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026219 }
26220 }
26221
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026222 /* when in a function use global variables */
26223 save_funccal = current_funccal;
26224 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026225 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026226 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026227
26228 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026229 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026230 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26231 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026232 }
26233 }
26234 }
26235
26236 return viminfo_readline(virp);
26237}
26238
26239/*
26240 * Write global vars that start with a capital to the viminfo file
26241 */
26242 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026243write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026244{
Bram Moolenaar33570922005-01-25 22:26:29 +000026245 hashitem_T *hi;
26246 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026247 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026248 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026249 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026250 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026251 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026252
26253 if (find_viminfo_parameter('!') == NULL)
26254 return;
26255
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026256 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026257
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026258 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026259 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026260 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026261 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026262 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026263 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026264 this_var = HI2DI(hi);
26265 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026266 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026267 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026268 {
26269 case VAR_STRING: s = "STR"; break;
26270 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026271 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026272 case VAR_DICT: s = "DIC"; break;
26273 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026274 case VAR_SPECIAL: s = "XPL"; break;
26275
26276 case VAR_UNKNOWN:
26277 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026278 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026279 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026280 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026281 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026282 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026283 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026284 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026285 if (p != NULL)
26286 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026287 vim_free(tofree);
26288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026289 }
26290 }
26291}
26292#endif
26293
26294#if defined(FEAT_SESSION) || defined(PROTO)
26295 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026296store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026297{
Bram Moolenaar33570922005-01-25 22:26:29 +000026298 hashitem_T *hi;
26299 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026300 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026301 char_u *p, *t;
26302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026303 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026304 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026305 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026306 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026307 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026308 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026309 this_var = HI2DI(hi);
26310 if ((this_var->di_tv.v_type == VAR_NUMBER
26311 || this_var->di_tv.v_type == VAR_STRING)
26312 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026313 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026314 /* Escape special characters with a backslash. Turn a LF and
26315 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026316 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026317 (char_u *)"\\\"\n\r");
26318 if (p == NULL) /* out of memory */
26319 break;
26320 for (t = p; *t != NUL; ++t)
26321 if (*t == '\n')
26322 *t = 'n';
26323 else if (*t == '\r')
26324 *t = 'r';
26325 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026326 this_var->di_key,
26327 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26328 : ' ',
26329 p,
26330 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26331 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026332 || put_eol(fd) == FAIL)
26333 {
26334 vim_free(p);
26335 return FAIL;
26336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026337 vim_free(p);
26338 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026339#ifdef FEAT_FLOAT
26340 else if (this_var->di_tv.v_type == VAR_FLOAT
26341 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26342 {
26343 float_T f = this_var->di_tv.vval.v_float;
26344 int sign = ' ';
26345
26346 if (f < 0)
26347 {
26348 f = -f;
26349 sign = '-';
26350 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026351 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026352 this_var->di_key, sign, f) < 0)
26353 || put_eol(fd) == FAIL)
26354 return FAIL;
26355 }
26356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026357 }
26358 }
26359 return OK;
26360}
26361#endif
26362
Bram Moolenaar661b1822005-07-28 22:36:45 +000026363/*
26364 * Display script name where an item was last set.
26365 * Should only be invoked when 'verbose' is non-zero.
26366 */
26367 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026368last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026369{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026370 char_u *p;
26371
Bram Moolenaar661b1822005-07-28 22:36:45 +000026372 if (scriptID != 0)
26373 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026374 p = home_replace_save(NULL, get_scriptname(scriptID));
26375 if (p != NULL)
26376 {
26377 verbose_enter();
26378 MSG_PUTS(_("\n\tLast set from "));
26379 MSG_PUTS(p);
26380 vim_free(p);
26381 verbose_leave();
26382 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026383 }
26384}
26385
Bram Moolenaard812df62008-11-09 12:46:09 +000026386/*
26387 * List v:oldfiles in a nice way.
26388 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026390ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026391{
26392 list_T *l = vimvars[VV_OLDFILES].vv_list;
26393 listitem_T *li;
26394 int nr = 0;
26395
26396 if (l == NULL)
26397 msg((char_u *)_("No old files"));
26398 else
26399 {
26400 msg_start();
26401 msg_scroll = TRUE;
26402 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26403 {
26404 msg_outnum((long)++nr);
26405 MSG_PUTS(": ");
26406 msg_outtrans(get_tv_string(&li->li_tv));
26407 msg_putchar('\n');
26408 out_flush(); /* output one line at a time */
26409 ui_breakcheck();
26410 }
26411 /* Assume "got_int" was set to truncate the listing. */
26412 got_int = FALSE;
26413
26414#ifdef FEAT_BROWSE_CMD
26415 if (cmdmod.browse)
26416 {
26417 quit_more = FALSE;
26418 nr = prompt_for_number(FALSE);
26419 msg_starthere();
26420 if (nr > 0)
26421 {
26422 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26423 (long)nr);
26424
26425 if (p != NULL)
26426 {
26427 p = expand_env_save(p);
26428 eap->arg = p;
26429 eap->cmdidx = CMD_edit;
26430 cmdmod.browse = FALSE;
26431 do_exedit(eap, NULL);
26432 vim_free(p);
26433 }
26434 }
26435 }
26436#endif
26437 }
26438}
26439
Bram Moolenaar53744302015-07-17 17:38:22 +020026440/* reset v:option_new, v:option_old and v:option_type */
26441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026442reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026443{
26444 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26445 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26446 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26447}
26448
26449
Bram Moolenaar071d4272004-06-13 20:20:40 +000026450#endif /* FEAT_EVAL */
26451
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026453#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026454
26455#ifdef WIN3264
26456/*
26457 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26458 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026459static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26460static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26461static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026462
26463/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026464 * Get the short path (8.3) for the filename in "fnamep".
26465 * Only works for a valid file name.
26466 * When the path gets longer "fnamep" is changed and the allocated buffer
26467 * is put in "bufp".
26468 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26469 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026470 */
26471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026472get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026473{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026474 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026475 char_u *newbuf;
26476
26477 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026478 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026479 if (l > len - 1)
26480 {
26481 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026482 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026483 newbuf = vim_strnsave(*fnamep, l);
26484 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026485 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026486
26487 vim_free(*bufp);
26488 *fnamep = *bufp = newbuf;
26489
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026490 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026491 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026492 }
26493
26494 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026495 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026496}
26497
26498/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026499 * Get the short path (8.3) for the filename in "fname". The converted
26500 * path is returned in "bufp".
26501 *
26502 * Some of the directories specified in "fname" may not exist. This function
26503 * will shorten the existing directories at the beginning of the path and then
26504 * append the remaining non-existing path.
26505 *
26506 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026507 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026508 * bufp - Pointer to an allocated buffer for the filename.
26509 * fnamelen - Length of the filename pointed to by fname
26510 *
26511 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026512 */
26513 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026514shortpath_for_invalid_fname(
26515 char_u **fname,
26516 char_u **bufp,
26517 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026518{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026519 char_u *short_fname, *save_fname, *pbuf_unused;
26520 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026521 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026522 int old_len, len;
26523 int new_len, sfx_len;
26524 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026525
26526 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026527 old_len = *fnamelen;
26528 save_fname = vim_strnsave(*fname, old_len);
26529 pbuf_unused = NULL;
26530 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026531
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026532 endp = save_fname + old_len - 1; /* Find the end of the copy */
26533 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026534
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026535 /*
26536 * Try shortening the supplied path till it succeeds by removing one
26537 * directory at a time from the tail of the path.
26538 */
26539 len = 0;
26540 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026541 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026542 /* go back one path-separator */
26543 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26544 --endp;
26545 if (endp <= save_fname)
26546 break; /* processed the complete path */
26547
26548 /*
26549 * Replace the path separator with a NUL and try to shorten the
26550 * resulting path.
26551 */
26552 ch = *endp;
26553 *endp = 0;
26554 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026555 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026556 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26557 {
26558 retval = FAIL;
26559 goto theend;
26560 }
26561 *endp = ch; /* preserve the string */
26562
26563 if (len > 0)
26564 break; /* successfully shortened the path */
26565
26566 /* failed to shorten the path. Skip the path separator */
26567 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026568 }
26569
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026570 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026571 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026572 /*
26573 * Succeeded in shortening the path. Now concatenate the shortened
26574 * path with the remaining path at the tail.
26575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026576
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026577 /* Compute the length of the new path. */
26578 sfx_len = (int)(save_endp - endp) + 1;
26579 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026580
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026581 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026582 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026583 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026584 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026585 /* There is not enough space in the currently allocated string,
26586 * copy it to a buffer big enough. */
26587 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026588 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026589 {
26590 retval = FAIL;
26591 goto theend;
26592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026593 }
26594 else
26595 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026596 /* Transfer short_fname to the main buffer (it's big enough),
26597 * unless get_short_pathname() did its work in-place. */
26598 *fname = *bufp = save_fname;
26599 if (short_fname != save_fname)
26600 vim_strncpy(save_fname, short_fname, len);
26601 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026602 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026603
26604 /* concat the not-shortened part of the path */
26605 vim_strncpy(*fname + len, endp, sfx_len);
26606 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026607 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026608
26609theend:
26610 vim_free(pbuf_unused);
26611 vim_free(save_fname);
26612
26613 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026614}
26615
26616/*
26617 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026618 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026619 */
26620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026621shortpath_for_partial(
26622 char_u **fnamep,
26623 char_u **bufp,
26624 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026625{
26626 int sepcount, len, tflen;
26627 char_u *p;
26628 char_u *pbuf, *tfname;
26629 int hasTilde;
26630
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026631 /* Count up the path separators from the RHS.. so we know which part
26632 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026633 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026634 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026635 if (vim_ispathsep(*p))
26636 ++sepcount;
26637
26638 /* Need full path first (use expand_env() to remove a "~/") */
26639 hasTilde = (**fnamep == '~');
26640 if (hasTilde)
26641 pbuf = tfname = expand_env_save(*fnamep);
26642 else
26643 pbuf = tfname = FullName_save(*fnamep, FALSE);
26644
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026645 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026646
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026647 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26648 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026649
26650 if (len == 0)
26651 {
26652 /* Don't have a valid filename, so shorten the rest of the
26653 * path if we can. This CAN give us invalid 8.3 filenames, but
26654 * there's not a lot of point in guessing what it might be.
26655 */
26656 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026657 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26658 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026659 }
26660
26661 /* Count the paths backward to find the beginning of the desired string. */
26662 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026663 {
26664#ifdef FEAT_MBYTE
26665 if (has_mbyte)
26666 p -= mb_head_off(tfname, p);
26667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026668 if (vim_ispathsep(*p))
26669 {
26670 if (sepcount == 0 || (hasTilde && sepcount == 1))
26671 break;
26672 else
26673 sepcount --;
26674 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026676 if (hasTilde)
26677 {
26678 --p;
26679 if (p >= tfname)
26680 *p = '~';
26681 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026682 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026683 }
26684 else
26685 ++p;
26686
26687 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26688 vim_free(*bufp);
26689 *fnamelen = (int)STRLEN(p);
26690 *bufp = pbuf;
26691 *fnamep = p;
26692
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026693 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026694}
26695#endif /* WIN3264 */
26696
26697/*
26698 * Adjust a filename, according to a string of modifiers.
26699 * *fnamep must be NUL terminated when called. When returning, the length is
26700 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026701 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026702 * When there is an error, *fnamep is set to NULL.
26703 */
26704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026705modify_fname(
26706 char_u *src, /* string with modifiers */
26707 int *usedlen, /* characters after src that are used */
26708 char_u **fnamep, /* file name so far */
26709 char_u **bufp, /* buffer for allocated file name or NULL */
26710 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026711{
26712 int valid = 0;
26713 char_u *tail;
26714 char_u *s, *p, *pbuf;
26715 char_u dirname[MAXPATHL];
26716 int c;
26717 int has_fullname = 0;
26718#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026719 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026720 int has_shortname = 0;
26721#endif
26722
26723repeat:
26724 /* ":p" - full path/file_name */
26725 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26726 {
26727 has_fullname = 1;
26728
26729 valid |= VALID_PATH;
26730 *usedlen += 2;
26731
26732 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26733 if ((*fnamep)[0] == '~'
26734#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26735 && ((*fnamep)[1] == '/'
26736# ifdef BACKSLASH_IN_FILENAME
26737 || (*fnamep)[1] == '\\'
26738# endif
26739 || (*fnamep)[1] == NUL)
26740
26741#endif
26742 )
26743 {
26744 *fnamep = expand_env_save(*fnamep);
26745 vim_free(*bufp); /* free any allocated file name */
26746 *bufp = *fnamep;
26747 if (*fnamep == NULL)
26748 return -1;
26749 }
26750
26751 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026752 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026753 {
26754 if (vim_ispathsep(*p)
26755 && p[1] == '.'
26756 && (p[2] == NUL
26757 || vim_ispathsep(p[2])
26758 || (p[2] == '.'
26759 && (p[3] == NUL || vim_ispathsep(p[3])))))
26760 break;
26761 }
26762
26763 /* FullName_save() is slow, don't use it when not needed. */
26764 if (*p != NUL || !vim_isAbsName(*fnamep))
26765 {
26766 *fnamep = FullName_save(*fnamep, *p != NUL);
26767 vim_free(*bufp); /* free any allocated file name */
26768 *bufp = *fnamep;
26769 if (*fnamep == NULL)
26770 return -1;
26771 }
26772
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026773#ifdef WIN3264
26774# if _WIN32_WINNT >= 0x0500
26775 if (vim_strchr(*fnamep, '~') != NULL)
26776 {
26777 /* Expand 8.3 filename to full path. Needed to make sure the same
26778 * file does not have two different names.
26779 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26780 p = alloc(_MAX_PATH + 1);
26781 if (p != NULL)
26782 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026783 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026784 {
26785 vim_free(*bufp);
26786 *bufp = *fnamep = p;
26787 }
26788 else
26789 vim_free(p);
26790 }
26791 }
26792# endif
26793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026794 /* Append a path separator to a directory. */
26795 if (mch_isdir(*fnamep))
26796 {
26797 /* Make room for one or two extra characters. */
26798 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26799 vim_free(*bufp); /* free any allocated file name */
26800 *bufp = *fnamep;
26801 if (*fnamep == NULL)
26802 return -1;
26803 add_pathsep(*fnamep);
26804 }
26805 }
26806
26807 /* ":." - path relative to the current directory */
26808 /* ":~" - path relative to the home directory */
26809 /* ":8" - shortname path - postponed till after */
26810 while (src[*usedlen] == ':'
26811 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26812 {
26813 *usedlen += 2;
26814 if (c == '8')
26815 {
26816#ifdef WIN3264
26817 has_shortname = 1; /* Postpone this. */
26818#endif
26819 continue;
26820 }
26821 pbuf = NULL;
26822 /* Need full path first (use expand_env() to remove a "~/") */
26823 if (!has_fullname)
26824 {
26825 if (c == '.' && **fnamep == '~')
26826 p = pbuf = expand_env_save(*fnamep);
26827 else
26828 p = pbuf = FullName_save(*fnamep, FALSE);
26829 }
26830 else
26831 p = *fnamep;
26832
26833 has_fullname = 0;
26834
26835 if (p != NULL)
26836 {
26837 if (c == '.')
26838 {
26839 mch_dirname(dirname, MAXPATHL);
26840 s = shorten_fname(p, dirname);
26841 if (s != NULL)
26842 {
26843 *fnamep = s;
26844 if (pbuf != NULL)
26845 {
26846 vim_free(*bufp); /* free any allocated file name */
26847 *bufp = pbuf;
26848 pbuf = NULL;
26849 }
26850 }
26851 }
26852 else
26853 {
26854 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26855 /* Only replace it when it starts with '~' */
26856 if (*dirname == '~')
26857 {
26858 s = vim_strsave(dirname);
26859 if (s != NULL)
26860 {
26861 *fnamep = s;
26862 vim_free(*bufp);
26863 *bufp = s;
26864 }
26865 }
26866 }
26867 vim_free(pbuf);
26868 }
26869 }
26870
26871 tail = gettail(*fnamep);
26872 *fnamelen = (int)STRLEN(*fnamep);
26873
26874 /* ":h" - head, remove "/file_name", can be repeated */
26875 /* Don't remove the first "/" or "c:\" */
26876 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26877 {
26878 valid |= VALID_HEAD;
26879 *usedlen += 2;
26880 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026881 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026882 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026883 *fnamelen = (int)(tail - *fnamep);
26884#ifdef VMS
26885 if (*fnamelen > 0)
26886 *fnamelen += 1; /* the path separator is part of the path */
26887#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026888 if (*fnamelen == 0)
26889 {
26890 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26891 p = vim_strsave((char_u *)".");
26892 if (p == NULL)
26893 return -1;
26894 vim_free(*bufp);
26895 *bufp = *fnamep = tail = p;
26896 *fnamelen = 1;
26897 }
26898 else
26899 {
26900 while (tail > s && !after_pathsep(s, tail))
26901 mb_ptr_back(*fnamep, tail);
26902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026903 }
26904
26905 /* ":8" - shortname */
26906 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26907 {
26908 *usedlen += 2;
26909#ifdef WIN3264
26910 has_shortname = 1;
26911#endif
26912 }
26913
26914#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026915 /*
26916 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026917 */
26918 if (has_shortname)
26919 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026920 /* Copy the string if it is shortened by :h and when it wasn't copied
26921 * yet, because we are going to change it in place. Avoids changing
26922 * the buffer name for "%:8". */
26923 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026924 {
26925 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026926 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026927 return -1;
26928 vim_free(*bufp);
26929 *bufp = *fnamep = p;
26930 }
26931
26932 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026933 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026934 if (!has_fullname && !vim_isAbsName(*fnamep))
26935 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026936 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026937 return -1;
26938 }
26939 else
26940 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026941 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026942
Bram Moolenaardc935552011-08-17 15:23:23 +020026943 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026944 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026945 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026946 return -1;
26947
26948 if (l == 0)
26949 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026950 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026951 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026952 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026953 return -1;
26954 }
26955 *fnamelen = l;
26956 }
26957 }
26958#endif /* WIN3264 */
26959
26960 /* ":t" - tail, just the basename */
26961 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26962 {
26963 *usedlen += 2;
26964 *fnamelen -= (int)(tail - *fnamep);
26965 *fnamep = tail;
26966 }
26967
26968 /* ":e" - extension, can be repeated */
26969 /* ":r" - root, without extension, can be repeated */
26970 while (src[*usedlen] == ':'
26971 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26972 {
26973 /* find a '.' in the tail:
26974 * - for second :e: before the current fname
26975 * - otherwise: The last '.'
26976 */
26977 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26978 s = *fnamep - 2;
26979 else
26980 s = *fnamep + *fnamelen - 1;
26981 for ( ; s > tail; --s)
26982 if (s[0] == '.')
26983 break;
26984 if (src[*usedlen + 1] == 'e') /* :e */
26985 {
26986 if (s > tail)
26987 {
26988 *fnamelen += (int)(*fnamep - (s + 1));
26989 *fnamep = s + 1;
26990#ifdef VMS
26991 /* cut version from the extension */
26992 s = *fnamep + *fnamelen - 1;
26993 for ( ; s > *fnamep; --s)
26994 if (s[0] == ';')
26995 break;
26996 if (s > *fnamep)
26997 *fnamelen = s - *fnamep;
26998#endif
26999 }
27000 else if (*fnamep <= tail)
27001 *fnamelen = 0;
27002 }
27003 else /* :r */
27004 {
27005 if (s > tail) /* remove one extension */
27006 *fnamelen = (int)(s - *fnamep);
27007 }
27008 *usedlen += 2;
27009 }
27010
27011 /* ":s?pat?foo?" - substitute */
27012 /* ":gs?pat?foo?" - global substitute */
27013 if (src[*usedlen] == ':'
27014 && (src[*usedlen + 1] == 's'
27015 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27016 {
27017 char_u *str;
27018 char_u *pat;
27019 char_u *sub;
27020 int sep;
27021 char_u *flags;
27022 int didit = FALSE;
27023
27024 flags = (char_u *)"";
27025 s = src + *usedlen + 2;
27026 if (src[*usedlen + 1] == 'g')
27027 {
27028 flags = (char_u *)"g";
27029 ++s;
27030 }
27031
27032 sep = *s++;
27033 if (sep)
27034 {
27035 /* find end of pattern */
27036 p = vim_strchr(s, sep);
27037 if (p != NULL)
27038 {
27039 pat = vim_strnsave(s, (int)(p - s));
27040 if (pat != NULL)
27041 {
27042 s = p + 1;
27043 /* find end of substitution */
27044 p = vim_strchr(s, sep);
27045 if (p != NULL)
27046 {
27047 sub = vim_strnsave(s, (int)(p - s));
27048 str = vim_strnsave(*fnamep, *fnamelen);
27049 if (sub != NULL && str != NULL)
27050 {
27051 *usedlen = (int)(p + 1 - src);
27052 s = do_string_sub(str, pat, sub, flags);
27053 if (s != NULL)
27054 {
27055 *fnamep = s;
27056 *fnamelen = (int)STRLEN(s);
27057 vim_free(*bufp);
27058 *bufp = s;
27059 didit = TRUE;
27060 }
27061 }
27062 vim_free(sub);
27063 vim_free(str);
27064 }
27065 vim_free(pat);
27066 }
27067 }
27068 /* after using ":s", repeat all the modifiers */
27069 if (didit)
27070 goto repeat;
27071 }
27072 }
27073
Bram Moolenaar26df0922014-02-23 23:39:13 +010027074 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27075 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027076 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027077 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027078 if (c != NUL)
27079 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027080 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027081 if (c != NUL)
27082 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027083 if (p == NULL)
27084 return -1;
27085 vim_free(*bufp);
27086 *bufp = *fnamep = p;
27087 *fnamelen = (int)STRLEN(p);
27088 *usedlen += 2;
27089 }
27090
Bram Moolenaar071d4272004-06-13 20:20:40 +000027091 return valid;
27092}
27093
27094/*
27095 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27096 * "flags" can be "g" to do a global substitute.
27097 * Returns an allocated string, NULL for error.
27098 */
27099 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027100do_string_sub(
27101 char_u *str,
27102 char_u *pat,
27103 char_u *sub,
27104 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027105{
27106 int sublen;
27107 regmatch_T regmatch;
27108 int i;
27109 int do_all;
27110 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027111 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027112 garray_T ga;
27113 char_u *ret;
27114 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027115 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027116
27117 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27118 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027119 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027120
27121 ga_init2(&ga, 1, 200);
27122
27123 do_all = (flags[0] == 'g');
27124
27125 regmatch.rm_ic = p_ic;
27126 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27127 if (regmatch.regprog != NULL)
27128 {
27129 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027130 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027131 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27132 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027133 /* Skip empty match except for first match. */
27134 if (regmatch.startp[0] == regmatch.endp[0])
27135 {
27136 if (zero_width == regmatch.startp[0])
27137 {
27138 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027139 i = MB_PTR2LEN(tail);
27140 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27141 (size_t)i);
27142 ga.ga_len += i;
27143 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027144 continue;
27145 }
27146 zero_width = regmatch.startp[0];
27147 }
27148
Bram Moolenaar071d4272004-06-13 20:20:40 +000027149 /*
27150 * Get some space for a temporary buffer to do the substitution
27151 * into. It will contain:
27152 * - The text up to where the match is.
27153 * - The substituted text.
27154 * - The text after the match.
27155 */
27156 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027157 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027158 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27159 {
27160 ga_clear(&ga);
27161 break;
27162 }
27163
27164 /* copy the text up to where the match is */
27165 i = (int)(regmatch.startp[0] - tail);
27166 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27167 /* add the substituted text */
27168 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27169 + ga.ga_len + i, TRUE, TRUE, FALSE);
27170 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027171 tail = regmatch.endp[0];
27172 if (*tail == NUL)
27173 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027174 if (!do_all)
27175 break;
27176 }
27177
27178 if (ga.ga_data != NULL)
27179 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27180
Bram Moolenaar473de612013-06-08 18:19:48 +020027181 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027182 }
27183
27184 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27185 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027186 if (p_cpo == empty_option)
27187 p_cpo = save_cpo;
27188 else
27189 /* Darn, evaluating {sub} expression changed the value. */
27190 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027191
27192 return ret;
27193}
27194
27195#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */