blob: d30a766531788b53bbfb9ef6f04aa1748a757399 [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);
448static char_u *list2string(typval_T *tv, int copyID);
449static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
450static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
451static 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);
456static char_u *dict2string(typval_T *tv, int copyID);
457static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
458static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100462static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
463static 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 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200467static void dict_free_contents(dict_T *d);
468static void dict_free_dict(dict_T *d);
469
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_abs(typval_T *argvars, typval_T *rettv);
472static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100475static void f_and(typval_T *argvars, typval_T *rettv);
476static void f_append(typval_T *argvars, typval_T *rettv);
477static void f_argc(typval_T *argvars, typval_T *rettv);
478static void f_argidx(typval_T *argvars, typval_T *rettv);
479static void f_arglistid(typval_T *argvars, typval_T *rettv);
480static void f_argv(typval_T *argvars, typval_T *rettv);
481static void f_assert_equal(typval_T *argvars, typval_T *rettv);
482static void f_assert_exception(typval_T *argvars, typval_T *rettv);
483static void f_assert_fails(typval_T *argvars, typval_T *rettv);
484static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200485static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200486static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
487static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100488static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100490static void f_asin(typval_T *argvars, typval_T *rettv);
491static void f_atan(typval_T *argvars, typval_T *rettv);
492static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100494static void f_browse(typval_T *argvars, typval_T *rettv);
495static void f_browsedir(typval_T *argvars, typval_T *rettv);
496static void f_bufexists(typval_T *argvars, typval_T *rettv);
497static void f_buflisted(typval_T *argvars, typval_T *rettv);
498static void f_bufloaded(typval_T *argvars, typval_T *rettv);
499static void f_bufname(typval_T *argvars, typval_T *rettv);
500static void f_bufnr(typval_T *argvars, typval_T *rettv);
501static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
502static void f_byte2line(typval_T *argvars, typval_T *rettv);
503static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
504static void f_byteidx(typval_T *argvars, typval_T *rettv);
505static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
506static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000509#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100510#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100511static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100512static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100514static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100515static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100516static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100517static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100518static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
519static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100520static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100521static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100522static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
523static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100524static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100525static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_changenr(typval_T *argvars, typval_T *rettv);
528static void f_char2nr(typval_T *argvars, typval_T *rettv);
529static void f_cindent(typval_T *argvars, typval_T *rettv);
530static void f_clearmatches(typval_T *argvars, typval_T *rettv);
531static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000532#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_complete(typval_T *argvars, typval_T *rettv);
534static void f_complete_add(typval_T *argvars, typval_T *rettv);
535static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000536#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_confirm(typval_T *argvars, typval_T *rettv);
538static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000539#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_cos(typval_T *argvars, typval_T *rettv);
541static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100543static void f_count(typval_T *argvars, typval_T *rettv);
544static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
545static void f_cursor(typval_T *argsvars, typval_T *rettv);
546static void f_deepcopy(typval_T *argvars, typval_T *rettv);
547static void f_delete(typval_T *argvars, typval_T *rettv);
548static void f_did_filetype(typval_T *argvars, typval_T *rettv);
549static void f_diff_filler(typval_T *argvars, typval_T *rettv);
550static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
551static void f_empty(typval_T *argvars, typval_T *rettv);
552static void f_escape(typval_T *argvars, typval_T *rettv);
553static void f_eval(typval_T *argvars, typval_T *rettv);
554static void f_eventhandler(typval_T *argvars, typval_T *rettv);
555static void f_executable(typval_T *argvars, typval_T *rettv);
556static void f_exepath(typval_T *argvars, typval_T *rettv);
557static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200560#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_expand(typval_T *argvars, typval_T *rettv);
562static void f_extend(typval_T *argvars, typval_T *rettv);
563static void f_feedkeys(typval_T *argvars, typval_T *rettv);
564static void f_filereadable(typval_T *argvars, typval_T *rettv);
565static void f_filewritable(typval_T *argvars, typval_T *rettv);
566static void f_filter(typval_T *argvars, typval_T *rettv);
567static void f_finddir(typval_T *argvars, typval_T *rettv);
568static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000569#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100570static void f_float2nr(typval_T *argvars, typval_T *rettv);
571static void f_floor(typval_T *argvars, typval_T *rettv);
572static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000573#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100574static void f_fnameescape(typval_T *argvars, typval_T *rettv);
575static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
576static void f_foldclosed(typval_T *argvars, typval_T *rettv);
577static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
578static void f_foldlevel(typval_T *argvars, typval_T *rettv);
579static void f_foldtext(typval_T *argvars, typval_T *rettv);
580static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
581static void f_foreground(typval_T *argvars, typval_T *rettv);
582static void f_function(typval_T *argvars, typval_T *rettv);
583static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
584static void f_get(typval_T *argvars, typval_T *rettv);
585static void f_getbufline(typval_T *argvars, typval_T *rettv);
586static void f_getbufvar(typval_T *argvars, typval_T *rettv);
587static void f_getchar(typval_T *argvars, typval_T *rettv);
588static void f_getcharmod(typval_T *argvars, typval_T *rettv);
589static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
590static void f_getcmdline(typval_T *argvars, typval_T *rettv);
591static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
592static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
593static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
594static void f_getcwd(typval_T *argvars, typval_T *rettv);
595static void f_getfontname(typval_T *argvars, typval_T *rettv);
596static void f_getfperm(typval_T *argvars, typval_T *rettv);
597static void f_getfsize(typval_T *argvars, typval_T *rettv);
598static void f_getftime(typval_T *argvars, typval_T *rettv);
599static void f_getftype(typval_T *argvars, typval_T *rettv);
600static void f_getline(typval_T *argvars, typval_T *rettv);
601static void f_getmatches(typval_T *argvars, typval_T *rettv);
602static void f_getpid(typval_T *argvars, typval_T *rettv);
603static void f_getcurpos(typval_T *argvars, typval_T *rettv);
604static void f_getpos(typval_T *argvars, typval_T *rettv);
605static void f_getqflist(typval_T *argvars, typval_T *rettv);
606static void f_getreg(typval_T *argvars, typval_T *rettv);
607static void f_getregtype(typval_T *argvars, typval_T *rettv);
608static void f_gettabvar(typval_T *argvars, typval_T *rettv);
609static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
610static void f_getwinposx(typval_T *argvars, typval_T *rettv);
611static void f_getwinposy(typval_T *argvars, typval_T *rettv);
612static void f_getwinvar(typval_T *argvars, typval_T *rettv);
613static void f_glob(typval_T *argvars, typval_T *rettv);
614static void f_globpath(typval_T *argvars, typval_T *rettv);
615static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
616static void f_has(typval_T *argvars, typval_T *rettv);
617static void f_has_key(typval_T *argvars, typval_T *rettv);
618static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
619static void f_hasmapto(typval_T *argvars, typval_T *rettv);
620static void f_histadd(typval_T *argvars, typval_T *rettv);
621static void f_histdel(typval_T *argvars, typval_T *rettv);
622static void f_histget(typval_T *argvars, typval_T *rettv);
623static void f_histnr(typval_T *argvars, typval_T *rettv);
624static void f_hlID(typval_T *argvars, typval_T *rettv);
625static void f_hlexists(typval_T *argvars, typval_T *rettv);
626static void f_hostname(typval_T *argvars, typval_T *rettv);
627static void f_iconv(typval_T *argvars, typval_T *rettv);
628static void f_indent(typval_T *argvars, typval_T *rettv);
629static void f_index(typval_T *argvars, typval_T *rettv);
630static void f_input(typval_T *argvars, typval_T *rettv);
631static void f_inputdialog(typval_T *argvars, typval_T *rettv);
632static void f_inputlist(typval_T *argvars, typval_T *rettv);
633static void f_inputrestore(typval_T *argvars, typval_T *rettv);
634static void f_inputsave(typval_T *argvars, typval_T *rettv);
635static void f_inputsecret(typval_T *argvars, typval_T *rettv);
636static void f_insert(typval_T *argvars, typval_T *rettv);
637static void f_invert(typval_T *argvars, typval_T *rettv);
638static void f_isdirectory(typval_T *argvars, typval_T *rettv);
639static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100640#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
641static void f_isnan(typval_T *argvars, typval_T *rettv);
642#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100643static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100644#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100645static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100646static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100647static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100648static void f_job_start(typval_T *argvars, typval_T *rettv);
649static void f_job_stop(typval_T *argvars, typval_T *rettv);
650static void f_job_status(typval_T *argvars, typval_T *rettv);
651#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100652static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100653static void f_js_decode(typval_T *argvars, typval_T *rettv);
654static void f_js_encode(typval_T *argvars, typval_T *rettv);
655static void f_json_decode(typval_T *argvars, typval_T *rettv);
656static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_keys(typval_T *argvars, typval_T *rettv);
658static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
659static void f_len(typval_T *argvars, typval_T *rettv);
660static void f_libcall(typval_T *argvars, typval_T *rettv);
661static void f_libcallnr(typval_T *argvars, typval_T *rettv);
662static void f_line(typval_T *argvars, typval_T *rettv);
663static void f_line2byte(typval_T *argvars, typval_T *rettv);
664static void f_lispindent(typval_T *argvars, typval_T *rettv);
665static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000666#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100667static void f_log(typval_T *argvars, typval_T *rettv);
668static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000669#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200670#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200672#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100673static void f_map(typval_T *argvars, typval_T *rettv);
674static void f_maparg(typval_T *argvars, typval_T *rettv);
675static void f_mapcheck(typval_T *argvars, typval_T *rettv);
676static void f_match(typval_T *argvars, typval_T *rettv);
677static void f_matchadd(typval_T *argvars, typval_T *rettv);
678static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
679static void f_matcharg(typval_T *argvars, typval_T *rettv);
680static void f_matchdelete(typval_T *argvars, typval_T *rettv);
681static void f_matchend(typval_T *argvars, typval_T *rettv);
682static void f_matchlist(typval_T *argvars, typval_T *rettv);
683static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200684static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_max(typval_T *argvars, typval_T *rettv);
686static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000687#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000689#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100691#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100693#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
695static void f_nr2char(typval_T *argvars, typval_T *rettv);
696static void f_or(typval_T *argvars, typval_T *rettv);
697static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100698#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100700#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000701#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
705static void f_printf(typval_T *argvars, typval_T *rettv);
706static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200707#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200709#endif
710#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100711static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200712#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_range(typval_T *argvars, typval_T *rettv);
714static void f_readfile(typval_T *argvars, typval_T *rettv);
715static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100716#ifdef FEAT_FLOAT
717static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
718#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_reltimestr(typval_T *argvars, typval_T *rettv);
720static void f_remote_expr(typval_T *argvars, typval_T *rettv);
721static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
722static void f_remote_peek(typval_T *argvars, typval_T *rettv);
723static void f_remote_read(typval_T *argvars, typval_T *rettv);
724static void f_remote_send(typval_T *argvars, typval_T *rettv);
725static void f_remove(typval_T *argvars, typval_T *rettv);
726static void f_rename(typval_T *argvars, typval_T *rettv);
727static void f_repeat(typval_T *argvars, typval_T *rettv);
728static void f_resolve(typval_T *argvars, typval_T *rettv);
729static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100731static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000732#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100733static void f_screenattr(typval_T *argvars, typval_T *rettv);
734static void f_screenchar(typval_T *argvars, typval_T *rettv);
735static void f_screencol(typval_T *argvars, typval_T *rettv);
736static void f_screenrow(typval_T *argvars, typval_T *rettv);
737static void f_search(typval_T *argvars, typval_T *rettv);
738static void f_searchdecl(typval_T *argvars, typval_T *rettv);
739static void f_searchpair(typval_T *argvars, typval_T *rettv);
740static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
741static void f_searchpos(typval_T *argvars, typval_T *rettv);
742static void f_server2client(typval_T *argvars, typval_T *rettv);
743static void f_serverlist(typval_T *argvars, typval_T *rettv);
744static void f_setbufvar(typval_T *argvars, typval_T *rettv);
745static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
746static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100747static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_setline(typval_T *argvars, typval_T *rettv);
749static void f_setloclist(typval_T *argvars, typval_T *rettv);
750static void f_setmatches(typval_T *argvars, typval_T *rettv);
751static void f_setpos(typval_T *argvars, typval_T *rettv);
752static void f_setqflist(typval_T *argvars, typval_T *rettv);
753static void f_setreg(typval_T *argvars, typval_T *rettv);
754static void f_settabvar(typval_T *argvars, typval_T *rettv);
755static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
756static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100757#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100759#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_shellescape(typval_T *argvars, typval_T *rettv);
761static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
762static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_sin(typval_T *argvars, typval_T *rettv);
765static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_sort(typval_T *argvars, typval_T *rettv);
768static void f_soundfold(typval_T *argvars, typval_T *rettv);
769static void f_spellbadword(typval_T *argvars, typval_T *rettv);
770static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
771static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000772#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_sqrt(typval_T *argvars, typval_T *rettv);
774static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_str2nr(typval_T *argvars, typval_T *rettv);
777static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000778#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100779static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000780#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200781static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_stridx(typval_T *argvars, typval_T *rettv);
783static void f_string(typval_T *argvars, typval_T *rettv);
784static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200785static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100786static void f_strpart(typval_T *argvars, typval_T *rettv);
787static void f_strridx(typval_T *argvars, typval_T *rettv);
788static void f_strtrans(typval_T *argvars, typval_T *rettv);
789static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
790static void f_strwidth(typval_T *argvars, typval_T *rettv);
791static void f_submatch(typval_T *argvars, typval_T *rettv);
792static void f_substitute(typval_T *argvars, typval_T *rettv);
793static void f_synID(typval_T *argvars, typval_T *rettv);
794static void f_synIDattr(typval_T *argvars, typval_T *rettv);
795static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
796static void f_synstack(typval_T *argvars, typval_T *rettv);
797static void f_synconcealed(typval_T *argvars, typval_T *rettv);
798static void f_system(typval_T *argvars, typval_T *rettv);
799static void f_systemlist(typval_T *argvars, typval_T *rettv);
800static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
801static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
802static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
803static void f_taglist(typval_T *argvars, typval_T *rettv);
804static void f_tagfiles(typval_T *argvars, typval_T *rettv);
805static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200806static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
807static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200808static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
809#ifdef FEAT_JOB_CHANNEL
810static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
811#endif
812static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
813#ifdef FEAT_JOB_CHANNEL
814static void f_test_null_job(typval_T *argvars, typval_T *rettv);
815#endif
816static void f_test_null_list(typval_T *argvars, typval_T *rettv);
817static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
818static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200819#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100820static void f_tan(typval_T *argvars, typval_T *rettv);
821static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200822#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100823#ifdef FEAT_TIMERS
824static void f_timer_start(typval_T *argvars, typval_T *rettv);
825static void f_timer_stop(typval_T *argvars, typval_T *rettv);
826#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static void f_tolower(typval_T *argvars, typval_T *rettv);
828static void f_toupper(typval_T *argvars, typval_T *rettv);
829static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000830#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100831static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000832#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static void f_type(typval_T *argvars, typval_T *rettv);
834static void f_undofile(typval_T *argvars, typval_T *rettv);
835static void f_undotree(typval_T *argvars, typval_T *rettv);
836static void f_uniq(typval_T *argvars, typval_T *rettv);
837static void f_values(typval_T *argvars, typval_T *rettv);
838static void f_virtcol(typval_T *argvars, typval_T *rettv);
839static void f_visualmode(typval_T *argvars, typval_T *rettv);
840static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100841static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100842static void f_win_getid(typval_T *argvars, typval_T *rettv);
843static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
844static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
845static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static void f_winbufnr(typval_T *argvars, typval_T *rettv);
847static void f_wincol(typval_T *argvars, typval_T *rettv);
848static void f_winheight(typval_T *argvars, typval_T *rettv);
849static void f_winline(typval_T *argvars, typval_T *rettv);
850static void f_winnr(typval_T *argvars, typval_T *rettv);
851static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
852static void f_winrestview(typval_T *argvars, typval_T *rettv);
853static void f_winsaveview(typval_T *argvars, typval_T *rettv);
854static void f_winwidth(typval_T *argvars, typval_T *rettv);
855static void f_writefile(typval_T *argvars, typval_T *rettv);
856static void f_wordcount(typval_T *argvars, typval_T *rettv);
857static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
860static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
861static int get_env_len(char_u **arg);
862static int get_id_len(char_u **arg);
863static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
864static 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 +0000865#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
866#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
867 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100868static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
869static int eval_isnamec(int c);
870static int eval_isnamec1(int c);
871static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
872static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873static typval_T *alloc_string_tv(char_u *string);
874static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100875#ifdef FEAT_FLOAT
876static float_T get_tv_float(typval_T *varp);
877#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static linenr_T get_tv_lnum(typval_T *argvars);
879static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
881static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
882static hashtab_T *find_var_ht(char_u *name, char_u **varname);
883static funccall_T *get_funccal(void);
884static void vars_clear_ext(hashtab_T *ht, int free_val);
885static void delete_var(hashtab_T *ht, hashitem_T *hi);
886static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
887static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
888static void set_var(char_u *name, typval_T *varp, int copy);
889static int var_check_ro(int flags, char_u *name, int use_gettext);
890static int var_check_fixed(int flags, char_u *name, int use_gettext);
891static int var_check_func_name(char_u *name, int new_var);
892static int valid_varname(char_u *varname);
893static int tv_check_lock(int lock, char_u *name, int use_gettext);
894static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
895static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100896static 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 +0100897static int eval_fname_script(char_u *p);
898static int eval_fname_sid(char_u *p);
899static void list_func_head(ufunc_T *fp, int indent);
900static ufunc_T *find_func(char_u *name);
901static int function_exists(char_u *name);
902static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100904static void func_do_profile(ufunc_T *fp);
905static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
906static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000907static int
908# ifdef __BORLANDC__
909 _RTLENTRYF
910# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100911 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000912static int
913# ifdef __BORLANDC__
914 _RTLENTRYF
915# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100916 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100918static int script_autoload(char_u *name, int reload);
919static char_u *autoload_name(char_u *name);
920static void cat_func_name(char_u *buf, ufunc_T *fp);
921static void func_free(ufunc_T *fp);
922static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
923static int can_free_funccal(funccall_T *fc, int copyID) ;
924static void free_funccal(funccall_T *fc, int free_val);
925static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
926static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
927static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
928static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
929static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
930static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
931static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
932static int write_list(FILE *fd, list_T *list, int binary);
933static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000934
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200935
936#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100937static int compare_func_name(const void *s1, const void *s2);
938static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200939#endif
940
Bram Moolenaar33570922005-01-25 22:26:29 +0000941/*
942 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000943 */
944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100945eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 int i;
948 struct vimvar *p;
949
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200950 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
951 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200952 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000953 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000954 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000955
956 for (i = 0; i < VV_LEN; ++i)
957 {
958 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200959 if (STRLEN(p->vv_name) > 16)
960 {
961 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
962 getout(1);
963 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000964 STRCPY(p->vv_di.di_key, p->vv_name);
965 if (p->vv_flags & VV_RO)
966 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
967 else if (p->vv_flags & VV_RO_SBX)
968 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
969 else
970 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000971
972 /* add to v: scope dict, unless the value is not always available */
973 if (p->vv_type != VAR_UNKNOWN)
974 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000976 /* add to compat scope dict */
977 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000978 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100979 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
980
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000981 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100982 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200983 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100984 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100985
986 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
987 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
988 set_vim_var_nr(VV_NONE, VVAL_NONE);
989 set_vim_var_nr(VV_NULL, VVAL_NULL);
990
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200991 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200992
993#ifdef EBCDIC
994 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100995 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200996 */
997 sortFunctions();
998#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000999}
1000
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001001#if defined(EXITFREE) || defined(PROTO)
1002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001003eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001004{
1005 int i;
1006 struct vimvar *p;
1007
1008 for (i = 0; i < VV_LEN; ++i)
1009 {
1010 p = &vimvars[i];
1011 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001012 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001013 vim_free(p->vv_str);
1014 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001015 }
1016 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1017 {
1018 list_unref(p->vv_list);
1019 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001020 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001021 }
1022 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001023 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001024 hash_clear(&compat_hashtab);
1025
Bram Moolenaard9fba312005-06-26 22:34:35 +00001026 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001027# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001028 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001029# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001030
1031 /* global variables */
1032 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001033
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001034 /* autoloaded script names */
1035 ga_clear_strings(&ga_loaded);
1036
Bram Moolenaarcca74132013-09-25 21:00:28 +02001037 /* Script-local variables. First clear all the variables and in a second
1038 * loop free the scriptvar_T, because a variable in one script might hold
1039 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001040 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001041 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001042 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001043 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001044 ga_clear(&ga_scripts);
1045
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001046 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001047 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001048
1049 /* functions */
1050 free_all_functions();
1051 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001052}
1053#endif
1054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 * Return the name of the executed function.
1057 */
1058 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001059func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062}
1063
1064/*
1065 * Return the address holding the next breakpoint line for a funccall cookie.
1066 */
1067 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001068func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar33570922005-01-25 22:26:29 +00001070 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071}
1072
1073/*
1074 * Return the address holding the debug tick for a funccall cookie.
1075 */
1076 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001077func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/*
1083 * Return the nesting level for a funccall cookie.
1084 */
1085 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001086func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaar33570922005-01-25 22:26:29 +00001088 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089}
1090
1091/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001092funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001094/* pointer to list of previously used funccal, still around because some
1095 * item in it is still being used. */
1096funccall_T *previous_funccal = NULL;
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098/*
1099 * Return TRUE when a function was ended by a ":return" command.
1100 */
1101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
1104 return current_funccal->returned;
1105}
1106
1107
1108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 * Set an internal variable to a string value. Creates the variable if it does
1110 * not already exist.
1111 */
1112 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001113set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001115 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001116 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118 val = vim_strsave(value);
1119 if (val != NULL)
1120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001122 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001124 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 }
1127 }
1128}
1129
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132static char_u *redir_endp = NULL;
1133static char_u *redir_varname = NULL;
1134
1135/*
1136 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001137 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 * Returns OK if successfully completed the setup. FAIL otherwise.
1139 */
1140 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001141var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142{
1143 int save_emsg;
1144 int err;
1145 typval_T tv;
1146
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001147 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001148 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 {
1150 EMSG(_(e_invarg));
1151 return FAIL;
1152 }
1153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 redir_varname = vim_strsave(name);
1156 if (redir_varname == NULL)
1157 return FAIL;
1158
1159 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1160 if (redir_lval == NULL)
1161 {
1162 var_redir_stop();
1163 return FAIL;
1164 }
1165
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166 /* The output is stored in growarray "redir_ga" until redirection ends. */
1167 ga_init2(&redir_ga, (int)sizeof(char), 500);
1168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001170 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001171 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1173 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001174 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175 if (redir_endp != NULL && *redir_endp != NUL)
1176 /* Trailing characters are present after the variable name */
1177 EMSG(_(e_trailing));
1178 else
1179 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 var_redir_stop();
1182 return FAIL;
1183 }
1184
1185 /* check if we can write to the variable: set it to or append an empty
1186 * string */
1187 save_emsg = did_emsg;
1188 did_emsg = FALSE;
1189 tv.v_type = VAR_STRING;
1190 tv.vval.v_string = (char_u *)"";
1191 if (append)
1192 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1193 else
1194 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001195 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001197 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 if (err)
1199 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001200 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 var_redir_stop();
1202 return FAIL;
1203 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204
1205 return OK;
1206}
1207
1208/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209 * Append "value[value_len]" to the variable set by var_redir_start().
1210 * The actual appending is postponed until redirection ends, because the value
1211 * appended may in fact be the string we write to, changing it may cause freed
1212 * memory to be used:
1213 * :redir => foo
1214 * :let foo
1215 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 */
1217 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001218var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001219{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001220 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221
1222 if (redir_lval == NULL)
1223 return;
1224
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001225 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001226 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001228 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001230 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001231 {
1232 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234 }
1235 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237}
1238
1239/*
1240 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001241 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001242 */
1243 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001244var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001245{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001246 typval_T tv;
1247
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001248 if (redir_lval != NULL)
1249 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001250 /* If there was no error: assign the text to the variable. */
1251 if (redir_endp != NULL)
1252 {
1253 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1254 tv.v_type = VAR_STRING;
1255 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001256 /* Call get_lval() again, if it's inside a Dict or List it may
1257 * have changed. */
1258 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001259 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001260 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1261 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1262 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001263 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001264
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001265 /* free the collected output */
1266 vim_free(redir_ga.ga_data);
1267 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001268
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001269 vim_free(redir_lval);
1270 redir_lval = NULL;
1271 }
1272 vim_free(redir_varname);
1273 redir_varname = NULL;
1274}
1275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276# if defined(FEAT_MBYTE) || defined(PROTO)
1277 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001278eval_charconvert(
1279 char_u *enc_from,
1280 char_u *enc_to,
1281 char_u *fname_from,
1282 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 int err = FALSE;
1285
1286 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1287 set_vim_var_string(VV_CC_TO, enc_to, -1);
1288 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1289 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1290 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1291 err = TRUE;
1292 set_vim_var_string(VV_CC_FROM, NULL, -1);
1293 set_vim_var_string(VV_CC_TO, NULL, -1);
1294 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1295 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1296
1297 if (err)
1298 return FAIL;
1299 return OK;
1300}
1301# endif
1302
1303# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 int err = FALSE;
1308
1309 set_vim_var_string(VV_FNAME_IN, fname, -1);
1310 set_vim_var_string(VV_CMDARG, args, -1);
1311 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1312 err = TRUE;
1313 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1314 set_vim_var_string(VV_CMDARG, NULL, -1);
1315
1316 if (err)
1317 {
1318 mch_remove(fname);
1319 return FAIL;
1320 }
1321 return OK;
1322}
1323# endif
1324
1325# if defined(FEAT_DIFF) || defined(PROTO)
1326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327eval_diff(
1328 char_u *origfile,
1329 char_u *newfile,
1330 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 int err = FALSE;
1333
1334 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1335 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1336 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1337 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1338 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1339 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1340 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1341}
1342
1343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001344eval_patch(
1345 char_u *origfile,
1346 char_u *difffile,
1347 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int err;
1350
1351 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1352 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1353 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1354 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1355 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1356 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1357 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1358}
1359# endif
1360
1361/*
1362 * Top level evaluation function, returning a boolean.
1363 * Sets "error" to TRUE if there was an error.
1364 * Return TRUE or FALSE.
1365 */
1366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367eval_to_bool(
1368 char_u *arg,
1369 int *error,
1370 char_u **nextcmd,
1371 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar33570922005-01-25 22:26:29 +00001373 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 int retval = FALSE;
1375
1376 if (skip)
1377 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 else
1381 {
1382 *error = FALSE;
1383 if (!skip)
1384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001385 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 }
1389 if (skip)
1390 --emsg_skip;
1391
1392 return retval;
1393}
1394
1395/*
1396 * Top level evaluation function, returning a string. If "skip" is TRUE,
1397 * only parsing to "nextcmd" is done, without reporting errors. Return
1398 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1399 */
1400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001401eval_to_string_skip(
1402 char_u *arg,
1403 char_u **nextcmd,
1404 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
Bram Moolenaar33570922005-01-25 22:26:29 +00001406 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 char_u *retval;
1408
1409 if (skip)
1410 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 retval = NULL;
1413 else
1414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 retval = vim_strsave(get_tv_string(&tv));
1416 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 if (skip)
1419 --emsg_skip;
1420
1421 return retval;
1422}
1423
1424/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001425 * Skip over an expression at "*pp".
1426 * Return FAIL for an error, OK otherwise.
1427 */
1428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001429skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001430{
Bram Moolenaar33570922005-01-25 22:26:29 +00001431 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001432
1433 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001435}
1436
1437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001439 * When "convert" is TRUE convert a List into a sequence of lines and convert
1440 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 * Return pointer to allocated memory, or NULL for failure.
1442 */
1443 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001444eval_to_string(
1445 char_u *arg,
1446 char_u **nextcmd,
1447 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001451 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001452#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001453 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001456 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 retval = NULL;
1458 else
1459 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001460 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001461 {
1462 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001463 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001464 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001465 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001466 if (tv.vval.v_list->lv_len > 0)
1467 ga_append(&ga, NL);
1468 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001469 ga_append(&ga, NUL);
1470 retval = (char_u *)ga.ga_data;
1471 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001472#ifdef FEAT_FLOAT
1473 else if (convert && tv.v_type == VAR_FLOAT)
1474 {
1475 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1476 retval = vim_strsave(numbuf);
1477 }
1478#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001479 else
1480 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483
1484 return retval;
1485}
1486
1487/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001488 * Call eval_to_string() without using current local variables and using
1489 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
1491 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492eval_to_string_safe(
1493 char_u *arg,
1494 char_u **nextcmd,
1495 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 char_u *retval;
1498 void *save_funccalp;
1499
1500 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001501 if (use_sandbox)
1502 ++sandbox;
1503 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001504 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001505 if (use_sandbox)
1506 --sandbox;
1507 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 restore_funccal(save_funccalp);
1509 return retval;
1510}
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512/*
1513 * Top level evaluation function, returning a number.
1514 * Evaluates "expr" silently.
1515 * Returns -1 for an error.
1516 */
1517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001518eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar33570922005-01-25 22:26:29 +00001520 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001522 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 ++emsg_off;
1525
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 retval = -1;
1528 else
1529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001530 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533 --emsg_off;
1534
1535 return retval;
1536}
1537
Bram Moolenaara40058a2005-07-11 22:42:07 +00001538/*
1539 * Prepare v: variable "idx" to be used.
1540 * Save the current typeval in "save_tv".
1541 * When not used yet add the variable to the v: hashtable.
1542 */
1543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001545{
1546 *save_tv = vimvars[idx].vv_tv;
1547 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1548 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1549}
1550
1551/*
1552 * Restore v: variable "idx" to typeval "save_tv".
1553 * When no longer defined, remove the variable from the v: hashtable.
1554 */
1555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001556restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001557{
1558 hashitem_T *hi;
1559
Bram Moolenaara40058a2005-07-11 22:42:07 +00001560 vimvars[idx].vv_tv = *save_tv;
1561 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1562 {
1563 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1564 if (HASHITEM_EMPTY(hi))
1565 EMSG2(_(e_intern2), "restore_vimvar()");
1566 else
1567 hash_remove(&vimvarht, hi);
1568 }
1569}
1570
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001571#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001572/*
1573 * Evaluate an expression to a list with suggestions.
1574 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001575 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001576 */
1577 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001579{
1580 typval_T save_val;
1581 typval_T rettv;
1582 list_T *list = NULL;
1583 char_u *p = skipwhite(expr);
1584
1585 /* Set "v:val" to the bad word. */
1586 prepare_vimvar(VV_VAL, &save_val);
1587 vimvars[VV_VAL].vv_type = VAR_STRING;
1588 vimvars[VV_VAL].vv_str = badword;
1589 if (p_verbose == 0)
1590 ++emsg_off;
1591
1592 if (eval1(&p, &rettv, TRUE) == OK)
1593 {
1594 if (rettv.v_type != VAR_LIST)
1595 clear_tv(&rettv);
1596 else
1597 list = rettv.vval.v_list;
1598 }
1599
1600 if (p_verbose == 0)
1601 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001602 restore_vimvar(VV_VAL, &save_val);
1603
1604 return list;
1605}
1606
1607/*
1608 * "list" is supposed to contain two items: a word and a number. Return the
1609 * word in "pp" and the number as the return value.
1610 * Return -1 if anything isn't right.
1611 * Used to get the good word and score from the eval_spell_expr() result.
1612 */
1613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001614get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001615{
1616 listitem_T *li;
1617
1618 li = list->lv_first;
1619 if (li == NULL)
1620 return -1;
1621 *pp = get_tv_string(&li->li_tv);
1622
1623 li = li->li_next;
1624 if (li == NULL)
1625 return -1;
1626 return get_tv_number(&li->li_tv);
1627}
1628#endif
1629
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001630/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001631 * Top level evaluation function.
1632 * Returns an allocated typval_T with the result.
1633 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001634 */
1635 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001636eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001637{
1638 typval_T *tv;
1639
1640 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001641 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001642 {
1643 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001644 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001645 }
1646
1647 return tv;
1648}
1649
1650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001653 * Uses argv[argc] for the function arguments. Only Number and String
1654 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001657 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001658call_vim_function(
1659 char_u *func,
1660 int argc,
1661 char_u **argv,
1662 int safe, /* use the sandbox */
1663 int str_arg_only, /* all arguments are strings */
1664 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 long n;
1668 int len;
1669 int i;
1670 int doesrange;
1671 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001674 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 for (i = 0; i < argc; i++)
1679 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001680 /* Pass a NULL or empty argument as an empty string */
1681 if (argv[i] == NULL || *argv[i] == NUL)
1682 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001683 argvars[i].v_type = VAR_STRING;
1684 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001685 continue;
1686 }
1687
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001688 if (str_arg_only)
1689 len = 0;
1690 else
1691 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001692 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (len != 0 && len == (int)STRLEN(argv[i]))
1694 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001695 argvars[i].v_type = VAR_NUMBER;
1696 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
1698 else
1699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001700 argvars[i].v_type = VAR_STRING;
1701 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703 }
1704
1705 if (safe)
1706 {
1707 save_funccalp = save_funccal();
1708 ++sandbox;
1709 }
1710
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1712 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001714 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (safe)
1716 {
1717 --sandbox;
1718 restore_funccal(save_funccalp);
1719 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 vim_free(argvars);
1721
1722 if (ret == FAIL)
1723 clear_tv(rettv);
1724
1725 return ret;
1726}
1727
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001728/*
1729 * Call vimL function "func" and return the result as a number.
1730 * Returns -1 when calling the function fails.
1731 * Uses argv[argc] for the function arguments.
1732 */
1733 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001734call_func_retnr(
1735 char_u *func,
1736 int argc,
1737 char_u **argv,
1738 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001739{
1740 typval_T rettv;
1741 long retval;
1742
1743 /* All arguments are passed as strings, no conversion to number. */
1744 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1745 return -1;
1746
1747 retval = get_tv_number_chk(&rettv, NULL);
1748 clear_tv(&rettv);
1749 return retval;
1750}
1751
1752#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1753 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1754
Bram Moolenaar4f688582007-07-24 12:34:30 +00001755# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001757 * Call vimL function "func" and return the result as a string.
1758 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001759 * Uses argv[argc] for the function arguments.
1760 */
1761 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001762call_func_retstr(
1763 char_u *func,
1764 int argc,
1765 char_u **argv,
1766 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001767{
1768 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001769 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001770
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001771 /* All arguments are passed as strings, no conversion to number. */
1772 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773 return NULL;
1774
1775 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 return retval;
1778}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001779# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001780
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001781/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001782 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001784 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001785 */
1786 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001787call_func_retlist(
1788 char_u *func,
1789 int argc,
1790 char_u **argv,
1791 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001792{
1793 typval_T rettv;
1794
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001795 /* All arguments are passed as strings, no conversion to number. */
1796 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001797 return NULL;
1798
1799 if (rettv.v_type != VAR_LIST)
1800 {
1801 clear_tv(&rettv);
1802 return NULL;
1803 }
1804
1805 return rettv.vval.v_list;
1806}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
1808
1809/*
1810 * Save the current function call pointer, and set it to NULL.
1811 * Used when executing autocommands and for ":source".
1812 */
1813 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 current_funccal = NULL;
1819 return (void *)fc;
1820}
1821
1822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001825 funccall_T *fc = (funccall_T *)vfc;
1826
1827 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828}
1829
Bram Moolenaar05159a02005-02-26 23:04:13 +00001830#if defined(FEAT_PROFILE) || defined(PROTO)
1831/*
1832 * Prepare profiling for entering a child or something else that is not
1833 * counted for the script/function itself.
1834 * Should always be called in pair with prof_child_exit().
1835 */
1836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837prof_child_enter(
1838 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001839{
1840 funccall_T *fc = current_funccal;
1841
1842 if (fc != NULL && fc->func->uf_profiling)
1843 profile_start(&fc->prof_child);
1844 script_prof_save(tm);
1845}
1846
1847/*
1848 * Take care of time spent in a child.
1849 * Should always be called after prof_child_enter().
1850 */
1851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852prof_child_exit(
1853 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001854{
1855 funccall_T *fc = current_funccal;
1856
1857 if (fc != NULL && fc->func->uf_profiling)
1858 {
1859 profile_end(&fc->prof_child);
1860 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1861 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1862 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1863 }
1864 script_prof_restore(tm);
1865}
1866#endif
1867
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869#ifdef FEAT_FOLDING
1870/*
1871 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1872 * it in "*cp". Doesn't give error messages.
1873 */
1874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 int retval;
1879 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001880 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1881 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001884 if (use_sandbox)
1885 ++sandbox;
1886 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 retval = 0;
1890 else
1891 {
1892 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (tv.v_type == VAR_NUMBER)
1894 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001895 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 retval = 0;
1897 else
1898 {
1899 /* If the result is a string, check if there is a non-digit before
1900 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (!VIM_ISDIGIT(*s) && *s != '-')
1903 *cp = *s++;
1904 retval = atol((char *)s);
1905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001909 if (use_sandbox)
1910 --sandbox;
1911 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 return retval;
1914}
1915#endif
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 * ":let" list all variable values
1919 * ":let var1 var2" list variable values
1920 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 * ":let var += expr" assignment command.
1922 * ":let var -= expr" assignment command.
1923 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 */
1926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001927ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 int var_count = 0;
1934 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001936 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001937 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaardb552d602006-03-23 22:59:57 +00001939 argend = skip_var_list(arg, &var_count, &semicolon);
1940 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001942 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1943 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001944 expr = skipwhite(argend);
1945 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1946 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001948 /*
1949 * ":let" without "=": list variables
1950 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 if (*arg == '[')
1952 EMSG(_(e_invarg));
1953 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001955 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001959 list_glob_vars(&first);
1960 list_buf_vars(&first);
1961 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001962#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001963 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001964#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965 list_script_vars(&first);
1966 list_func_vars(&first);
1967 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 eap->nextcmd = check_nextcmd(arg);
1970 }
1971 else
1972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 op[0] = '=';
1974 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001975 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001977 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1978 op[0] = *expr; /* +=, -= or .= */
1979 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001981 else
1982 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (eap->skip)
1985 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (eap->skip)
1988 {
1989 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 --emsg_skip;
1992 }
1993 else if (i != FAIL)
1994 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 }
2000}
2001
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002/*
2003 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2004 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002005 * When "nextchars" is not NULL it points to a string with characters that
2006 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2007 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 * Returns OK or FAIL;
2009 */
2010 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002011ex_let_vars(
2012 char_u *arg_start,
2013 typval_T *tv,
2014 int copy, /* copy values from "tv", don't move */
2015 int semicolon, /* from skip_var_list() */
2016 int var_count, /* from skip_var_list() */
2017 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018{
2019 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002020 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002022 listitem_T *item;
2023 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024
2025 if (*arg != '[')
2026 {
2027 /*
2028 * ":let var = expr" or ":for var in list"
2029 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 return FAIL;
2032 return OK;
2033 }
2034
2035 /*
2036 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2037 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002038 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 {
2040 EMSG(_(e_listreq));
2041 return FAIL;
2042 }
2043
2044 i = list_len(l);
2045 if (semicolon == 0 && var_count < i)
2046 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002047 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 return FAIL;
2049 }
2050 if (var_count - semicolon > i)
2051 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002052 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002053 return FAIL;
2054 }
2055
2056 item = l->lv_first;
2057 while (*arg != ']')
2058 {
2059 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002060 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 item = item->li_next;
2062 if (arg == NULL)
2063 return FAIL;
2064
2065 arg = skipwhite(arg);
2066 if (*arg == ';')
2067 {
2068 /* Put the rest of the list (may be empty) in the var after ';'.
2069 * Create a new list for this. */
2070 l = list_alloc();
2071 if (l == NULL)
2072 return FAIL;
2073 while (item != NULL)
2074 {
2075 list_append_tv(l, &item->li_tv);
2076 item = item->li_next;
2077 }
2078
2079 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002080 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 ltv.vval.v_list = l;
2082 l->lv_refcount = 1;
2083
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002084 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2085 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 clear_tv(&ltv);
2087 if (arg == NULL)
2088 return FAIL;
2089 break;
2090 }
2091 else if (*arg != ',' && *arg != ']')
2092 {
2093 EMSG2(_(e_intern2), "ex_let_vars()");
2094 return FAIL;
2095 }
2096 }
2097
2098 return OK;
2099}
2100
2101/*
2102 * Skip over assignable variable "var" or list of variables "[var, var]".
2103 * Used for ":let varvar = expr" and ":for varvar in expr".
2104 * For "[var, var]" increment "*var_count" for each variable.
2105 * for "[var, var; var]" set "semicolon".
2106 * Return NULL for an error.
2107 */
2108 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002109skip_var_list(
2110 char_u *arg,
2111 int *var_count,
2112 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002113{
2114 char_u *p, *s;
2115
2116 if (*arg == '[')
2117 {
2118 /* "[var, var]": find the matching ']'. */
2119 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002120 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121 {
2122 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2123 s = skip_var_one(p);
2124 if (s == p)
2125 {
2126 EMSG2(_(e_invarg2), p);
2127 return NULL;
2128 }
2129 ++*var_count;
2130
2131 p = skipwhite(s);
2132 if (*p == ']')
2133 break;
2134 else if (*p == ';')
2135 {
2136 if (*semicolon == 1)
2137 {
2138 EMSG(_("Double ; in list of variables"));
2139 return NULL;
2140 }
2141 *semicolon = 1;
2142 }
2143 else if (*p != ',')
2144 {
2145 EMSG2(_(e_invarg2), p);
2146 return NULL;
2147 }
2148 }
2149 return p + 1;
2150 }
2151 else
2152 return skip_var_one(arg);
2153}
2154
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002155/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002156 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002157 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002159 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002160skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002162 if (*arg == '@' && arg[1] != NUL)
2163 return arg + 2;
2164 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2165 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166}
2167
Bram Moolenaara7043832005-01-21 11:56:39 +00002168/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002169 * List variables for hashtab "ht" with prefix "prefix".
2170 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173list_hashtable_vars(
2174 hashtab_T *ht,
2175 char_u *prefix,
2176 int empty,
2177 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002178{
Bram Moolenaar33570922005-01-25 22:26:29 +00002179 hashitem_T *hi;
2180 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002181 int todo;
2182
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002183 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2185 {
2186 if (!HASHITEM_EMPTY(hi))
2187 {
2188 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002189 di = HI2DI(hi);
2190 if (empty || di->di_tv.v_type != VAR_STRING
2191 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002193 }
2194 }
2195}
2196
2197/*
2198 * List global variables.
2199 */
2200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002202{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002204}
2205
2206/*
2207 * List buffer variables.
2208 */
2209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002211{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u numbuf[NUMBUFLEN];
2213
Bram Moolenaar429fa852013-04-15 12:27:36 +02002214 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216
2217 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2219 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002220}
2221
2222/*
2223 * List window variables.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002227{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002228 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002230}
2231
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002232#ifdef FEAT_WINDOWS
2233/*
2234 * List tab page variables.
2235 */
2236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002239 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241}
2242#endif
2243
Bram Moolenaara7043832005-01-21 11:56:39 +00002244/*
2245 * List Vim variables.
2246 */
2247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251}
2252
2253/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002254 * List script-local variables, if there is a script.
2255 */
2256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002257list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002258{
2259 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2261 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002262}
2263
2264/*
2265 * List function variables, if there is a function.
2266 */
2267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002268list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002269{
2270 if (current_funccal != NULL)
2271 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002272 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002273}
2274
2275/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 * List variables in "arg".
2277 */
2278 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002279list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280{
2281 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 char_u *name_start;
2285 char_u *arg_subsc;
2286 char_u *tofree;
2287 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288
2289 while (!ends_excmd(*arg) && !got_int)
2290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002293 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2295 {
2296 emsg_severe = TRUE;
2297 EMSG(_(e_trailing));
2298 break;
2299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 /* get_name_len() takes care of expanding curly braces */
2304 name_start = name = arg;
2305 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2306 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 /* This is mainly to keep test 49 working: when expanding
2309 * curly braces fails overrule the exception error message. */
2310 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 emsg_severe = TRUE;
2313 EMSG2(_(e_invarg2), arg);
2314 break;
2315 }
2316 error = TRUE;
2317 }
2318 else
2319 {
2320 if (tofree != NULL)
2321 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002322 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 else
2325 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 /* handle d.key, l[idx], f(expr) */
2327 arg_subsc = arg;
2328 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002329 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002335 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002336 case 'g': list_glob_vars(first); break;
2337 case 'b': list_buf_vars(first); break;
2338 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002339#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002340 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002341#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002342 case 'v': list_vim_vars(first); break;
2343 case 's': list_script_vars(first); break;
2344 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345 default:
2346 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002347 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 }
2349 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002350 {
2351 char_u numbuf[NUMBUFLEN];
2352 char_u *tf;
2353 int c;
2354 char_u *s;
2355
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002356 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002357 c = *arg;
2358 *arg = NUL;
2359 list_one_var_a((char_u *)"",
2360 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002361 tv.v_type,
2362 s == NULL ? (char_u *)"" : s,
2363 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002364 *arg = c;
2365 vim_free(tf);
2366 }
2367 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 }
2370 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002371
2372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002374
2375 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 }
2377
2378 return arg;
2379}
2380
2381/*
2382 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2383 * Returns a pointer to the char just after the var name.
2384 * Returns NULL if there is an error.
2385 */
2386 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002387ex_let_one(
2388 char_u *arg, /* points to variable name */
2389 typval_T *tv, /* value to assign to variable */
2390 int copy, /* copy value from "tv" */
2391 char_u *endchars, /* valid chars after variable name or NULL */
2392 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393{
2394 int c1;
2395 char_u *name;
2396 char_u *p;
2397 char_u *arg_end = NULL;
2398 int len;
2399 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401
2402 /*
2403 * ":let $VAR = expr": Set environment variable.
2404 */
2405 if (*arg == '$')
2406 {
2407 /* Find the end of the name. */
2408 ++arg;
2409 name = arg;
2410 len = get_env_len(&arg);
2411 if (len == 0)
2412 EMSG2(_(e_invarg2), name - 1);
2413 else
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 if (op != NULL && (*op == '+' || *op == '-'))
2416 EMSG2(_(e_letwrong), op);
2417 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002420 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 {
2422 c1 = name[len];
2423 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 p = get_tv_string_chk(tv);
2425 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 {
2427 int mustfree = FALSE;
2428 char_u *s = vim_getenv(name, &mustfree);
2429
2430 if (s != NULL)
2431 {
2432 p = tofree = concat_str(s, p);
2433 if (mustfree)
2434 vim_free(s);
2435 }
2436 }
2437 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 if (STRICMP(name, "HOME") == 0)
2441 init_homedir();
2442 else if (didset_vim && STRICMP(name, "VIM") == 0)
2443 didset_vim = FALSE;
2444 else if (didset_vimruntime
2445 && STRICMP(name, "VIMRUNTIME") == 0)
2446 didset_vimruntime = FALSE;
2447 arg_end = arg;
2448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 }
2452 }
2453 }
2454
2455 /*
2456 * ":let &option = expr": Set option value.
2457 * ":let &l:option = expr": Set local option value.
2458 * ":let &g:option = expr": Set global option value.
2459 */
2460 else if (*arg == '&')
2461 {
2462 /* Find the end of the name. */
2463 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 if (p == NULL || (endchars != NULL
2465 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 EMSG(_(e_letunexp));
2467 else
2468 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 long n;
2470 int opt_type;
2471 long numval;
2472 char_u *stringval = NULL;
2473 char_u *s;
2474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 c1 = *p;
2476 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477
2478 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 s = get_tv_string_chk(tv); /* != NULL if number or string */
2480 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 {
2482 opt_type = get_option_value(arg, &numval,
2483 &stringval, opt_flags);
2484 if ((opt_type == 1 && *op == '.')
2485 || (opt_type == 0 && *op != '.'))
2486 EMSG2(_(e_letwrong), op);
2487 else
2488 {
2489 if (opt_type == 1) /* number */
2490 {
2491 if (*op == '+')
2492 n = numval + n;
2493 else
2494 n = numval - n;
2495 }
2496 else if (opt_type == 0 && stringval != NULL) /* string */
2497 {
2498 s = concat_str(stringval, s);
2499 vim_free(stringval);
2500 stringval = s;
2501 }
2502 }
2503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 if (s != NULL)
2505 {
2506 set_option_value(arg, n, s, opt_flags);
2507 arg_end = p;
2508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512 }
2513
2514 /*
2515 * ":let @r = expr": Set register contents.
2516 */
2517 else if (*arg == '@')
2518 {
2519 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002520 if (op != NULL && (*op == '+' || *op == '-'))
2521 EMSG2(_(e_letwrong), op);
2522 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002523 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 EMSG(_(e_letunexp));
2525 else
2526 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002527 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002528 char_u *s;
2529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002530 p = get_tv_string_chk(tv);
2531 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002533 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 if (s != NULL)
2535 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 vim_free(s);
2538 }
2539 }
2540 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002543 arg_end = arg + 1;
2544 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002545 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 }
2547 }
2548
2549 /*
2550 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2561 EMSG(_(e_letunexp));
2562 else
2563 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 arg_end = p;
2566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 }
2570
2571 else
2572 EMSG2(_(e_invarg2), arg);
2573
2574 return arg_end;
2575}
2576
2577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2579 */
2580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002581check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582{
2583 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2584 {
2585 EMSG2(_(e_readonlyvar), arg);
2586 return TRUE;
2587 }
2588 return FALSE;
2589}
2590
2591/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 * Get an lval: variable, Dict item or List item that can be assigned a value
2593 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2594 * "name.key", "name.key[expr]" etc.
2595 * Indexing only works if "name" is an existing List or Dictionary.
2596 * "name" points to the start of the name.
2597 * If "rettv" is not NULL it points to the value to be assigned.
2598 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2599 * wrong; must end in space or cmd separator.
2600 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 * flags:
2602 * GLV_QUIET: do not give error messages
2603 * GLV_NO_AUTOLOAD: do not use script autoloading
2604 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002606 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 */
2609 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002610get_lval(
2611 char_u *name,
2612 typval_T *rettv,
2613 lval_T *lp,
2614 int unlet,
2615 int skip,
2616 int flags, /* GLV_ values */
2617 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 char_u *expr_start, *expr_end;
2621 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 dictitem_T *v;
2623 typval_T var1;
2624 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002629 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002630 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002633 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634
2635 if (skip)
2636 {
2637 /* When skipping just find the end of the name. */
2638 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002639 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 }
2641
2642 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002643 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (expr_start != NULL)
2645 {
2646 /* Don't expand the name when we already know there is an error. */
2647 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2648 && *p != '[' && *p != '.')
2649 {
2650 EMSG(_(e_trailing));
2651 return NULL;
2652 }
2653
2654 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2655 if (lp->ll_exp_name == NULL)
2656 {
2657 /* Report an invalid expression in braces, unless the
2658 * expression evaluation has been cancelled due to an
2659 * aborting error, an interrupt, or an exception. */
2660 if (!aborting() && !quiet)
2661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002662 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 EMSG2(_(e_invarg2), name);
2664 return NULL;
2665 }
2666 }
2667 lp->ll_name = lp->ll_exp_name;
2668 }
2669 else
2670 lp->ll_name = name;
2671
2672 /* Without [idx] or .key we are done. */
2673 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2674 return p;
2675
2676 cc = *p;
2677 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002678 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (v == NULL && !quiet)
2680 EMSG2(_(e_undefvar), lp->ll_name);
2681 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682 if (v == NULL)
2683 return NULL;
2684
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 /*
2686 * Loop until no more [idx] or .key is following.
2687 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002688 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2692 && !(lp->ll_tv->v_type == VAR_DICT
2693 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
2696 EMSG(_("E689: Can only index a List or Dictionary"));
2697 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_("E708: [:] must come last"));
2703 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 len = -1;
2707 if (*p == '.')
2708 {
2709 key = p + 1;
2710 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2711 ;
2712 if (len == 0)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
2716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 p = key + len;
2719 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 else
2721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p == ':')
2725 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726 else
2727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 empty1 = FALSE;
2729 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002731 if (get_tv_string_chk(&var1) == NULL)
2732 {
2733 /* not a number or string */
2734 clear_tv(&var1);
2735 return NULL;
2736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 }
2738
2739 /* Optionally get the second index [ :expr]. */
2740 if (*p == ':')
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 if (!empty1)
2747 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (rettv != NULL && (rettv->v_type != VAR_LIST
2751 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (!quiet)
2754 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 if (!empty1)
2756 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 }
2759 p = skipwhite(p + 1);
2760 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 else
2763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (!empty1)
2768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 if (get_tv_string_chk(&var2) == NULL)
2772 {
2773 /* not a number or string */
2774 if (!empty1)
2775 clear_tv(&var1);
2776 clear_tv(&var2);
2777 return NULL;
2778 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002784
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (!quiet)
2788 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (!empty1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 }
2795
2796 /* Skip to past ']'. */
2797 ++p;
2798 }
2799
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
2802 if (len == -1)
2803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002805 key = get_tv_string_chk(&var1); /* is number or string */
2806 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 lp->ll_list = NULL;
2813 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002814 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002815
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002816 /* When assigning to a scope dictionary check that a function and
2817 * variable name is valid (only variable name unless it is l: or
2818 * g: dictionary). Disallow overwriting a builtin function. */
2819 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002821 int prevval;
2822 int wrong;
2823
2824 if (len != -1)
2825 {
2826 prevval = key[len];
2827 key[len] = NUL;
2828 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002829 else
2830 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002831 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2832 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002834 || !valid_varname(key);
2835 if (len != -1)
2836 key[len] = prevval;
2837 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 return NULL;
2839 }
2840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002843 /* Can't add "v:" variable. */
2844 if (lp->ll_dict == &vimvardict)
2845 {
2846 EMSG2(_(e_illvar), name);
2847 return NULL;
2848 }
2849
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002850 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 if (len == -1)
2856 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 }
2859 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (len == -1)
2864 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 p = NULL;
2867 break;
2868 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002869 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002870 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002871 return NULL;
2872
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 if (len == -1)
2874 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 }
2877 else
2878 {
2879 /*
2880 * Get the number and item for the only or first index of the List.
2881 */
2882 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 else
2885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002886 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 clear_tv(&var1);
2888 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_list = lp->ll_tv->vval.v_list;
2891 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2892 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002894 if (lp->ll_n1 < 0)
2895 {
2896 lp->ll_n1 = 0;
2897 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2898 }
2899 }
2900 if (lp->ll_li == NULL)
2901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 }
2908
2909 /*
2910 * May need to find the item or absolute index for the second
2911 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 * When no index given: "lp->ll_empty2" is TRUE.
2913 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002914 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002917 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002923 {
2924 if (!quiet)
2925 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002927 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929 }
2930
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2932 if (lp->ll_n1 < 0)
2933 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2934 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002935 {
2936 if (!quiet)
2937 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 }
2941
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 }
2945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 return p;
2947}
2948
2949/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 */
2952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002953clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954{
2955 vim_free(lp->ll_exp_name);
2956 vim_free(lp->ll_newkey);
2957}
2958
2959/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 */
2964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002965set_var_lval(
2966 lval_T *lp,
2967 char_u *endp,
2968 typval_T *rettv,
2969 int copy,
2970 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971{
2972 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002973 listitem_T *ri;
2974 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975
2976 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 cc = *endp;
2981 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 if (op != NULL && *op != '=')
2983 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002984 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002986 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002987 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002988 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002989 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002991 if ((di == NULL
2992 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2993 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2994 FALSE)))
2995 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 set_var(lp->ll_name, &tv, FALSE);
2997 clear_tv(&tv);
2998 }
2999 }
3000 else
3001 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003005 else if (tv_check_lock(lp->ll_newkey == NULL
3006 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003007 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003008 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 else if (lp->ll_range)
3010 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003011 listitem_T *ll_li = lp->ll_li;
3012 int ll_n1 = lp->ll_n1;
3013
3014 /*
3015 * Check whether any of the list items is locked
3016 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003017 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003018 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003019 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003020 return;
3021 ri = ri->li_next;
3022 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3023 break;
3024 ll_li = ll_li->li_next;
3025 ++ll_n1;
3026 }
3027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 /*
3029 * Assign the List values to the list items.
3030 */
3031 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 if (op != NULL && *op != '=')
3034 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3035 else
3036 {
3037 clear_tv(&lp->ll_li->li_tv);
3038 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 ri = ri->li_next;
3041 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3042 break;
3043 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003046 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 ri = NULL;
3049 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003050 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 lp->ll_li = lp->ll_li->li_next;
3053 ++lp->ll_n1;
3054 }
3055 if (ri != NULL)
3056 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 else if (lp->ll_empty2
3058 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 : lp->ll_n1 != lp->ll_n2)
3060 EMSG(_("E711: List value has not enough items"));
3061 }
3062 else
3063 {
3064 /*
3065 * Assign to a List or Dictionary item.
3066 */
3067 if (lp->ll_newkey != NULL)
3068 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 if (op != NULL && *op != '=')
3070 {
3071 EMSG2(_(e_letwrong), op);
3072 return;
3073 }
3074
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003076 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 if (di == NULL)
3078 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003079 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3080 {
3081 vim_free(di);
3082 return;
3083 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003084 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 else if (op != NULL && *op != '=')
3087 {
3088 tv_op(lp->ll_tv, rettv, op);
3089 return;
3090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 /*
3095 * Assign the value to the variable or list item.
3096 */
3097 if (copy)
3098 copy_tv(rettv, lp->ll_tv);
3099 else
3100 {
3101 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003102 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003103 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003104 }
3105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106}
3107
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3110 * Returns OK or FAIL.
3111 */
3112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114{
3115 long n;
3116 char_u numbuf[NUMBUFLEN];
3117 char_u *s;
3118
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003119 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3120 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3121 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 {
3123 switch (tv1->v_type)
3124 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003125 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 case VAR_DICT:
3127 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003128 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003129 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003130 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003131 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132 break;
3133
3134 case VAR_LIST:
3135 if (*op != '+' || tv2->v_type != VAR_LIST)
3136 break;
3137 /* List += List */
3138 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3139 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3140 return OK;
3141
3142 case VAR_NUMBER:
3143 case VAR_STRING:
3144 if (tv2->v_type == VAR_LIST)
3145 break;
3146 if (*op == '+' || *op == '-')
3147 {
3148 /* nr += nr or nr -= nr*/
3149 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150#ifdef FEAT_FLOAT
3151 if (tv2->v_type == VAR_FLOAT)
3152 {
3153 float_T f = n;
3154
3155 if (*op == '+')
3156 f += tv2->vval.v_float;
3157 else
3158 f -= tv2->vval.v_float;
3159 clear_tv(tv1);
3160 tv1->v_type = VAR_FLOAT;
3161 tv1->vval.v_float = f;
3162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003163 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164#endif
3165 {
3166 if (*op == '+')
3167 n += get_tv_number(tv2);
3168 else
3169 n -= get_tv_number(tv2);
3170 clear_tv(tv1);
3171 tv1->v_type = VAR_NUMBER;
3172 tv1->vval.v_number = n;
3173 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003174 }
3175 else
3176 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177 if (tv2->v_type == VAR_FLOAT)
3178 break;
3179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 /* str .= str */
3181 s = get_tv_string(tv1);
3182 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3183 clear_tv(tv1);
3184 tv1->v_type = VAR_STRING;
3185 tv1->vval.v_string = s;
3186 }
3187 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003190#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003191 {
3192 float_T f;
3193
3194 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3195 && tv2->v_type != VAR_NUMBER
3196 && tv2->v_type != VAR_STRING))
3197 break;
3198 if (tv2->v_type == VAR_FLOAT)
3199 f = tv2->vval.v_float;
3200 else
3201 f = get_tv_number(tv2);
3202 if (*op == '+')
3203 tv1->vval.v_float += f;
3204 else
3205 tv1->vval.v_float -= f;
3206 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003208 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003209 }
3210 }
3211
3212 EMSG2(_(e_letwrong), op);
3213 return FAIL;
3214}
3215
3216/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 * Add a watcher to a list.
3218 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221{
3222 lw->lw_next = l->lv_watch;
3223 l->lv_watch = lw;
3224}
3225
3226/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003227 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 * No warning when it isn't found...
3229 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003231list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234
3235 lwp = &l->lv_watch;
3236 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3237 {
3238 if (lw == lwrem)
3239 {
3240 *lwp = lw->lw_next;
3241 break;
3242 }
3243 lwp = &lw->lw_next;
3244 }
3245}
3246
3247/*
3248 * Just before removing an item from a list: advance watchers to the next
3249 * item.
3250 */
3251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
3256 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3257 if (lw->lw_item == item)
3258 lw->lw_item = item->li_next;
3259}
3260
3261/*
3262 * Evaluate the expression used in a ":for var in expr" command.
3263 * "arg" points to "var".
3264 * Set "*errp" to TRUE for an error, FALSE otherwise;
3265 * Return a pointer that holds the info. Null when there is an error.
3266 */
3267 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268eval_for_line(
3269 char_u *arg,
3270 int *errp,
3271 char_u **nextcmdp,
3272 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273{
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 typval_T tv;
3277 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278
3279 *errp = TRUE; /* default: there is an error */
3280
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 if (fi == NULL)
3283 return NULL;
3284
3285 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3286 if (expr == NULL)
3287 return fi;
3288
3289 expr = skipwhite(expr);
3290 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3291 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003292 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 return fi;
3294 }
3295
3296 if (skip)
3297 ++emsg_skip;
3298 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3299 {
3300 *errp = FALSE;
3301 if (!skip)
3302 {
3303 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003304 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003305 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 clear_tv(&tv);
3308 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003309 else if (l == NULL)
3310 {
3311 /* a null list is like an empty list: do nothing */
3312 clear_tv(&tv);
3313 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 else
3315 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003316 /* No need to increment the refcount, it's already set for the
3317 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 fi->fi_list = l;
3319 list_add_watch(l, &fi->fi_lw);
3320 fi->fi_lw.lw_item = l->lv_first;
3321 }
3322 }
3323 }
3324 if (skip)
3325 --emsg_skip;
3326
3327 return fi;
3328}
3329
3330/*
3331 * Use the first item in a ":for" list. Advance to the next.
3332 * Assign the values to the variable (list). "arg" points to the first one.
3333 * Return TRUE when a valid item was found, FALSE when at end of list or
3334 * something wrong.
3335 */
3336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003339 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003341 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342
3343 item = fi->fi_lw.lw_item;
3344 if (item == NULL)
3345 result = FALSE;
3346 else
3347 {
3348 fi->fi_lw.lw_item = item->li_next;
3349 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3350 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3351 }
3352 return result;
3353}
3354
3355/*
3356 * Free the structure used to store info used by ":for".
3357 */
3358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003359free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360{
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003363 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003364 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003366 list_unref(fi->fi_list);
3367 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 vim_free(fi);
3369}
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3372
3373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374set_context_for_expression(
3375 expand_T *xp,
3376 char_u *arg,
3377 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 int got_eq = FALSE;
3380 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003383 if (cmdidx == CMD_let)
3384 {
3385 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003386 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003387 {
3388 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003389 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003390 {
3391 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003393 if (vim_iswhite(*p))
3394 break;
3395 }
3396 return;
3397 }
3398 }
3399 else
3400 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3401 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 while ((xp->xp_pattern = vim_strpbrk(arg,
3403 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3404 {
3405 c = *xp->xp_pattern;
3406 if (c == '&')
3407 {
3408 c = xp->xp_pattern[1];
3409 if (c == '&')
3410 {
3411 ++xp->xp_pattern;
3412 xp->xp_context = cmdidx != CMD_let || got_eq
3413 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3414 }
3415 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003418 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3419 xp->xp_pattern += 2;
3420
3421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423 else if (c == '$')
3424 {
3425 /* environment variable */
3426 xp->xp_context = EXPAND_ENV_VARS;
3427 }
3428 else if (c == '=')
3429 {
3430 got_eq = TRUE;
3431 xp->xp_context = EXPAND_EXPRESSION;
3432 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003433 else if (c == '#'
3434 && xp->xp_context == EXPAND_EXPRESSION)
3435 {
3436 /* Autoload function/variable contains '#'. */
3437 break;
3438 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003439 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 && xp->xp_context == EXPAND_FUNCTIONS
3441 && vim_strchr(xp->xp_pattern, '(') == NULL)
3442 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003443 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 break;
3445 }
3446 else if (cmdidx != CMD_let || got_eq)
3447 {
3448 if (c == '"') /* string */
3449 {
3450 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3451 if (c == '\\' && xp->xp_pattern[1] != NUL)
3452 ++xp->xp_pattern;
3453 xp->xp_context = EXPAND_NOTHING;
3454 }
3455 else if (c == '\'') /* literal string */
3456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003457 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3459 /* skip */ ;
3460 xp->xp_context = EXPAND_NOTHING;
3461 }
3462 else if (c == '|')
3463 {
3464 if (xp->xp_pattern[1] == '|')
3465 {
3466 ++xp->xp_pattern;
3467 xp->xp_context = EXPAND_EXPRESSION;
3468 }
3469 else
3470 xp->xp_context = EXPAND_COMMANDS;
3471 }
3472 else
3473 xp->xp_context = EXPAND_EXPRESSION;
3474 }
3475 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003476 /* Doesn't look like something valid, expand as an expression
3477 * anyway. */
3478 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 arg = xp->xp_pattern;
3480 if (*arg != NUL)
3481 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3482 /* skip */ ;
3483 }
3484 xp->xp_pattern = arg;
3485}
3486
3487#endif /* FEAT_CMDL_COMPL */
3488
3489/*
3490 * ":1,25call func(arg1, arg2)" function call.
3491 */
3492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003493ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 char_u *arg = eap->arg;
3496 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003498 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 linenr_T lnum;
3502 int doesrange;
3503 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003504 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003505 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003507 if (eap->skip)
3508 {
3509 /* trans_function_name() doesn't work well when skipping, use eval0()
3510 * instead to skip to any following command, e.g. for:
3511 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003512 ++emsg_skip;
3513 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3514 clear_tv(&rettv);
3515 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003516 return;
3517 }
3518
Bram Moolenaar65639032016-03-16 21:40:30 +01003519 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003520 if (fudi.fd_newkey != NULL)
3521 {
3522 /* Still need to give an error message for missing key. */
3523 EMSG2(_(e_dictkey), fudi.fd_newkey);
3524 vim_free(fudi.fd_newkey);
3525 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003526 if (tofree == NULL)
3527 return;
3528
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003529 /* Increase refcount on dictionary, it could get deleted when evaluating
3530 * the arguments. */
3531 if (fudi.fd_dict != NULL)
3532 ++fudi.fd_dict->dv_refcount;
3533
Bram Moolenaar65639032016-03-16 21:40:30 +01003534 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3535 * contents. For VAR_PARTIAL get its partial, unless we already have one
3536 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003537 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003538 name = deref_func_name(tofree, &len,
3539 partial != NULL ? NULL : &partial, FALSE);
3540
Bram Moolenaar532c7802005-01-27 14:44:31 +00003541 /* Skip white space to allow ":call func ()". Not good, but required for
3542 * backward compatibility. */
3543 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546 if (*startarg != '(')
3547 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003548 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 goto end;
3550 }
3551
3552 /*
3553 * When skipping, evaluate the function once, to find the end of the
3554 * arguments.
3555 * When the function takes a range, this is discovered after the first
3556 * call, and the loop is broken.
3557 */
3558 if (eap->skip)
3559 {
3560 ++emsg_skip;
3561 lnum = eap->line2; /* do it once, also with an invalid range */
3562 }
3563 else
3564 lnum = eap->line1;
3565 for ( ; lnum <= eap->line2; ++lnum)
3566 {
3567 if (!eap->skip && eap->addr_count > 0)
3568 {
3569 curwin->w_cursor.lnum = lnum;
3570 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003571#ifdef FEAT_VIRTUALEDIT
3572 curwin->w_cursor.coladd = 0;
3573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003576 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003577 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003578 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 failed = TRUE;
3581 break;
3582 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003583
3584 /* Handle a function returning a Funcref, Dictionary or List. */
3585 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3586 {
3587 failed = TRUE;
3588 break;
3589 }
3590
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003591 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (doesrange || eap->skip)
3593 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003597 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003598 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (aborting())
3600 break;
3601 }
3602 if (eap->skip)
3603 --emsg_skip;
3604
3605 if (!failed)
3606 {
3607 /* Check for trailing illegal characters and a following command. */
3608 if (!ends_excmd(*arg))
3609 {
3610 emsg_severe = TRUE;
3611 EMSG(_(e_trailing));
3612 }
3613 else
3614 eap->nextcmd = check_nextcmd(arg);
3615 }
3616
3617end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003618 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003619 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620}
3621
3622/*
3623 * ":unlet[!] var1 ... " command.
3624 */
3625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 ex_unletlock(eap, eap->arg, 0);
3629}
3630
3631/*
3632 * ":lockvar" and ":unlockvar" commands
3633 */
3634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 int deep = 2;
3639
3640 if (eap->forceit)
3641 deep = -1;
3642 else if (vim_isdigit(*arg))
3643 {
3644 deep = getdigits(&arg);
3645 arg = skipwhite(arg);
3646 }
3647
3648 ex_unletlock(eap, arg, deep);
3649}
3650
3651/*
3652 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3653 */
3654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655ex_unletlock(
3656 exarg_T *eap,
3657 char_u *argstart,
3658 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003659{
3660 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 do
3666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003668 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003669 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 if (lv.ll_name == NULL)
3671 error = TRUE; /* error but continue parsing */
3672 if (name_end == NULL || (!vim_iswhite(*name_end)
3673 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 if (name_end != NULL)
3676 {
3677 emsg_severe = TRUE;
3678 EMSG(_(e_trailing));
3679 }
3680 if (!(eap->skip || error))
3681 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 break;
3683 }
3684
3685 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 {
3687 if (eap->cmdidx == CMD_unlet)
3688 {
3689 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3690 error = TRUE;
3691 }
3692 else
3693 {
3694 if (do_lock_var(&lv, name_end, deep,
3695 eap->cmdidx == CMD_lockvar) == FAIL)
3696 error = TRUE;
3697 }
3698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 if (!eap->skip)
3701 clear_lval(&lv);
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 arg = skipwhite(name_end);
3704 } while (!ends_excmd(*arg));
3705
3706 eap->nextcmd = check_nextcmd(arg);
3707}
3708
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710do_unlet_var(
3711 lval_T *lp,
3712 char_u *name_end,
3713 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 int ret = OK;
3716 int cc;
3717
3718 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 cc = *name_end;
3721 *name_end = NUL;
3722
3723 /* Normal name or expanded name. */
3724 if (check_changedtick(lp->ll_name))
3725 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003728 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003730 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003731 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003733 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003734 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735 else if (lp->ll_range)
3736 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003738 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003739 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003740
3741 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3742 {
3743 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003744 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003745 return FAIL;
3746 ll_li = li;
3747 ++ll_n1;
3748 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003749
3750 /* Delete a range of List items. */
3751 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3752 {
3753 li = lp->ll_li->li_next;
3754 listitem_remove(lp->ll_list, lp->ll_li);
3755 lp->ll_li = li;
3756 ++lp->ll_n1;
3757 }
3758 }
3759 else
3760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003761 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003762 /* unlet a List item. */
3763 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003764 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003765 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003766 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003767 }
3768
3769 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003770}
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772/*
3773 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003774 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 */
3776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 hashtab_T *ht;
3780 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003781 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003782 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003783 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 ht = find_var_ht(name, &varname);
3786 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003788 if (ht == &globvarht)
3789 d = &globvardict;
3790 else if (current_funccal != NULL
3791 && ht == &current_funccal->l_vars.dv_hashtab)
3792 d = &current_funccal->l_vars;
3793 else if (ht == &compat_hashtab)
3794 d = &vimvardict;
3795 else
3796 {
3797 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3798 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3799 }
3800 if (d == NULL)
3801 {
3802 EMSG2(_(e_intern2), "do_unlet()");
3803 return FAIL;
3804 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003805 hi = hash_find(ht, varname);
3806 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003807 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003808 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003809 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003810 || var_check_ro(di->di_flags, name, FALSE)
3811 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003812 return FAIL;
3813
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003814 delete_var(ht, hi);
3815 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 if (forceit)
3819 return OK;
3820 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 return FAIL;
3822}
3823
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003824/*
3825 * Lock or unlock variable indicated by "lp".
3826 * "deep" is the levels to go (-1 for unlimited);
3827 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3828 */
3829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003830do_lock_var(
3831 lval_T *lp,
3832 char_u *name_end,
3833 int deep,
3834 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003835{
3836 int ret = OK;
3837 int cc;
3838 dictitem_T *di;
3839
3840 if (deep == 0) /* nothing to do */
3841 return OK;
3842
3843 if (lp->ll_tv == NULL)
3844 {
3845 cc = *name_end;
3846 *name_end = NUL;
3847
3848 /* Normal name or expanded name. */
3849 if (check_changedtick(lp->ll_name))
3850 ret = FAIL;
3851 else
3852 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003853 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003854 if (di == NULL)
3855 ret = FAIL;
3856 else
3857 {
3858 if (lock)
3859 di->di_flags |= DI_FLAGS_LOCK;
3860 else
3861 di->di_flags &= ~DI_FLAGS_LOCK;
3862 item_lock(&di->di_tv, deep, lock);
3863 }
3864 }
3865 *name_end = cc;
3866 }
3867 else if (lp->ll_range)
3868 {
3869 listitem_T *li = lp->ll_li;
3870
3871 /* (un)lock a range of List items. */
3872 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3873 {
3874 item_lock(&li->li_tv, deep, lock);
3875 li = li->li_next;
3876 ++lp->ll_n1;
3877 }
3878 }
3879 else if (lp->ll_list != NULL)
3880 /* (un)lock a List item. */
3881 item_lock(&lp->ll_li->li_tv, deep, lock);
3882 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003883 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003884 item_lock(&lp->ll_di->di_tv, deep, lock);
3885
3886 return ret;
3887}
3888
3889/*
3890 * Lock or unlock an item. "deep" is nr of levels to go.
3891 */
3892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003893item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003894{
3895 static int recurse = 0;
3896 list_T *l;
3897 listitem_T *li;
3898 dict_T *d;
3899 hashitem_T *hi;
3900 int todo;
3901
3902 if (recurse >= DICT_MAXNEST)
3903 {
3904 EMSG(_("E743: variable nested too deep for (un)lock"));
3905 return;
3906 }
3907 if (deep == 0)
3908 return;
3909 ++recurse;
3910
3911 /* lock/unlock the item itself */
3912 if (lock)
3913 tv->v_lock |= VAR_LOCKED;
3914 else
3915 tv->v_lock &= ~VAR_LOCKED;
3916
3917 switch (tv->v_type)
3918 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003919 case VAR_UNKNOWN:
3920 case VAR_NUMBER:
3921 case VAR_STRING:
3922 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003923 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003924 case VAR_FLOAT:
3925 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003926 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003927 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003928 break;
3929
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003930 case VAR_LIST:
3931 if ((l = tv->vval.v_list) != NULL)
3932 {
3933 if (lock)
3934 l->lv_lock |= VAR_LOCKED;
3935 else
3936 l->lv_lock &= ~VAR_LOCKED;
3937 if (deep < 0 || deep > 1)
3938 /* recursive: lock/unlock the items the List contains */
3939 for (li = l->lv_first; li != NULL; li = li->li_next)
3940 item_lock(&li->li_tv, deep - 1, lock);
3941 }
3942 break;
3943 case VAR_DICT:
3944 if ((d = tv->vval.v_dict) != NULL)
3945 {
3946 if (lock)
3947 d->dv_lock |= VAR_LOCKED;
3948 else
3949 d->dv_lock &= ~VAR_LOCKED;
3950 if (deep < 0 || deep > 1)
3951 {
3952 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003953 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003954 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3955 {
3956 if (!HASHITEM_EMPTY(hi))
3957 {
3958 --todo;
3959 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3960 }
3961 }
3962 }
3963 }
3964 }
3965 --recurse;
3966}
3967
Bram Moolenaara40058a2005-07-11 22:42:07 +00003968/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003969 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3970 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003971 */
3972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003974{
3975 return (tv->v_lock & VAR_LOCKED)
3976 || (tv->v_type == VAR_LIST
3977 && tv->vval.v_list != NULL
3978 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3979 || (tv->v_type == VAR_DICT
3980 && tv->vval.v_dict != NULL
3981 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3982}
3983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3985/*
3986 * Delete all "menutrans_" variables.
3987 */
3988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003989del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003992 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaar33570922005-01-25 22:26:29 +00003994 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003995 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003997 {
3998 if (!HASHITEM_EMPTY(hi))
3999 {
4000 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004001 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4002 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 }
4004 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004005 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006}
4007#endif
4008
4009#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4010
4011/*
4012 * Local string buffer for the next two functions to store a variable name
4013 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4014 * get_user_var_name().
4015 */
4016
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004017static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
4019static char_u *varnamebuf = NULL;
4020static int varnamebuflen = 0;
4021
4022/*
4023 * Function to concatenate a prefix and a variable name.
4024 */
4025 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004026cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028 int len;
4029
4030 len = (int)STRLEN(name) + 3;
4031 if (len > varnamebuflen)
4032 {
4033 vim_free(varnamebuf);
4034 len += 10; /* some additional space */
4035 varnamebuf = alloc(len);
4036 if (varnamebuf == NULL)
4037 {
4038 varnamebuflen = 0;
4039 return NULL;
4040 }
4041 varnamebuflen = len;
4042 }
4043 *varnamebuf = prefix;
4044 varnamebuf[1] = ':';
4045 STRCPY(varnamebuf + 2, name);
4046 return varnamebuf;
4047}
4048
4049/*
4050 * Function given to ExpandGeneric() to obtain the list of user defined
4051 * (global/buffer/window/built-in) variable names.
4052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004054get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 static long_u gdone;
4057 static long_u bdone;
4058 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059#ifdef FEAT_WINDOWS
4060 static long_u tdone;
4061#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 static int vidx;
4063 static hashitem_T *hi;
4064 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004068 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069#ifdef FEAT_WINDOWS
4070 tdone = 0;
4071#endif
4072 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004073
4074 /* Global variables */
4075 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004077 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004079 else
4080 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004081 while (HASHITEM_EMPTY(hi))
4082 ++hi;
4083 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4084 return cat_prefix_varname('g', hi->hi_key);
4085 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004087
4088 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004089 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004092 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004094 else
4095 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 while (HASHITEM_EMPTY(hi))
4097 ++hi;
4098 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004100 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004102 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return (char_u *)"b:changedtick";
4104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004105
4106 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004107 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004110 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004112 else
4113 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004114 while (HASHITEM_EMPTY(hi))
4115 ++hi;
4116 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004118
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004119#ifdef FEAT_WINDOWS
4120 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004121 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004122 if (tdone < ht->ht_used)
4123 {
4124 if (tdone++ == 0)
4125 hi = ht->ht_array;
4126 else
4127 ++hi;
4128 while (HASHITEM_EMPTY(hi))
4129 ++hi;
4130 return cat_prefix_varname('t', hi->hi_key);
4131 }
4132#endif
4133
Bram Moolenaar33570922005-01-25 22:26:29 +00004134 /* v: variables */
4135 if (vidx < VV_LEN)
4136 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 vim_free(varnamebuf);
4139 varnamebuf = NULL;
4140 varnamebuflen = 0;
4141 return NULL;
4142}
4143
4144#endif /* FEAT_CMDL_COMPL */
4145
4146/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004147 * Return TRUE if "pat" matches "text".
4148 * Does not use 'cpo' and always uses 'magic'.
4149 */
4150 static int
4151pattern_match(char_u *pat, char_u *text, int ic)
4152{
4153 int matches = FALSE;
4154 char_u *save_cpo;
4155 regmatch_T regmatch;
4156
4157 /* avoid 'l' flag in 'cpoptions' */
4158 save_cpo = p_cpo;
4159 p_cpo = (char_u *)"";
4160 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4161 if (regmatch.regprog != NULL)
4162 {
4163 regmatch.rm_ic = ic;
4164 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4165 vim_regfree(regmatch.regprog);
4166 }
4167 p_cpo = save_cpo;
4168 return matches;
4169}
4170
4171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * types for expressions.
4173 */
4174typedef enum
4175{
4176 TYPE_UNKNOWN = 0
4177 , TYPE_EQUAL /* == */
4178 , TYPE_NEQUAL /* != */
4179 , TYPE_GREATER /* > */
4180 , TYPE_GEQUAL /* >= */
4181 , TYPE_SMALLER /* < */
4182 , TYPE_SEQUAL /* <= */
4183 , TYPE_MATCH /* =~ */
4184 , TYPE_NOMATCH /* !~ */
4185} exptype_T;
4186
4187/*
4188 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4191 */
4192
4193/*
4194 * Handle zero level expression.
4195 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004197 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 * Return OK or FAIL.
4199 */
4200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201eval0(
4202 char_u *arg,
4203 typval_T *rettv,
4204 char_u **nextcmd,
4205 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
4207 int ret;
4208 char_u *p;
4209
4210 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 if (ret == FAIL || !ends_excmd(*p))
4213 {
4214 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 /*
4217 * Report the invalid expression unless the expression evaluation has
4218 * been cancelled due to an aborting error, an interrupt, or an
4219 * exception.
4220 */
4221 if (!aborting())
4222 EMSG2(_(e_invexpr2), arg);
4223 ret = FAIL;
4224 }
4225 if (nextcmd != NULL)
4226 *nextcmd = check_nextcmd(p);
4227
4228 return ret;
4229}
4230
4231/*
4232 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004233 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 *
4235 * "arg" must point to the first non-white of the expression.
4236 * "arg" is advanced to the next non-white after the recognized expression.
4237 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004238 * Note: "rettv.v_lock" is not set.
4239 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 * Return OK or FAIL.
4241 */
4242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004243eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248 /*
4249 * Get the first variable.
4250 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return FAIL;
4253
4254 if ((*arg)[0] == '?')
4255 {
4256 result = FALSE;
4257 if (evaluate)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 int error = FALSE;
4260
4261 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 return FAIL;
4274
4275 /*
4276 * Check for the ":".
4277 */
4278 if ((*arg)[0] != ':')
4279 {
4280 EMSG(_("E109: Missing ':' after '?'"));
4281 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 return FAIL;
4284 }
4285
4286 /*
4287 * Get the third variable.
4288 */
4289 *arg = skipwhite(*arg + 1);
4290 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4291 {
4292 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 return FAIL;
4295 }
4296 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299
4300 return OK;
4301}
4302
4303/*
4304 * Handle first level expression:
4305 * expr2 || expr2 || expr2 logical OR
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004313eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 long result;
4317 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 /*
4321 * Get the first variable.
4322 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 return FAIL;
4325
4326 /*
4327 * Repeat until there is no following "||".
4328 */
4329 first = TRUE;
4330 result = FALSE;
4331 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4332 {
4333 if (evaluate && first)
4334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (error)
4339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 first = FALSE;
4341 }
4342
4343 /*
4344 * Get the second variable.
4345 */
4346 *arg = skipwhite(*arg + 2);
4347 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4348 return FAIL;
4349
4350 /*
4351 * Compute the result.
4352 */
4353 if (evaluate && !result)
4354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (error)
4359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 if (evaluate)
4362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 rettv->v_type = VAR_NUMBER;
4364 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 }
4367
4368 return OK;
4369}
4370
4371/*
4372 * Handle second level expression:
4373 * expr3 && expr3 && expr3 logical AND
4374 *
4375 * "arg" must point to the first non-white of the expression.
4376 * "arg" is advanced to the next non-white after the recognized expression.
4377 *
4378 * Return OK or FAIL.
4379 */
4380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004381eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 long result;
4385 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
4388 /*
4389 * Get the first variable.
4390 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 return FAIL;
4393
4394 /*
4395 * Repeat until there is no following "&&".
4396 */
4397 first = TRUE;
4398 result = TRUE;
4399 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4400 {
4401 if (evaluate && first)
4402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 if (error)
4407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 first = FALSE;
4409 }
4410
4411 /*
4412 * Get the second variable.
4413 */
4414 *arg = skipwhite(*arg + 2);
4415 if (eval4(arg, &var2, evaluate && result) == FAIL)
4416 return FAIL;
4417
4418 /*
4419 * Compute the result.
4420 */
4421 if (evaluate && result)
4422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004423 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004425 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004426 if (error)
4427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 if (evaluate)
4430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 rettv->v_type = VAR_NUMBER;
4432 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 }
4435
4436 return OK;
4437}
4438
4439/*
4440 * Handle third level expression:
4441 * var1 == var2
4442 * var1 =~ var2
4443 * var1 != var2
4444 * var1 !~ var2
4445 * var1 > var2
4446 * var1 >= var2
4447 * var1 < var2
4448 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 * var1 is var2
4450 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 *
4452 * "arg" must point to the first non-white of the expression.
4453 * "arg" is advanced to the next non-white after the recognized expression.
4454 *
4455 * Return OK or FAIL.
4456 */
4457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004458eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 char_u *p;
4462 int i;
4463 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 int len = 2;
4466 long n1, n2;
4467 char_u *s1, *s2;
4468 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 /*
4472 * Get the first variable.
4473 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 return FAIL;
4476
4477 p = *arg;
4478 switch (p[0])
4479 {
4480 case '=': if (p[1] == '=')
4481 type = TYPE_EQUAL;
4482 else if (p[1] == '~')
4483 type = TYPE_MATCH;
4484 break;
4485 case '!': if (p[1] == '=')
4486 type = TYPE_NEQUAL;
4487 else if (p[1] == '~')
4488 type = TYPE_NOMATCH;
4489 break;
4490 case '>': if (p[1] != '=')
4491 {
4492 type = TYPE_GREATER;
4493 len = 1;
4494 }
4495 else
4496 type = TYPE_GEQUAL;
4497 break;
4498 case '<': if (p[1] != '=')
4499 {
4500 type = TYPE_SMALLER;
4501 len = 1;
4502 }
4503 else
4504 type = TYPE_SEQUAL;
4505 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 case 'i': if (p[1] == 's')
4507 {
4508 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4509 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004510 i = p[len];
4511 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 {
4513 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4514 type_is = TRUE;
4515 }
4516 }
4517 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519
4520 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004521 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
4523 if (type != TYPE_UNKNOWN)
4524 {
4525 /* extra question mark appended: ignore case */
4526 if (p[len] == '?')
4527 {
4528 ic = TRUE;
4529 ++len;
4530 }
4531 /* extra '#' appended: match case */
4532 else if (p[len] == '#')
4533 {
4534 ic = FALSE;
4535 ++len;
4536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004537 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else
4539 ic = p_ic;
4540
4541 /*
4542 * Get the second variable.
4543 */
4544 *arg = skipwhite(p + len);
4545 if (eval5(arg, &var2, evaluate) == FAIL)
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 return FAIL;
4549 }
4550
4551 if (evaluate)
4552 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 if (type_is && rettv->v_type != var2.v_type)
4554 {
4555 /* For "is" a different type always means FALSE, for "notis"
4556 * it means TRUE. */
4557 n1 = (type == TYPE_NEQUAL);
4558 }
4559 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4560 {
4561 if (type_is)
4562 {
4563 n1 = (rettv->v_type == var2.v_type
4564 && rettv->vval.v_list == var2.vval.v_list);
4565 if (type == TYPE_NEQUAL)
4566 n1 = !n1;
4567 }
4568 else if (rettv->v_type != var2.v_type
4569 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4570 {
4571 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004572 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004573 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004574 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004575 clear_tv(rettv);
4576 clear_tv(&var2);
4577 return FAIL;
4578 }
4579 else
4580 {
4581 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004582 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4583 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004584 if (type == TYPE_NEQUAL)
4585 n1 = !n1;
4586 }
4587 }
4588
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004589 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4590 {
4591 if (type_is)
4592 {
4593 n1 = (rettv->v_type == var2.v_type
4594 && rettv->vval.v_dict == var2.vval.v_dict);
4595 if (type == TYPE_NEQUAL)
4596 n1 = !n1;
4597 }
4598 else if (rettv->v_type != var2.v_type
4599 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4600 {
4601 if (rettv->v_type != var2.v_type)
4602 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4603 else
4604 EMSG(_("E736: Invalid operation for Dictionary"));
4605 clear_tv(rettv);
4606 clear_tv(&var2);
4607 return FAIL;
4608 }
4609 else
4610 {
4611 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004612 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4613 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004614 if (type == TYPE_NEQUAL)
4615 n1 = !n1;
4616 }
4617 }
4618
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004619 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4620 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004621 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004622 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004624 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004625 clear_tv(rettv);
4626 clear_tv(&var2);
4627 return FAIL;
4628 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004629 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004630 if (type == TYPE_NEQUAL)
4631 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004632 }
4633
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634#ifdef FEAT_FLOAT
4635 /*
4636 * If one of the two variables is a float, compare as a float.
4637 * When using "=~" or "!~", always compare as string.
4638 */
4639 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4640 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4641 {
4642 float_T f1, f2;
4643
4644 if (rettv->v_type == VAR_FLOAT)
4645 f1 = rettv->vval.v_float;
4646 else
4647 f1 = get_tv_number(rettv);
4648 if (var2.v_type == VAR_FLOAT)
4649 f2 = var2.vval.v_float;
4650 else
4651 f2 = get_tv_number(&var2);
4652 n1 = FALSE;
4653 switch (type)
4654 {
4655 case TYPE_EQUAL: n1 = (f1 == f2); break;
4656 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4657 case TYPE_GREATER: n1 = (f1 > f2); break;
4658 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4659 case TYPE_SMALLER: n1 = (f1 < f2); break;
4660 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4661 case TYPE_UNKNOWN:
4662 case TYPE_MATCH:
4663 case TYPE_NOMATCH: break; /* avoid gcc warning */
4664 }
4665 }
4666#endif
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 /*
4669 * If one of the two variables is a number, compare as a number.
4670 * When using "=~" or "!~", always compare as string.
4671 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004672 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 n1 = get_tv_number(rettv);
4676 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 switch (type)
4678 {
4679 case TYPE_EQUAL: n1 = (n1 == n2); break;
4680 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4681 case TYPE_GREATER: n1 = (n1 > n2); break;
4682 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4683 case TYPE_SMALLER: n1 = (n1 < n2); break;
4684 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4685 case TYPE_UNKNOWN:
4686 case TYPE_MATCH:
4687 case TYPE_NOMATCH: break; /* avoid gcc warning */
4688 }
4689 }
4690 else
4691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692 s1 = get_tv_string_buf(rettv, buf1);
4693 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4695 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4696 else
4697 i = 0;
4698 n1 = FALSE;
4699 switch (type)
4700 {
4701 case TYPE_EQUAL: n1 = (i == 0); break;
4702 case TYPE_NEQUAL: n1 = (i != 0); break;
4703 case TYPE_GREATER: n1 = (i > 0); break;
4704 case TYPE_GEQUAL: n1 = (i >= 0); break;
4705 case TYPE_SMALLER: n1 = (i < 0); break;
4706 case TYPE_SEQUAL: n1 = (i <= 0); break;
4707
4708 case TYPE_MATCH:
4709 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004710 n1 = pattern_match(s2, s1, ic);
4711 if (type == TYPE_NOMATCH)
4712 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 break;
4714
4715 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4716 }
4717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718 clear_tv(rettv);
4719 clear_tv(&var2);
4720 rettv->v_type = VAR_NUMBER;
4721 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 }
4723 }
4724
4725 return OK;
4726}
4727
4728/*
4729 * Handle fourth level expression:
4730 * + number addition
4731 * - number subtraction
4732 * . string concatenation
4733 *
4734 * "arg" must point to the first non-white of the expression.
4735 * "arg" is advanced to the next non-white after the recognized expression.
4736 *
4737 * Return OK or FAIL.
4738 */
4739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004740eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
Bram Moolenaar33570922005-01-25 22:26:29 +00004742 typval_T var2;
4743 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 int op;
4745 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746#ifdef FEAT_FLOAT
4747 float_T f1 = 0, f2 = 0;
4748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 char_u *s1, *s2;
4750 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4751 char_u *p;
4752
4753 /*
4754 * Get the first variable.
4755 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004756 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 return FAIL;
4758
4759 /*
4760 * Repeat computing, until no '+', '-' or '.' is following.
4761 */
4762 for (;;)
4763 {
4764 op = **arg;
4765 if (op != '+' && op != '-' && op != '.')
4766 break;
4767
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768 if ((op != '+' || rettv->v_type != VAR_LIST)
4769#ifdef FEAT_FLOAT
4770 && (op == '.' || rettv->v_type != VAR_FLOAT)
4771#endif
4772 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 {
4774 /* For "list + ...", an illegal use of the first operand as
4775 * a number cannot be determined before evaluating the 2nd
4776 * operand: if this is also a list, all is ok.
4777 * For "something . ...", "something - ..." or "non-list + ...",
4778 * we know that the first operand needs to be a string or number
4779 * without evaluating the 2nd operand. So check before to avoid
4780 * side effects after an error. */
4781 if (evaluate && get_tv_string_chk(rettv) == NULL)
4782 {
4783 clear_tv(rettv);
4784 return FAIL;
4785 }
4786 }
4787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 /*
4789 * Get the second variable.
4790 */
4791 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004792 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796 }
4797
4798 if (evaluate)
4799 {
4800 /*
4801 * Compute the result.
4802 */
4803 if (op == '.')
4804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4806 s2 = get_tv_string_buf_chk(&var2, buf2);
4807 if (s2 == NULL) /* type error ? */
4808 {
4809 clear_tv(rettv);
4810 clear_tv(&var2);
4811 return FAIL;
4812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004813 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(rettv);
4815 rettv->v_type = VAR_STRING;
4816 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004818 else if (op == '+' && rettv->v_type == VAR_LIST
4819 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004820 {
4821 /* concatenate Lists */
4822 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4823 &var3) == FAIL)
4824 {
4825 clear_tv(rettv);
4826 clear_tv(&var2);
4827 return FAIL;
4828 }
4829 clear_tv(rettv);
4830 *rettv = var3;
4831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 else
4833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834 int error = FALSE;
4835
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836#ifdef FEAT_FLOAT
4837 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839 f1 = rettv->vval.v_float;
4840 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842 else
4843#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845 n1 = get_tv_number_chk(rettv, &error);
4846 if (error)
4847 {
4848 /* This can only happen for "list + non-list". For
4849 * "non-list + ..." or "something - ...", we returned
4850 * before evaluating the 2nd operand. */
4851 clear_tv(rettv);
4852 return FAIL;
4853 }
4854#ifdef FEAT_FLOAT
4855 if (var2.v_type == VAR_FLOAT)
4856 f1 = n1;
4857#endif
4858 }
4859#ifdef FEAT_FLOAT
4860 if (var2.v_type == VAR_FLOAT)
4861 {
4862 f2 = var2.vval.v_float;
4863 n2 = 0;
4864 }
4865 else
4866#endif
4867 {
4868 n2 = get_tv_number_chk(&var2, &error);
4869 if (error)
4870 {
4871 clear_tv(rettv);
4872 clear_tv(&var2);
4873 return FAIL;
4874 }
4875#ifdef FEAT_FLOAT
4876 if (rettv->v_type == VAR_FLOAT)
4877 f2 = n2;
4878#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881
4882#ifdef FEAT_FLOAT
4883 /* If there is a float on either side the result is a float. */
4884 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4885 {
4886 if (op == '+')
4887 f1 = f1 + f2;
4888 else
4889 f1 = f1 - f2;
4890 rettv->v_type = VAR_FLOAT;
4891 rettv->vval.v_float = f1;
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#endif
4895 {
4896 if (op == '+')
4897 n1 = n1 + n2;
4898 else
4899 n1 = n1 - n2;
4900 rettv->v_type = VAR_NUMBER;
4901 rettv->vval.v_number = n1;
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 }
4907 return OK;
4908}
4909
4910/*
4911 * Handle fifth level expression:
4912 * * number multiplication
4913 * / number division
4914 * % number modulo
4915 *
4916 * "arg" must point to the first non-white of the expression.
4917 * "arg" is advanced to the next non-white after the recognized expression.
4918 *
4919 * Return OK or FAIL.
4920 */
4921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004922eval6(
4923 char_u **arg,
4924 typval_T *rettv,
4925 int evaluate,
4926 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
Bram Moolenaar33570922005-01-25 22:26:29 +00004928 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 int op;
4930 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931#ifdef FEAT_FLOAT
4932 int use_float = FALSE;
4933 float_T f1 = 0, f2;
4934#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004935 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
4937 /*
4938 * Get the first variable.
4939 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004940 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 return FAIL;
4942
4943 /*
4944 * Repeat computing, until no '*', '/' or '%' is following.
4945 */
4946 for (;;)
4947 {
4948 op = **arg;
4949 if (op != '*' && op != '/' && op != '%')
4950 break;
4951
4952 if (evaluate)
4953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954#ifdef FEAT_FLOAT
4955 if (rettv->v_type == VAR_FLOAT)
4956 {
4957 f1 = rettv->vval.v_float;
4958 use_float = TRUE;
4959 n1 = 0;
4960 }
4961 else
4962#endif
4963 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004965 if (error)
4966 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 else
4969 n1 = 0;
4970
4971 /*
4972 * Get the second variable.
4973 */
4974 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004975 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 return FAIL;
4977
4978 if (evaluate)
4979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980#ifdef FEAT_FLOAT
4981 if (var2.v_type == VAR_FLOAT)
4982 {
4983 if (!use_float)
4984 {
4985 f1 = n1;
4986 use_float = TRUE;
4987 }
4988 f2 = var2.vval.v_float;
4989 n2 = 0;
4990 }
4991 else
4992#endif
4993 {
4994 n2 = get_tv_number_chk(&var2, &error);
4995 clear_tv(&var2);
4996 if (error)
4997 return FAIL;
4998#ifdef FEAT_FLOAT
4999 if (use_float)
5000 f2 = n2;
5001#endif
5002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004 /*
5005 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008#ifdef FEAT_FLOAT
5009 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 if (op == '*')
5012 f1 = f1 * f2;
5013 else if (op == '/')
5014 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005015# ifdef VMS
5016 /* VMS crashes on divide by zero, work around it */
5017 if (f2 == 0.0)
5018 {
5019 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005020 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005021 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005022 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005023 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005024 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005025 }
5026 else
5027 f1 = f1 / f2;
5028# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 /* We rely on the floating point library to handle divide
5030 * by zero to result in "inf" and not a crash. */
5031 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005032# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005036 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 return FAIL;
5038 }
5039 rettv->v_type = VAR_FLOAT;
5040 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 if (op == '*')
5046 n1 = n1 * n2;
5047 else if (op == '/')
5048 {
5049 if (n2 == 0) /* give an error message? */
5050 {
5051 if (n1 == 0)
5052 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5053 else if (n1 < 0)
5054 n1 = -0x7fffffffL;
5055 else
5056 n1 = 0x7fffffffL;
5057 }
5058 else
5059 n1 = n1 / n2;
5060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005062 {
5063 if (n2 == 0) /* give an error message? */
5064 n1 = 0;
5065 else
5066 n1 = n1 % n2;
5067 }
5068 rettv->v_type = VAR_NUMBER;
5069 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 }
5073
5074 return OK;
5075}
5076
5077/*
5078 * Handle sixth level expression:
5079 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005080 * "string" string constant
5081 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 * &option-name option value
5083 * @r register contents
5084 * identifier variable value
5085 * function() function call
5086 * $VAR environment variable
5087 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005088 * [expr, expr] List
5089 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 *
5091 * Also handle:
5092 * ! in front logical NOT
5093 * - in front unary minus
5094 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 * trailing [] subscript in String or List
5096 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 *
5098 * "arg" must point to the first non-white of the expression.
5099 * "arg" is advanced to the next non-white after the recognized expression.
5100 *
5101 * Return OK or FAIL.
5102 */
5103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005104eval7(
5105 char_u **arg,
5106 typval_T *rettv,
5107 int evaluate,
5108 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 long n;
5111 int len;
5112 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 char_u *start_leader, *end_leader;
5114 int ret = OK;
5115 char_u *alias;
5116
5117 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 /*
5124 * Skip '!' and '-' characters. They are handled later.
5125 */
5126 start_leader = *arg;
5127 while (**arg == '!' || **arg == '-' || **arg == '+')
5128 *arg = skipwhite(*arg + 1);
5129 end_leader = *arg;
5130
5131 switch (**arg)
5132 {
5133 /*
5134 * Number constant.
5135 */
5136 case '0':
5137 case '1':
5138 case '2':
5139 case '3':
5140 case '4':
5141 case '5':
5142 case '6':
5143 case '7':
5144 case '8':
5145 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 {
5147#ifdef FEAT_FLOAT
5148 char_u *p = skipdigits(*arg + 1);
5149 int get_float = FALSE;
5150
5151 /* We accept a float when the format matches
5152 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005153 * strict to avoid backwards compatibility problems.
5154 * Don't look for a float after the "." operator, so that
5155 * ":let vers = 1.2.3" doesn't fail. */
5156 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005158 get_float = TRUE;
5159 p = skipdigits(p + 2);
5160 if (*p == 'e' || *p == 'E')
5161 {
5162 ++p;
5163 if (*p == '-' || *p == '+')
5164 ++p;
5165 if (!vim_isdigit(*p))
5166 get_float = FALSE;
5167 else
5168 p = skipdigits(p + 1);
5169 }
5170 if (ASCII_ISALPHA(*p) || *p == '.')
5171 get_float = FALSE;
5172 }
5173 if (get_float)
5174 {
5175 float_T f;
5176
5177 *arg += string2float(*arg, &f);
5178 if (evaluate)
5179 {
5180 rettv->v_type = VAR_FLOAT;
5181 rettv->vval.v_float = f;
5182 }
5183 }
5184 else
5185#endif
5186 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005187 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005188 *arg += len;
5189 if (evaluate)
5190 {
5191 rettv->v_type = VAR_NUMBER;
5192 rettv->vval.v_number = n;
5193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198 /*
5199 * String constant: "string".
5200 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 break;
5203
5204 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208 break;
5209
5210 /*
5211 * List: [expr, expr]
5212 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215
5216 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 * Dictionary: {key: val, key: val}
5218 */
5219 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5220 break;
5221
5222 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005223 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005225 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 break;
5227
5228 /*
5229 * Environment variable: $VAR.
5230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005231 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 break;
5233
5234 /*
5235 * Register contents: @r.
5236 */
5237 case '@': ++*arg;
5238 if (evaluate)
5239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005240 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005241 rettv->vval.v_string = get_reg_contents(**arg,
5242 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244 if (**arg != NUL)
5245 ++*arg;
5246 break;
5247
5248 /*
5249 * nested expression: (expression).
5250 */
5251 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (**arg == ')')
5254 ++*arg;
5255 else if (ret == OK)
5256 {
5257 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005258 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 ret = FAIL;
5260 }
5261 break;
5262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 break;
5265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266
5267 if (ret == NOTDONE)
5268 {
5269 /*
5270 * Must be a variable or function name.
5271 * Can also be a curly-braces kind of name: {expr}.
5272 */
5273 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 if (alias != NULL)
5276 s = alias;
5277
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 ret = FAIL;
5280 else
5281 {
5282 if (**arg == '(') /* recursive! */
5283 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005284 partial_T *partial;
5285
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 /* If "s" is the name of a variable of type VAR_FUNC
5287 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005288 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289
5290 /* Invoke the function. */
5291 ret = get_func_tv(s, len, rettv, arg,
5292 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005293 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005294
5295 /* If evaluate is FALSE rettv->v_type was not set in
5296 * get_func_tv, but it's needed in handle_subscript() to parse
5297 * what follows. So set it here. */
5298 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5299 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005300 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005301 rettv->v_type = VAR_FUNC;
5302 }
5303
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 /* Stop the expression evaluation when immediately
5305 * aborting on error, or when an interrupt occurred or
5306 * an exception was thrown but not caught. */
5307 if (aborting())
5308 {
5309 if (ret == OK)
5310 clear_tv(rettv);
5311 ret = FAIL;
5312 }
5313 }
5314 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005315 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005316 else
5317 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005319 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 }
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 *arg = skipwhite(*arg);
5323
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005324 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5325 * expr(expr). */
5326 if (ret == OK)
5327 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 /*
5330 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5331 */
5332 if (ret == OK && evaluate && end_leader > start_leader)
5333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005335 int val = 0;
5336#ifdef FEAT_FLOAT
5337 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005339 if (rettv->v_type == VAR_FLOAT)
5340 f = rettv->vval.v_float;
5341 else
5342#endif
5343 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 clear_tv(rettv);
5347 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 else
5350 {
5351 while (end_leader > start_leader)
5352 {
5353 --end_leader;
5354 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005355 {
5356#ifdef FEAT_FLOAT
5357 if (rettv->v_type == VAR_FLOAT)
5358 f = !f;
5359 else
5360#endif
5361 val = !val;
5362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005364 {
5365#ifdef FEAT_FLOAT
5366 if (rettv->v_type == VAR_FLOAT)
5367 f = -f;
5368 else
5369#endif
5370 val = -val;
5371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005372 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005373#ifdef FEAT_FLOAT
5374 if (rettv->v_type == VAR_FLOAT)
5375 {
5376 clear_tv(rettv);
5377 rettv->vval.v_float = f;
5378 }
5379 else
5380#endif
5381 {
5382 clear_tv(rettv);
5383 rettv->v_type = VAR_NUMBER;
5384 rettv->vval.v_number = val;
5385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388
5389 return ret;
5390}
5391
5392/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005393 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5394 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5396 */
5397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005398eval_index(
5399 char_u **arg,
5400 typval_T *rettv,
5401 int evaluate,
5402 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403{
5404 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 long len = -1;
5408 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005410 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411
Bram Moolenaara03f2332016-02-06 18:09:59 +01005412 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005414 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005415 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005416 if (verbose)
5417 EMSG(_("E695: Cannot index a Funcref"));
5418 return FAIL;
5419 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005420#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005421 if (verbose)
5422 EMSG(_(e_float_as_string));
5423 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005424#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005425 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005426 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005427 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005428 if (verbose)
5429 EMSG(_("E909: Cannot index a special variable"));
5430 return FAIL;
5431 case VAR_UNKNOWN:
5432 if (evaluate)
5433 return FAIL;
5434 /* FALLTHROUGH */
5435
5436 case VAR_STRING:
5437 case VAR_NUMBER:
5438 case VAR_LIST:
5439 case VAR_DICT:
5440 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005443 init_tv(&var1);
5444 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 /*
5448 * dict.name
5449 */
5450 key = *arg + 1;
5451 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5452 ;
5453 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 /*
5460 * something[idx]
5461 *
5462 * Get the (first) variable from inside the [].
5463 */
5464 *arg = skipwhite(*arg + 1);
5465 if (**arg == ':')
5466 empty1 = TRUE;
5467 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5468 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005469 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5470 {
5471 /* not a number or string */
5472 clear_tv(&var1);
5473 return FAIL;
5474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475
5476 /*
5477 * Get the second variable from inside the [:].
5478 */
5479 if (**arg == ':')
5480 {
5481 range = TRUE;
5482 *arg = skipwhite(*arg + 1);
5483 if (**arg == ']')
5484 empty2 = TRUE;
5485 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005487 if (!empty1)
5488 clear_tv(&var1);
5489 return FAIL;
5490 }
5491 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5492 {
5493 /* not a number or string */
5494 if (!empty1)
5495 clear_tv(&var1);
5496 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 return FAIL;
5498 }
5499 }
5500
5501 /* Check for the ']'. */
5502 if (**arg != ']')
5503 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005504 if (verbose)
5505 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 clear_tv(&var1);
5507 if (range)
5508 clear_tv(&var2);
5509 return FAIL;
5510 }
5511 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 }
5513
5514 if (evaluate)
5515 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 n1 = 0;
5517 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 n1 = get_tv_number(&var1);
5520 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 }
5522 if (range)
5523 {
5524 if (empty2)
5525 n2 = -1;
5526 else
5527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 n2 = get_tv_number(&var2);
5529 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 }
5532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005535 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005536 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005537 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005538 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005539 case VAR_SPECIAL:
5540 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005541 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005542 break; /* not evaluating, skipping over subscript */
5543
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 case VAR_NUMBER:
5545 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 len = (long)STRLEN(s);
5548 if (range)
5549 {
5550 /* The resulting variable is a substring. If the indexes
5551 * are out of range the result is empty. */
5552 if (n1 < 0)
5553 {
5554 n1 = len + n1;
5555 if (n1 < 0)
5556 n1 = 0;
5557 }
5558 if (n2 < 0)
5559 n2 = len + n2;
5560 else if (n2 >= len)
5561 n2 = len;
5562 if (n1 >= len || n2 < 0 || n1 > n2)
5563 s = NULL;
5564 else
5565 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5566 }
5567 else
5568 {
5569 /* The resulting variable is a string of a single
5570 * character. If the index is too big or negative the
5571 * result is empty. */
5572 if (n1 >= len || n1 < 0)
5573 s = NULL;
5574 else
5575 s = vim_strnsave(s + n1, 1);
5576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 clear_tv(rettv);
5578 rettv->v_type = VAR_STRING;
5579 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 break;
5581
5582 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 if (n1 < 0)
5585 n1 = len + n1;
5586 if (!empty1 && (n1 < 0 || n1 >= len))
5587 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005588 /* For a range we allow invalid values and return an empty
5589 * list. A list index out of range is an error. */
5590 if (!range)
5591 {
5592 if (verbose)
5593 EMSGN(_(e_listidx), n1);
5594 return FAIL;
5595 }
5596 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 }
5598 if (range)
5599 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 list_T *l;
5601 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602
5603 if (n2 < 0)
5604 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005605 else if (n2 >= len)
5606 n2 = len - 1;
5607 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005608 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 l = list_alloc();
5610 if (l == NULL)
5611 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 n1 <= n2; ++n1)
5614 {
5615 if (list_append_tv(l, &item->li_tv) == FAIL)
5616 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005617 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 return FAIL;
5619 }
5620 item = item->li_next;
5621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622 clear_tv(rettv);
5623 rettv->v_type = VAR_LIST;
5624 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005625 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 }
5627 else
5628 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005629 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 clear_tv(rettv);
5631 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 }
5633 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634
5635 case VAR_DICT:
5636 if (range)
5637 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005638 if (verbose)
5639 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 if (len == -1)
5641 clear_tv(&var1);
5642 return FAIL;
5643 }
5644 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005645 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646
5647 if (len == -1)
5648 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005649 key = get_tv_string_chk(&var1);
5650 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 clear_tv(&var1);
5653 return FAIL;
5654 }
5655 }
5656
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005657 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005659 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005660 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005661 if (len == -1)
5662 clear_tv(&var1);
5663 if (item == NULL)
5664 return FAIL;
5665
5666 copy_tv(&item->di_tv, &var1);
5667 clear_tv(rettv);
5668 *rettv = var1;
5669 }
5670 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 }
5672 }
5673
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 return OK;
5675}
5676
5677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 * Get an option value.
5679 * "arg" points to the '&' or '+' before the option name.
5680 * "arg" is advanced to character after the option name.
5681 * Return OK or FAIL.
5682 */
5683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005684get_option_tv(
5685 char_u **arg,
5686 typval_T *rettv, /* when NULL, only check if option exists */
5687 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 char_u *option_end;
5690 long numval;
5691 char_u *stringval;
5692 int opt_type;
5693 int c;
5694 int working = (**arg == '+'); /* has("+option") */
5695 int ret = OK;
5696 int opt_flags;
5697
5698 /*
5699 * Isolate the option name and find its value.
5700 */
5701 option_end = find_option_end(arg, &opt_flags);
5702 if (option_end == NULL)
5703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 EMSG2(_("E112: Option name missing: %s"), *arg);
5706 return FAIL;
5707 }
5708
5709 if (!evaluate)
5710 {
5711 *arg = option_end;
5712 return OK;
5713 }
5714
5715 c = *option_end;
5716 *option_end = NUL;
5717 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005718 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 if (opt_type == -3) /* invalid name */
5721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005722 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 EMSG2(_("E113: Unknown option: %s"), *arg);
5724 ret = FAIL;
5725 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 if (opt_type == -2) /* hidden string option */
5729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730 rettv->v_type = VAR_STRING;
5731 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 else if (opt_type == -1) /* hidden number option */
5734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005735 rettv->v_type = VAR_NUMBER;
5736 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738 else if (opt_type == 1) /* number option */
5739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005740 rettv->v_type = VAR_NUMBER;
5741 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743 else /* string option */
5744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005745 rettv->v_type = VAR_STRING;
5746 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 }
5749 else if (working && (opt_type == -2 || opt_type == -1))
5750 ret = FAIL;
5751
5752 *option_end = c; /* put back for error messages */
5753 *arg = option_end;
5754
5755 return ret;
5756}
5757
5758/*
5759 * Allocate a variable for a string constant.
5760 * Return OK or FAIL.
5761 */
5762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 char_u *p;
5766 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 int extra = 0;
5768
5769 /*
5770 * Find the end of the string, skipping backslashed characters.
5771 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
5774 if (*p == '\\' && p[1] != NUL)
5775 {
5776 ++p;
5777 /* A "\<x>" form occupies at least 4 characters, and produces up
5778 * to 6 characters: reserve space for 2 extra */
5779 if (*p == '<')
5780 extra += 2;
5781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783
5784 if (*p != '"')
5785 {
5786 EMSG2(_("E114: Missing quote: %s"), *arg);
5787 return FAIL;
5788 }
5789
5790 /* If only parsing, set *arg and return here */
5791 if (!evaluate)
5792 {
5793 *arg = p + 1;
5794 return OK;
5795 }
5796
5797 /*
5798 * Copy the string into allocated memory, handling backslashed
5799 * characters.
5800 */
5801 name = alloc((unsigned)(p - *arg + extra));
5802 if (name == NULL)
5803 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 rettv->v_type = VAR_STRING;
5805 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
5809 if (*p == '\\')
5810 {
5811 switch (*++p)
5812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 case 'b': *name++ = BS; ++p; break;
5814 case 'e': *name++ = ESC; ++p; break;
5815 case 'f': *name++ = FF; ++p; break;
5816 case 'n': *name++ = NL; ++p; break;
5817 case 'r': *name++ = CAR; ++p; break;
5818 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
5820 case 'X': /* hex: "\x1", "\x12" */
5821 case 'x':
5822 case 'u': /* Unicode: "\u0023" */
5823 case 'U':
5824 if (vim_isxdigit(p[1]))
5825 {
5826 int n, nr;
5827 int c = toupper(*p);
5828
5829 if (c == 'X')
5830 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005831 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005833 else
5834 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 nr = 0;
5836 while (--n >= 0 && vim_isxdigit(p[1]))
5837 {
5838 ++p;
5839 nr = (nr << 4) + hex2nr(*p);
5840 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842#ifdef FEAT_MBYTE
5843 /* For "\u" store the number according to
5844 * 'encoding'. */
5845 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 else
5848#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 break;
5852
5853 /* octal: "\1", "\12", "\123" */
5854 case '0':
5855 case '1':
5856 case '2':
5857 case '3':
5858 case '4':
5859 case '5':
5860 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 case '7': *name = *p++ - '0';
5862 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 *name = (*name << 3) + *p++ - '0';
5865 if (*p >= '0' && *p <= '7')
5866 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 break;
5870
5871 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (extra != 0)
5874 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 break;
5877 }
5878 /* FALLTHROUGH */
5879
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 break;
5882 }
5883 }
5884 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 *arg = p + 1;
5890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 return OK;
5892}
5893
5894/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 * Return OK or FAIL.
5897 */
5898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005899get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 int reduce = 0;
5904
5905 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 */
5908 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5909 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 break;
5914 ++reduce;
5915 ++p;
5916 }
5917 }
5918
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 return FAIL;
5923 }
5924
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 if (!evaluate)
5927 {
5928 *arg = p + 1;
5929 return OK;
5930 }
5931
5932 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 */
5935 str = alloc((unsigned)((p - *arg) - reduce));
5936 if (str == NULL)
5937 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 rettv->v_type = VAR_STRING;
5939 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 break;
5947 ++p;
5948 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 *arg = p + 1;
5953
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 return OK;
5955}
5956
Bram Moolenaarddecc252016-04-06 22:59:37 +02005957 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005958partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005959{
5960 int i;
5961
5962 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005963 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005964 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005966 func_unref(pt->pt_name);
5967 vim_free(pt->pt_name);
5968 vim_free(pt);
5969}
5970
5971/*
5972 * Unreference a closure: decrement the reference count and free it when it
5973 * becomes zero.
5974 */
5975 void
5976partial_unref(partial_T *pt)
5977{
5978 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005979 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005980}
5981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 * Allocate a variable for a List and fill it from "*arg".
5984 * Return OK or FAIL.
5985 */
5986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005987get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988{
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 list_T *l = NULL;
5990 typval_T tv;
5991 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992
5993 if (evaluate)
5994 {
5995 l = list_alloc();
5996 if (l == NULL)
5997 return FAIL;
5998 }
5999
6000 *arg = skipwhite(*arg + 1);
6001 while (**arg != ']' && **arg != NUL)
6002 {
6003 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6004 goto failret;
6005 if (evaluate)
6006 {
6007 item = listitem_alloc();
6008 if (item != NULL)
6009 {
6010 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006011 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 list_append(l, item);
6013 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006014 else
6015 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 }
6017
6018 if (**arg == ']')
6019 break;
6020 if (**arg != ',')
6021 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006022 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023 goto failret;
6024 }
6025 *arg = skipwhite(*arg + 1);
6026 }
6027
6028 if (**arg != ']')
6029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031failret:
6032 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006033 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 return FAIL;
6035 }
6036
6037 *arg = skipwhite(*arg + 1);
6038 if (evaluate)
6039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 rettv->v_type = VAR_LIST;
6041 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 ++l->lv_refcount;
6043 }
6044
6045 return OK;
6046}
6047
6048/*
6049 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006050 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006052 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 list_T *l;
6056
6057 l = (list_T *)alloc_clear(sizeof(list_T));
6058 if (l != NULL)
6059 {
6060 /* Prepend the list to the list of lists for garbage collection. */
6061 if (first_list != NULL)
6062 first_list->lv_used_prev = l;
6063 l->lv_used_prev = NULL;
6064 l->lv_used_next = first_list;
6065 first_list = l;
6066 }
6067 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068}
6069
6070/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006071 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006072 * Returns OK or FAIL.
6073 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006076{
6077 list_T *l = list_alloc();
6078
6079 if (l == NULL)
6080 return FAIL;
6081
6082 rettv->vval.v_list = l;
6083 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006084 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006085 ++l->lv_refcount;
6086 return OK;
6087}
6088
6089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 * Unreference a list: decrement the reference count and free it when it
6091 * becomes zero.
6092 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006094list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006096 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006097 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098}
6099
6100/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006101 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 * Ignores the reference count.
6103 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006104 static void
6105list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106{
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006108
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006109 for (item = l->lv_first; item != NULL; item = l->lv_first)
6110 {
6111 /* Remove the item before deleting it. */
6112 l->lv_first = item->li_next;
6113 clear_tv(&item->li_tv);
6114 vim_free(item);
6115 }
6116}
6117
6118 static void
6119list_free_list(list_T *l)
6120{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 /* Remove the list from the list of lists for garbage collection. */
6122 if (l->lv_used_prev == NULL)
6123 first_list = l->lv_used_next;
6124 else
6125 l->lv_used_prev->lv_used_next = l->lv_used_next;
6126 if (l->lv_used_next != NULL)
6127 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6128
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 vim_free(l);
6130}
6131
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006132 void
6133list_free(list_T *l)
6134{
6135 if (!in_free_unref_items)
6136 {
6137 list_free_contents(l);
6138 list_free_list(l);
6139 }
6140}
6141
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142/*
6143 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006144 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006146 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006147listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148{
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150}
6151
6152/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006153 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006156listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006158 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 vim_free(item);
6160}
6161
6162/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006163 * Remove a list item from a List and free it. Also clears the value.
6164 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006166listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006167{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006168 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006169 listitem_free(item);
6170}
6171
6172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 * Get the number of items in a list.
6174 */
6175 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 if (l == NULL)
6179 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006180 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181}
6182
6183/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 * Return TRUE when two lists have exactly the same values.
6185 */
6186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006187list_equal(
6188 list_T *l1,
6189 list_T *l2,
6190 int ic, /* ignore case for strings */
6191 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192{
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006195 if (l1 == NULL || l2 == NULL)
6196 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 if (l1 == l2)
6198 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006199 if (list_len(l1) != list_len(l2))
6200 return FALSE;
6201
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006202 for (item1 = l1->lv_first, item2 = l2->lv_first;
6203 item1 != NULL && item2 != NULL;
6204 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206 return FALSE;
6207 return item1 == NULL && item2 == NULL;
6208}
6209
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006210/*
6211 * Return the dictitem that an entry in a hashtable points to.
6212 */
6213 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006215{
6216 return HI2DI(hi);
6217}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006218
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006220 * Return TRUE when two dictionaries have exactly the same key/values.
6221 */
6222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006223dict_equal(
6224 dict_T *d1,
6225 dict_T *d2,
6226 int ic, /* ignore case for strings */
6227 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006228{
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 hashitem_T *hi;
6230 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006231 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006232
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006233 if (d1 == NULL && d2 == NULL)
6234 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006235 if (d1 == NULL || d2 == NULL)
6236 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006237 if (d1 == d2)
6238 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006239 if (dict_len(d1) != dict_len(d2))
6240 return FALSE;
6241
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006242 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006244 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006245 if (!HASHITEM_EMPTY(hi))
6246 {
6247 item2 = dict_find(d2, hi->hi_key, -1);
6248 if (item2 == NULL)
6249 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006250 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006251 return FALSE;
6252 --todo;
6253 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006254 }
6255 return TRUE;
6256}
6257
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006258static int tv_equal_recurse_limit;
6259
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006260/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006261 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006262 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006263 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006264 */
6265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006266tv_equal(
6267 typval_T *tv1,
6268 typval_T *tv2,
6269 int ic, /* ignore case */
6270 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006271{
6272 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006273 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006274 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006275 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006276
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006277 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6278 if ((tv1->v_type == VAR_FUNC
6279 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6280 && (tv2->v_type == VAR_FUNC
6281 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6282 {
6283 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6284 : tv1->vval.v_partial->pt_name;
6285 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6286 : tv2->vval.v_partial->pt_name;
6287 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6288 }
6289
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006290 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006291 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006292
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006293 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006294 * recursiveness to a limit. We guess they are equal then.
6295 * A fixed limit has the problem of still taking an awful long time.
6296 * Reduce the limit every time running into it. That should work fine for
6297 * deeply linked structures that are not recursively linked and catch
6298 * recursiveness quickly. */
6299 if (!recursive)
6300 tv_equal_recurse_limit = 1000;
6301 if (recursive_cnt >= tv_equal_recurse_limit)
6302 {
6303 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006304 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006305 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006306
6307 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006308 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006309 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006310 ++recursive_cnt;
6311 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6312 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006313 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006314
6315 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006316 ++recursive_cnt;
6317 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6318 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006319 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006320
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006321 case VAR_NUMBER:
6322 return tv1->vval.v_number == tv2->vval.v_number;
6323
6324 case VAR_STRING:
6325 s1 = get_tv_string_buf(tv1, buf1);
6326 s2 = get_tv_string_buf(tv2, buf2);
6327 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006328
6329 case VAR_SPECIAL:
6330 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006331
6332 case VAR_FLOAT:
6333#ifdef FEAT_FLOAT
6334 return tv1->vval.v_float == tv2->vval.v_float;
6335#endif
6336 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006337#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006338 return tv1->vval.v_job == tv2->vval.v_job;
6339#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006340 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006341#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006342 return tv1->vval.v_channel == tv2->vval.v_channel;
6343#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006344 case VAR_FUNC:
6345 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006346 case VAR_UNKNOWN:
6347 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006348 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006349
Bram Moolenaara03f2332016-02-06 18:09:59 +01006350 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6351 * does not equal anything, not even itself. */
6352 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006353}
6354
6355/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356 * Locate item with index "n" in list "l" and return it.
6357 * A negative index is counted from the end; -1 is the last item.
6358 * Returns NULL when "n" is out of range.
6359 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006360 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006361list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362{
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006364 long idx;
6365
6366 if (l == NULL)
6367 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006368
6369 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006371 n = l->lv_len + n;
6372
6373 /* Check for index out of range. */
6374 if (n < 0 || n >= l->lv_len)
6375 return NULL;
6376
6377 /* When there is a cached index may start search from there. */
6378 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006380 if (n < l->lv_idx / 2)
6381 {
6382 /* closest to the start of the list */
6383 item = l->lv_first;
6384 idx = 0;
6385 }
6386 else if (n > (l->lv_idx + l->lv_len) / 2)
6387 {
6388 /* closest to the end of the list */
6389 item = l->lv_last;
6390 idx = l->lv_len - 1;
6391 }
6392 else
6393 {
6394 /* closest to the cached index */
6395 item = l->lv_idx_item;
6396 idx = l->lv_idx;
6397 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006398 }
6399 else
6400 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006401 if (n < l->lv_len / 2)
6402 {
6403 /* closest to the start of the list */
6404 item = l->lv_first;
6405 idx = 0;
6406 }
6407 else
6408 {
6409 /* closest to the end of the list */
6410 item = l->lv_last;
6411 idx = l->lv_len - 1;
6412 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006414
6415 while (n > idx)
6416 {
6417 /* search forward */
6418 item = item->li_next;
6419 ++idx;
6420 }
6421 while (n < idx)
6422 {
6423 /* search backward */
6424 item = item->li_prev;
6425 --idx;
6426 }
6427
6428 /* cache the used index */
6429 l->lv_idx = idx;
6430 l->lv_idx_item = item;
6431
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432 return item;
6433}
6434
6435/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006436 * Get list item "l[idx]" as a number.
6437 */
6438 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006439list_find_nr(
6440 list_T *l,
6441 long idx,
6442 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006443{
6444 listitem_T *li;
6445
6446 li = list_find(l, idx);
6447 if (li == NULL)
6448 {
6449 if (errorp != NULL)
6450 *errorp = TRUE;
6451 return -1L;
6452 }
6453 return get_tv_number_chk(&li->li_tv, errorp);
6454}
6455
6456/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006457 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6458 */
6459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006461{
6462 listitem_T *li;
6463
6464 li = list_find(l, idx - 1);
6465 if (li == NULL)
6466 {
6467 EMSGN(_(e_listidx), idx);
6468 return NULL;
6469 }
6470 return get_tv_string(&li->li_tv);
6471}
6472
6473/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006474 * Locate "item" list "l" and return its index.
6475 * Returns -1 when "item" is not in the list.
6476 */
6477 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006479{
6480 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006481 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006482
6483 if (l == NULL)
6484 return -1;
6485 idx = 0;
6486 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6487 ++idx;
6488 if (li == NULL)
6489 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006490 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006491}
6492
6493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494 * Append item "item" to the end of list "l".
6495 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006497list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006498{
6499 if (l->lv_last == NULL)
6500 {
6501 /* empty list */
6502 l->lv_first = item;
6503 l->lv_last = item;
6504 item->li_prev = NULL;
6505 }
6506 else
6507 {
6508 l->lv_last->li_next = item;
6509 item->li_prev = l->lv_last;
6510 l->lv_last = item;
6511 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006512 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 item->li_next = NULL;
6514}
6515
6516/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006520 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006521list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006523 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524
Bram Moolenaar05159a02005-02-26 23:04:13 +00006525 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006527 copy_tv(tv, &li->li_tv);
6528 list_append(l, li);
6529 return OK;
6530}
6531
6532/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006533 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006534 * Return FAIL when out of memory.
6535 */
6536 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006537list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006538{
6539 listitem_T *li = listitem_alloc();
6540
6541 if (li == NULL)
6542 return FAIL;
6543 li->li_tv.v_type = VAR_DICT;
6544 li->li_tv.v_lock = 0;
6545 li->li_tv.vval.v_dict = dict;
6546 list_append(list, li);
6547 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548 return OK;
6549}
6550
6551/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006552 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006553 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006554 * Returns FAIL when out of memory.
6555 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006556 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006557list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006558{
6559 listitem_T *li = listitem_alloc();
6560
6561 if (li == NULL)
6562 return FAIL;
6563 list_append(l, li);
6564 li->li_tv.v_type = VAR_STRING;
6565 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006566 if (str == NULL)
6567 li->li_tv.vval.v_string = NULL;
6568 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006569 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006570 return FAIL;
6571 return OK;
6572}
6573
6574/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006575 * Append "n" to list "l".
6576 * Returns FAIL when out of memory.
6577 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006579list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006580{
6581 listitem_T *li;
6582
6583 li = listitem_alloc();
6584 if (li == NULL)
6585 return FAIL;
6586 li->li_tv.v_type = VAR_NUMBER;
6587 li->li_tv.v_lock = 0;
6588 li->li_tv.vval.v_number = n;
6589 list_append(l, li);
6590 return OK;
6591}
6592
6593/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006594 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595 * If "item" is NULL append at the end.
6596 * Return FAIL when out of memory.
6597 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006599list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006600{
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006602
6603 if (ni == NULL)
6604 return FAIL;
6605 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006606 list_insert(l, ni, item);
6607 return OK;
6608}
6609
6610 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006611list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006612{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613 if (item == NULL)
6614 /* Append new item at end of list. */
6615 list_append(l, ni);
6616 else
6617 {
6618 /* Insert new item before existing item. */
6619 ni->li_prev = item->li_prev;
6620 ni->li_next = item;
6621 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006622 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006623 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006624 ++l->lv_idx;
6625 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006627 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006628 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006629 l->lv_idx_item = NULL;
6630 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006632 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634}
6635
6636/*
6637 * Extend "l1" with "l2".
6638 * If "bef" is NULL append at the end, otherwise insert before this item.
6639 * Returns FAIL when out of memory.
6640 */
6641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006642list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006643{
Bram Moolenaar33570922005-01-25 22:26:29 +00006644 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006645 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006647 /* We also quit the loop when we have inserted the original item count of
6648 * the list, avoid a hang when we extend a list with itself. */
6649 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006650 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6651 return FAIL;
6652 return OK;
6653}
6654
6655/*
6656 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6657 * Return FAIL when out of memory.
6658 */
6659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006660list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661{
Bram Moolenaar33570922005-01-25 22:26:29 +00006662 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006664 if (l1 == NULL || l2 == NULL)
6665 return FAIL;
6666
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006667 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006668 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669 if (l == NULL)
6670 return FAIL;
6671 tv->v_type = VAR_LIST;
6672 tv->vval.v_list = l;
6673
6674 /* append all items from the second list */
6675 return list_extend(l, l2, NULL);
6676}
6677
6678/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006679 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682 * Returns NULL when out of memory.
6683 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006686{
Bram Moolenaar33570922005-01-25 22:26:29 +00006687 list_T *copy;
6688 listitem_T *item;
6689 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690
6691 if (orig == NULL)
6692 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006693
6694 copy = list_alloc();
6695 if (copy != NULL)
6696 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006697 if (copyID != 0)
6698 {
6699 /* Do this before adding the items, because one of the items may
6700 * refer back to this list. */
6701 orig->lv_copyID = copyID;
6702 orig->lv_copylist = copy;
6703 }
6704 for (item = orig->lv_first; item != NULL && !got_int;
6705 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706 {
6707 ni = listitem_alloc();
6708 if (ni == NULL)
6709 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006710 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006711 {
6712 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6713 {
6714 vim_free(ni);
6715 break;
6716 }
6717 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006718 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006719 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006720 list_append(copy, ni);
6721 }
6722 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006723 if (item != NULL)
6724 {
6725 list_unref(copy);
6726 copy = NULL;
6727 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006728 }
6729
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006730 return copy;
6731}
6732
6733/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006734 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006735 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006736 * This used to be called list_remove, but that conflicts with a Sun header
6737 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006738 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006740vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741{
Bram Moolenaar33570922005-01-25 22:26:29 +00006742 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006743
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006744 /* notify watchers */
6745 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006746 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006747 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006748 list_fix_watch(l, ip);
6749 if (ip == item2)
6750 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006752
6753 if (item2->li_next == NULL)
6754 l->lv_last = item->li_prev;
6755 else
6756 item2->li_next->li_prev = item->li_prev;
6757 if (item->li_prev == NULL)
6758 l->lv_first = item2->li_next;
6759 else
6760 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006761 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762}
6763
6764/*
6765 * Return an allocated string with the string representation of a list.
6766 * May return NULL.
6767 */
6768 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006769list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006770{
6771 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006772
6773 if (tv->vval.v_list == NULL)
6774 return NULL;
6775 ga_init2(&ga, (int)sizeof(char), 80);
6776 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006777 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006778 {
6779 vim_free(ga.ga_data);
6780 return NULL;
6781 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006782 ga_append(&ga, ']');
6783 ga_append(&ga, NUL);
6784 return (char_u *)ga.ga_data;
6785}
6786
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006787typedef struct join_S {
6788 char_u *s;
6789 char_u *tofree;
6790} join_T;
6791
6792 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006793list_join_inner(
6794 garray_T *gap, /* to store the result in */
6795 list_T *l,
6796 char_u *sep,
6797 int echo_style,
6798 int copyID,
6799 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006800{
6801 int i;
6802 join_T *p;
6803 int len;
6804 int sumlen = 0;
6805 int first = TRUE;
6806 char_u *tofree;
6807 char_u numbuf[NUMBUFLEN];
6808 listitem_T *item;
6809 char_u *s;
6810
6811 /* Stringify each item in the list. */
6812 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6813 {
6814 if (echo_style)
6815 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6816 else
6817 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6818 if (s == NULL)
6819 return FAIL;
6820
6821 len = (int)STRLEN(s);
6822 sumlen += len;
6823
Bram Moolenaarcde88542015-08-11 19:14:00 +02006824 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006825 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6826 if (tofree != NULL || s != numbuf)
6827 {
6828 p->s = s;
6829 p->tofree = tofree;
6830 }
6831 else
6832 {
6833 p->s = vim_strnsave(s, len);
6834 p->tofree = p->s;
6835 }
6836
6837 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006838 if (did_echo_string_emsg) /* recursion error, bail out */
6839 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006840 }
6841
6842 /* Allocate result buffer with its total size, avoid re-allocation and
6843 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6844 if (join_gap->ga_len >= 2)
6845 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6846 if (ga_grow(gap, sumlen + 2) == FAIL)
6847 return FAIL;
6848
6849 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6850 {
6851 if (first)
6852 first = FALSE;
6853 else
6854 ga_concat(gap, sep);
6855 p = ((join_T *)join_gap->ga_data) + i;
6856
6857 if (p->s != NULL)
6858 ga_concat(gap, p->s);
6859 line_breakcheck();
6860 }
6861
6862 return OK;
6863}
6864
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006865/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006866 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006867 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006868 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006869 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006871list_join(
6872 garray_T *gap,
6873 list_T *l,
6874 char_u *sep,
6875 int echo_style,
6876 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006877{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006878 garray_T join_ga;
6879 int retval;
6880 join_T *p;
6881 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006882
Bram Moolenaard39a7512015-04-16 22:51:22 +02006883 if (l->lv_len < 1)
6884 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006885 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6886 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6887
6888 /* Dispose each item in join_ga. */
6889 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006890 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006891 p = (join_T *)join_ga.ga_data;
6892 for (i = 0; i < join_ga.ga_len; ++i)
6893 {
6894 vim_free(p->tofree);
6895 ++p;
6896 }
6897 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006898 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006899
6900 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901}
6902
6903/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006904 * Return the next (unique) copy ID.
6905 * Used for serializing nested structures.
6906 */
6907 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006908get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006909{
6910 current_copyID += COPYID_INC;
6911 return current_copyID;
6912}
6913
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006914/* Used by get_func_tv() */
6915static garray_T funcargs = GA_EMPTY;
6916
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006917/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918 * Garbage collection for lists and dictionaries.
6919 *
6920 * We use reference counts to be able to free most items right away when they
6921 * are no longer used. But for composite items it's possible that it becomes
6922 * unused while the reference count is > 0: When there is a recursive
6923 * reference. Example:
6924 * :let l = [1, 2, 3]
6925 * :let d = {9: l}
6926 * :let l[1] = d
6927 *
6928 * Since this is quite unusual we handle this with garbage collection: every
6929 * once in a while find out which lists and dicts are not referenced from any
6930 * variable.
6931 *
6932 * Here is a good reference text about garbage collection (refers to Python
6933 * but it applies to all reference-counting mechanisms):
6934 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006935 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006936
6937/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02006939 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006941 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006942 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006943garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006944{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006945 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006947 buf_T *buf;
6948 win_T *wp;
6949 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006950 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006951 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006953#ifdef FEAT_WINDOWS
6954 tabpage_T *tp;
6955#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006957 if (!testing)
6958 {
6959 /* Only do this once. */
6960 want_garbage_collect = FALSE;
6961 may_garbage_collect = FALSE;
6962 garbage_collect_at_exit = FALSE;
6963 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006964
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 /* We advance by two because we add one for items referenced through
6966 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006967 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 /*
6970 * 1. Go through all accessible variables and mark all lists and dicts
6971 * with copyID.
6972 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973
6974 /* Don't free variables in the previous_funccal list unless they are only
6975 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006976 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6978 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006979 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6980 NULL);
6981 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6982 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006983 }
6984
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 /* script-local variables */
6986 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006987 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988
6989 /* buffer-local variables */
6990 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006991 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6992 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993
6994 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006995 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6997 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006998#ifdef FEAT_AUTOCMD
6999 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007000 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7001 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007002#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007004#ifdef FEAT_WINDOWS
7005 /* tabpage-local variables */
7006 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007007 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7008 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007009#endif
7010
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007012 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013
7014 /* function-local variables */
7015 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7016 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7018 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 }
7020
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007021 /* function call arguments, if v:testing is set. */
7022 for (i = 0; i < funcargs.ga_len; ++i)
7023 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7024 copyID, NULL, NULL);
7025
Bram Moolenaard812df62008-11-09 12:46:09 +00007026 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007028
Bram Moolenaar1dced572012-04-05 16:54:08 +02007029#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007031#endif
7032
Bram Moolenaardb913952012-06-29 12:54:53 +02007033#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007035#endif
7036
7037#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007039#endif
7040
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007041#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007042 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007043 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007044#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007045#ifdef FEAT_NETBEANS_INTG
7046 abort = abort || set_ref_in_nb_channel(copyID);
7047#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007048
Bram Moolenaare3188e22016-05-31 21:13:04 +02007049#ifdef FEAT_TIMERS
7050 abort = abort || set_ref_in_timer(copyID);
7051#endif
7052
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007054 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007055 /*
7056 * 2. Free lists and dictionaries that are not referenced.
7057 */
7058 did_free = free_unref_items(copyID);
7059
7060 /*
7061 * 3. Check if any funccal can be freed now.
7062 */
7063 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007064 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 if (can_free_funccal(*pfc, copyID))
7066 {
7067 fc = *pfc;
7068 *pfc = fc->caller;
7069 free_funccal(fc, TRUE);
7070 did_free = TRUE;
7071 did_free_funccal = TRUE;
7072 }
7073 else
7074 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007075 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 if (did_free_funccal)
7077 /* When a funccal was freed some more items might be garbage
7078 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007079 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007080 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 else if (p_verbose > 0)
7082 {
7083 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7084 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007085
7086 return did_free;
7087}
7088
7089/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007090 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007091 */
7092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007093free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007094{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007095 dict_T *dd, *dd_next;
7096 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007097 int did_free = FALSE;
7098
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007099 /* Let all "free" functions know that we are here. This means no
7100 * dictionaries, lists, channels or jobs are to be freed, because we will
7101 * do that here. */
7102 in_free_unref_items = TRUE;
7103
7104 /*
7105 * PASS 1: free the contents of the items. We don't free the items
7106 * themselves yet, so that it is possible to decrement refcount counters
7107 */
7108
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007110 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007111 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007112 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007113 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007115 /* Free the Dictionary and ordinary items it contains, but don't
7116 * recurse into Lists and Dictionaries, they will be in the list
7117 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007118 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007119 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007121
7122 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007123 * Go through the list of lists and free items without the copyID.
7124 * But don't free a list that has a watcher (used in a for loop), these
7125 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007126 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007127 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7128 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7129 && ll->lv_watch == NULL)
7130 {
7131 /* Free the List and ordinary items it contains, but don't recurse
7132 * into Lists and Dictionaries, they will be in the list of dicts
7133 * or list of lists. */
7134 list_free_contents(ll);
7135 did_free = TRUE;
7136 }
7137
7138#ifdef FEAT_JOB_CHANNEL
7139 /* Go through the list of jobs and free items without the copyID. This
7140 * must happen before doing channels, because jobs refer to channels, but
7141 * the reference from the channel to the job isn't tracked. */
7142 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7143
7144 /* Go through the list of channels and free items without the copyID. */
7145 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7146#endif
7147
7148 /*
7149 * PASS 2: free the items themselves.
7150 */
7151 for (dd = first_dict; dd != NULL; dd = dd_next)
7152 {
7153 dd_next = dd->dv_used_next;
7154 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7155 dict_free_dict(dd);
7156 }
7157
7158 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007159 {
7160 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007161 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7162 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007163 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007164 /* Free the List and ordinary items it contains, but don't recurse
7165 * into Lists and Dictionaries, they will be in the list of dicts
7166 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007167 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007168 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007169 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007170
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007171#ifdef FEAT_JOB_CHANNEL
7172 /* Go through the list of jobs and free items without the copyID. This
7173 * must happen before doing channels, because jobs refer to channels, but
7174 * the reference from the channel to the job isn't tracked. */
7175 free_unused_jobs(copyID, COPYID_MASK);
7176
7177 /* Go through the list of channels and free items without the copyID. */
7178 free_unused_channels(copyID, COPYID_MASK);
7179#endif
7180
7181 in_free_unref_items = FALSE;
7182
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007183 return did_free;
7184}
7185
7186/*
7187 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007188 * "list_stack" is used to add lists to be marked. Can be NULL.
7189 *
7190 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007191 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007193set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007194{
7195 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007196 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007197 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007198 hashtab_T *cur_ht;
7199 ht_stack_T *ht_stack = NULL;
7200 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007201
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007202 cur_ht = ht;
7203 for (;;)
7204 {
7205 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007206 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007207 /* Mark each item in the hashtab. If the item contains a hashtab
7208 * it is added to ht_stack, if it contains a list it is added to
7209 * list_stack. */
7210 todo = (int)cur_ht->ht_used;
7211 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7212 if (!HASHITEM_EMPTY(hi))
7213 {
7214 --todo;
7215 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7216 &ht_stack, list_stack);
7217 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007218 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007219
7220 if (ht_stack == NULL)
7221 break;
7222
7223 /* take an item from the stack */
7224 cur_ht = ht_stack->ht;
7225 tempitem = ht_stack;
7226 ht_stack = ht_stack->prev;
7227 free(tempitem);
7228 }
7229
7230 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007231}
7232
7233/*
7234 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007235 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7236 *
7237 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007238 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007240set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007241{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007242 listitem_T *li;
7243 int abort = FALSE;
7244 list_T *cur_l;
7245 list_stack_T *list_stack = NULL;
7246 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007247
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007248 cur_l = l;
7249 for (;;)
7250 {
7251 if (!abort)
7252 /* Mark each item in the list. If the item contains a hashtab
7253 * it is added to ht_stack, if it contains a list it is added to
7254 * list_stack. */
7255 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7256 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7257 ht_stack, &list_stack);
7258 if (list_stack == NULL)
7259 break;
7260
7261 /* take an item from the stack */
7262 cur_l = list_stack->list;
7263 tempitem = list_stack;
7264 list_stack = list_stack->prev;
7265 free(tempitem);
7266 }
7267
7268 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007269}
7270
7271/*
7272 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007273 * "list_stack" is used to add lists to be marked. Can be NULL.
7274 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7275 *
7276 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007277 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007279set_ref_in_item(
7280 typval_T *tv,
7281 int copyID,
7282 ht_stack_T **ht_stack,
7283 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007284{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007285 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007286
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007287 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007288 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007289 dict_T *dd = tv->vval.v_dict;
7290
Bram Moolenaara03f2332016-02-06 18:09:59 +01007291 if (dd != NULL && dd->dv_copyID != copyID)
7292 {
7293 /* Didn't see this dict yet. */
7294 dd->dv_copyID = copyID;
7295 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007296 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007297 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7298 }
7299 else
7300 {
7301 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7302 if (newitem == NULL)
7303 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007304 else
7305 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007306 newitem->ht = &dd->dv_hashtab;
7307 newitem->prev = *ht_stack;
7308 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007309 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007310 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007311 }
7312 }
7313 else if (tv->v_type == VAR_LIST)
7314 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007315 list_T *ll = tv->vval.v_list;
7316
Bram Moolenaara03f2332016-02-06 18:09:59 +01007317 if (ll != NULL && ll->lv_copyID != copyID)
7318 {
7319 /* Didn't see this list yet. */
7320 ll->lv_copyID = copyID;
7321 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007322 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007323 abort = set_ref_in_list(ll, copyID, ht_stack);
7324 }
7325 else
7326 {
7327 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007328 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007329 if (newitem == NULL)
7330 abort = TRUE;
7331 else
7332 {
7333 newitem->list = ll;
7334 newitem->prev = *list_stack;
7335 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007336 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007337 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007338 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007339 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007340 else if (tv->v_type == VAR_PARTIAL)
7341 {
7342 partial_T *pt = tv->vval.v_partial;
7343 int i;
7344
7345 /* A partial does not have a copyID, because it cannot contain itself.
7346 */
7347 if (pt != NULL)
7348 {
7349 if (pt->pt_dict != NULL)
7350 {
7351 typval_T dtv;
7352
7353 dtv.v_type = VAR_DICT;
7354 dtv.vval.v_dict = pt->pt_dict;
7355 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7356 }
7357
7358 for (i = 0; i < pt->pt_argc; ++i)
7359 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7360 ht_stack, list_stack);
7361 }
7362 }
7363#ifdef FEAT_JOB_CHANNEL
7364 else if (tv->v_type == VAR_JOB)
7365 {
7366 job_T *job = tv->vval.v_job;
7367 typval_T dtv;
7368
7369 if (job != NULL && job->jv_copyID != copyID)
7370 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007371 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007372 if (job->jv_channel != NULL)
7373 {
7374 dtv.v_type = VAR_CHANNEL;
7375 dtv.vval.v_channel = job->jv_channel;
7376 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7377 }
7378 if (job->jv_exit_partial != NULL)
7379 {
7380 dtv.v_type = VAR_PARTIAL;
7381 dtv.vval.v_partial = job->jv_exit_partial;
7382 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7383 }
7384 }
7385 }
7386 else if (tv->v_type == VAR_CHANNEL)
7387 {
7388 channel_T *ch =tv->vval.v_channel;
7389 int part;
7390 typval_T dtv;
7391 jsonq_T *jq;
7392 cbq_T *cq;
7393
7394 if (ch != NULL && ch->ch_copyID != copyID)
7395 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007396 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007397 for (part = PART_SOCK; part <= PART_IN; ++part)
7398 {
7399 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7400 jq = jq->jq_next)
7401 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7402 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7403 cq = cq->cq_next)
7404 if (cq->cq_partial != NULL)
7405 {
7406 dtv.v_type = VAR_PARTIAL;
7407 dtv.vval.v_partial = cq->cq_partial;
7408 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7409 }
7410 if (ch->ch_part[part].ch_partial != NULL)
7411 {
7412 dtv.v_type = VAR_PARTIAL;
7413 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7414 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7415 }
7416 }
7417 if (ch->ch_partial != NULL)
7418 {
7419 dtv.v_type = VAR_PARTIAL;
7420 dtv.vval.v_partial = ch->ch_partial;
7421 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7422 }
7423 if (ch->ch_close_partial != NULL)
7424 {
7425 dtv.v_type = VAR_PARTIAL;
7426 dtv.vval.v_partial = ch->ch_close_partial;
7427 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7428 }
7429 }
7430 }
7431#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007432 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007433}
7434
7435/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436 * Allocate an empty header for a dictionary.
7437 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007438 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007439dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440{
Bram Moolenaar33570922005-01-25 22:26:29 +00007441 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007442
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007444 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007445 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007446 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007447 if (first_dict != NULL)
7448 first_dict->dv_used_prev = d;
7449 d->dv_used_next = first_dict;
7450 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007451 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007452
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007454 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007455 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007456 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007457 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007458 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007459 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460}
7461
7462/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007463 * Allocate an empty dict for a return value.
7464 * Returns OK or FAIL.
7465 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007467rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007468{
7469 dict_T *d = dict_alloc();
7470
7471 if (d == NULL)
7472 return FAIL;
7473
7474 rettv->vval.v_dict = d;
7475 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007476 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007477 ++d->dv_refcount;
7478 return OK;
7479}
7480
7481
7482/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 * Unreference a Dictionary: decrement the reference count and free it when it
7484 * becomes zero.
7485 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007486 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007487dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007489 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007490 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491}
7492
7493/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007494 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 * Ignores the reference count.
7496 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007497 static void
7498dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007502 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007504 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007505 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007506 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 if (!HASHITEM_EMPTY(hi))
7510 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007511 /* Remove the item before deleting it, just in case there is
7512 * something recursive causing trouble. */
7513 di = HI2DI(hi);
7514 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007515 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007516 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 --todo;
7518 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007521}
7522
7523 static void
7524dict_free_dict(dict_T *d)
7525{
7526 /* Remove the dict from the list of dicts for garbage collection. */
7527 if (d->dv_used_prev == NULL)
7528 first_dict = d->dv_used_next;
7529 else
7530 d->dv_used_prev->dv_used_next = d->dv_used_next;
7531 if (d->dv_used_next != NULL)
7532 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 vim_free(d);
7534}
7535
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007536 void
7537dict_free(dict_T *d)
7538{
7539 if (!in_free_unref_items)
7540 {
7541 dict_free_contents(d);
7542 dict_free_dict(d);
7543 }
7544}
7545
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546/*
7547 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007548 * The "key" is copied to the new item.
7549 * Note that the value of the item "di_tv" still needs to be initialized!
7550 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007552 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007553dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554{
Bram Moolenaar33570922005-01-25 22:26:29 +00007555 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007557 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007558 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007559 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007561 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007562 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564}
7565
7566/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567 * Make a copy of a Dictionary item.
7568 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007570dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571{
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007573
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007574 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7575 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007576 if (di != NULL)
7577 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007579 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007580 copy_tv(&org->di_tv, &di->di_tv);
7581 }
7582 return di;
7583}
7584
7585/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586 * Remove item "item" from Dictionary "dict" and free it.
7587 */
7588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007589dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590{
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 if (HASHITEM_EMPTY(hi))
7595 EMSG2(_(e_intern2), "dictitem_remove()");
7596 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 dictitem_free(item);
7599}
7600
7601/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 * Free a dict item. Also clears the value.
7603 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007605dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007608 if (item->di_flags & DI_FLAGS_ALLOC)
7609 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610}
7611
7612/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007613 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7614 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007615 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007616 * Returns NULL when out of memory.
7617 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007619dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007620{
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 dict_T *copy;
7622 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007625
7626 if (orig == NULL)
7627 return NULL;
7628
7629 copy = dict_alloc();
7630 if (copy != NULL)
7631 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007632 if (copyID != 0)
7633 {
7634 orig->dv_copyID = copyID;
7635 orig->dv_copydict = copy;
7636 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007637 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007638 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007640 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007642 --todo;
7643
7644 di = dictitem_alloc(hi->hi_key);
7645 if (di == NULL)
7646 break;
7647 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007648 {
7649 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7650 copyID) == FAIL)
7651 {
7652 vim_free(di);
7653 break;
7654 }
7655 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656 else
7657 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7658 if (dict_add(copy, di) == FAIL)
7659 {
7660 dictitem_free(di);
7661 break;
7662 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007663 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007664 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007665
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007667 if (todo > 0)
7668 {
7669 dict_unref(copy);
7670 copy = NULL;
7671 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007672 }
7673
7674 return copy;
7675}
7676
7677/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007679 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007681 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007682dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683{
Bram Moolenaar33570922005-01-25 22:26:29 +00007684 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685}
7686
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007688 * Add a number or string entry to dictionary "d".
7689 * When "str" is NULL use number "nr", otherwise use "str".
7690 * Returns FAIL when out of memory and when key already exists.
7691 */
7692 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007693dict_add_nr_str(
7694 dict_T *d,
7695 char *key,
7696 long nr,
7697 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007698{
7699 dictitem_T *item;
7700
7701 item = dictitem_alloc((char_u *)key);
7702 if (item == NULL)
7703 return FAIL;
7704 item->di_tv.v_lock = 0;
7705 if (str == NULL)
7706 {
7707 item->di_tv.v_type = VAR_NUMBER;
7708 item->di_tv.vval.v_number = nr;
7709 }
7710 else
7711 {
7712 item->di_tv.v_type = VAR_STRING;
7713 item->di_tv.vval.v_string = vim_strsave(str);
7714 }
7715 if (dict_add(d, item) == FAIL)
7716 {
7717 dictitem_free(item);
7718 return FAIL;
7719 }
7720 return OK;
7721}
7722
7723/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007724 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007725 * Returns FAIL when out of memory and when key already exists.
7726 */
7727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007728dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007729{
7730 dictitem_T *item;
7731
7732 item = dictitem_alloc((char_u *)key);
7733 if (item == NULL)
7734 return FAIL;
7735 item->di_tv.v_lock = 0;
7736 item->di_tv.v_type = VAR_LIST;
7737 item->di_tv.vval.v_list = list;
7738 if (dict_add(d, item) == FAIL)
7739 {
7740 dictitem_free(item);
7741 return FAIL;
7742 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007743 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007744 return OK;
7745}
7746
7747/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007748 * Get the number of items in a Dictionary.
7749 */
7750 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007751dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007752{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007753 if (d == NULL)
7754 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007755 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007756}
7757
7758/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759 * Find item "key[len]" in Dictionary "d".
7760 * If "len" is negative use strlen(key).
7761 * Returns NULL when not found.
7762 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007763 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007764dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007766#define AKEYLEN 200
7767 char_u buf[AKEYLEN];
7768 char_u *akey;
7769 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007770 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007772 if (d == NULL)
7773 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007774 if (len < 0)
7775 akey = key;
7776 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007777 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007778 tofree = akey = vim_strnsave(key, len);
7779 if (akey == NULL)
7780 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007781 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007782 else
7783 {
7784 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007785 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007786 akey = buf;
7787 }
7788
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007790 vim_free(tofree);
7791 if (HASHITEM_EMPTY(hi))
7792 return NULL;
7793 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007794}
7795
7796/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007797 * Get a string item from a dictionary.
7798 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007799 * Returns NULL if the entry doesn't exist or out of memory.
7800 */
7801 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007802get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007803{
7804 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007805 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007806
7807 di = dict_find(d, key, -1);
7808 if (di == NULL)
7809 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007810 s = get_tv_string(&di->di_tv);
7811 if (save && s != NULL)
7812 s = vim_strsave(s);
7813 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007814}
7815
7816/*
7817 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007818 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007819 */
7820 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007821get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007822{
7823 dictitem_T *di;
7824
7825 di = dict_find(d, key, -1);
7826 if (di == NULL)
7827 return 0;
7828 return get_tv_number(&di->di_tv);
7829}
7830
7831/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007832 * Return an allocated string with the string representation of a Dictionary.
7833 * May return NULL.
7834 */
7835 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007836dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007837{
7838 garray_T ga;
7839 int first = TRUE;
7840 char_u *tofree;
7841 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007843 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007844 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007845 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007847 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848 return NULL;
7849 ga_init2(&ga, (int)sizeof(char), 80);
7850 ga_append(&ga, '{');
7851
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007852 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007853 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007854 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007855 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007857 --todo;
7858
7859 if (first)
7860 first = FALSE;
7861 else
7862 ga_concat(&ga, (char_u *)", ");
7863
7864 tofree = string_quote(hi->hi_key, FALSE);
7865 if (tofree != NULL)
7866 {
7867 ga_concat(&ga, tofree);
7868 vim_free(tofree);
7869 }
7870 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007871 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007872 if (s != NULL)
7873 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007874 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007875 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007876 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007877 line_breakcheck();
7878
Bram Moolenaar8c711452005-01-14 21:53:12 +00007879 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007880 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007881 if (todo > 0)
7882 {
7883 vim_free(ga.ga_data);
7884 return NULL;
7885 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007886
7887 ga_append(&ga, '}');
7888 ga_append(&ga, NUL);
7889 return (char_u *)ga.ga_data;
7890}
7891
7892/*
7893 * Allocate a variable for a Dictionary and fill it from "*arg".
7894 * Return OK or FAIL. Returns NOTDONE for {expr}.
7895 */
7896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007897get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007898{
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 dict_T *d = NULL;
7900 typval_T tvkey;
7901 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007902 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007904 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007905 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007906
7907 /*
7908 * First check if it's not a curly-braces thing: {expr}.
7909 * Must do this without evaluating, otherwise a function may be called
7910 * twice. Unfortunately this means we need to call eval1() twice for the
7911 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007912 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007914 if (*start != '}')
7915 {
7916 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7917 return FAIL;
7918 if (*start == '}')
7919 return NOTDONE;
7920 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007921
7922 if (evaluate)
7923 {
7924 d = dict_alloc();
7925 if (d == NULL)
7926 return FAIL;
7927 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007928 tvkey.v_type = VAR_UNKNOWN;
7929 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007930
7931 *arg = skipwhite(*arg + 1);
7932 while (**arg != '}' && **arg != NUL)
7933 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007934 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 goto failret;
7936 if (**arg != ':')
7937 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007938 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007939 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007940 goto failret;
7941 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007942 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007944 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007945 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007946 {
7947 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007948 clear_tv(&tvkey);
7949 goto failret;
7950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007951 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007952
7953 *arg = skipwhite(*arg + 1);
7954 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7955 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007956 if (evaluate)
7957 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 goto failret;
7959 }
7960 if (evaluate)
7961 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007962 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007963 if (item != NULL)
7964 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007965 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007966 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 clear_tv(&tv);
7968 goto failret;
7969 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007970 item = dictitem_alloc(key);
7971 clear_tv(&tvkey);
7972 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007973 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007974 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007975 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007976 if (dict_add(d, item) == FAIL)
7977 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 }
7979 }
7980
7981 if (**arg == '}')
7982 break;
7983 if (**arg != ',')
7984 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007985 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007986 goto failret;
7987 }
7988 *arg = skipwhite(*arg + 1);
7989 }
7990
7991 if (**arg != '}')
7992 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007993 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007994failret:
7995 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007996 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007997 return FAIL;
7998 }
7999
8000 *arg = skipwhite(*arg + 1);
8001 if (evaluate)
8002 {
8003 rettv->v_type = VAR_DICT;
8004 rettv->vval.v_dict = d;
8005 ++d->dv_refcount;
8006 }
8007
8008 return OK;
8009}
8010
Bram Moolenaar17a13432016-01-24 14:22:10 +01008011 static char *
8012get_var_special_name(int nr)
8013{
8014 switch (nr)
8015 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008016 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008017 case VVAL_TRUE: return "v:true";
8018 case VVAL_NONE: return "v:none";
8019 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008020 }
8021 EMSG2(_(e_intern2), "get_var_special_name()");
8022 return "42";
8023}
8024
Bram Moolenaar8c711452005-01-14 21:53:12 +00008025/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008026 * Return a string with the string representation of a variable.
8027 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008028 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008029 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008030 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008031 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008032 */
8033 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008034echo_string(
8035 typval_T *tv,
8036 char_u **tofree,
8037 char_u *numbuf,
8038 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008039{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008040 static int recurse = 0;
8041 char_u *r = NULL;
8042
Bram Moolenaar33570922005-01-25 22:26:29 +00008043 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008044 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008045 if (!did_echo_string_emsg)
8046 {
8047 /* Only give this message once for a recursive call to avoid
8048 * flooding the user with errors. And stop iterating over lists
8049 * and dicts. */
8050 did_echo_string_emsg = TRUE;
8051 EMSG(_("E724: variable nested too deep for displaying"));
8052 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008053 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008054 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008055 }
8056 ++recurse;
8057
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008058 switch (tv->v_type)
8059 {
8060 case VAR_FUNC:
8061 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008062 r = tv->vval.v_string;
8063 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008064
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008065 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008066 {
8067 partial_T *pt = tv->vval.v_partial;
8068 char_u *fname = string_quote(pt == NULL ? NULL
8069 : pt->pt_name, FALSE);
8070 garray_T ga;
8071 int i;
8072 char_u *tf;
8073
8074 ga_init2(&ga, 1, 100);
8075 ga_concat(&ga, (char_u *)"function(");
8076 if (fname != NULL)
8077 {
8078 ga_concat(&ga, fname);
8079 vim_free(fname);
8080 }
8081 if (pt != NULL && pt->pt_argc > 0)
8082 {
8083 ga_concat(&ga, (char_u *)", [");
8084 for (i = 0; i < pt->pt_argc; ++i)
8085 {
8086 if (i > 0)
8087 ga_concat(&ga, (char_u *)", ");
8088 ga_concat(&ga,
8089 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8090 vim_free(tf);
8091 }
8092 ga_concat(&ga, (char_u *)"]");
8093 }
8094 if (pt != NULL && pt->pt_dict != NULL)
8095 {
8096 typval_T dtv;
8097
8098 ga_concat(&ga, (char_u *)", ");
8099 dtv.v_type = VAR_DICT;
8100 dtv.vval.v_dict = pt->pt_dict;
8101 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8102 vim_free(tf);
8103 }
8104 ga_concat(&ga, (char_u *)")");
8105
8106 *tofree = ga.ga_data;
8107 r = *tofree;
8108 break;
8109 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008110
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008111 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008112 if (tv->vval.v_list == NULL)
8113 {
8114 *tofree = NULL;
8115 r = NULL;
8116 }
8117 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8118 {
8119 *tofree = NULL;
8120 r = (char_u *)"[...]";
8121 }
8122 else
8123 {
8124 tv->vval.v_list->lv_copyID = copyID;
8125 *tofree = list2string(tv, copyID);
8126 r = *tofree;
8127 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008128 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008129
Bram Moolenaar8c711452005-01-14 21:53:12 +00008130 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008131 if (tv->vval.v_dict == NULL)
8132 {
8133 *tofree = NULL;
8134 r = NULL;
8135 }
8136 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8137 {
8138 *tofree = NULL;
8139 r = (char_u *)"{...}";
8140 }
8141 else
8142 {
8143 tv->vval.v_dict->dv_copyID = copyID;
8144 *tofree = dict2string(tv, copyID);
8145 r = *tofree;
8146 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008147 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008148
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008149 case VAR_STRING:
8150 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008151 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008152 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008153 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008154 *tofree = NULL;
8155 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008156 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008157
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008158 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008159#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008160 *tofree = NULL;
8161 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8162 r = numbuf;
8163 break;
8164#endif
8165
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008166 case VAR_SPECIAL:
8167 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008168 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008169 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008170 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008171
Bram Moolenaar8502c702014-06-17 12:51:16 +02008172 if (--recurse == 0)
8173 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008174 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008175}
8176
8177/*
8178 * Return a string with the string representation of a variable.
8179 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8180 * "numbuf" is used for a number.
8181 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008182 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008183 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008184 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008185tv2string(
8186 typval_T *tv,
8187 char_u **tofree,
8188 char_u *numbuf,
8189 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008190{
8191 switch (tv->v_type)
8192 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008193 case VAR_FUNC:
8194 *tofree = string_quote(tv->vval.v_string, TRUE);
8195 return *tofree;
8196 case VAR_STRING:
8197 *tofree = string_quote(tv->vval.v_string, FALSE);
8198 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008199 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008200#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008201 *tofree = NULL;
8202 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8203 return numbuf;
8204#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008205 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008206 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008207 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008208 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008209 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008210 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008211 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008212 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008213 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008214 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008215 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008216}
8217
8218/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008219 * Return string "str" in ' quotes, doubling ' characters.
8220 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008221 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008222 */
8223 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008224string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008225{
Bram Moolenaar33570922005-01-25 22:26:29 +00008226 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008227 char_u *p, *r, *s;
8228
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 len = (function ? 13 : 3);
8230 if (str != NULL)
8231 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008232 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008233 for (p = str; *p != NUL; mb_ptr_adv(p))
8234 if (*p == '\'')
8235 ++len;
8236 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008237 s = r = alloc(len);
8238 if (r != NULL)
8239 {
8240 if (function)
8241 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008242 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008243 r += 10;
8244 }
8245 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008246 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008247 if (str != NULL)
8248 for (p = str; *p != NUL; )
8249 {
8250 if (*p == '\'')
8251 *r++ = '\'';
8252 MB_COPY_CHAR(p, r);
8253 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008254 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008255 if (function)
8256 *r++ = ')';
8257 *r++ = NUL;
8258 }
8259 return s;
8260}
8261
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008262#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008263/*
8264 * Convert the string "text" to a floating point number.
8265 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8266 * this always uses a decimal point.
8267 * Returns the length of the text that was consumed.
8268 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008270string2float(
8271 char_u *text,
8272 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273{
8274 char *s = (char *)text;
8275 float_T f;
8276
8277 f = strtod(s, &s);
8278 *value = f;
8279 return (int)((char_u *)s - text);
8280}
8281#endif
8282
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 * Get the value of an environment variable.
8285 * "arg" is pointing to the '$'. It is advanced to after the name.
8286 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008287 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 */
8289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008290get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291{
8292 char_u *string = NULL;
8293 int len;
8294 int cc;
8295 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008296 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297
8298 ++*arg;
8299 name = *arg;
8300 len = get_env_len(arg);
8301 if (evaluate)
8302 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008303 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008304 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008305
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008306 cc = name[len];
8307 name[len] = NUL;
8308 /* first try vim_getenv(), fast for normal environment vars */
8309 string = vim_getenv(name, &mustfree);
8310 if (string != NULL && *string != NUL)
8311 {
8312 if (!mustfree)
8313 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008315 else
8316 {
8317 if (mustfree)
8318 vim_free(string);
8319
8320 /* next try expanding things like $VIM and ${HOME} */
8321 string = expand_env_save(name - 1);
8322 if (string != NULL && *string == '$')
8323 {
8324 vim_free(string);
8325 string = NULL;
8326 }
8327 }
8328 name[len] = cc;
8329
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008330 rettv->v_type = VAR_STRING;
8331 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 }
8333
8334 return OK;
8335}
8336
8337/*
8338 * Array with names and number of arguments of all internal functions
8339 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8340 */
8341static struct fst
8342{
8343 char *f_name; /* function name */
8344 char f_min_argc; /* minimal number of arguments */
8345 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008346 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008347 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348} functions[] =
8349{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008350#ifdef FEAT_FLOAT
8351 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008352 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008353#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008354 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008355 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"append", 2, 2, f_append},
8357 {"argc", 0, 0, f_argc},
8358 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008359 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008360 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008361#ifdef FEAT_FLOAT
8362 {"asin", 1, 1, f_asin}, /* WJMc */
8363#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008364 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008365 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008366 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008367 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008368 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008369 {"assert_notequal", 2, 3, f_assert_notequal},
8370 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008371 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008372#ifdef FEAT_FLOAT
8373 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008374 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008377 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {"bufexists", 1, 1, f_bufexists},
8379 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8380 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8381 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8382 {"buflisted", 1, 1, f_buflisted},
8383 {"bufloaded", 1, 1, f_bufloaded},
8384 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008385 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"bufwinnr", 1, 1, f_bufwinnr},
8387 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008388 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008389 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008390 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008391#ifdef FEAT_FLOAT
8392 {"ceil", 1, 1, f_ceil},
8393#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008394#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008395 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008396 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8397 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008398 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008399 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008400 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008401 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008402 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008403 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008404 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008405 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008406 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8407 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008408 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008409 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008410#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008411 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008412 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008414 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008416#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008417 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008418 {"complete_add", 1, 1, f_complete_add},
8419 {"complete_check", 0, 0, f_complete_check},
8420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008422 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008423#ifdef FEAT_FLOAT
8424 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008425 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008426#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008427 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008429 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008430 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008431 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008433 {"diff_filler", 1, 1, f_diff_filler},
8434 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008435 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008437 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {"eventhandler", 0, 0, f_eventhandler},
8439 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008440 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008442#ifdef FEAT_FLOAT
8443 {"exp", 1, 1, f_exp},
8444#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008445 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008446 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008447 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8449 {"filereadable", 1, 1, f_filereadable},
8450 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008451 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008452 {"finddir", 1, 3, f_finddir},
8453 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008454#ifdef FEAT_FLOAT
8455 {"float2nr", 1, 1, f_float2nr},
8456 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008457 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008458#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008459 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {"fnamemodify", 2, 2, f_fnamemodify},
8461 {"foldclosed", 1, 1, f_foldclosed},
8462 {"foldclosedend", 1, 1, f_foldclosedend},
8463 {"foldlevel", 1, 1, f_foldlevel},
8464 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008465 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008467 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008468 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008469 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008470 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008471 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 {"getchar", 0, 1, f_getchar},
8473 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008474 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {"getcmdline", 0, 0, f_getcmdline},
8476 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008477 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008478 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008479 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008480 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008481 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008482 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {"getfsize", 1, 1, f_getfsize},
8484 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008485 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008486 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008487 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008488 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008489 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008490 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008491 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008492 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008494 {"gettabvar", 2, 3, f_gettabvar},
8495 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"getwinposx", 0, 0, f_getwinposx},
8497 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008498 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008499 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008500 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008501 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008504 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008505 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8507 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8508 {"histadd", 2, 2, f_histadd},
8509 {"histdel", 1, 2, f_histdel},
8510 {"histget", 1, 2, f_histget},
8511 {"histnr", 1, 1, f_histnr},
8512 {"hlID", 1, 1, f_hlID},
8513 {"hlexists", 1, 1, f_hlexists},
8514 {"hostname", 0, 0, f_hostname},
8515 {"iconv", 3, 3, f_iconv},
8516 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008517 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008518 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008520 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {"inputrestore", 0, 0, f_inputrestore},
8522 {"inputsave", 0, 0, f_inputsave},
8523 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008525 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008527 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008528#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8529 {"isnan", 1, 1, f_isnan},
8530#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008531 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008532#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008533 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008534 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008535 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008536 {"job_start", 1, 2, f_job_start},
8537 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008538 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008539#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008540 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008541 {"js_decode", 1, 1, f_js_decode},
8542 {"js_encode", 1, 1, f_js_encode},
8543 {"json_decode", 1, 1, f_json_decode},
8544 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008545 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008547 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 {"libcall", 3, 3, f_libcall},
8549 {"libcallnr", 3, 3, f_libcallnr},
8550 {"line", 1, 1, f_line},
8551 {"line2byte", 1, 1, f_line2byte},
8552 {"lispindent", 1, 1, f_lispindent},
8553 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008554#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008555 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008556 {"log10", 1, 1, f_log10},
8557#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008558#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008559 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008560#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008561 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008562 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008563 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008564 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008565 {"matchadd", 2, 5, f_matchadd},
8566 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008567 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008568 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008569 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008570 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008571 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008572 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008573 {"max", 1, 1, f_max},
8574 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008575#ifdef vim_mkdir
8576 {"mkdir", 1, 3, f_mkdir},
8577#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008578 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008579#ifdef FEAT_MZSCHEME
8580 {"mzeval", 1, 1, f_mzeval},
8581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008583 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008584 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008585 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008586#ifdef FEAT_PERL
8587 {"perleval", 1, 1, f_perleval},
8588#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008589#ifdef FEAT_FLOAT
8590 {"pow", 2, 2, f_pow},
8591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008593 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008594 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008595#ifdef FEAT_PYTHON3
8596 {"py3eval", 1, 1, f_py3eval},
8597#endif
8598#ifdef FEAT_PYTHON
8599 {"pyeval", 1, 1, f_pyeval},
8600#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008601 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008602 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008603 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008604#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008605 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008606#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008607 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 {"remote_expr", 2, 3, f_remote_expr},
8609 {"remote_foreground", 1, 1, f_remote_foreground},
8610 {"remote_peek", 1, 2, f_remote_peek},
8611 {"remote_read", 1, 1, f_remote_read},
8612 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008613 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008615 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008617 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008618#ifdef FEAT_FLOAT
8619 {"round", 1, 1, f_round},
8620#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008621 {"screenattr", 2, 2, f_screenattr},
8622 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008623 {"screencol", 0, 0, f_screencol},
8624 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008625 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008626 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008627 {"searchpair", 3, 7, f_searchpair},
8628 {"searchpairpos", 3, 7, f_searchpairpos},
8629 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"server2client", 2, 2, f_server2client},
8631 {"serverlist", 0, 0, f_serverlist},
8632 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008633 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008635 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008637 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008638 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008639 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008640 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008642 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008643 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008645#ifdef FEAT_CRYPT
8646 {"sha256", 1, 1, f_sha256},
8647#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008648 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008649 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008651#ifdef FEAT_FLOAT
8652 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008653 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008654#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008655 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008656 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008657 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008658 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008659 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008660#ifdef FEAT_FLOAT
8661 {"sqrt", 1, 1, f_sqrt},
8662 {"str2float", 1, 1, f_str2float},
8663#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008664 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008665 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008666 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008667 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668#ifdef HAVE_STRFTIME
8669 {"strftime", 1, 2, f_strftime},
8670#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008671 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008673 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {"strlen", 1, 1, f_strlen},
8675 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008676 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008678 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008679 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {"substitute", 4, 4, f_substitute},
8681 {"synID", 3, 3, f_synID},
8682 {"synIDattr", 2, 3, f_synIDattr},
8683 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008684 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008685 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008686 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008687 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008688 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008689 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008690 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008691 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008692 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008693#ifdef FEAT_FLOAT
8694 {"tan", 1, 1, f_tan},
8695 {"tanh", 1, 1, f_tanh},
8696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008698 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8699 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008700 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8701#ifdef FEAT_JOB_CHANNEL
8702 {"test_null_channel", 0, 0, f_test_null_channel},
8703#endif
8704 {"test_null_dict", 0, 0, f_test_null_dict},
8705#ifdef FEAT_JOB_CHANNEL
8706 {"test_null_job", 0, 0, f_test_null_job},
8707#endif
8708 {"test_null_list", 0, 0, f_test_null_list},
8709 {"test_null_partial", 0, 0, f_test_null_partial},
8710 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008711#ifdef FEAT_TIMERS
8712 {"timer_start", 2, 3, f_timer_start},
8713 {"timer_stop", 1, 1, f_timer_stop},
8714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 {"tolower", 1, 1, f_tolower},
8716 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008717 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008718#ifdef FEAT_FLOAT
8719 {"trunc", 1, 1, f_trunc},
8720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008722 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008723 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008724 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008725 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 {"virtcol", 1, 1, f_virtcol},
8727 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008728 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008729 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008730 {"win_getid", 0, 2, f_win_getid},
8731 {"win_gotoid", 1, 1, f_win_gotoid},
8732 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8733 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 {"winbufnr", 1, 1, f_winbufnr},
8735 {"wincol", 0, 0, f_wincol},
8736 {"winheight", 1, 1, f_winheight},
8737 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008738 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008740 {"winrestview", 1, 1, f_winrestview},
8741 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008743 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008744 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008745 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746};
8747
8748#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8749
8750/*
8751 * Function given to ExpandGeneric() to obtain the list of internal
8752 * or user defined function names.
8753 */
8754 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008755get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757 static int intidx = -1;
8758 char_u *name;
8759
8760 if (idx == 0)
8761 intidx = -1;
8762 if (intidx < 0)
8763 {
8764 name = get_user_func_name(xp, idx);
8765 if (name != NULL)
8766 return name;
8767 }
8768 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8769 {
8770 STRCPY(IObuff, functions[intidx].f_name);
8771 STRCAT(IObuff, "(");
8772 if (functions[intidx].f_max_argc == 0)
8773 STRCAT(IObuff, ")");
8774 return IObuff;
8775 }
8776
8777 return NULL;
8778}
8779
8780/*
8781 * Function given to ExpandGeneric() to obtain the list of internal or
8782 * user defined variable or function names.
8783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008785get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786{
8787 static int intidx = -1;
8788 char_u *name;
8789
8790 if (idx == 0)
8791 intidx = -1;
8792 if (intidx < 0)
8793 {
8794 name = get_function_name(xp, idx);
8795 if (name != NULL)
8796 return name;
8797 }
8798 return get_user_var_name(xp, ++intidx);
8799}
8800
8801#endif /* FEAT_CMDL_COMPL */
8802
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008803#if defined(EBCDIC) || defined(PROTO)
8804/*
8805 * Compare struct fst by function name.
8806 */
8807 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008808compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008809{
8810 struct fst *p1 = (struct fst *)s1;
8811 struct fst *p2 = (struct fst *)s2;
8812
8813 return STRCMP(p1->f_name, p2->f_name);
8814}
8815
8816/*
8817 * Sort the function table by function name.
8818 * The sorting of the table above is ASCII dependant.
8819 * On machines using EBCDIC we have to sort it.
8820 */
8821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008822sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008823{
8824 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8825
8826 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8827}
8828#endif
8829
8830
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831/*
8832 * Find internal function in table above.
8833 * Return index, or -1 if not found
8834 */
8835 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008836find_internal_func(
8837 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 int first = 0;
8840 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8841 int cmp;
8842 int x;
8843
8844 /*
8845 * Find the function name in the table. Binary search.
8846 */
8847 while (first <= last)
8848 {
8849 x = first + ((unsigned)(last - first) >> 1);
8850 cmp = STRCMP(name, functions[x].f_name);
8851 if (cmp < 0)
8852 last = x - 1;
8853 else if (cmp > 0)
8854 first = x + 1;
8855 else
8856 return x;
8857 }
8858 return -1;
8859}
8860
8861/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008862 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8863 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008864 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8865 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008866 */
8867 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008868deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008869{
Bram Moolenaar33570922005-01-25 22:26:29 +00008870 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008871 int cc;
8872
Bram Moolenaar65639032016-03-16 21:40:30 +01008873 if (partialp != NULL)
8874 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008875
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008876 cc = name[*lenp];
8877 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008878 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008879 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008881 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008882 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008883 {
8884 *lenp = 0;
8885 return (char_u *)""; /* just in case */
8886 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008887 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008888 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008889 }
8890
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008891 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8892 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008893 partial_T *pt = v->di_tv.vval.v_partial;
8894
8895 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008896 {
8897 *lenp = 0;
8898 return (char_u *)""; /* just in case */
8899 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008900 if (partialp != NULL)
8901 *partialp = pt;
8902 *lenp = (int)STRLEN(pt->pt_name);
8903 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008904 }
8905
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008906 return name;
8907}
8908
8909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 * Allocate a variable for the result of a function.
8911 * Return OK or FAIL.
8912 */
8913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008914get_func_tv(
8915 char_u *name, /* name of the function */
8916 int len, /* length of "name" */
8917 typval_T *rettv,
8918 char_u **arg, /* argument, pointing to the '(' */
8919 linenr_T firstline, /* first line of range */
8920 linenr_T lastline, /* last line of range */
8921 int *doesrange, /* return: function handled range */
8922 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008923 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008924 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926 char_u *argp;
8927 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008928 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 int argcount = 0; /* number of arguments found */
8930
8931 /*
8932 * Get the arguments.
8933 */
8934 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008935 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 {
8937 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8938 if (*argp == ')' || *argp == ',' || *argp == NUL)
8939 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8941 {
8942 ret = FAIL;
8943 break;
8944 }
8945 ++argcount;
8946 if (*argp != ',')
8947 break;
8948 }
8949 if (*argp == ')')
8950 ++argp;
8951 else
8952 ret = FAIL;
8953
8954 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008955 {
8956 int i = 0;
8957
8958 if (get_vim_var_nr(VV_TESTING))
8959 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008960 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008961 * what variables are used on the call stack. */
8962 if (funcargs.ga_itemsize == 0)
8963 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8964 for (i = 0; i < argcount; ++i)
8965 if (ga_grow(&funcargs, 1) == OK)
8966 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8967 &argvars[i];
8968 }
8969
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008971 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008972
8973 funcargs.ga_len -= i;
8974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 {
8977 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008978 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008979 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008980 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982
8983 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985
8986 *arg = skipwhite(argp);
8987 return ret;
8988}
8989
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008990#define ERROR_UNKNOWN 0
8991#define ERROR_TOOMANY 1
8992#define ERROR_TOOFEW 2
8993#define ERROR_SCRIPT 3
8994#define ERROR_DICT 4
8995#define ERROR_NONE 5
8996#define ERROR_OTHER 6
8997#define FLEN_FIXED 40
8998
8999/*
9000 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9001 * Change <SNR>123_name() to K_SNR 123_name().
9002 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9003 * (slow).
9004 */
9005 static char_u *
9006fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9007{
9008 int llen;
9009 char_u *fname;
9010 int i;
9011
9012 llen = eval_fname_script(name);
9013 if (llen > 0)
9014 {
9015 fname_buf[0] = K_SPECIAL;
9016 fname_buf[1] = KS_EXTRA;
9017 fname_buf[2] = (int)KE_SNR;
9018 i = 3;
9019 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9020 {
9021 if (current_SID <= 0)
9022 *error = ERROR_SCRIPT;
9023 else
9024 {
9025 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9026 i = (int)STRLEN(fname_buf);
9027 }
9028 }
9029 if (i + STRLEN(name + llen) < FLEN_FIXED)
9030 {
9031 STRCPY(fname_buf + i, name + llen);
9032 fname = fname_buf;
9033 }
9034 else
9035 {
9036 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9037 if (fname == NULL)
9038 *error = ERROR_OTHER;
9039 else
9040 {
9041 *tofree = fname;
9042 mch_memmove(fname, fname_buf, (size_t)i);
9043 STRCPY(fname + i, name + llen);
9044 }
9045 }
9046 }
9047 else
9048 fname = name;
9049 return fname;
9050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051
9052/*
9053 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009054 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009055 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009058call_func(
9059 char_u *funcname, /* name of the function */
9060 int len, /* length of "name" */
9061 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009062 int argcount_in, /* number of "argvars" */
9063 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009064 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009065 linenr_T firstline, /* first line of range */
9066 linenr_T lastline, /* last line of range */
9067 int *doesrange, /* return: function handled range */
9068 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009069 partial_T *partial, /* optional, can be NULL */
9070 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071{
9072 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 int error = ERROR_NONE;
9074 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009077 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009079 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009080 int argcount = argcount_in;
9081 typval_T *argvars = argvars_in;
9082 dict_T *selfdict = selfdict_in;
9083 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9084 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009085
9086 /* Make a copy of the name, if it comes from a funcref variable it could
9087 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009088 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009089 if (name == NULL)
9090 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009092 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093
9094 *doesrange = FALSE;
9095
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009096 if (partial != NULL)
9097 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009098 /* When the function has a partial with a dict and there is a dict
9099 * argument, use the dict argument. That is backwards compatible.
9100 * When the dict was bound explicitly use the one from the partial. */
9101 if (partial->pt_dict != NULL
9102 && (selfdict_in == NULL || !partial->pt_auto))
9103 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009104 if (error == ERROR_NONE && partial->pt_argc > 0)
9105 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009106 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9107 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9108 for (i = 0; i < argcount_in; ++i)
9109 argv[i + argv_clear] = argvars_in[i];
9110 argvars = argv;
9111 argcount = partial->pt_argc + argcount_in;
9112 }
9113 }
9114
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115
9116 /* execute the function if no errors detected and executing */
9117 if (evaluate && error == ERROR_NONE)
9118 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009119 char_u *rfname = fname;
9120
9121 /* Ignore "g:" before a function name. */
9122 if (fname[0] == 'g' && fname[1] == ':')
9123 rfname = fname + 2;
9124
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009125 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9126 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 error = ERROR_UNKNOWN;
9128
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009129 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 {
9131 /*
9132 * User defined function.
9133 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009134 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009135
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009137 /* Trigger FuncUndefined event, may load the function. */
9138 if (fp == NULL
9139 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009140 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009141 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009143 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009144 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 }
9146#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009147 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009148 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009149 {
9150 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009151 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009152 }
9153
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 if (fp != NULL)
9155 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009156 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009158 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009160 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009162 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009163 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 else
9165 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009166 int did_save_redo = FALSE;
9167
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 /*
9169 * Call the user function.
9170 * Save and restore search patterns, script variables and
9171 * redo buffer.
9172 */
9173 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009174#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009175 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009176#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009177 {
9178 saveRedobuff();
9179 did_save_redo = TRUE;
9180 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009181 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009183 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009184 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9185 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9186 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009187 /* Function was unreferenced while being used, free it
9188 * now. */
9189 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009190 if (did_save_redo)
9191 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 restore_search_patterns();
9193 error = ERROR_NONE;
9194 }
9195 }
9196 }
9197 else
9198 {
9199 /*
9200 * Find the function name in the table, call its implementation.
9201 */
9202 i = find_internal_func(fname);
9203 if (i >= 0)
9204 {
9205 if (argcount < functions[i].f_min_argc)
9206 error = ERROR_TOOFEW;
9207 else if (argcount > functions[i].f_max_argc)
9208 error = ERROR_TOOMANY;
9209 else
9210 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009211 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 error = ERROR_NONE;
9214 }
9215 }
9216 }
9217 /*
9218 * The function call (or "FuncUndefined" autocommand sequence) might
9219 * have been aborted by an error, an interrupt, or an explicitly thrown
9220 * exception that has not been caught so far. This situation can be
9221 * tested for by calling aborting(). For an error in an internal
9222 * function or for the "E132" error in call_user_func(), however, the
9223 * throw point at which the "force_abort" flag (temporarily reset by
9224 * emsg()) is normally updated has not been reached yet. We need to
9225 * update that flag first to make aborting() reliable.
9226 */
9227 update_force_abort();
9228 }
9229 if (error == ERROR_NONE)
9230 ret = OK;
9231
9232 /*
9233 * Report an error unless the argument evaluation or function call has been
9234 * cancelled due to an aborting error, an interrupt, or an exception.
9235 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009236 if (!aborting())
9237 {
9238 switch (error)
9239 {
9240 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009241 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009242 break;
9243 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009244 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009245 break;
9246 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009247 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009248 name);
9249 break;
9250 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009251 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009252 name);
9253 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009254 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009255 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009256 name);
9257 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009258 }
9259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009261 while (argv_clear > 0)
9262 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009263 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009264 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265
9266 return ret;
9267}
9268
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009269/*
9270 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009271 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009272 */
9273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009274emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009275{
9276 char_u *p;
9277
9278 if (*name == K_SPECIAL)
9279 p = concat_str((char_u *)"<SNR>", name + 3);
9280 else
9281 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009282 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009283 if (p != name)
9284 vim_free(p);
9285}
9286
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009287/*
9288 * Return TRUE for a non-zero Number and a non-empty String.
9289 */
9290 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009291non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009292{
9293 return ((argvars[0].v_type == VAR_NUMBER
9294 && argvars[0].vval.v_number != 0)
9295 || (argvars[0].v_type == VAR_STRING
9296 && argvars[0].vval.v_string != NULL
9297 && *argvars[0].vval.v_string != NUL));
9298}
9299
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300/*********************************************
9301 * Implementation of the built-in functions
9302 */
9303
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009304#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009305static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009306
9307/*
9308 * Get the float value of "argvars[0]" into "f".
9309 * Returns FAIL when the argument is not a Number or Float.
9310 */
9311 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009312get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009313{
9314 if (argvars[0].v_type == VAR_FLOAT)
9315 {
9316 *f = argvars[0].vval.v_float;
9317 return OK;
9318 }
9319 if (argvars[0].v_type == VAR_NUMBER)
9320 {
9321 *f = (float_T)argvars[0].vval.v_number;
9322 return OK;
9323 }
9324 EMSG(_("E808: Number or Float required"));
9325 return FAIL;
9326}
9327
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009328/*
9329 * "abs(expr)" function
9330 */
9331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009332f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009333{
9334 if (argvars[0].v_type == VAR_FLOAT)
9335 {
9336 rettv->v_type = VAR_FLOAT;
9337 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9338 }
9339 else
9340 {
9341 varnumber_T n;
9342 int error = FALSE;
9343
9344 n = get_tv_number_chk(&argvars[0], &error);
9345 if (error)
9346 rettv->vval.v_number = -1;
9347 else if (n > 0)
9348 rettv->vval.v_number = n;
9349 else
9350 rettv->vval.v_number = -n;
9351 }
9352}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009353
9354/*
9355 * "acos()" function
9356 */
9357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009358f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009359{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009360 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009361
9362 rettv->v_type = VAR_FLOAT;
9363 if (get_float_arg(argvars, &f) == OK)
9364 rettv->vval.v_float = acos(f);
9365 else
9366 rettv->vval.v_float = 0.0;
9367}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009368#endif
9369
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009371 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 */
9373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009374f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009378 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009379 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009381 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009382 && !tv_check_lock(l->lv_lock,
9383 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009384 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009385 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009386 }
9387 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009388 EMSG(_(e_listreq));
9389}
9390
9391/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009392 * "and(expr, expr)" function
9393 */
9394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009395f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009396{
9397 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9398 & get_tv_number_chk(&argvars[1], NULL);
9399}
9400
9401/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009402 * "append(lnum, string/list)" function
9403 */
9404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009405f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009406{
9407 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009409 list_T *l = NULL;
9410 listitem_T *li = NULL;
9411 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009412 long added = 0;
9413
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009414 /* When coming here from Insert mode, sync undo, so that this can be
9415 * undone separately from what was previously inserted. */
9416 if (u_sync_once == 2)
9417 {
9418 u_sync_once = 1; /* notify that u_sync() was called */
9419 u_sync(TRUE);
9420 }
9421
Bram Moolenaar0d660222005-01-07 21:51:51 +00009422 lnum = get_tv_lnum(argvars);
9423 if (lnum >= 0
9424 && lnum <= curbuf->b_ml.ml_line_count
9425 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009426 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009427 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009428 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009429 l = argvars[1].vval.v_list;
9430 if (l == NULL)
9431 return;
9432 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009433 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009434 for (;;)
9435 {
9436 if (l == NULL)
9437 tv = &argvars[1]; /* append a string */
9438 else if (li == NULL)
9439 break; /* end of list */
9440 else
9441 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009442 line = get_tv_string_chk(tv);
9443 if (line == NULL) /* type error */
9444 {
9445 rettv->vval.v_number = 1; /* Failed */
9446 break;
9447 }
9448 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009449 ++added;
9450 if (l == NULL)
9451 break;
9452 li = li->li_next;
9453 }
9454
9455 appended_lines_mark(lnum, added);
9456 if (curwin->w_cursor.lnum > lnum)
9457 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009459 else
9460 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461}
9462
9463/*
9464 * "argc()" function
9465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009467f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470}
9471
9472/*
9473 * "argidx()" function
9474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009476f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009478 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479}
9480
9481/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009482 * "arglistid()" function
9483 */
9484 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009485f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009486{
9487 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009488
9489 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009490 wp = find_tabwin(&argvars[0], &argvars[1]);
9491 if (wp != NULL)
9492 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009493}
9494
9495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 * "argv(nr)" function
9497 */
9498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
9501 int idx;
9502
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009503 if (argvars[0].v_type != VAR_UNKNOWN)
9504 {
9505 idx = get_tv_number_chk(&argvars[0], NULL);
9506 if (idx >= 0 && idx < ARGCOUNT)
9507 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9508 else
9509 rettv->vval.v_string = NULL;
9510 rettv->v_type = VAR_STRING;
9511 }
9512 else if (rettv_list_alloc(rettv) == OK)
9513 for (idx = 0; idx < ARGCOUNT; ++idx)
9514 list_append_string(rettv->vval.v_list,
9515 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516}
9517
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009518typedef enum
9519{
9520 ASSERT_EQUAL,
9521 ASSERT_NOTEQUAL,
9522 ASSERT_MATCH,
9523 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009524 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009525} assert_type_T;
9526
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009527static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009528static 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 +01009529static void assert_error(garray_T *gap);
9530static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009531
9532/*
9533 * Prepare "gap" for an assert error and add the sourcing position.
9534 */
9535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009536prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009537{
9538 char buf[NUMBUFLEN];
9539
9540 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009541 if (sourcing_name != NULL)
9542 {
9543 ga_concat(gap, sourcing_name);
9544 if (sourcing_lnum > 0)
9545 ga_concat(gap, (char_u *)" ");
9546 }
9547 if (sourcing_lnum > 0)
9548 {
9549 sprintf(buf, "line %ld", (long)sourcing_lnum);
9550 ga_concat(gap, (char_u *)buf);
9551 }
9552 if (sourcing_name != NULL || sourcing_lnum > 0)
9553 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009554}
9555
9556/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009557 * Append "str" to "gap", escaping unprintable characters.
9558 * Changes NL to \n, CR to \r, etc.
9559 */
9560 static void
9561ga_concat_esc(garray_T *gap, char_u *str)
9562{
9563 char_u *p;
9564 char_u buf[NUMBUFLEN];
9565
Bram Moolenaarf1551962016-03-15 12:55:58 +01009566 if (str == NULL)
9567 {
9568 ga_concat(gap, (char_u *)"NULL");
9569 return;
9570 }
9571
Bram Moolenaar23689172016-02-15 22:37:37 +01009572 for (p = str; *p != NUL; ++p)
9573 switch (*p)
9574 {
9575 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9576 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9577 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9578 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9579 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9580 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9581 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9582 default:
9583 if (*p < ' ')
9584 {
9585 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9586 ga_concat(gap, buf);
9587 }
9588 else
9589 ga_append(gap, *p);
9590 break;
9591 }
9592}
9593
9594/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009595 * Fill "gap" with information about an assert error.
9596 */
9597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009598fill_assert_error(
9599 garray_T *gap,
9600 typval_T *opt_msg_tv,
9601 char_u *exp_str,
9602 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009603 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009604 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009605{
9606 char_u numbuf[NUMBUFLEN];
9607 char_u *tofree;
9608
9609 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9610 {
9611 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9612 vim_free(tofree);
9613 }
9614 else
9615 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009616 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009617 ga_concat(gap, (char_u *)"Pattern ");
9618 else
9619 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009620 if (exp_str == NULL)
9621 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009622 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009623 vim_free(tofree);
9624 }
9625 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009626 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009627 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009628 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009629 else if (atype == ASSERT_NOTMATCH)
9630 ga_concat(gap, (char_u *)" does match ");
9631 else if (atype == ASSERT_NOTEQUAL)
9632 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009633 else
9634 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009635 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009636 vim_free(tofree);
9637 }
9638}
Bram Moolenaar43345542015-11-29 17:35:35 +01009639
9640/*
9641 * Add an assert error to v:errors.
9642 */
9643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009644assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009645{
9646 struct vimvar *vp = &vimvars[VV_ERRORS];
9647
9648 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9649 /* Make sure v:errors is a list. */
9650 set_vim_var_list(VV_ERRORS, list_alloc());
9651 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9652}
9653
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009654 static void
9655assert_equal_common(typval_T *argvars, assert_type_T atype)
9656{
9657 garray_T ga;
9658
9659 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9660 != (atype == ASSERT_EQUAL))
9661 {
9662 prepare_assert_error(&ga);
9663 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9664 atype);
9665 assert_error(&ga);
9666 ga_clear(&ga);
9667 }
9668}
9669
Bram Moolenaar43345542015-11-29 17:35:35 +01009670/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009671 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009672 */
9673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009674f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009675{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009676 assert_equal_common(argvars, ASSERT_EQUAL);
9677}
Bram Moolenaar43345542015-11-29 17:35:35 +01009678
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009679/*
9680 * "assert_notequal(expected, actual[, msg])" function
9681 */
9682 static void
9683f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9684{
9685 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009686}
9687
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009688/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009689 * "assert_exception(string[, msg])" function
9690 */
9691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009692f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009693{
9694 garray_T ga;
9695 char *error;
9696
9697 error = (char *)get_tv_string_chk(&argvars[0]);
9698 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9699 {
9700 prepare_assert_error(&ga);
9701 ga_concat(&ga, (char_u *)"v:exception is not set");
9702 assert_error(&ga);
9703 ga_clear(&ga);
9704 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009705 else if (error != NULL
9706 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009707 {
9708 prepare_assert_error(&ga);
9709 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009710 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009711 assert_error(&ga);
9712 ga_clear(&ga);
9713 }
9714}
9715
9716/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009717 * "assert_fails(cmd [, error])" function
9718 */
9719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009720f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009721{
9722 char_u *cmd = get_tv_string_chk(&argvars[0]);
9723 garray_T ga;
9724
9725 called_emsg = FALSE;
9726 suppress_errthrow = TRUE;
9727 emsg_silent = TRUE;
9728 do_cmdline_cmd(cmd);
9729 if (!called_emsg)
9730 {
9731 prepare_assert_error(&ga);
9732 ga_concat(&ga, (char_u *)"command did not fail: ");
9733 ga_concat(&ga, cmd);
9734 assert_error(&ga);
9735 ga_clear(&ga);
9736 }
9737 else if (argvars[1].v_type != VAR_UNKNOWN)
9738 {
9739 char_u buf[NUMBUFLEN];
9740 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9741
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009742 if (error == NULL
9743 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009744 {
9745 prepare_assert_error(&ga);
9746 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009747 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009748 assert_error(&ga);
9749 ga_clear(&ga);
9750 }
9751 }
9752
9753 called_emsg = FALSE;
9754 suppress_errthrow = FALSE;
9755 emsg_silent = FALSE;
9756 emsg_on_display = FALSE;
9757 set_vim_var_string(VV_ERRMSG, NULL, 0);
9758}
9759
9760/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009761 * Common for assert_true() and assert_false().
9762 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009764assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009765{
9766 int error = FALSE;
9767 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009768
Bram Moolenaar37127922016-02-06 20:29:28 +01009769 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009770 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009771 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009772 if (argvars[0].v_type != VAR_NUMBER
9773 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9774 || error)
9775 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009776 prepare_assert_error(&ga);
9777 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009778 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009779 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009780 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009781 ga_clear(&ga);
9782 }
9783}
9784
9785/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009786 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009787 */
9788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009789f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009790{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009791 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009792}
9793
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009794 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009795assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009796{
9797 garray_T ga;
9798 char_u buf1[NUMBUFLEN];
9799 char_u buf2[NUMBUFLEN];
9800 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9801 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9802
Bram Moolenaar72188e92016-03-28 22:48:29 +02009803 if (pat == NULL || text == NULL)
9804 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009805 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009806 {
9807 prepare_assert_error(&ga);
9808 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009809 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009810 assert_error(&ga);
9811 ga_clear(&ga);
9812 }
9813}
9814
9815/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009816 * "assert_match(pattern, actual[, msg])" function
9817 */
9818 static void
9819f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9820{
9821 assert_match_common(argvars, ASSERT_MATCH);
9822}
9823
9824/*
9825 * "assert_notmatch(pattern, actual[, msg])" function
9826 */
9827 static void
9828f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9829{
9830 assert_match_common(argvars, ASSERT_NOTMATCH);
9831}
9832
9833/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009834 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009835 */
9836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009837f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009838{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009839 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009840}
9841
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009842#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009843/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009844 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009845 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009847f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009848{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009849 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009850
9851 rettv->v_type = VAR_FLOAT;
9852 if (get_float_arg(argvars, &f) == OK)
9853 rettv->vval.v_float = asin(f);
9854 else
9855 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009856}
9857
9858/*
9859 * "atan()" function
9860 */
9861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009862f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009863{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009864 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009865
9866 rettv->v_type = VAR_FLOAT;
9867 if (get_float_arg(argvars, &f) == OK)
9868 rettv->vval.v_float = atan(f);
9869 else
9870 rettv->vval.v_float = 0.0;
9871}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009872
9873/*
9874 * "atan2()" function
9875 */
9876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009877f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009878{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009879 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009880
9881 rettv->v_type = VAR_FLOAT;
9882 if (get_float_arg(argvars, &fx) == OK
9883 && get_float_arg(&argvars[1], &fy) == OK)
9884 rettv->vval.v_float = atan2(fx, fy);
9885 else
9886 rettv->vval.v_float = 0.0;
9887}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009888#endif
9889
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890/*
9891 * "browse(save, title, initdir, default)" function
9892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009894f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
9896#ifdef FEAT_BROWSE
9897 int save;
9898 char_u *title;
9899 char_u *initdir;
9900 char_u *defname;
9901 char_u buf[NUMBUFLEN];
9902 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009903 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009905 save = get_tv_number_chk(&argvars[0], &error);
9906 title = get_tv_string_chk(&argvars[1]);
9907 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9908 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009910 if (error || title == NULL || initdir == NULL || defname == NULL)
9911 rettv->vval.v_string = NULL;
9912 else
9913 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009914 do_browse(save ? BROWSE_SAVE : 0,
9915 title, defname, NULL, initdir, NULL, curbuf);
9916#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009918#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009920}
9921
9922/*
9923 * "browsedir(title, initdir)" function
9924 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009926f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009927{
9928#ifdef FEAT_BROWSE
9929 char_u *title;
9930 char_u *initdir;
9931 char_u buf[NUMBUFLEN];
9932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 title = get_tv_string_chk(&argvars[0]);
9934 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009936 if (title == NULL || initdir == NULL)
9937 rettv->vval.v_string = NULL;
9938 else
9939 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009940 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945}
9946
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009947static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009948
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949/*
9950 * Find a buffer by number or exact name.
9951 */
9952 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009953find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954{
9955 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009957 if (avar->v_type == VAR_NUMBER)
9958 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009959 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009961 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009962 if (buf == NULL)
9963 {
9964 /* No full path name match, try a match with a URL or a "nofile"
9965 * buffer, these don't use the full path. */
9966 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9967 if (buf->b_fname != NULL
9968 && (path_with_url(buf->b_fname)
9969#ifdef FEAT_QUICKFIX
9970 || bt_nofile(buf)
9971#endif
9972 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009973 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009974 break;
9975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 }
9977 return buf;
9978}
9979
9980/*
9981 * "bufexists(expr)" function
9982 */
9983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009984f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987}
9988
9989/*
9990 * "buflisted(expr)" function
9991 */
9992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009993f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994{
9995 buf_T *buf;
9996
9997 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999}
10000
10001/*
10002 * "bufloaded(expr)" function
10003 */
10004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010005f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 buf_T *buf;
10008
10009 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010010 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011}
10012
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010013 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010014buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 int save_magic;
10017 char_u *save_cpo;
10018 buf_T *buf;
10019
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10021 save_magic = p_magic;
10022 p_magic = TRUE;
10023 save_cpo = p_cpo;
10024 p_cpo = (char_u *)"";
10025
10026 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010027 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028
10029 p_magic = save_magic;
10030 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010031 return buf;
10032}
10033
10034/*
10035 * Get buffer by number or pattern.
10036 */
10037 static buf_T *
10038get_buf_tv(typval_T *tv, int curtab_only)
10039{
10040 char_u *name = tv->vval.v_string;
10041 buf_T *buf;
10042
10043 if (tv->v_type == VAR_NUMBER)
10044 return buflist_findnr((int)tv->vval.v_number);
10045 if (tv->v_type != VAR_STRING)
10046 return NULL;
10047 if (name == NULL || *name == NUL)
10048 return curbuf;
10049 if (name[0] == '$' && name[1] == NUL)
10050 return lastbuf;
10051
10052 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053
10054 /* If not found, try expanding the name, like done for bufexists(). */
10055 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
10058 return buf;
10059}
10060
10061/*
10062 * "bufname(expr)" function
10063 */
10064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010065f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066{
10067 buf_T *buf;
10068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010071 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 --emsg_off;
10078}
10079
10080/*
10081 * "bufnr(expr)" function
10082 */
10083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010084f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085{
10086 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010087 int error = FALSE;
10088 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010092 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010093 --emsg_off;
10094
10095 /* If the buffer isn't found and the second argument is not zero create a
10096 * new buffer. */
10097 if (buf == NULL
10098 && argvars[1].v_type != VAR_UNKNOWN
10099 && get_tv_number_chk(&argvars[1], &error) != 0
10100 && !error
10101 && (name = get_tv_string_chk(&argvars[0])) != NULL
10102 && !error)
10103 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10104
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109}
10110
10111/*
10112 * "bufwinnr(nr)" function
10113 */
10114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010115f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116{
10117#ifdef FEAT_WINDOWS
10118 win_T *wp;
10119 int winnr = 0;
10120#endif
10121 buf_T *buf;
10122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010125 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126#ifdef FEAT_WINDOWS
10127 for (wp = firstwin; wp; wp = wp->w_next)
10128 {
10129 ++winnr;
10130 if (wp->w_buffer == buf)
10131 break;
10132 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010133 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136#endif
10137 --emsg_off;
10138}
10139
10140/*
10141 * "byte2line(byte)" function
10142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010144f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145{
10146#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148#else
10149 long boff = 0;
10150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010151 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 (linenr_T)0, &boff);
10157#endif
10158}
10159
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010161byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010162{
10163#ifdef FEAT_MBYTE
10164 char_u *t;
10165#endif
10166 char_u *str;
10167 long idx;
10168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010169 str = get_tv_string_chk(&argvars[0]);
10170 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010172 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010173 return;
10174
10175#ifdef FEAT_MBYTE
10176 t = str;
10177 for ( ; idx > 0; idx--)
10178 {
10179 if (*t == NUL) /* EOL reached */
10180 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010181 if (enc_utf8 && comp)
10182 t += utf_ptr2len(t);
10183 else
10184 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010185 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010186 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010187#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010188 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010190#endif
10191}
10192
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010193/*
10194 * "byteidx()" function
10195 */
10196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010197f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010198{
10199 byteidx(argvars, rettv, FALSE);
10200}
10201
10202/*
10203 * "byteidxcomp()" function
10204 */
10205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010206f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010207{
10208 byteidx(argvars, rettv, TRUE);
10209}
10210
Bram Moolenaardb913952012-06-29 12:54:53 +020010211 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010212func_call(
10213 char_u *name,
10214 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010215 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010216 dict_T *selfdict,
10217 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010218{
10219 listitem_T *item;
10220 typval_T argv[MAX_FUNC_ARGS + 1];
10221 int argc = 0;
10222 int dummy;
10223 int r = 0;
10224
10225 for (item = args->vval.v_list->lv_first; item != NULL;
10226 item = item->li_next)
10227 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010228 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010229 {
10230 EMSG(_("E699: Too many arguments"));
10231 break;
10232 }
10233 /* Make a copy of each argument. This is needed to be able to set
10234 * v_lock to VAR_FIXED in the copy without changing the original list.
10235 */
10236 copy_tv(&item->li_tv, &argv[argc++]);
10237 }
10238
10239 if (item == NULL)
10240 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10241 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010242 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010243
10244 /* Free the arguments. */
10245 while (argc > 0)
10246 clear_tv(&argv[--argc]);
10247
10248 return r;
10249}
10250
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010251/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010252 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010253 */
10254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010255f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010256{
10257 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010258 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010259 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010260
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010261 if (argvars[1].v_type != VAR_LIST)
10262 {
10263 EMSG(_(e_listreq));
10264 return;
10265 }
10266 if (argvars[1].vval.v_list == NULL)
10267 return;
10268
10269 if (argvars[0].v_type == VAR_FUNC)
10270 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010271 else if (argvars[0].v_type == VAR_PARTIAL)
10272 {
10273 partial = argvars[0].vval.v_partial;
10274 func = partial->pt_name;
10275 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276 else
10277 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010278 if (*func == NUL)
10279 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010280
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 if (argvars[2].v_type != VAR_UNKNOWN)
10282 {
10283 if (argvars[2].v_type != VAR_DICT)
10284 {
10285 EMSG(_(e_dictreq));
10286 return;
10287 }
10288 selfdict = argvars[2].vval.v_dict;
10289 }
10290
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010291 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010292}
10293
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010294#ifdef FEAT_FLOAT
10295/*
10296 * "ceil({float})" function
10297 */
10298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010299f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010300{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010301 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010302
10303 rettv->v_type = VAR_FLOAT;
10304 if (get_float_arg(argvars, &f) == OK)
10305 rettv->vval.v_float = ceil(f);
10306 else
10307 rettv->vval.v_float = 0.0;
10308}
10309#endif
10310
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010311#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010312/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010313 * "ch_close()" function
10314 */
10315 static void
10316f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10317{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010318 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010319
10320 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010321 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010322 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010323 channel_clear(channel);
10324 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010325}
10326
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010327/*
10328 * "ch_getbufnr()" function
10329 */
10330 static void
10331f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10332{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010333 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010334
10335 rettv->vval.v_number = -1;
10336 if (channel != NULL)
10337 {
10338 char_u *what = get_tv_string(&argvars[1]);
10339 int part;
10340
10341 if (STRCMP(what, "err") == 0)
10342 part = PART_ERR;
10343 else if (STRCMP(what, "out") == 0)
10344 part = PART_OUT;
10345 else if (STRCMP(what, "in") == 0)
10346 part = PART_IN;
10347 else
10348 part = PART_SOCK;
10349 if (channel->ch_part[part].ch_buffer != NULL)
10350 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10351 }
10352}
10353
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010354/*
10355 * "ch_getjob()" function
10356 */
10357 static void
10358f_ch_getjob(typval_T *argvars, typval_T *rettv)
10359{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010360 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010361
10362 if (channel != NULL)
10363 {
10364 rettv->v_type = VAR_JOB;
10365 rettv->vval.v_job = channel->ch_job;
10366 if (channel->ch_job != NULL)
10367 ++channel->ch_job->jv_refcount;
10368 }
10369}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010370
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010371/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010372 * "ch_info()" function
10373 */
10374 static void
10375f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10376{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010377 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010378
10379 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10380 channel_info(channel, rettv->vval.v_dict);
10381}
10382
10383/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010384 * "ch_log()" function
10385 */
10386 static void
10387f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10388{
10389 char_u *msg = get_tv_string(&argvars[0]);
10390 channel_T *channel = NULL;
10391
10392 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010393 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010394
10395 ch_log(channel, (char *)msg);
10396}
10397
10398/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010399 * "ch_logfile()" function
10400 */
10401 static void
10402f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10403{
10404 char_u *fname;
10405 char_u *opt = (char_u *)"";
10406 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010407
10408 fname = get_tv_string(&argvars[0]);
10409 if (argvars[1].v_type == VAR_STRING)
10410 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010411 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010412}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010413
10414/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010415 * "ch_open()" function
10416 */
10417 static void
10418f_ch_open(typval_T *argvars, typval_T *rettv)
10419{
Bram Moolenaar77073442016-02-13 23:23:53 +010010420 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010421 if (check_restricted() || check_secure())
10422 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010423 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010424}
10425
10426/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010427 * "ch_read()" function
10428 */
10429 static void
10430f_ch_read(typval_T *argvars, typval_T *rettv)
10431{
10432 common_channel_read(argvars, rettv, FALSE);
10433}
10434
10435/*
10436 * "ch_readraw()" function
10437 */
10438 static void
10439f_ch_readraw(typval_T *argvars, typval_T *rettv)
10440{
10441 common_channel_read(argvars, rettv, TRUE);
10442}
10443
10444/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010445 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010446 */
10447 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010448f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10449{
10450 ch_expr_common(argvars, rettv, TRUE);
10451}
10452
10453/*
10454 * "ch_sendexpr()" function
10455 */
10456 static void
10457f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10458{
10459 ch_expr_common(argvars, rettv, FALSE);
10460}
10461
10462/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010463 * "ch_evalraw()" function
10464 */
10465 static void
10466f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10467{
10468 ch_raw_common(argvars, rettv, TRUE);
10469}
10470
10471/*
10472 * "ch_sendraw()" function
10473 */
10474 static void
10475f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10476{
10477 ch_raw_common(argvars, rettv, FALSE);
10478}
10479
10480/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010481 * "ch_setoptions()" function
10482 */
10483 static void
10484f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10485{
10486 channel_T *channel;
10487 jobopt_T opt;
10488
Bram Moolenaar437905c2016-04-26 19:01:05 +020010489 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010490 if (channel == NULL)
10491 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010492 clear_job_options(&opt);
10493 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010494 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10495 channel_set_options(channel, &opt);
10496 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010497}
10498
10499/*
10500 * "ch_status()" function
10501 */
10502 static void
10503f_ch_status(typval_T *argvars, typval_T *rettv)
10504{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010505 channel_T *channel;
10506
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010507 /* return an empty string by default */
10508 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010509 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010510
Bram Moolenaar437905c2016-04-26 19:01:05 +020010511 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010512 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010513}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010514#endif
10515
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010516/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010517 * "changenr()" function
10518 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010520f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010521{
10522 rettv->vval.v_number = curbuf->b_u_seq_cur;
10523}
10524
10525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 * "char2nr(string)" function
10527 */
10528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010529f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530{
10531#ifdef FEAT_MBYTE
10532 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010533 {
10534 int utf8 = 0;
10535
10536 if (argvars[1].v_type != VAR_UNKNOWN)
10537 utf8 = get_tv_number_chk(&argvars[1], NULL);
10538
10539 if (utf8)
10540 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10541 else
10542 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 else
10545#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547}
10548
10549/*
10550 * "cindent(lnum)" function
10551 */
10552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010553f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554{
10555#ifdef FEAT_CINDENT
10556 pos_T pos;
10557 linenr_T lnum;
10558
10559 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010560 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10562 {
10563 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010564 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 curwin->w_cursor = pos;
10566 }
10567 else
10568#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010569 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570}
10571
10572/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010573 * "clearmatches()" function
10574 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010576f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010577{
10578#ifdef FEAT_SEARCH_EXTRA
10579 clear_matches(curwin);
10580#endif
10581}
10582
10583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 * "col(string)" function
10585 */
10586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010587f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
10589 colnr_T col = 0;
10590 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010591 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010593 fp = var2fpos(&argvars[0], FALSE, &fnum);
10594 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 {
10596 if (fp->col == MAXCOL)
10597 {
10598 /* '> can be MAXCOL, get the length of the line then */
10599 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010600 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 else
10602 col = MAXCOL;
10603 }
10604 else
10605 {
10606 col = fp->col + 1;
10607#ifdef FEAT_VIRTUALEDIT
10608 /* col(".") when the cursor is on the NUL at the end of the line
10609 * because of "coladd" can be seen as an extra column. */
10610 if (virtual_active() && fp == &curwin->w_cursor)
10611 {
10612 char_u *p = ml_get_cursor();
10613
10614 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10615 curwin->w_virtcol - curwin->w_cursor.coladd))
10616 {
10617# ifdef FEAT_MBYTE
10618 int l;
10619
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010620 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 col += l;
10622# else
10623 if (*p != NUL && p[1] == NUL)
10624 ++col;
10625# endif
10626 }
10627 }
10628#endif
10629 }
10630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632}
10633
Bram Moolenaar572cb562005-08-05 21:35:02 +000010634#if defined(FEAT_INS_EXPAND)
10635/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010636 * "complete()" function
10637 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010639f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010640{
10641 int startcol;
10642
10643 if ((State & INSERT) == 0)
10644 {
10645 EMSG(_("E785: complete() can only be used in Insert mode"));
10646 return;
10647 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010648
10649 /* Check for undo allowed here, because if something was already inserted
10650 * the line was already saved for undo and this check isn't done. */
10651 if (!undo_allowed())
10652 return;
10653
Bram Moolenaarade00832006-03-10 21:46:58 +000010654 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10655 {
10656 EMSG(_(e_invarg));
10657 return;
10658 }
10659
10660 startcol = get_tv_number_chk(&argvars[0], NULL);
10661 if (startcol <= 0)
10662 return;
10663
10664 set_completion(startcol - 1, argvars[1].vval.v_list);
10665}
10666
10667/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010668 * "complete_add()" function
10669 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010671f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010672{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010673 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010674}
10675
10676/*
10677 * "complete_check()" function
10678 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010680f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010681{
10682 int saved = RedrawingDisabled;
10683
10684 RedrawingDisabled = 0;
10685 ins_compl_check_keys(0);
10686 rettv->vval.v_number = compl_interrupted;
10687 RedrawingDisabled = saved;
10688}
10689#endif
10690
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691/*
10692 * "confirm(message, buttons[, default [, type]])" function
10693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010695f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696{
10697#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10698 char_u *message;
10699 char_u *buttons = NULL;
10700 char_u buf[NUMBUFLEN];
10701 char_u buf2[NUMBUFLEN];
10702 int def = 1;
10703 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010704 char_u *typestr;
10705 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 message = get_tv_string_chk(&argvars[0]);
10708 if (message == NULL)
10709 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010710 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010712 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10713 if (buttons == NULL)
10714 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010715 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010717 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010718 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010720 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10721 if (typestr == NULL)
10722 error = TRUE;
10723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010725 switch (TOUPPER_ASC(*typestr))
10726 {
10727 case 'E': type = VIM_ERROR; break;
10728 case 'Q': type = VIM_QUESTION; break;
10729 case 'I': type = VIM_INFO; break;
10730 case 'W': type = VIM_WARNING; break;
10731 case 'G': type = VIM_GENERIC; break;
10732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 }
10734 }
10735 }
10736 }
10737
10738 if (buttons == NULL || *buttons == NUL)
10739 buttons = (char_u *)_("&Ok");
10740
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010741 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010742 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010743 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744#endif
10745}
10746
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010747/*
10748 * "copy()" function
10749 */
10750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010751f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010752{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010753 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010754}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010756#ifdef FEAT_FLOAT
10757/*
10758 * "cos()" function
10759 */
10760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010761f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010762{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010763 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010764
10765 rettv->v_type = VAR_FLOAT;
10766 if (get_float_arg(argvars, &f) == OK)
10767 rettv->vval.v_float = cos(f);
10768 else
10769 rettv->vval.v_float = 0.0;
10770}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010771
10772/*
10773 * "cosh()" function
10774 */
10775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010776f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010777{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010778 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010779
10780 rettv->v_type = VAR_FLOAT;
10781 if (get_float_arg(argvars, &f) == OK)
10782 rettv->vval.v_float = cosh(f);
10783 else
10784 rettv->vval.v_float = 0.0;
10785}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010786#endif
10787
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010789 * "count()" function
10790 */
10791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010792f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010793{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010794 long n = 0;
10795 int ic = FALSE;
10796
Bram Moolenaare9a41262005-01-15 22:18:47 +000010797 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010798 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010799 listitem_T *li;
10800 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010801 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010802
Bram Moolenaare9a41262005-01-15 22:18:47 +000010803 if ((l = argvars[0].vval.v_list) != NULL)
10804 {
10805 li = l->lv_first;
10806 if (argvars[2].v_type != VAR_UNKNOWN)
10807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010808 int error = FALSE;
10809
10810 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010811 if (argvars[3].v_type != VAR_UNKNOWN)
10812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010813 idx = get_tv_number_chk(&argvars[3], &error);
10814 if (!error)
10815 {
10816 li = list_find(l, idx);
10817 if (li == NULL)
10818 EMSGN(_(e_listidx), idx);
10819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010821 if (error)
10822 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010823 }
10824
10825 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010826 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010827 ++n;
10828 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010829 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010830 else if (argvars[0].v_type == VAR_DICT)
10831 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 int todo;
10833 dict_T *d;
10834 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010836 if ((d = argvars[0].vval.v_dict) != NULL)
10837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010838 int error = FALSE;
10839
Bram Moolenaare9a41262005-01-15 22:18:47 +000010840 if (argvars[2].v_type != VAR_UNKNOWN)
10841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010843 if (argvars[3].v_type != VAR_UNKNOWN)
10844 EMSG(_(e_invarg));
10845 }
10846
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010847 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010848 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010849 {
10850 if (!HASHITEM_EMPTY(hi))
10851 {
10852 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010853 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010854 ++n;
10855 }
10856 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010857 }
10858 }
10859 else
10860 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010861 rettv->vval.v_number = n;
10862}
10863
10864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10866 *
10867 * Checks the existence of a cscope connection.
10868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010870f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871{
10872#ifdef FEAT_CSCOPE
10873 int num = 0;
10874 char_u *dbpath = NULL;
10875 char_u *prepend = NULL;
10876 char_u buf[NUMBUFLEN];
10877
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010878 if (argvars[0].v_type != VAR_UNKNOWN
10879 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 num = (int)get_tv_number(&argvars[0]);
10882 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010883 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 }
10886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888#endif
10889}
10890
10891/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010892 * "cursor(lnum, col)" function, or
10893 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010895 * Moves the cursor to the specified line and column.
10896 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010899f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010902#ifdef FEAT_VIRTUALEDIT
10903 long coladd = 0;
10904#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010905 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010907 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010908 if (argvars[1].v_type == VAR_UNKNOWN)
10909 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010910 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010911 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010912
Bram Moolenaar493c1782014-05-28 14:34:46 +020010913 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010914 {
10915 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010916 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010917 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010918 line = pos.lnum;
10919 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010920#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010921 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010922#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010923 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010924 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010925 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010926 set_curswant = FALSE;
10927 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010928 }
10929 else
10930 {
10931 line = get_tv_lnum(argvars);
10932 col = get_tv_number_chk(&argvars[1], NULL);
10933#ifdef FEAT_VIRTUALEDIT
10934 if (argvars[2].v_type != VAR_UNKNOWN)
10935 coladd = get_tv_number_chk(&argvars[2], NULL);
10936#endif
10937 }
10938 if (line < 0 || col < 0
10939#ifdef FEAT_VIRTUALEDIT
10940 || coladd < 0
10941#endif
10942 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010943 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 if (line > 0)
10945 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 if (col > 0)
10947 curwin->w_cursor.col = col - 1;
10948#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010949 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950#endif
10951
10952 /* Make sure the cursor is in a valid position. */
10953 check_cursor();
10954#ifdef FEAT_MBYTE
10955 /* Correct cursor for multi-byte character. */
10956 if (has_mbyte)
10957 mb_adjust_cursor();
10958#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010959
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010960 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010961 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962}
10963
10964/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010965 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 */
10967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010968f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010970 int noref = 0;
10971
10972 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010973 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010974 if (noref < 0 || noref > 1)
10975 EMSG(_(e_invarg));
10976 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010977 {
10978 current_copyID += COPYID_INC;
10979 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981}
10982
10983/*
10984 * "delete()" function
10985 */
10986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010987f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010989 char_u nbuf[NUMBUFLEN];
10990 char_u *name;
10991 char_u *flags;
10992
10993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010995 return;
10996
10997 name = get_tv_string(&argvars[0]);
10998 if (name == NULL || *name == NUL)
10999 {
11000 EMSG(_(e_invarg));
11001 return;
11002 }
11003
11004 if (argvars[1].v_type != VAR_UNKNOWN)
11005 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011007 flags = (char_u *)"";
11008
11009 if (*flags == NUL)
11010 /* delete a file */
11011 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11012 else if (STRCMP(flags, "d") == 0)
11013 /* delete an empty directory */
11014 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11015 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011016 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011017 rettv->vval.v_number = delete_recursive(name);
11018 else
11019 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020}
11021
11022/*
11023 * "did_filetype()" function
11024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011026f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027{
11028#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030#endif
11031}
11032
11033/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011034 * "diff_filler()" function
11035 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011037f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011038{
11039#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011041#endif
11042}
11043
11044/*
11045 * "diff_hlID()" function
11046 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011048f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011049{
11050#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011052 static linenr_T prev_lnum = 0;
11053 static int changedtick = 0;
11054 static int fnum = 0;
11055 static int change_start = 0;
11056 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011057 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011058 int filler_lines;
11059 int col;
11060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 if (lnum < 0) /* ignore type error in {lnum} arg */
11062 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011063 if (lnum != prev_lnum
11064 || changedtick != curbuf->b_changedtick
11065 || fnum != curbuf->b_fnum)
11066 {
11067 /* New line, buffer, change: need to get the values. */
11068 filler_lines = diff_check(curwin, lnum);
11069 if (filler_lines < 0)
11070 {
11071 if (filler_lines == -1)
11072 {
11073 change_start = MAXCOL;
11074 change_end = -1;
11075 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11076 hlID = HLF_ADD; /* added line */
11077 else
11078 hlID = HLF_CHD; /* changed line */
11079 }
11080 else
11081 hlID = HLF_ADD; /* added line */
11082 }
11083 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011084 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011085 prev_lnum = lnum;
11086 changedtick = curbuf->b_changedtick;
11087 fnum = curbuf->b_fnum;
11088 }
11089
11090 if (hlID == HLF_CHD || hlID == HLF_TXD)
11091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011093 if (col >= change_start && col <= change_end)
11094 hlID = HLF_TXD; /* changed text */
11095 else
11096 hlID = HLF_CHD; /* changed line */
11097 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011098 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011099#endif
11100}
11101
11102/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011103 * "empty({expr})" function
11104 */
11105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011106f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011107{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011108 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011109
11110 switch (argvars[0].v_type)
11111 {
11112 case VAR_STRING:
11113 case VAR_FUNC:
11114 n = argvars[0].vval.v_string == NULL
11115 || *argvars[0].vval.v_string == NUL;
11116 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011117 case VAR_PARTIAL:
11118 n = FALSE;
11119 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011120 case VAR_NUMBER:
11121 n = argvars[0].vval.v_number == 0;
11122 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011123 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011124#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011125 n = argvars[0].vval.v_float == 0.0;
11126 break;
11127#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011128 case VAR_LIST:
11129 n = argvars[0].vval.v_list == NULL
11130 || argvars[0].vval.v_list->lv_first == NULL;
11131 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 case VAR_DICT:
11133 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011134 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011135 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011136 case VAR_SPECIAL:
11137 n = argvars[0].vval.v_number != VVAL_TRUE;
11138 break;
11139
Bram Moolenaar835dc632016-02-07 14:27:38 +010011140 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011141#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011142 n = argvars[0].vval.v_job == NULL
11143 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11144 break;
11145#endif
11146 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011147#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011148 n = argvars[0].vval.v_channel == NULL
11149 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011150 break;
11151#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011152 case VAR_UNKNOWN:
11153 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11154 n = TRUE;
11155 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011156 }
11157
11158 rettv->vval.v_number = n;
11159}
11160
11161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 * "escape({string}, {chars})" function
11163 */
11164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011165f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166{
11167 char_u buf[NUMBUFLEN];
11168
Bram Moolenaar758711c2005-02-02 23:11:38 +000011169 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11170 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172}
11173
11174/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011175 * "eval()" function
11176 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011178f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011179{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011180 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011182 s = get_tv_string_chk(&argvars[0]);
11183 if (s != NULL)
11184 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011185
Bram Moolenaar615b9972015-01-14 17:15:05 +010011186 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11188 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011189 if (p != NULL && !aborting())
11190 EMSG2(_(e_invexpr2), p);
11191 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011193 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011195 else if (*s != NUL)
11196 EMSG(_(e_trailing));
11197}
11198
11199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 * "eventhandler()" function
11201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011203f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206}
11207
11208/*
11209 * "executable()" function
11210 */
11211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011212f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011214 char_u *name = get_tv_string(&argvars[0]);
11215
11216 /* Check in $PATH and also check directly if there is a directory name. */
11217 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11218 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011219}
11220
11221/*
11222 * "exepath()" function
11223 */
11224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011225f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011226{
11227 char_u *p = NULL;
11228
Bram Moolenaarb5971142015-03-21 17:32:19 +010011229 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011230 rettv->v_type = VAR_STRING;
11231 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232}
11233
11234/*
11235 * "exists()" function
11236 */
11237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011238f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239{
11240 char_u *p;
11241 char_u *name;
11242 int n = FALSE;
11243 int len = 0;
11244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 if (*p == '$') /* environment variable */
11247 {
11248 /* first try "normal" environment variables (fast) */
11249 if (mch_getenv(p + 1) != NULL)
11250 n = TRUE;
11251 else
11252 {
11253 /* try expanding things like $VIM and ${HOME} */
11254 p = expand_env_save(p);
11255 if (p != NULL && *p != '$')
11256 n = TRUE;
11257 vim_free(p);
11258 }
11259 }
11260 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011263 if (*skipwhite(p) != NUL)
11264 n = FALSE; /* trailing garbage */
11265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 else if (*p == '*') /* internal or user defined function */
11267 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011268 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 }
11270 else if (*p == ':')
11271 {
11272 n = cmd_exists(p + 1);
11273 }
11274 else if (*p == '#')
11275 {
11276#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011277 if (p[1] == '#')
11278 n = autocmd_supported(p + 2);
11279 else
11280 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281#endif
11282 }
11283 else /* internal variable */
11284 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011285 char_u *tofree;
11286 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011288 /* get_name_len() takes care of expanding curly braces */
11289 name = p;
11290 len = get_name_len(&p, &tofree, TRUE, FALSE);
11291 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011293 if (tofree != NULL)
11294 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011295 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011296 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011298 /* handle d.key, l[idx], f(expr) */
11299 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11300 if (n)
11301 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 }
11303 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011304 if (*p != NUL)
11305 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011307 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 }
11309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011310 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311}
11312
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011313#ifdef FEAT_FLOAT
11314/*
11315 * "exp()" function
11316 */
11317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011318f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011319{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011320 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011321
11322 rettv->v_type = VAR_FLOAT;
11323 if (get_float_arg(argvars, &f) == OK)
11324 rettv->vval.v_float = exp(f);
11325 else
11326 rettv->vval.v_float = 0.0;
11327}
11328#endif
11329
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330/*
11331 * "expand()" function
11332 */
11333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011334f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335{
11336 char_u *s;
11337 int len;
11338 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011339 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011341 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011342 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011345 if (argvars[1].v_type != VAR_UNKNOWN
11346 && argvars[2].v_type != VAR_UNKNOWN
11347 && get_tv_number_chk(&argvars[2], &error)
11348 && !error)
11349 {
11350 rettv->v_type = VAR_LIST;
11351 rettv->vval.v_list = NULL;
11352 }
11353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 if (*s == '%' || *s == '#' || *s == '<')
11356 {
11357 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011358 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011360 if (rettv->v_type == VAR_LIST)
11361 {
11362 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11363 list_append_string(rettv->vval.v_list, result, -1);
11364 else
11365 vim_free(result);
11366 }
11367 else
11368 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 }
11370 else
11371 {
11372 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011373 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011374 if (argvars[1].v_type != VAR_UNKNOWN
11375 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011376 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377 if (!error)
11378 {
11379 ExpandInit(&xpc);
11380 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011381 if (p_wic)
11382 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011383 if (rettv->v_type == VAR_STRING)
11384 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11385 options, WILD_ALL);
11386 else if (rettv_list_alloc(rettv) != FAIL)
11387 {
11388 int i;
11389
11390 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11391 for (i = 0; i < xpc.xp_numfiles; i++)
11392 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11393 ExpandCleanup(&xpc);
11394 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 }
11396 else
11397 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 }
11399}
11400
11401/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011402 * Go over all entries in "d2" and add them to "d1".
11403 * When "action" is "error" then a duplicate key is an error.
11404 * When "action" is "force" then a duplicate key is overwritten.
11405 * Otherwise duplicate keys are ignored ("action" is "keep").
11406 */
11407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011408dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011409{
11410 dictitem_T *di1;
11411 hashitem_T *hi2;
11412 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011413 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011414
11415 todo = (int)d2->dv_hashtab.ht_used;
11416 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11417 {
11418 if (!HASHITEM_EMPTY(hi2))
11419 {
11420 --todo;
11421 di1 = dict_find(d1, hi2->hi_key, -1);
11422 if (d1->dv_scope != 0)
11423 {
11424 /* Disallow replacing a builtin function in l: and g:.
11425 * Check the key to be valid when adding to any
11426 * scope. */
11427 if (d1->dv_scope == VAR_DEF_SCOPE
11428 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11429 && var_check_func_name(hi2->hi_key,
11430 di1 == NULL))
11431 break;
11432 if (!valid_varname(hi2->hi_key))
11433 break;
11434 }
11435 if (di1 == NULL)
11436 {
11437 di1 = dictitem_copy(HI2DI(hi2));
11438 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11439 dictitem_free(di1);
11440 }
11441 else if (*action == 'e')
11442 {
11443 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11444 break;
11445 }
11446 else if (*action == 'f' && HI2DI(hi2) != di1)
11447 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011448 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11449 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011450 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011451 clear_tv(&di1->di_tv);
11452 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11453 }
11454 }
11455 }
11456}
11457
11458/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011459 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011460 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011461 */
11462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011463f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011464{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011465 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011466
Bram Moolenaare9a41262005-01-15 22:18:47 +000011467 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011468 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011469 list_T *l1, *l2;
11470 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011471 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011472 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011473
Bram Moolenaare9a41262005-01-15 22:18:47 +000011474 l1 = argvars[0].vval.v_list;
11475 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011476 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011477 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011478 {
11479 if (argvars[2].v_type != VAR_UNKNOWN)
11480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011481 before = get_tv_number_chk(&argvars[2], &error);
11482 if (error)
11483 return; /* type error; errmsg already given */
11484
Bram Moolenaar758711c2005-02-02 23:11:38 +000011485 if (before == l1->lv_len)
11486 item = NULL;
11487 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011488 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011489 item = list_find(l1, before);
11490 if (item == NULL)
11491 {
11492 EMSGN(_(e_listidx), before);
11493 return;
11494 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011495 }
11496 }
11497 else
11498 item = NULL;
11499 list_extend(l1, l2, item);
11500
Bram Moolenaare9a41262005-01-15 22:18:47 +000011501 copy_tv(&argvars[0], rettv);
11502 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011503 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011504 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11505 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011506 dict_T *d1, *d2;
11507 char_u *action;
11508 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011509
11510 d1 = argvars[0].vval.v_dict;
11511 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011512 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011513 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011514 {
11515 /* Check the third argument. */
11516 if (argvars[2].v_type != VAR_UNKNOWN)
11517 {
11518 static char *(av[]) = {"keep", "force", "error"};
11519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011520 action = get_tv_string_chk(&argvars[2]);
11521 if (action == NULL)
11522 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011523 for (i = 0; i < 3; ++i)
11524 if (STRCMP(action, av[i]) == 0)
11525 break;
11526 if (i == 3)
11527 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011528 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011529 return;
11530 }
11531 }
11532 else
11533 action = (char_u *)"force";
11534
Bram Moolenaara9922d62013-05-30 13:01:18 +020011535 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011536
Bram Moolenaare9a41262005-01-15 22:18:47 +000011537 copy_tv(&argvars[0], rettv);
11538 }
11539 }
11540 else
11541 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011542}
11543
11544/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011545 * "feedkeys()" function
11546 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011548f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011549{
11550 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011551 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011552 char_u *keys, *flags;
11553 char_u nbuf[NUMBUFLEN];
11554 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011555 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011556 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011557 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011558
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011559 /* This is not allowed in the sandbox. If the commands would still be
11560 * executed in the sandbox it would be OK, but it probably happens later,
11561 * when "sandbox" is no longer set. */
11562 if (check_secure())
11563 return;
11564
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011565 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011566
11567 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011568 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011569 flags = get_tv_string_buf(&argvars[1], nbuf);
11570 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011571 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011572 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011573 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011574 case 'n': remap = FALSE; break;
11575 case 'm': remap = TRUE; break;
11576 case 't': typed = TRUE; break;
11577 case 'i': insert = TRUE; break;
11578 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011579 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011580 }
11581 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011582 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011583
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011584 if (*keys != NUL || execute)
11585 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011586 /* Need to escape K_SPECIAL and CSI before putting the string in the
11587 * typeahead buffer. */
11588 keys_esc = vim_strsave_escape_csi(keys);
11589 if (keys_esc != NULL)
11590 {
11591 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011592 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011593 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011594 if (vgetc_busy)
11595 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011596 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011597 {
11598 int save_msg_scroll = msg_scroll;
11599
11600 /* Avoid a 1 second delay when the keys start Insert mode. */
11601 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011602
Bram Moolenaar245c4102016-04-20 17:37:41 +020011603 if (!dangerous)
11604 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011605 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011606 if (!dangerous)
11607 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011608 msg_scroll |= save_msg_scroll;
11609 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011610 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011611 }
11612}
11613
11614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 * "filereadable()" function
11616 */
11617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011618f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011620 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 char_u *p;
11622 int n;
11623
Bram Moolenaarc236c162008-07-13 17:41:49 +000011624#ifndef O_NONBLOCK
11625# define O_NONBLOCK 0
11626#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011628 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11629 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 {
11631 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011632 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633 }
11634 else
11635 n = FALSE;
11636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638}
11639
11640/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011641 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642 * rights to write into.
11643 */
11644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011645f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011647 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648}
11649
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011650 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011651findfilendir(
11652 typval_T *argvars UNUSED,
11653 typval_T *rettv,
11654 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011655{
11656#ifdef FEAT_SEARCHPATH
11657 char_u *fname;
11658 char_u *fresult = NULL;
11659 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11660 char_u *p;
11661 char_u pathbuf[NUMBUFLEN];
11662 int count = 1;
11663 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011664 int error = FALSE;
11665#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011666
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011667 rettv->vval.v_string = NULL;
11668 rettv->v_type = VAR_STRING;
11669
11670#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011672
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011673 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11676 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011677 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 else
11679 {
11680 if (*p != NUL)
11681 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011683 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011684 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011685 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011686 }
11687
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011688 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11689 error = TRUE;
11690
11691 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 do
11694 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011695 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011696 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011697 fresult = find_file_in_path_option(first ? fname : NULL,
11698 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011699 0, first, path,
11700 find_what,
11701 curbuf->b_ffname,
11702 find_what == FINDFILE_DIR
11703 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011704 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011705
11706 if (fresult != NULL && rettv->v_type == VAR_LIST)
11707 list_append_string(rettv->vval.v_list, fresult, -1);
11708
11709 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011711
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011712 if (rettv->v_type == VAR_STRING)
11713 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011714#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011715}
11716
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011717static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11718static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011719
11720/*
11721 * Implementation of map() and filter().
11722 */
11723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011724filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011725{
11726 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011727 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011728 listitem_T *li, *nli;
11729 list_T *l = NULL;
11730 dictitem_T *di;
11731 hashtab_T *ht;
11732 hashitem_T *hi;
11733 dict_T *d = NULL;
11734 typval_T save_val;
11735 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011736 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011737 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011738 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011739 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011740 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011741 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011742 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011743
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011745 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011746 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011747 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 return;
11749 }
11750 else if (argvars[0].v_type == VAR_DICT)
11751 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011752 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011753 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011754 return;
11755 }
11756 else
11757 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011758 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011759 return;
11760 }
11761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011762 expr = get_tv_string_buf_chk(&argvars[1], buf);
11763 /* On type errors, the preceding call has already displayed an error
11764 * message. Avoid a misleading error message for an empty string that
11765 * was not passed as argument. */
11766 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011768 prepare_vimvar(VV_VAL, &save_val);
11769 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011770
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011771 /* We reset "did_emsg" to be able to detect whether an error
11772 * occurred during evaluation of the expression. */
11773 save_did_emsg = did_emsg;
11774 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011775
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011776 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011777 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779 vimvars[VV_KEY].vv_type = VAR_STRING;
11780
11781 ht = &d->dv_hashtab;
11782 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011783 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011784 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 if (!HASHITEM_EMPTY(hi))
11787 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011788 int r;
11789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011790 --todo;
11791 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011792 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011793 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11794 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011795 break;
11796 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011797 r = filter_map_one(&di->di_tv, expr, map, &rem);
11798 clear_tv(&vimvars[VV_KEY].vv_tv);
11799 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011800 break;
11801 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011802 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011803 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11804 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011805 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011807 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 }
11809 }
11810 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011811 }
11812 else
11813 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011814 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 for (li = l->lv_first; li != NULL; li = nli)
11817 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011818 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011819 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011821 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011822 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011823 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011824 break;
11825 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011826 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011827 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011828 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011829 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011830
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011831 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011832 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011833
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011834 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011835 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011836
11837 copy_tv(&argvars[0], rettv);
11838}
11839
11840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011841filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011842{
Bram Moolenaar33570922005-01-25 22:26:29 +000011843 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011844 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011845 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011846
Bram Moolenaar33570922005-01-25 22:26:29 +000011847 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848 s = expr;
11849 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011850 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011851 if (*s != NUL) /* check for trailing chars after expr */
11852 {
11853 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011854 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011855 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011856 }
11857 if (map)
11858 {
11859 /* map(): replace the list item value */
11860 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011861 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011862 *tv = rettv;
11863 }
11864 else
11865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 int error = FALSE;
11867
Bram Moolenaare9a41262005-01-15 22:18:47 +000011868 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011869 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011871 /* On type error, nothing has been removed; return FAIL to stop the
11872 * loop. The error message was given by get_tv_number_chk(). */
11873 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011874 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011875 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011876 retval = OK;
11877theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011878 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011879 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011880}
11881
11882/*
11883 * "filter()" function
11884 */
11885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011886f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011887{
11888 filter_map(argvars, rettv, FALSE);
11889}
11890
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011891/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892 * "finddir({fname}[, {path}[, {count}]])" function
11893 */
11894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011895f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011896{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011897 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011898}
11899
11900/*
11901 * "findfile({fname}[, {path}[, {count}]])" function
11902 */
11903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011904f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011905{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011906 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011907}
11908
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011909#ifdef FEAT_FLOAT
11910/*
11911 * "float2nr({float})" function
11912 */
11913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011914f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011915{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011916 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011917
11918 if (get_float_arg(argvars, &f) == OK)
11919 {
11920 if (f < -0x7fffffff)
11921 rettv->vval.v_number = -0x7fffffff;
11922 else if (f > 0x7fffffff)
11923 rettv->vval.v_number = 0x7fffffff;
11924 else
11925 rettv->vval.v_number = (varnumber_T)f;
11926 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011927}
11928
11929/*
11930 * "floor({float})" function
11931 */
11932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011933f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011934{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011935 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011936
11937 rettv->v_type = VAR_FLOAT;
11938 if (get_float_arg(argvars, &f) == OK)
11939 rettv->vval.v_float = floor(f);
11940 else
11941 rettv->vval.v_float = 0.0;
11942}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011943
11944/*
11945 * "fmod()" function
11946 */
11947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011948f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011949{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011950 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011951
11952 rettv->v_type = VAR_FLOAT;
11953 if (get_float_arg(argvars, &fx) == OK
11954 && get_float_arg(&argvars[1], &fy) == OK)
11955 rettv->vval.v_float = fmod(fx, fy);
11956 else
11957 rettv->vval.v_float = 0.0;
11958}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011959#endif
11960
Bram Moolenaar0d660222005-01-07 21:51:51 +000011961/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011962 * "fnameescape({string})" function
11963 */
11964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011965f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011966{
11967 rettv->vval.v_string = vim_strsave_fnameescape(
11968 get_tv_string(&argvars[0]), FALSE);
11969 rettv->v_type = VAR_STRING;
11970}
11971
11972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 * "fnamemodify({fname}, {mods})" function
11974 */
11975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011976f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977{
11978 char_u *fname;
11979 char_u *mods;
11980 int usedlen = 0;
11981 int len;
11982 char_u *fbuf = NULL;
11983 char_u buf[NUMBUFLEN];
11984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011985 fname = get_tv_string_chk(&argvars[0]);
11986 mods = get_tv_string_buf_chk(&argvars[1], buf);
11987 if (fname == NULL || mods == NULL)
11988 fname = NULL;
11989 else
11990 {
11991 len = (int)STRLEN(fname);
11992 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011999 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 vim_free(fbuf);
12001}
12002
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012003static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004
12005/*
12006 * "foldclosed()" function
12007 */
12008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012009foldclosed_both(
12010 typval_T *argvars UNUSED,
12011 typval_T *rettv,
12012 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013{
12014#ifdef FEAT_FOLDING
12015 linenr_T lnum;
12016 linenr_T first, last;
12017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012018 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12020 {
12021 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12022 {
12023 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012024 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 return;
12028 }
12029 }
12030#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032}
12033
12034/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012035 * "foldclosed()" function
12036 */
12037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012038f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012039{
12040 foldclosed_both(argvars, rettv, FALSE);
12041}
12042
12043/*
12044 * "foldclosedend()" function
12045 */
12046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012047f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012048{
12049 foldclosed_both(argvars, rettv, TRUE);
12050}
12051
12052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 * "foldlevel()" function
12054 */
12055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012056f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057{
12058#ifdef FEAT_FOLDING
12059 linenr_T lnum;
12060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012061 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065}
12066
12067/*
12068 * "foldtext()" function
12069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012071f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072{
12073#ifdef FEAT_FOLDING
12074 linenr_T lnum;
12075 char_u *s;
12076 char_u *r;
12077 int len;
12078 char *txt;
12079#endif
12080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 rettv->v_type = VAR_STRING;
12082 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012084 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12085 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12086 <= curbuf->b_ml.ml_line_count
12087 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 {
12089 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012090 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12091 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092 {
12093 if (!linewhite(lnum))
12094 break;
12095 ++lnum;
12096 }
12097
12098 /* Find interesting text in this line. */
12099 s = skipwhite(ml_get(lnum));
12100 /* skip C comment-start */
12101 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012104 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012105 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012106 {
12107 s = skipwhite(ml_get(lnum + 1));
12108 if (*s == '*')
12109 s = skipwhite(s + 1);
12110 }
12111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 txt = _("+-%s%3ld lines: ");
12113 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012114 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115 + 20 /* for %3ld */
12116 + STRLEN(s))); /* concatenated */
12117 if (r != NULL)
12118 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012119 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12120 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12121 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 len = (int)STRLEN(r);
12123 STRCAT(r, s);
12124 /* remove 'foldmarker' and 'commentstring' */
12125 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012126 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127 }
12128 }
12129#endif
12130}
12131
12132/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012133 * "foldtextresult(lnum)" function
12134 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012136f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012137{
12138#ifdef FEAT_FOLDING
12139 linenr_T lnum;
12140 char_u *text;
12141 char_u buf[51];
12142 foldinfo_T foldinfo;
12143 int fold_count;
12144#endif
12145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146 rettv->v_type = VAR_STRING;
12147 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012148#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012150 /* treat illegal types and illegal string values for {lnum} the same */
12151 if (lnum < 0)
12152 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012153 fold_count = foldedCount(curwin, lnum, &foldinfo);
12154 if (fold_count > 0)
12155 {
12156 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12157 &foldinfo, buf);
12158 if (text == buf)
12159 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012161 }
12162#endif
12163}
12164
12165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 * "foreground()" function
12167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012169f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171#ifdef FEAT_GUI
12172 if (gui.in_use)
12173 gui_mch_set_foreground();
12174#else
12175# ifdef WIN32
12176 win32_set_foreground();
12177# endif
12178#endif
12179}
12180
12181/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012182 * "function()" function
12183 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012185f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012186{
12187 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012188 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012189 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012190 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012191
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012192 if (argvars[0].v_type == VAR_FUNC)
12193 {
12194 /* function(MyFunc, [arg], dict) */
12195 s = argvars[0].vval.v_string;
12196 }
12197 else if (argvars[0].v_type == VAR_PARTIAL
12198 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012199 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012200 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012201 arg_pt = argvars[0].vval.v_partial;
12202 s = arg_pt->pt_name;
12203 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012204 else
12205 {
12206 /* function('MyFunc', [arg], dict) */
12207 s = get_tv_string(&argvars[0]);
12208 use_string = TRUE;
12209 }
12210
12211 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012212 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012213 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012214 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12215 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012216 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012217 else
12218 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012219 int dict_idx = 0;
12220 int arg_idx = 0;
12221 list_T *list = NULL;
12222
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012223 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012224 {
12225 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012226 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012227
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012228 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12229 * also be called from another script. Using trans_function_name()
12230 * would also work, but some plugins depend on the name being
12231 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012232 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012233 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12234 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012235 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012236 STRCPY(name, sid_buf);
12237 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012238 }
12239 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012240 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012241 name = vim_strsave(s);
12242
12243 if (argvars[1].v_type != VAR_UNKNOWN)
12244 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012245 if (argvars[2].v_type != VAR_UNKNOWN)
12246 {
12247 /* function(name, [args], dict) */
12248 arg_idx = 1;
12249 dict_idx = 2;
12250 }
12251 else if (argvars[1].v_type == VAR_DICT)
12252 /* function(name, dict) */
12253 dict_idx = 1;
12254 else
12255 /* function(name, [args]) */
12256 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012257 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012258 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012259 if (argvars[dict_idx].v_type != VAR_DICT)
12260 {
12261 EMSG(_("E922: expected a dict"));
12262 vim_free(name);
12263 return;
12264 }
12265 if (argvars[dict_idx].vval.v_dict == NULL)
12266 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012267 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012268 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012269 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012270 if (argvars[arg_idx].v_type != VAR_LIST)
12271 {
12272 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12273 vim_free(name);
12274 return;
12275 }
12276 list = argvars[arg_idx].vval.v_list;
12277 if (list == NULL || list->lv_len == 0)
12278 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012279 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012280 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012281 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012282 {
12283 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012284
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012285 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012286 if (pt == NULL)
12287 vim_free(name);
12288 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012289 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012290 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012291 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012292 listitem_T *li;
12293 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012294 int arg_len = 0;
12295 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012296
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012297 if (arg_pt != NULL)
12298 arg_len = arg_pt->pt_argc;
12299 if (list != NULL)
12300 lv_len = list->lv_len;
12301 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012302 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012303 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012304 if (pt->pt_argv == NULL)
12305 {
12306 vim_free(pt);
12307 vim_free(name);
12308 return;
12309 }
12310 else
12311 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012312 for (i = 0; i < arg_len; i++)
12313 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12314 if (lv_len > 0)
12315 for (li = list->lv_first; li != NULL;
12316 li = li->li_next)
12317 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012318 }
12319 }
12320
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012321 /* For "function(dict.func, [], dict)" and "func" is a partial
12322 * use "dict". That is backwards compatible. */
12323 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012324 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012325 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012326 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12327 ++pt->pt_dict->dv_refcount;
12328 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012329 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012330 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012331 /* If the dict was bound automatically the result is also
12332 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012333 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012334 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012335 if (pt->pt_dict != NULL)
12336 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012337 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012338
12339 pt->pt_refcount = 1;
12340 pt->pt_name = name;
12341 func_ref(pt->pt_name);
12342 }
12343 rettv->v_type = VAR_PARTIAL;
12344 rettv->vval.v_partial = pt;
12345 }
12346 else
12347 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012348 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012349 rettv->v_type = VAR_FUNC;
12350 rettv->vval.v_string = name;
12351 func_ref(name);
12352 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012353 }
12354}
12355
12356/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012357 * "garbagecollect()" function
12358 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012360f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012361{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012362 /* This is postponed until we are back at the toplevel, because we may be
12363 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12364 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012365
12366 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12367 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012368}
12369
12370/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371 * "get()" function
12372 */
12373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012374f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012375{
Bram Moolenaar33570922005-01-25 22:26:29 +000012376 listitem_T *li;
12377 list_T *l;
12378 dictitem_T *di;
12379 dict_T *d;
12380 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012381
Bram Moolenaare9a41262005-01-15 22:18:47 +000012382 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012383 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012384 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012386 int error = FALSE;
12387
12388 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12389 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012390 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012391 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012393 else if (argvars[0].v_type == VAR_DICT)
12394 {
12395 if ((d = argvars[0].vval.v_dict) != NULL)
12396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012397 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012398 if (di != NULL)
12399 tv = &di->di_tv;
12400 }
12401 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012402 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012403 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012404 partial_T *pt;
12405 partial_T fref_pt;
12406
12407 if (argvars[0].v_type == VAR_PARTIAL)
12408 pt = argvars[0].vval.v_partial;
12409 else
12410 {
12411 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12412 fref_pt.pt_name = argvars[0].vval.v_string;
12413 pt = &fref_pt;
12414 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012415
12416 if (pt != NULL)
12417 {
12418 char_u *what = get_tv_string(&argvars[1]);
12419
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012420 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012421 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012422 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012423 if (pt->pt_name == NULL)
12424 rettv->vval.v_string = NULL;
12425 else
12426 rettv->vval.v_string = vim_strsave(pt->pt_name);
12427 }
12428 else if (STRCMP(what, "dict") == 0)
12429 {
12430 rettv->v_type = VAR_DICT;
12431 rettv->vval.v_dict = pt->pt_dict;
12432 if (pt->pt_dict != NULL)
12433 ++pt->pt_dict->dv_refcount;
12434 }
12435 else if (STRCMP(what, "args") == 0)
12436 {
12437 rettv->v_type = VAR_LIST;
12438 if (rettv_list_alloc(rettv) == OK)
12439 {
12440 int i;
12441
12442 for (i = 0; i < pt->pt_argc; ++i)
12443 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12444 }
12445 }
12446 else
12447 EMSG2(_(e_invarg2), what);
12448 return;
12449 }
12450 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012451 else
12452 EMSG2(_(e_listdictarg), "get()");
12453
12454 if (tv == NULL)
12455 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012456 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012457 copy_tv(&argvars[2], rettv);
12458 }
12459 else
12460 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012461}
12462
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012463static 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 +000012464
12465/*
12466 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012467 * Return a range (from start to end) of lines in rettv from the specified
12468 * buffer.
12469 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012470 */
12471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012472get_buffer_lines(
12473 buf_T *buf,
12474 linenr_T start,
12475 linenr_T end,
12476 int retlist,
12477 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012478{
12479 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012480
Bram Moolenaar959a1432013-12-14 12:17:38 +010012481 rettv->v_type = VAR_STRING;
12482 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012483 if (retlist && rettv_list_alloc(rettv) == FAIL)
12484 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012485
12486 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12487 return;
12488
12489 if (!retlist)
12490 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012491 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12492 p = ml_get_buf(buf, start, FALSE);
12493 else
12494 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012495 rettv->vval.v_string = vim_strsave(p);
12496 }
12497 else
12498 {
12499 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012500 return;
12501
12502 if (start < 1)
12503 start = 1;
12504 if (end > buf->b_ml.ml_line_count)
12505 end = buf->b_ml.ml_line_count;
12506 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012507 if (list_append_string(rettv->vval.v_list,
12508 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012509 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012510 }
12511}
12512
12513/*
12514 * "getbufline()" function
12515 */
12516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012517f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012518{
12519 linenr_T lnum;
12520 linenr_T end;
12521 buf_T *buf;
12522
12523 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12524 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012525 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012526 --emsg_off;
12527
Bram Moolenaar661b1822005-07-28 22:36:45 +000012528 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012529 if (argvars[2].v_type == VAR_UNKNOWN)
12530 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012531 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012532 end = get_tv_lnum_buf(&argvars[2], buf);
12533
Bram Moolenaar342337a2005-07-21 21:11:17 +000012534 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012535}
12536
Bram Moolenaar0d660222005-01-07 21:51:51 +000012537/*
12538 * "getbufvar()" function
12539 */
12540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012541f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012542{
12543 buf_T *buf;
12544 buf_T *save_curbuf;
12545 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012546 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012547 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012549 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12550 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012551 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012552 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012553
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012554 rettv->v_type = VAR_STRING;
12555 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012556
12557 if (buf != NULL && varname != NULL)
12558 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012559 /* set curbuf to be our buf, temporarily */
12560 save_curbuf = curbuf;
12561 curbuf = buf;
12562
Bram Moolenaar0d660222005-01-07 21:51:51 +000012563 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012564 {
12565 if (get_option_tv(&varname, rettv, TRUE) == OK)
12566 done = TRUE;
12567 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012568 else if (STRCMP(varname, "changedtick") == 0)
12569 {
12570 rettv->v_type = VAR_NUMBER;
12571 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012572 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012573 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012574 else
12575 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012576 /* Look up the variable. */
12577 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12578 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12579 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012580 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012581 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012582 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012583 done = TRUE;
12584 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012585 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012586
12587 /* restore previous notion of curbuf */
12588 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012589 }
12590
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012591 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12592 /* use the default value */
12593 copy_tv(&argvars[2], rettv);
12594
Bram Moolenaar0d660222005-01-07 21:51:51 +000012595 --emsg_off;
12596}
12597
12598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599 * "getchar()" function
12600 */
12601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012602f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603{
12604 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012605 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012607 /* Position the cursor. Needed after a message that ends in a space. */
12608 windgoto(msg_row, msg_col);
12609
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610 ++no_mapping;
12611 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012612 for (;;)
12613 {
12614 if (argvars[0].v_type == VAR_UNKNOWN)
12615 /* getchar(): blocking wait. */
12616 n = safe_vgetc();
12617 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12618 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012619 n = vpeekc_any();
12620 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012621 /* illegal argument or getchar(0) and no char avail: return zero */
12622 n = 0;
12623 else
12624 /* getchar(0) and char avail: return char */
12625 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012626
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012627 if (n == K_IGNORE)
12628 continue;
12629 break;
12630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 --no_mapping;
12632 --allow_keys;
12633
Bram Moolenaar219b8702006-11-01 14:32:36 +000012634 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12635 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12636 vimvars[VV_MOUSE_COL].vv_nr = 0;
12637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639 if (IS_SPECIAL(n) || mod_mask != 0)
12640 {
12641 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12642 int i = 0;
12643
12644 /* Turn a special key into three bytes, plus modifier. */
12645 if (mod_mask != 0)
12646 {
12647 temp[i++] = K_SPECIAL;
12648 temp[i++] = KS_MODIFIER;
12649 temp[i++] = mod_mask;
12650 }
12651 if (IS_SPECIAL(n))
12652 {
12653 temp[i++] = K_SPECIAL;
12654 temp[i++] = K_SECOND(n);
12655 temp[i++] = K_THIRD(n);
12656 }
12657#ifdef FEAT_MBYTE
12658 else if (has_mbyte)
12659 i += (*mb_char2bytes)(n, temp + i);
12660#endif
12661 else
12662 temp[i++] = n;
12663 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012664 rettv->v_type = VAR_STRING;
12665 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012666
12667#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012668 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012669 {
12670 int row = mouse_row;
12671 int col = mouse_col;
12672 win_T *win;
12673 linenr_T lnum;
12674# ifdef FEAT_WINDOWS
12675 win_T *wp;
12676# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012677 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012678
12679 if (row >= 0 && col >= 0)
12680 {
12681 /* Find the window at the mouse coordinates and compute the
12682 * text position. */
12683 win = mouse_find_win(&row, &col);
12684 (void)mouse_comp_pos(win, &row, &col, &lnum);
12685# ifdef FEAT_WINDOWS
12686 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012687 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012688# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012689 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012690 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12691 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12692 }
12693 }
12694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 }
12696}
12697
12698/*
12699 * "getcharmod()" function
12700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012702f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705}
12706
12707/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012708 * "getcharsearch()" function
12709 */
12710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012711f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012712{
12713 if (rettv_dict_alloc(rettv) != FAIL)
12714 {
12715 dict_T *dict = rettv->vval.v_dict;
12716
12717 dict_add_nr_str(dict, "char", 0L, last_csearch());
12718 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12719 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12720 }
12721}
12722
12723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 * "getcmdline()" function
12725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012727f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012729 rettv->v_type = VAR_STRING;
12730 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731}
12732
12733/*
12734 * "getcmdpos()" function
12735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012737f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740}
12741
12742/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012743 * "getcmdtype()" function
12744 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012746f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012747{
12748 rettv->v_type = VAR_STRING;
12749 rettv->vval.v_string = alloc(2);
12750 if (rettv->vval.v_string != NULL)
12751 {
12752 rettv->vval.v_string[0] = get_cmdline_type();
12753 rettv->vval.v_string[1] = NUL;
12754 }
12755}
12756
12757/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012758 * "getcmdwintype()" function
12759 */
12760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012761f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012762{
12763 rettv->v_type = VAR_STRING;
12764 rettv->vval.v_string = NULL;
12765#ifdef FEAT_CMDWIN
12766 rettv->vval.v_string = alloc(2);
12767 if (rettv->vval.v_string != NULL)
12768 {
12769 rettv->vval.v_string[0] = cmdwin_type;
12770 rettv->vval.v_string[1] = NUL;
12771 }
12772#endif
12773}
12774
12775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 * "getcwd()" function
12777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012779f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012781 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012782 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012785 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012786
12787 wp = find_tabwin(&argvars[0], &argvars[1]);
12788 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012790 if (wp->w_localdir != NULL)
12791 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12792 else if(globaldir != NULL)
12793 rettv->vval.v_string = vim_strsave(globaldir);
12794 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012795 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012796 cwd = alloc(MAXPATHL);
12797 if (cwd != NULL)
12798 {
12799 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12800 rettv->vval.v_string = vim_strsave(cwd);
12801 vim_free(cwd);
12802 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012803 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012804#ifdef BACKSLASH_IN_FILENAME
12805 if (rettv->vval.v_string != NULL)
12806 slash_adjust(rettv->vval.v_string);
12807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 }
12809}
12810
12811/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012812 * "getfontname()" function
12813 */
12814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012815f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012816{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->v_type = VAR_STRING;
12818 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012819#ifdef FEAT_GUI
12820 if (gui.in_use)
12821 {
12822 GuiFont font;
12823 char_u *name = NULL;
12824
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012825 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012826 {
12827 /* Get the "Normal" font. Either the name saved by
12828 * hl_set_font_name() or from the font ID. */
12829 font = gui.norm_font;
12830 name = hl_get_font_name();
12831 }
12832 else
12833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012835 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12836 return;
12837 font = gui_mch_get_font(name, FALSE);
12838 if (font == NOFONT)
12839 return; /* Invalid font name, return empty string. */
12840 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012842 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012843 gui_mch_free_font(font);
12844 }
12845#endif
12846}
12847
12848/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012849 * "getfperm({fname})" function
12850 */
12851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012852f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012853{
12854 char_u *fname;
12855 struct stat st;
12856 char_u *perm = NULL;
12857 char_u flags[] = "rwx";
12858 int i;
12859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012863 if (mch_stat((char *)fname, &st) >= 0)
12864 {
12865 perm = vim_strsave((char_u *)"---------");
12866 if (perm != NULL)
12867 {
12868 for (i = 0; i < 9; i++)
12869 {
12870 if (st.st_mode & (1 << (8 - i)))
12871 perm[i] = flags[i % 3];
12872 }
12873 }
12874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012876}
12877
12878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 * "getfsize({fname})" function
12880 */
12881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012882f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883{
12884 char_u *fname;
12885 struct stat st;
12886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890
12891 if (mch_stat((char *)fname, &st) >= 0)
12892 {
12893 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012898
12899 /* non-perfect check for overflow */
12900 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12901 rettv->vval.v_number = -2;
12902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 }
12904 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906}
12907
12908/*
12909 * "getftime({fname})" function
12910 */
12911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012912f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913{
12914 char_u *fname;
12915 struct stat st;
12916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012917 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918
12919 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923}
12924
12925/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012926 * "getftype({fname})" function
12927 */
12928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012929f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012930{
12931 char_u *fname;
12932 struct stat st;
12933 char_u *type = NULL;
12934 char *t;
12935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012936 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012939 if (mch_lstat((char *)fname, &st) >= 0)
12940 {
12941#ifdef S_ISREG
12942 if (S_ISREG(st.st_mode))
12943 t = "file";
12944 else if (S_ISDIR(st.st_mode))
12945 t = "dir";
12946# ifdef S_ISLNK
12947 else if (S_ISLNK(st.st_mode))
12948 t = "link";
12949# endif
12950# ifdef S_ISBLK
12951 else if (S_ISBLK(st.st_mode))
12952 t = "bdev";
12953# endif
12954# ifdef S_ISCHR
12955 else if (S_ISCHR(st.st_mode))
12956 t = "cdev";
12957# endif
12958# ifdef S_ISFIFO
12959 else if (S_ISFIFO(st.st_mode))
12960 t = "fifo";
12961# endif
12962# ifdef S_ISSOCK
12963 else if (S_ISSOCK(st.st_mode))
12964 t = "fifo";
12965# endif
12966 else
12967 t = "other";
12968#else
12969# ifdef S_IFMT
12970 switch (st.st_mode & S_IFMT)
12971 {
12972 case S_IFREG: t = "file"; break;
12973 case S_IFDIR: t = "dir"; break;
12974# ifdef S_IFLNK
12975 case S_IFLNK: t = "link"; break;
12976# endif
12977# ifdef S_IFBLK
12978 case S_IFBLK: t = "bdev"; break;
12979# endif
12980# ifdef S_IFCHR
12981 case S_IFCHR: t = "cdev"; break;
12982# endif
12983# ifdef S_IFIFO
12984 case S_IFIFO: t = "fifo"; break;
12985# endif
12986# ifdef S_IFSOCK
12987 case S_IFSOCK: t = "socket"; break;
12988# endif
12989 default: t = "other";
12990 }
12991# else
12992 if (mch_isdir(fname))
12993 t = "dir";
12994 else
12995 t = "file";
12996# endif
12997#endif
12998 type = vim_strsave((char_u *)t);
12999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013000 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013001}
13002
13003/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013004 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013005 */
13006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013007f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008{
13009 linenr_T lnum;
13010 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013011 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013012
13013 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013014 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013015 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013016 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013017 retlist = FALSE;
13018 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013019 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013020 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013021 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013022 retlist = TRUE;
13023 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013024
Bram Moolenaar342337a2005-07-21 21:11:17 +000013025 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013026}
13027
13028/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013029 * "getmatches()" function
13030 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013032f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013033{
13034#ifdef FEAT_SEARCH_EXTRA
13035 dict_T *dict;
13036 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013037 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013038
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013039 if (rettv_list_alloc(rettv) == OK)
13040 {
13041 while (cur != NULL)
13042 {
13043 dict = dict_alloc();
13044 if (dict == NULL)
13045 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013046 if (cur->match.regprog == NULL)
13047 {
13048 /* match added with matchaddpos() */
13049 for (i = 0; i < MAXPOSMATCH; ++i)
13050 {
13051 llpos_T *llpos;
13052 char buf[6];
13053 list_T *l;
13054
13055 llpos = &cur->pos.pos[i];
13056 if (llpos->lnum == 0)
13057 break;
13058 l = list_alloc();
13059 if (l == NULL)
13060 break;
13061 list_append_number(l, (varnumber_T)llpos->lnum);
13062 if (llpos->col > 0)
13063 {
13064 list_append_number(l, (varnumber_T)llpos->col);
13065 list_append_number(l, (varnumber_T)llpos->len);
13066 }
13067 sprintf(buf, "pos%d", i + 1);
13068 dict_add_list(dict, buf, l);
13069 }
13070 }
13071 else
13072 {
13073 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13074 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013075 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013076 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13077 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013078# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013079 if (cur->conceal_char)
13080 {
13081 char_u buf[MB_MAXBYTES + 1];
13082
13083 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13084 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13085 }
13086# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013087 list_append_dict(rettv->vval.v_list, dict);
13088 cur = cur->next;
13089 }
13090 }
13091#endif
13092}
13093
13094/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013095 * "getpid()" function
13096 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013098f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013099{
13100 rettv->vval.v_number = mch_get_pid();
13101}
13102
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013104getpos_both(
13105 typval_T *argvars,
13106 typval_T *rettv,
13107 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013108{
Bram Moolenaara5525202006-03-02 22:52:09 +000013109 pos_T *fp;
13110 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013111 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013112
13113 if (rettv_list_alloc(rettv) == OK)
13114 {
13115 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013116 if (getcurpos)
13117 fp = &curwin->w_cursor;
13118 else
13119 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013120 if (fnum != -1)
13121 list_append_number(l, (varnumber_T)fnum);
13122 else
13123 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013124 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13125 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013126 list_append_number(l, (fp != NULL)
13127 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013128 : (varnumber_T)0);
13129 list_append_number(l,
13130#ifdef FEAT_VIRTUALEDIT
13131 (fp != NULL) ? (varnumber_T)fp->coladd :
13132#endif
13133 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013134 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013135 {
13136 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013137 list_append_number(l, curwin->w_curswant == MAXCOL ?
13138 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013139 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013140 }
13141 else
13142 rettv->vval.v_number = FALSE;
13143}
13144
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013145
13146/*
13147 * "getcurpos()" function
13148 */
13149 static void
13150f_getcurpos(typval_T *argvars, typval_T *rettv)
13151{
13152 getpos_both(argvars, rettv, TRUE);
13153}
13154
13155/*
13156 * "getpos(string)" function
13157 */
13158 static void
13159f_getpos(typval_T *argvars, typval_T *rettv)
13160{
13161 getpos_both(argvars, rettv, FALSE);
13162}
13163
Bram Moolenaara5525202006-03-02 22:52:09 +000013164/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013165 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013166 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013168f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013169{
13170#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013171 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013172#endif
13173
Bram Moolenaar2641f772005-03-25 21:58:17 +000013174#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013175 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013176 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013177 wp = NULL;
13178 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13179 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013180 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013181 if (wp == NULL)
13182 return;
13183 }
13184
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013185 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013186 }
13187#endif
13188}
13189
13190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 * "getreg()" function
13192 */
13193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013194f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195{
13196 char_u *strregname;
13197 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013198 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013199 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013200 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013202 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013204 strregname = get_tv_string_chk(&argvars[0]);
13205 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013206 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013208 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013209 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13210 return_list = get_tv_number_chk(&argvars[2], &error);
13211 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013214 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013215
13216 if (error)
13217 return;
13218
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 regname = (strregname == NULL ? '"' : *strregname);
13220 if (regname == 0)
13221 regname = '"';
13222
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013223 if (return_list)
13224 {
13225 rettv->v_type = VAR_LIST;
13226 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13227 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013228 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013229 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013230 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013231 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013232 }
13233 else
13234 {
13235 rettv->v_type = VAR_STRING;
13236 rettv->vval.v_string = get_reg_contents(regname,
13237 arg2 ? GREG_EXPR_SRC : 0);
13238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239}
13240
13241/*
13242 * "getregtype()" function
13243 */
13244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013245f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246{
13247 char_u *strregname;
13248 int regname;
13249 char_u buf[NUMBUFLEN + 2];
13250 long reglen = 0;
13251
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013252 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013253 {
13254 strregname = get_tv_string_chk(&argvars[0]);
13255 if (strregname == NULL) /* type error; errmsg already given */
13256 {
13257 rettv->v_type = VAR_STRING;
13258 rettv->vval.v_string = NULL;
13259 return;
13260 }
13261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 else
13263 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013264 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265
13266 regname = (strregname == NULL ? '"' : *strregname);
13267 if (regname == 0)
13268 regname = '"';
13269
13270 buf[0] = NUL;
13271 buf[1] = NUL;
13272 switch (get_reg_type(regname, &reglen))
13273 {
13274 case MLINE: buf[0] = 'V'; break;
13275 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276 case MBLOCK:
13277 buf[0] = Ctrl_V;
13278 sprintf((char *)buf + 1, "%ld", reglen + 1);
13279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013281 rettv->v_type = VAR_STRING;
13282 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283}
13284
13285/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013286 * "gettabvar()" function
13287 */
13288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013289f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013290{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013291 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013292 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013293 dictitem_T *v;
13294 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013295 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013296
13297 rettv->v_type = VAR_STRING;
13298 rettv->vval.v_string = NULL;
13299
13300 varname = get_tv_string_chk(&argvars[1]);
13301 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13302 if (tp != NULL && varname != NULL)
13303 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013304 /* Set tp to be our tabpage, temporarily. Also set the window to the
13305 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013306 if (switch_win(&oldcurwin, &oldtabpage,
13307 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013308 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013309 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013310 /* look up the variable */
13311 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13312 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13313 if (v != NULL)
13314 {
13315 copy_tv(&v->di_tv, rettv);
13316 done = TRUE;
13317 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013318 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013319
13320 /* restore previous notion of curwin */
13321 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013322 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013323
13324 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013325 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013326}
13327
13328/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013329 * "gettabwinvar()" function
13330 */
13331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013332f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013333{
13334 getwinvar(argvars, rettv, 1);
13335}
13336
13337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 * "getwinposx()" function
13339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013341f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013343 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344#ifdef FEAT_GUI
13345 if (gui.in_use)
13346 {
13347 int x, y;
13348
13349 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351 }
13352#endif
13353}
13354
13355/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013356 * "win_findbuf()" function
13357 */
13358 static void
13359f_win_findbuf(typval_T *argvars, typval_T *rettv)
13360{
13361 if (rettv_list_alloc(rettv) != FAIL)
13362 win_findbuf(argvars, rettv->vval.v_list);
13363}
13364
13365/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013366 * "win_getid()" function
13367 */
13368 static void
13369f_win_getid(typval_T *argvars, typval_T *rettv)
13370{
13371 rettv->vval.v_number = win_getid(argvars);
13372}
13373
13374/*
13375 * "win_gotoid()" function
13376 */
13377 static void
13378f_win_gotoid(typval_T *argvars, typval_T *rettv)
13379{
13380 rettv->vval.v_number = win_gotoid(argvars);
13381}
13382
13383/*
13384 * "win_id2tabwin()" function
13385 */
13386 static void
13387f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13388{
13389 if (rettv_list_alloc(rettv) != FAIL)
13390 win_id2tabwin(argvars, rettv->vval.v_list);
13391}
13392
13393/*
13394 * "win_id2win()" function
13395 */
13396 static void
13397f_win_id2win(typval_T *argvars, typval_T *rettv)
13398{
13399 rettv->vval.v_number = win_id2win(argvars);
13400}
13401
13402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 * "getwinposy()" function
13404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013406f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013408 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409#ifdef FEAT_GUI
13410 if (gui.in_use)
13411 {
13412 int x, y;
13413
13414 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013415 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 }
13417#endif
13418}
13419
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013420/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013421 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013422 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013423 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013424find_win_by_nr(
13425 typval_T *vp,
13426 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013427{
13428#ifdef FEAT_WINDOWS
13429 win_T *wp;
13430#endif
13431 int nr;
13432
13433 nr = get_tv_number_chk(vp, NULL);
13434
13435#ifdef FEAT_WINDOWS
13436 if (nr < 0)
13437 return NULL;
13438 if (nr == 0)
13439 return curwin;
13440
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013441 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13442 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013443 if (--nr <= 0)
13444 break;
13445 return wp;
13446#else
13447 if (nr == 0 || nr == 1)
13448 return curwin;
13449 return NULL;
13450#endif
13451}
13452
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013454 * Find window specified by "wvp" in tabpage "tvp".
13455 */
13456 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013457find_tabwin(
13458 typval_T *wvp, /* VAR_UNKNOWN for current window */
13459 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013460{
13461 win_T *wp = NULL;
13462 tabpage_T *tp = NULL;
13463 long n;
13464
13465 if (wvp->v_type != VAR_UNKNOWN)
13466 {
13467 if (tvp->v_type != VAR_UNKNOWN)
13468 {
13469 n = get_tv_number(tvp);
13470 if (n >= 0)
13471 tp = find_tabpage(n);
13472 }
13473 else
13474 tp = curtab;
13475
13476 if (tp != NULL)
13477 wp = find_win_by_nr(wvp, tp);
13478 }
13479 else
13480 wp = curwin;
13481
13482 return wp;
13483}
13484
13485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 * "getwinvar()" function
13487 */
13488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013489f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013491 getwinvar(argvars, rettv, 0);
13492}
13493
13494/*
13495 * getwinvar() and gettabwinvar()
13496 */
13497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013498getwinvar(
13499 typval_T *argvars,
13500 typval_T *rettv,
13501 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013502{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013503 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013505 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013506 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013507 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013508#ifdef FEAT_WINDOWS
13509 win_T *oldcurwin;
13510 tabpage_T *oldtabpage;
13511 int need_switch_win;
13512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013514#ifdef FEAT_WINDOWS
13515 if (off == 1)
13516 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13517 else
13518 tp = curtab;
13519#endif
13520 win = find_win_by_nr(&argvars[off], tp);
13521 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013522 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013524 rettv->v_type = VAR_STRING;
13525 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526
13527 if (win != NULL && varname != NULL)
13528 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013529#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013530 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013531 * otherwise the window is not valid. Only do this when needed,
13532 * autocommands get blocked. */
13533 need_switch_win = !(tp == curtab && win == curwin);
13534 if (!need_switch_win
13535 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13536#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013537 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013538 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013539 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013540 if (get_option_tv(&varname, rettv, 1) == OK)
13541 done = TRUE;
13542 }
13543 else
13544 {
13545 /* Look up the variable. */
13546 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13547 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13548 varname, FALSE);
13549 if (v != NULL)
13550 {
13551 copy_tv(&v->di_tv, rettv);
13552 done = TRUE;
13553 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013556
Bram Moolenaarba117c22015-09-29 16:53:22 +020013557#ifdef FEAT_WINDOWS
13558 if (need_switch_win)
13559 /* restore previous notion of curwin */
13560 restore_win(oldcurwin, oldtabpage, TRUE);
13561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 }
13563
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013564 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13565 /* use the default return value */
13566 copy_tv(&argvars[off + 2], rettv);
13567
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568 --emsg_off;
13569}
13570
13571/*
13572 * "glob()" function
13573 */
13574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013575f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013577 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013579 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013581 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013582 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013584 if (argvars[1].v_type != VAR_UNKNOWN)
13585 {
13586 if (get_tv_number_chk(&argvars[1], &error))
13587 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013588 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013589 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013590 if (get_tv_number_chk(&argvars[2], &error))
13591 {
13592 rettv->v_type = VAR_LIST;
13593 rettv->vval.v_list = NULL;
13594 }
13595 if (argvars[3].v_type != VAR_UNKNOWN
13596 && get_tv_number_chk(&argvars[3], &error))
13597 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013598 }
13599 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013600 if (!error)
13601 {
13602 ExpandInit(&xpc);
13603 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013604 if (p_wic)
13605 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013606 if (rettv->v_type == VAR_STRING)
13607 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013608 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013609 else if (rettv_list_alloc(rettv) != FAIL)
13610 {
13611 int i;
13612
13613 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13614 NULL, options, WILD_ALL_KEEP);
13615 for (i = 0; i < xpc.xp_numfiles; i++)
13616 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13617
13618 ExpandCleanup(&xpc);
13619 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013620 }
13621 else
13622 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623}
13624
13625/*
13626 * "globpath()" function
13627 */
13628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013629f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013631 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013633 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013634 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013635 garray_T ga;
13636 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013638 /* When the optional second argument is non-zero, don't remove matches
13639 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013641 if (argvars[2].v_type != VAR_UNKNOWN)
13642 {
13643 if (get_tv_number_chk(&argvars[2], &error))
13644 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013645 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013646 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013647 if (get_tv_number_chk(&argvars[3], &error))
13648 {
13649 rettv->v_type = VAR_LIST;
13650 rettv->vval.v_list = NULL;
13651 }
13652 if (argvars[4].v_type != VAR_UNKNOWN
13653 && get_tv_number_chk(&argvars[4], &error))
13654 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013655 }
13656 }
13657 if (file != NULL && !error)
13658 {
13659 ga_init2(&ga, (int)sizeof(char_u *), 10);
13660 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13661 if (rettv->v_type == VAR_STRING)
13662 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13663 else if (rettv_list_alloc(rettv) != FAIL)
13664 for (i = 0; i < ga.ga_len; ++i)
13665 list_append_string(rettv->vval.v_list,
13666 ((char_u **)(ga.ga_data))[i], -1);
13667 ga_clear_strings(&ga);
13668 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013669 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671}
13672
13673/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013674 * "glob2regpat()" function
13675 */
13676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013677f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013678{
13679 char_u *pat = get_tv_string_chk(&argvars[0]);
13680
13681 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013682 rettv->vval.v_string = (pat == NULL)
13683 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013684}
13685
13686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 * "has()" function
13688 */
13689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013690f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691{
13692 int i;
13693 char_u *name;
13694 int n = FALSE;
13695 static char *(has_list[]) =
13696 {
13697#ifdef AMIGA
13698 "amiga",
13699# ifdef FEAT_ARP
13700 "arp",
13701# endif
13702#endif
13703#ifdef __BEOS__
13704 "beos",
13705#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013706#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707 "mac",
13708#endif
13709#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013710 "macunix", /* built with 'darwin' enabled */
13711#endif
13712#if defined(__APPLE__) && __APPLE__ == 1
13713 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715#ifdef __QNX__
13716 "qnx",
13717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718#ifdef UNIX
13719 "unix",
13720#endif
13721#ifdef VMS
13722 "vms",
13723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724#ifdef WIN32
13725 "win32",
13726#endif
13727#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13728 "win32unix",
13729#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013730#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 "win64",
13732#endif
13733#ifdef EBCDIC
13734 "ebcdic",
13735#endif
13736#ifndef CASE_INSENSITIVE_FILENAME
13737 "fname_case",
13738#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013739#ifdef HAVE_ACL
13740 "acl",
13741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742#ifdef FEAT_ARABIC
13743 "arabic",
13744#endif
13745#ifdef FEAT_AUTOCMD
13746 "autocmd",
13747#endif
13748#ifdef FEAT_BEVAL
13749 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013750# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13751 "balloon_multiline",
13752# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753#endif
13754#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13755 "builtin_terms",
13756# ifdef ALL_BUILTIN_TCAPS
13757 "all_builtin_terms",
13758# endif
13759#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013760#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13761 || defined(FEAT_GUI_W32) \
13762 || defined(FEAT_GUI_MOTIF))
13763 "browsefilter",
13764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765#ifdef FEAT_BYTEOFF
13766 "byte_offset",
13767#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013768#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013769 "channel",
13770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771#ifdef FEAT_CINDENT
13772 "cindent",
13773#endif
13774#ifdef FEAT_CLIENTSERVER
13775 "clientserver",
13776#endif
13777#ifdef FEAT_CLIPBOARD
13778 "clipboard",
13779#endif
13780#ifdef FEAT_CMDL_COMPL
13781 "cmdline_compl",
13782#endif
13783#ifdef FEAT_CMDHIST
13784 "cmdline_hist",
13785#endif
13786#ifdef FEAT_COMMENTS
13787 "comments",
13788#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013789#ifdef FEAT_CONCEAL
13790 "conceal",
13791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792#ifdef FEAT_CRYPT
13793 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013794 "crypt-blowfish",
13795 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796#endif
13797#ifdef FEAT_CSCOPE
13798 "cscope",
13799#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013800#ifdef FEAT_CURSORBIND
13801 "cursorbind",
13802#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013803#ifdef CURSOR_SHAPE
13804 "cursorshape",
13805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806#ifdef DEBUG
13807 "debug",
13808#endif
13809#ifdef FEAT_CON_DIALOG
13810 "dialog_con",
13811#endif
13812#ifdef FEAT_GUI_DIALOG
13813 "dialog_gui",
13814#endif
13815#ifdef FEAT_DIFF
13816 "diff",
13817#endif
13818#ifdef FEAT_DIGRAPHS
13819 "digraphs",
13820#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013821#ifdef FEAT_DIRECTX
13822 "directx",
13823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824#ifdef FEAT_DND
13825 "dnd",
13826#endif
13827#ifdef FEAT_EMACS_TAGS
13828 "emacs_tags",
13829#endif
13830 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013831 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832#ifdef FEAT_SEARCH_EXTRA
13833 "extra_search",
13834#endif
13835#ifdef FEAT_FKMAP
13836 "farsi",
13837#endif
13838#ifdef FEAT_SEARCHPATH
13839 "file_in_path",
13840#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013841#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013842 "filterpipe",
13843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844#ifdef FEAT_FIND_ID
13845 "find_in_path",
13846#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013847#ifdef FEAT_FLOAT
13848 "float",
13849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850#ifdef FEAT_FOLDING
13851 "folding",
13852#endif
13853#ifdef FEAT_FOOTER
13854 "footer",
13855#endif
13856#if !defined(USE_SYSTEM) && defined(UNIX)
13857 "fork",
13858#endif
13859#ifdef FEAT_GETTEXT
13860 "gettext",
13861#endif
13862#ifdef FEAT_GUI
13863 "gui",
13864#endif
13865#ifdef FEAT_GUI_ATHENA
13866# ifdef FEAT_GUI_NEXTAW
13867 "gui_neXtaw",
13868# else
13869 "gui_athena",
13870# endif
13871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872#ifdef FEAT_GUI_GTK
13873 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013874# ifdef USE_GTK3
13875 "gui_gtk3",
13876# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013880#ifdef FEAT_GUI_GNOME
13881 "gui_gnome",
13882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883#ifdef FEAT_GUI_MAC
13884 "gui_mac",
13885#endif
13886#ifdef FEAT_GUI_MOTIF
13887 "gui_motif",
13888#endif
13889#ifdef FEAT_GUI_PHOTON
13890 "gui_photon",
13891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892#ifdef FEAT_GUI_W32
13893 "gui_win32",
13894#endif
13895#ifdef FEAT_HANGULIN
13896 "hangul_input",
13897#endif
13898#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13899 "iconv",
13900#endif
13901#ifdef FEAT_INS_EXPAND
13902 "insert_expand",
13903#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013904#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013905 "job",
13906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907#ifdef FEAT_JUMPLIST
13908 "jumplist",
13909#endif
13910#ifdef FEAT_KEYMAP
13911 "keymap",
13912#endif
13913#ifdef FEAT_LANGMAP
13914 "langmap",
13915#endif
13916#ifdef FEAT_LIBCALL
13917 "libcall",
13918#endif
13919#ifdef FEAT_LINEBREAK
13920 "linebreak",
13921#endif
13922#ifdef FEAT_LISP
13923 "lispindent",
13924#endif
13925#ifdef FEAT_LISTCMDS
13926 "listcmds",
13927#endif
13928#ifdef FEAT_LOCALMAP
13929 "localmap",
13930#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013931#ifdef FEAT_LUA
13932# ifndef DYNAMIC_LUA
13933 "lua",
13934# endif
13935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936#ifdef FEAT_MENU
13937 "menu",
13938#endif
13939#ifdef FEAT_SESSION
13940 "mksession",
13941#endif
13942#ifdef FEAT_MODIFY_FNAME
13943 "modify_fname",
13944#endif
13945#ifdef FEAT_MOUSE
13946 "mouse",
13947#endif
13948#ifdef FEAT_MOUSESHAPE
13949 "mouseshape",
13950#endif
13951#if defined(UNIX) || defined(VMS)
13952# ifdef FEAT_MOUSE_DEC
13953 "mouse_dec",
13954# endif
13955# ifdef FEAT_MOUSE_GPM
13956 "mouse_gpm",
13957# endif
13958# ifdef FEAT_MOUSE_JSB
13959 "mouse_jsbterm",
13960# endif
13961# ifdef FEAT_MOUSE_NET
13962 "mouse_netterm",
13963# endif
13964# ifdef FEAT_MOUSE_PTERM
13965 "mouse_pterm",
13966# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013967# ifdef FEAT_MOUSE_SGR
13968 "mouse_sgr",
13969# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013970# ifdef FEAT_SYSMOUSE
13971 "mouse_sysmouse",
13972# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013973# ifdef FEAT_MOUSE_URXVT
13974 "mouse_urxvt",
13975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976# ifdef FEAT_MOUSE_XTERM
13977 "mouse_xterm",
13978# endif
13979#endif
13980#ifdef FEAT_MBYTE
13981 "multi_byte",
13982#endif
13983#ifdef FEAT_MBYTE_IME
13984 "multi_byte_ime",
13985#endif
13986#ifdef FEAT_MULTI_LANG
13987 "multi_lang",
13988#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013989#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013990#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013991 "mzscheme",
13992#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994#ifdef FEAT_OLE
13995 "ole",
13996#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013997 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998#ifdef FEAT_PATH_EXTRA
13999 "path_extra",
14000#endif
14001#ifdef FEAT_PERL
14002#ifndef DYNAMIC_PERL
14003 "perl",
14004#endif
14005#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014006#ifdef FEAT_PERSISTENT_UNDO
14007 "persistent_undo",
14008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009#ifdef FEAT_PYTHON
14010#ifndef DYNAMIC_PYTHON
14011 "python",
14012#endif
14013#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014014#ifdef FEAT_PYTHON3
14015#ifndef DYNAMIC_PYTHON3
14016 "python3",
14017#endif
14018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019#ifdef FEAT_POSTSCRIPT
14020 "postscript",
14021#endif
14022#ifdef FEAT_PRINTER
14023 "printer",
14024#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014025#ifdef FEAT_PROFILE
14026 "profile",
14027#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014028#ifdef FEAT_RELTIME
14029 "reltime",
14030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031#ifdef FEAT_QUICKFIX
14032 "quickfix",
14033#endif
14034#ifdef FEAT_RIGHTLEFT
14035 "rightleft",
14036#endif
14037#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14038 "ruby",
14039#endif
14040#ifdef FEAT_SCROLLBIND
14041 "scrollbind",
14042#endif
14043#ifdef FEAT_CMDL_INFO
14044 "showcmd",
14045 "cmdline_info",
14046#endif
14047#ifdef FEAT_SIGNS
14048 "signs",
14049#endif
14050#ifdef FEAT_SMARTINDENT
14051 "smartindent",
14052#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014053#ifdef STARTUPTIME
14054 "startuptime",
14055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056#ifdef FEAT_STL_OPT
14057 "statusline",
14058#endif
14059#ifdef FEAT_SUN_WORKSHOP
14060 "sun_workshop",
14061#endif
14062#ifdef FEAT_NETBEANS_INTG
14063 "netbeans_intg",
14064#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014065#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014066 "spell",
14067#endif
14068#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 "syntax",
14070#endif
14071#if defined(USE_SYSTEM) || !defined(UNIX)
14072 "system",
14073#endif
14074#ifdef FEAT_TAG_BINS
14075 "tag_binary",
14076#endif
14077#ifdef FEAT_TAG_OLDSTATIC
14078 "tag_old_static",
14079#endif
14080#ifdef FEAT_TAG_ANYWHITE
14081 "tag_any_white",
14082#endif
14083#ifdef FEAT_TCL
14084# ifndef DYNAMIC_TCL
14085 "tcl",
14086# endif
14087#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014088#ifdef FEAT_TERMGUICOLORS
14089 "termguicolors",
14090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091#ifdef TERMINFO
14092 "terminfo",
14093#endif
14094#ifdef FEAT_TERMRESPONSE
14095 "termresponse",
14096#endif
14097#ifdef FEAT_TEXTOBJ
14098 "textobjects",
14099#endif
14100#ifdef HAVE_TGETENT
14101 "tgetent",
14102#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014103#ifdef FEAT_TIMERS
14104 "timers",
14105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106#ifdef FEAT_TITLE
14107 "title",
14108#endif
14109#ifdef FEAT_TOOLBAR
14110 "toolbar",
14111#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014112#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14113 "unnamedplus",
14114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115#ifdef FEAT_USR_CMDS
14116 "user-commands", /* was accidentally included in 5.4 */
14117 "user_commands",
14118#endif
14119#ifdef FEAT_VIMINFO
14120 "viminfo",
14121#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014122#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 "vertsplit",
14124#endif
14125#ifdef FEAT_VIRTUALEDIT
14126 "virtualedit",
14127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129#ifdef FEAT_VISUALEXTRA
14130 "visualextra",
14131#endif
14132#ifdef FEAT_VREPLACE
14133 "vreplace",
14134#endif
14135#ifdef FEAT_WILDIGN
14136 "wildignore",
14137#endif
14138#ifdef FEAT_WILDMENU
14139 "wildmenu",
14140#endif
14141#ifdef FEAT_WINDOWS
14142 "windows",
14143#endif
14144#ifdef FEAT_WAK
14145 "winaltkeys",
14146#endif
14147#ifdef FEAT_WRITEBACKUP
14148 "writebackup",
14149#endif
14150#ifdef FEAT_XIM
14151 "xim",
14152#endif
14153#ifdef FEAT_XFONTSET
14154 "xfontset",
14155#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014156#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014157 "xpm",
14158 "xpm_w32", /* for backward compatibility */
14159#else
14160# if defined(HAVE_XPM)
14161 "xpm",
14162# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164#ifdef USE_XSMP
14165 "xsmp",
14166#endif
14167#ifdef USE_XSMP_INTERACT
14168 "xsmp_interact",
14169#endif
14170#ifdef FEAT_XCLIPBOARD
14171 "xterm_clipboard",
14172#endif
14173#ifdef FEAT_XTERM_SAVE
14174 "xterm_save",
14175#endif
14176#if defined(UNIX) && defined(FEAT_X11)
14177 "X11",
14178#endif
14179 NULL
14180 };
14181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183 for (i = 0; has_list[i] != NULL; ++i)
14184 if (STRICMP(name, has_list[i]) == 0)
14185 {
14186 n = TRUE;
14187 break;
14188 }
14189
14190 if (n == FALSE)
14191 {
14192 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014193 {
14194 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014195 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014196 && vim_isdigit(name[6])
14197 && vim_isdigit(name[8])
14198 && vim_isdigit(name[10]))
14199 {
14200 int major = atoi((char *)name + 6);
14201 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014202
14203 /* Expect "patch-9.9.01234". */
14204 n = (major < VIM_VERSION_MAJOR
14205 || (major == VIM_VERSION_MAJOR
14206 && (minor < VIM_VERSION_MINOR
14207 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014208 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014209 }
14210 else
14211 n = has_patch(atoi((char *)name + 5));
14212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 else if (STRICMP(name, "vim_starting") == 0)
14214 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014215#ifdef FEAT_MBYTE
14216 else if (STRICMP(name, "multi_byte_encoding") == 0)
14217 n = has_mbyte;
14218#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014219#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14220 else if (STRICMP(name, "balloon_multiline") == 0)
14221 n = multiline_balloon_available();
14222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223#ifdef DYNAMIC_TCL
14224 else if (STRICMP(name, "tcl") == 0)
14225 n = tcl_enabled(FALSE);
14226#endif
14227#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14228 else if (STRICMP(name, "iconv") == 0)
14229 n = iconv_enabled(FALSE);
14230#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014231#ifdef DYNAMIC_LUA
14232 else if (STRICMP(name, "lua") == 0)
14233 n = lua_enabled(FALSE);
14234#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014235#ifdef DYNAMIC_MZSCHEME
14236 else if (STRICMP(name, "mzscheme") == 0)
14237 n = mzscheme_enabled(FALSE);
14238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239#ifdef DYNAMIC_RUBY
14240 else if (STRICMP(name, "ruby") == 0)
14241 n = ruby_enabled(FALSE);
14242#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014243#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244#ifdef DYNAMIC_PYTHON
14245 else if (STRICMP(name, "python") == 0)
14246 n = python_enabled(FALSE);
14247#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014248#endif
14249#ifdef FEAT_PYTHON3
14250#ifdef DYNAMIC_PYTHON3
14251 else if (STRICMP(name, "python3") == 0)
14252 n = python3_enabled(FALSE);
14253#endif
14254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255#ifdef DYNAMIC_PERL
14256 else if (STRICMP(name, "perl") == 0)
14257 n = perl_enabled(FALSE);
14258#endif
14259#ifdef FEAT_GUI
14260 else if (STRICMP(name, "gui_running") == 0)
14261 n = (gui.in_use || gui.starting);
14262# ifdef FEAT_GUI_W32
14263 else if (STRICMP(name, "gui_win32s") == 0)
14264 n = gui_is_win32s();
14265# endif
14266# ifdef FEAT_BROWSE
14267 else if (STRICMP(name, "browse") == 0)
14268 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14269# endif
14270#endif
14271#ifdef FEAT_SYN_HL
14272 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014273 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274#endif
14275#if defined(WIN3264)
14276 else if (STRICMP(name, "win95") == 0)
14277 n = mch_windows95();
14278#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014279#ifdef FEAT_NETBEANS_INTG
14280 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014281 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 }
14284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286}
14287
14288/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014289 * "has_key()" function
14290 */
14291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014292f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014293{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014294 if (argvars[0].v_type != VAR_DICT)
14295 {
14296 EMSG(_(e_dictreq));
14297 return;
14298 }
14299 if (argvars[0].vval.v_dict == NULL)
14300 return;
14301
14302 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014303 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014304}
14305
14306/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014307 * "haslocaldir()" function
14308 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014310f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014311{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014312 win_T *wp = NULL;
14313
14314 wp = find_tabwin(&argvars[0], &argvars[1]);
14315 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014316}
14317
14318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 * "hasmapto()" function
14320 */
14321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014322f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323{
14324 char_u *name;
14325 char_u *mode;
14326 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014327 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014329 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014330 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 mode = (char_u *)"nvo";
14332 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014334 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014335 if (argvars[2].v_type != VAR_UNKNOWN)
14336 abbr = get_tv_number(&argvars[2]);
14337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338
Bram Moolenaar2c932302006-03-18 21:42:09 +000014339 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014342 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343}
14344
14345/*
14346 * "histadd()" function
14347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014349f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350{
14351#ifdef FEAT_CMDHIST
14352 int histype;
14353 char_u *str;
14354 char_u buf[NUMBUFLEN];
14355#endif
14356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 if (check_restricted() || check_secure())
14359 return;
14360#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14362 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 if (histype >= 0)
14364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 if (*str != NUL)
14367 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014368 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014370 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 return;
14372 }
14373 }
14374#endif
14375}
14376
14377/*
14378 * "histdel()" function
14379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014381f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382{
14383#ifdef FEAT_CMDHIST
14384 int n;
14385 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14389 if (str == NULL)
14390 n = 0;
14391 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014393 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014394 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398 else
14399 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014400 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014401 get_tv_string_buf(&argvars[1], buf));
14402 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403#endif
14404}
14405
14406/*
14407 * "histget()" function
14408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014410f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411{
14412#ifdef FEAT_CMDHIST
14413 int type;
14414 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14418 if (str == NULL)
14419 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 {
14422 type = get_histtype(str);
14423 if (argvars[1].v_type == VAR_UNKNOWN)
14424 idx = get_history_idx(type);
14425 else
14426 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14427 /* -1 on type error */
14428 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434}
14435
14436/*
14437 * "histnr()" function
14438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014440f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441{
14442 int i;
14443
14444#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445 char_u *history = get_tv_string_chk(&argvars[0]);
14446
14447 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 if (i >= HIST_CMD && i < HIST_COUNT)
14449 i = get_history_idx(i);
14450 else
14451#endif
14452 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014453 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454}
14455
14456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 * "highlightID(name)" function
14458 */
14459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014460f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463}
14464
14465/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014466 * "highlight_exists()" function
14467 */
14468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014469f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014470{
14471 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14472}
14473
14474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 * "hostname()" function
14476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014478f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479{
14480 char_u hostname[256];
14481
14482 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 rettv->v_type = VAR_STRING;
14484 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485}
14486
14487/*
14488 * iconv() function
14489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014491f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492{
14493#ifdef FEAT_MBYTE
14494 char_u buf1[NUMBUFLEN];
14495 char_u buf2[NUMBUFLEN];
14496 char_u *from, *to, *str;
14497 vimconv_T vimconv;
14498#endif
14499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500 rettv->v_type = VAR_STRING;
14501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502
14503#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 str = get_tv_string(&argvars[0]);
14505 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14506 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 vimconv.vc_type = CONV_NONE;
14508 convert_setup(&vimconv, from, to);
14509
14510 /* If the encodings are equal, no conversion needed. */
14511 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014512 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515
14516 convert_setup(&vimconv, NULL, NULL);
14517 vim_free(from);
14518 vim_free(to);
14519#endif
14520}
14521
14522/*
14523 * "indent()" function
14524 */
14525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014526f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527{
14528 linenr_T lnum;
14529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014532 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535}
14536
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014537/*
14538 * "index()" function
14539 */
14540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014541f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014542{
Bram Moolenaar33570922005-01-25 22:26:29 +000014543 list_T *l;
14544 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014545 long idx = 0;
14546 int ic = FALSE;
14547
14548 rettv->vval.v_number = -1;
14549 if (argvars[0].v_type != VAR_LIST)
14550 {
14551 EMSG(_(e_listreq));
14552 return;
14553 }
14554 l = argvars[0].vval.v_list;
14555 if (l != NULL)
14556 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014557 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014558 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 int error = FALSE;
14561
Bram Moolenaar758711c2005-02-02 23:11:38 +000014562 /* Start at specified item. Use the cached index that list_find()
14563 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014564 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014565 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014566 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 ic = get_tv_number_chk(&argvars[3], &error);
14568 if (error)
14569 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014570 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014571
Bram Moolenaar758711c2005-02-02 23:11:38 +000014572 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014573 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014574 {
14575 rettv->vval.v_number = idx;
14576 break;
14577 }
14578 }
14579}
14580
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581static int inputsecret_flag = 0;
14582
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014583static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014584
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014586 * This function is used by f_input() and f_inputdialog() functions. The third
14587 * argument to f_input() specifies the type of completion to use at the
14588 * prompt. The third argument to f_inputdialog() specifies the value to return
14589 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 */
14591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014592get_user_input(
14593 typval_T *argvars,
14594 typval_T *rettv,
14595 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014597 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 char_u *p = NULL;
14599 int c;
14600 char_u buf[NUMBUFLEN];
14601 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014602 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014603 int xp_type = EXPAND_NOTHING;
14604 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014607 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608
14609#ifdef NO_CONSOLE_INPUT
14610 /* While starting up, there is no place to enter text. */
14611 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613#endif
14614
14615 cmd_silent = FALSE; /* Want to see the prompt. */
14616 if (prompt != NULL)
14617 {
14618 /* Only the part of the message after the last NL is considered as
14619 * prompt for the command line */
14620 p = vim_strrchr(prompt, '\n');
14621 if (p == NULL)
14622 p = prompt;
14623 else
14624 {
14625 ++p;
14626 c = *p;
14627 *p = NUL;
14628 msg_start();
14629 msg_clr_eos();
14630 msg_puts_attr(prompt, echo_attr);
14631 msg_didout = FALSE;
14632 msg_starthere();
14633 *p = c;
14634 }
14635 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014637 if (argvars[1].v_type != VAR_UNKNOWN)
14638 {
14639 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14640 if (defstr != NULL)
14641 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014643 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014644 {
14645 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014646 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014647 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014648
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014649 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014650 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014651
Bram Moolenaar4463f292005-09-25 22:20:24 +000014652 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14653 if (xp_name == NULL)
14654 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014655
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014656 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014657
Bram Moolenaar4463f292005-09-25 22:20:24 +000014658 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14659 &xp_arg) == FAIL)
14660 return;
14661 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014662 }
14663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014664 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014665 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014666 int save_ex_normal_busy = ex_normal_busy;
14667 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014668 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014669 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14670 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014671 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014672 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014673 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014674 && argvars[1].v_type != VAR_UNKNOWN
14675 && argvars[2].v_type != VAR_UNKNOWN)
14676 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14677 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014678
14679 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014681 /* since the user typed this, no need to wait for return */
14682 need_wait_return = FALSE;
14683 msg_didout = FALSE;
14684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685 cmd_silent = cmd_silent_save;
14686}
14687
14688/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014689 * "input()" function
14690 * Also handles inputsecret() when inputsecret is set.
14691 */
14692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014693f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014694{
14695 get_user_input(argvars, rettv, FALSE);
14696}
14697
14698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699 * "inputdialog()" function
14700 */
14701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014702f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703{
14704#if defined(FEAT_GUI_TEXTDIALOG)
14705 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14706 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14707 {
14708 char_u *message;
14709 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014710 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014712 message = get_tv_string_chk(&argvars[0]);
14713 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014714 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014715 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 else
14717 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014718 if (message != NULL && defstr != NULL
14719 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014720 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014721 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722 else
14723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014724 if (message != NULL && defstr != NULL
14725 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014726 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014727 rettv->vval.v_string = vim_strsave(
14728 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014732 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 }
14734 else
14735#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014736 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737}
14738
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014739/*
14740 * "inputlist()" function
14741 */
14742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014743f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014744{
14745 listitem_T *li;
14746 int selected;
14747 int mouse_used;
14748
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014749#ifdef NO_CONSOLE_INPUT
14750 /* While starting up, there is no place to enter text. */
14751 if (no_console_input())
14752 return;
14753#endif
14754 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14755 {
14756 EMSG2(_(e_listarg), "inputlist()");
14757 return;
14758 }
14759
14760 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014761 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014762 lines_left = Rows; /* avoid more prompt */
14763 msg_scroll = TRUE;
14764 msg_clr_eos();
14765
14766 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14767 {
14768 msg_puts(get_tv_string(&li->li_tv));
14769 msg_putchar('\n');
14770 }
14771
14772 /* Ask for choice. */
14773 selected = prompt_for_number(&mouse_used);
14774 if (mouse_used)
14775 selected -= lines_left;
14776
14777 rettv->vval.v_number = selected;
14778}
14779
14780
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14782
14783/*
14784 * "inputrestore()" function
14785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014787f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788{
14789 if (ga_userinput.ga_len > 0)
14790 {
14791 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14793 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014794 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 }
14796 else if (p_verbose > 1)
14797 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014798 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014799 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800 }
14801}
14802
14803/*
14804 * "inputsave()" function
14805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014807f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014809 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810 if (ga_grow(&ga_userinput, 1) == OK)
14811 {
14812 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14813 + ga_userinput.ga_len);
14814 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014815 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 }
14817 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014818 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819}
14820
14821/*
14822 * "inputsecret()" function
14823 */
14824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014825f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826{
14827 ++cmdline_star;
14828 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014829 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830 --cmdline_star;
14831 --inputsecret_flag;
14832}
14833
14834/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014835 * "insert()" function
14836 */
14837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014838f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014839{
14840 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014841 listitem_T *item;
14842 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014843 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014844
14845 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014846 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014847 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014848 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014849 {
14850 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014851 before = get_tv_number_chk(&argvars[2], &error);
14852 if (error)
14853 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014854
Bram Moolenaar758711c2005-02-02 23:11:38 +000014855 if (before == l->lv_len)
14856 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014857 else
14858 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014859 item = list_find(l, before);
14860 if (item == NULL)
14861 {
14862 EMSGN(_(e_listidx), before);
14863 l = NULL;
14864 }
14865 }
14866 if (l != NULL)
14867 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014868 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014869 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014870 }
14871 }
14872}
14873
14874/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014875 * "invert(expr)" function
14876 */
14877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014878f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014879{
14880 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14881}
14882
14883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884 * "isdirectory()" function
14885 */
14886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014887f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014889 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890}
14891
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014892/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014893 * "islocked()" function
14894 */
14895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014896f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014897{
14898 lval_T lv;
14899 char_u *end;
14900 dictitem_T *di;
14901
14902 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014903 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14904 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014905 if (end != NULL && lv.ll_name != NULL)
14906 {
14907 if (*end != NUL)
14908 EMSG(_(e_trailing));
14909 else
14910 {
14911 if (lv.ll_tv == NULL)
14912 {
14913 if (check_changedtick(lv.ll_name))
14914 rettv->vval.v_number = 1; /* always locked */
14915 else
14916 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014917 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014918 if (di != NULL)
14919 {
14920 /* Consider a variable locked when:
14921 * 1. the variable itself is locked
14922 * 2. the value of the variable is locked.
14923 * 3. the List or Dict value is locked.
14924 */
14925 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14926 || tv_islocked(&di->di_tv));
14927 }
14928 }
14929 }
14930 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014931 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014932 else if (lv.ll_newkey != NULL)
14933 EMSG2(_(e_dictkey), lv.ll_newkey);
14934 else if (lv.ll_list != NULL)
14935 /* List item. */
14936 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14937 else
14938 /* Dictionary item. */
14939 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14940 }
14941 }
14942
14943 clear_lval(&lv);
14944}
14945
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014946#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14947/*
14948 * "isnan()" function
14949 */
14950 static void
14951f_isnan(typval_T *argvars, typval_T *rettv)
14952{
14953 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14954 && isnan(argvars[0].vval.v_float);
14955}
14956#endif
14957
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014958static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014959
14960/*
14961 * Turn a dict into a list:
14962 * "what" == 0: list of keys
14963 * "what" == 1: list of values
14964 * "what" == 2: list of items
14965 */
14966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014967dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014968{
Bram Moolenaar33570922005-01-25 22:26:29 +000014969 list_T *l2;
14970 dictitem_T *di;
14971 hashitem_T *hi;
14972 listitem_T *li;
14973 listitem_T *li2;
14974 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014975 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014976
Bram Moolenaar8c711452005-01-14 21:53:12 +000014977 if (argvars[0].v_type != VAR_DICT)
14978 {
14979 EMSG(_(e_dictreq));
14980 return;
14981 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014982 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014983 return;
14984
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014985 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014986 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014987
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014988 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014989 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014990 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014991 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014992 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014993 --todo;
14994 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014995
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014996 li = listitem_alloc();
14997 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014998 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014999 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015000
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015001 if (what == 0)
15002 {
15003 /* keys() */
15004 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015005 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015006 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15007 }
15008 else if (what == 1)
15009 {
15010 /* values() */
15011 copy_tv(&di->di_tv, &li->li_tv);
15012 }
15013 else
15014 {
15015 /* items() */
15016 l2 = list_alloc();
15017 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015018 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015019 li->li_tv.vval.v_list = l2;
15020 if (l2 == NULL)
15021 break;
15022 ++l2->lv_refcount;
15023
15024 li2 = listitem_alloc();
15025 if (li2 == NULL)
15026 break;
15027 list_append(l2, li2);
15028 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015029 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015030 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15031
15032 li2 = listitem_alloc();
15033 if (li2 == NULL)
15034 break;
15035 list_append(l2, li2);
15036 copy_tv(&di->di_tv, &li2->li_tv);
15037 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015038 }
15039 }
15040}
15041
15042/*
15043 * "items(dict)" function
15044 */
15045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015046f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015047{
15048 dict_list(argvars, rettv, 2);
15049}
15050
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015051#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015052/*
15053 * Get the job from the argument.
15054 * Returns NULL if the job is invalid.
15055 */
15056 static job_T *
15057get_job_arg(typval_T *tv)
15058{
15059 job_T *job;
15060
15061 if (tv->v_type != VAR_JOB)
15062 {
15063 EMSG2(_(e_invarg2), get_tv_string(tv));
15064 return NULL;
15065 }
15066 job = tv->vval.v_job;
15067
15068 if (job == NULL)
15069 EMSG(_("E916: not a valid job"));
15070 return job;
15071}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015072
Bram Moolenaar835dc632016-02-07 14:27:38 +010015073/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015074 * "job_getchannel()" function
15075 */
15076 static void
15077f_job_getchannel(typval_T *argvars, typval_T *rettv)
15078{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015079 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015080
Bram Moolenaar65edff82016-02-21 16:40:11 +010015081 if (job != NULL)
15082 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015083 rettv->v_type = VAR_CHANNEL;
15084 rettv->vval.v_channel = job->jv_channel;
15085 if (job->jv_channel != NULL)
15086 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015087 }
15088}
15089
15090/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015091 * "job_info()" function
15092 */
15093 static void
15094f_job_info(typval_T *argvars, typval_T *rettv)
15095{
15096 job_T *job = get_job_arg(&argvars[0]);
15097
15098 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15099 job_info(job, rettv->vval.v_dict);
15100}
15101
15102/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015103 * "job_setoptions()" function
15104 */
15105 static void
15106f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15107{
15108 job_T *job = get_job_arg(&argvars[0]);
15109 jobopt_T opt;
15110
15111 if (job == NULL)
15112 return;
15113 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015114 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15115 job_set_options(job, &opt);
15116 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015117}
15118
15119/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015120 * "job_start()" function
15121 */
15122 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015123f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015124{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015125 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015126 if (check_restricted() || check_secure())
15127 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015128 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015129}
15130
15131/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015132 * "job_status()" function
15133 */
15134 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015135f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015136{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015137 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015138
Bram Moolenaar65edff82016-02-21 16:40:11 +010015139 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015140 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015141 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015142 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015143 }
15144}
15145
15146/*
15147 * "job_stop()" function
15148 */
15149 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015150f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015151{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015152 job_T *job = get_job_arg(&argvars[0]);
15153
15154 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015155 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015156}
15157#endif
15158
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015160 * "join()" function
15161 */
15162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015163f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015164{
15165 garray_T ga;
15166 char_u *sep;
15167
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015168 if (argvars[0].v_type != VAR_LIST)
15169 {
15170 EMSG(_(e_listreq));
15171 return;
15172 }
15173 if (argvars[0].vval.v_list == NULL)
15174 return;
15175 if (argvars[1].v_type == VAR_UNKNOWN)
15176 sep = (char_u *)" ";
15177 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015178 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015179
15180 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015181
15182 if (sep != NULL)
15183 {
15184 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015185 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015186 ga_append(&ga, NUL);
15187 rettv->vval.v_string = (char_u *)ga.ga_data;
15188 }
15189 else
15190 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015191}
15192
15193/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015194 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015195 */
15196 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015197f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015198{
15199 js_read_T reader;
15200
15201 reader.js_buf = get_tv_string(&argvars[0]);
15202 reader.js_fill = NULL;
15203 reader.js_used = 0;
15204 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15205 EMSG(_(e_invarg));
15206}
15207
15208/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015209 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015210 */
15211 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015212f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015213{
15214 rettv->v_type = VAR_STRING;
15215 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15216}
15217
15218/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015219 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015220 */
15221 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015222f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015223{
15224 js_read_T reader;
15225
15226 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015227 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015228 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015229 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015230 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015231}
15232
15233/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015234 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015235 */
15236 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015237f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015238{
15239 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015240 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015241}
15242
15243/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015244 * "keys()" function
15245 */
15246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015247f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015248{
15249 dict_list(argvars, rettv, 0);
15250}
15251
15252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253 * "last_buffer_nr()" function.
15254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015256f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257{
15258 int n = 0;
15259 buf_T *buf;
15260
15261 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15262 if (n < buf->b_fnum)
15263 n = buf->b_fnum;
15264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015265 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015266}
15267
15268/*
15269 * "len()" function
15270 */
15271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015272f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015273{
15274 switch (argvars[0].v_type)
15275 {
15276 case VAR_STRING:
15277 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015278 rettv->vval.v_number = (varnumber_T)STRLEN(
15279 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015280 break;
15281 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015282 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015283 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015284 case VAR_DICT:
15285 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15286 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015287 case VAR_UNKNOWN:
15288 case VAR_SPECIAL:
15289 case VAR_FLOAT:
15290 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015291 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015292 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015293 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015294 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015295 break;
15296 }
15297}
15298
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015299static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015300
15301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015302libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015303{
15304#ifdef FEAT_LIBCALL
15305 char_u *string_in;
15306 char_u **string_result;
15307 int nr_result;
15308#endif
15309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015310 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015311 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015312 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015313
15314 if (check_restricted() || check_secure())
15315 return;
15316
15317#ifdef FEAT_LIBCALL
15318 /* The first two args must be strings, otherwise its meaningless */
15319 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15320 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015321 string_in = NULL;
15322 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015323 string_in = argvars[2].vval.v_string;
15324 if (type == VAR_NUMBER)
15325 string_result = NULL;
15326 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015327 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015328 if (mch_libcall(argvars[0].vval.v_string,
15329 argvars[1].vval.v_string,
15330 string_in,
15331 argvars[2].vval.v_number,
15332 string_result,
15333 &nr_result) == OK
15334 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015335 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015336 }
15337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338}
15339
15340/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015341 * "libcall()" function
15342 */
15343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015344f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015345{
15346 libcall_common(argvars, rettv, VAR_STRING);
15347}
15348
15349/*
15350 * "libcallnr()" function
15351 */
15352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015353f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015354{
15355 libcall_common(argvars, rettv, VAR_NUMBER);
15356}
15357
15358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 * "line(string)" function
15360 */
15361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015362f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363{
15364 linenr_T lnum = 0;
15365 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015366 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015368 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 if (fp != NULL)
15370 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015371 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372}
15373
15374/*
15375 * "line2byte(lnum)" function
15376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015378f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379{
15380#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382#else
15383 linenr_T lnum;
15384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015387 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15390 if (rettv->vval.v_number >= 0)
15391 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392#endif
15393}
15394
15395/*
15396 * "lispindent(lnum)" function
15397 */
15398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015399f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400{
15401#ifdef FEAT_LISP
15402 pos_T pos;
15403 linenr_T lnum;
15404
15405 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15408 {
15409 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015410 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 curwin->w_cursor = pos;
15412 }
15413 else
15414#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416}
15417
15418/*
15419 * "localtime()" function
15420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015422f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015424 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425}
15426
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015427static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428
15429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015430get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431{
15432 char_u *keys;
15433 char_u *which;
15434 char_u buf[NUMBUFLEN];
15435 char_u *keys_buf = NULL;
15436 char_u *rhs;
15437 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015438 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015439 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015440 mapblock_T *mp;
15441 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442
15443 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 rettv->v_type = VAR_STRING;
15445 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 if (*keys == NUL)
15449 return;
15450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015451 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015453 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015454 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015455 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015456 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015457 if (argvars[3].v_type != VAR_UNKNOWN)
15458 get_dict = get_tv_number(&argvars[3]);
15459 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 else
15462 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015463 if (which == NULL)
15464 return;
15465
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 mode = get_map_mode(&which, 0);
15467
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015468 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015469 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015471
15472 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015474 /* Return a string. */
15475 if (rhs != NULL)
15476 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477
Bram Moolenaarbd743252010-10-20 21:23:33 +020015478 }
15479 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15480 {
15481 /* Return a dictionary. */
15482 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15483 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15484 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485
Bram Moolenaarbd743252010-10-20 21:23:33 +020015486 dict_add_nr_str(dict, "lhs", 0L, lhs);
15487 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15488 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15489 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15490 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15491 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15492 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015493 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015494 dict_add_nr_str(dict, "mode", 0L, mapmode);
15495
15496 vim_free(lhs);
15497 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 }
15499}
15500
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015501#ifdef FEAT_FLOAT
15502/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015503 * "log()" function
15504 */
15505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015506f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015507{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015508 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015509
15510 rettv->v_type = VAR_FLOAT;
15511 if (get_float_arg(argvars, &f) == OK)
15512 rettv->vval.v_float = log(f);
15513 else
15514 rettv->vval.v_float = 0.0;
15515}
15516
15517/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015518 * "log10()" function
15519 */
15520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015521f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015522{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015523 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015524
15525 rettv->v_type = VAR_FLOAT;
15526 if (get_float_arg(argvars, &f) == OK)
15527 rettv->vval.v_float = log10(f);
15528 else
15529 rettv->vval.v_float = 0.0;
15530}
15531#endif
15532
Bram Moolenaar1dced572012-04-05 16:54:08 +020015533#ifdef FEAT_LUA
15534/*
15535 * "luaeval()" function
15536 */
15537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015538f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015539{
15540 char_u *str;
15541 char_u buf[NUMBUFLEN];
15542
15543 str = get_tv_string_buf(&argvars[0], buf);
15544 do_luaeval(str, argvars + 1, rettv);
15545}
15546#endif
15547
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015549 * "map()" function
15550 */
15551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015552f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015553{
15554 filter_map(argvars, rettv, TRUE);
15555}
15556
15557/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015558 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 */
15560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015561f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015563 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564}
15565
15566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 */
15569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015570f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573}
15574
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015575static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576
15577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015578find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015580 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015581 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015582 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 char_u *pat;
15584 regmatch_T regmatch;
15585 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015586 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 char_u *save_cpo;
15588 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015589 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015590 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015591 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015592 list_T *l = NULL;
15593 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015594 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015595 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596
15597 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15598 save_cpo = p_cpo;
15599 p_cpo = (char_u *)"";
15600
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015601 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015602 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015603 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015604 /* type 3: return empty list when there are no matches.
15605 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015606 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015607 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015608 if (type == 4
15609 && (list_append_string(rettv->vval.v_list,
15610 (char_u *)"", 0) == FAIL
15611 || list_append_number(rettv->vval.v_list,
15612 (varnumber_T)-1) == FAIL
15613 || list_append_number(rettv->vval.v_list,
15614 (varnumber_T)-1) == FAIL
15615 || list_append_number(rettv->vval.v_list,
15616 (varnumber_T)-1) == FAIL))
15617 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015618 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015619 rettv->vval.v_list = NULL;
15620 goto theend;
15621 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015622 }
15623 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015625 rettv->v_type = VAR_STRING;
15626 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015629 if (argvars[0].v_type == VAR_LIST)
15630 {
15631 if ((l = argvars[0].vval.v_list) == NULL)
15632 goto theend;
15633 li = l->lv_first;
15634 }
15635 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015636 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015637 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015638 len = (long)STRLEN(str);
15639 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015641 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15642 if (pat == NULL)
15643 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015644
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015645 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015647 int error = FALSE;
15648
15649 start = get_tv_number_chk(&argvars[2], &error);
15650 if (error)
15651 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015652 if (l != NULL)
15653 {
15654 li = list_find(l, start);
15655 if (li == NULL)
15656 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015657 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015658 }
15659 else
15660 {
15661 if (start < 0)
15662 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015663 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015664 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015665 /* When "count" argument is there ignore matches before "start",
15666 * otherwise skip part of the string. Differs when pattern is "^"
15667 * or "\<". */
15668 if (argvars[3].v_type != VAR_UNKNOWN)
15669 startcol = start;
15670 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015671 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015672 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015673 len -= start;
15674 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015675 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015676
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015677 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015678 nth = get_tv_number_chk(&argvars[3], &error);
15679 if (error)
15680 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 }
15682
15683 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15684 if (regmatch.regprog != NULL)
15685 {
15686 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015687
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015688 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015689 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015690 if (l != NULL)
15691 {
15692 if (li == NULL)
15693 {
15694 match = FALSE;
15695 break;
15696 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015697 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015698 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015699 if (str == NULL)
15700 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015701 }
15702
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015703 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015704
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015705 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015706 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015707 if (l == NULL && !match)
15708 break;
15709
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015710 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015711 if (l != NULL)
15712 {
15713 li = li->li_next;
15714 ++idx;
15715 }
15716 else
15717 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015718#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015719 startcol = (colnr_T)(regmatch.startp[0]
15720 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015721#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015722 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015723#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015724 if (startcol > (colnr_T)len
15725 || str + startcol <= regmatch.startp[0])
15726 {
15727 match = FALSE;
15728 break;
15729 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015730 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015731 }
15732
15733 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015735 if (type == 4)
15736 {
15737 listitem_T *li1 = rettv->vval.v_list->lv_first;
15738 listitem_T *li2 = li1->li_next;
15739 listitem_T *li3 = li2->li_next;
15740 listitem_T *li4 = li3->li_next;
15741
15742 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15743 (int)(regmatch.endp[0] - regmatch.startp[0]));
15744 li3->li_tv.vval.v_number =
15745 (varnumber_T)(regmatch.startp[0] - expr);
15746 li4->li_tv.vval.v_number =
15747 (varnumber_T)(regmatch.endp[0] - expr);
15748 if (l != NULL)
15749 li2->li_tv.vval.v_number = (varnumber_T)idx;
15750 }
15751 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015752 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015753 int i;
15754
15755 /* return list with matched string and submatches */
15756 for (i = 0; i < NSUBEXP; ++i)
15757 {
15758 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015759 {
15760 if (list_append_string(rettv->vval.v_list,
15761 (char_u *)"", 0) == FAIL)
15762 break;
15763 }
15764 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015765 regmatch.startp[i],
15766 (int)(regmatch.endp[i] - regmatch.startp[i]))
15767 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015768 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015769 }
15770 }
15771 else if (type == 2)
15772 {
15773 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015774 if (l != NULL)
15775 copy_tv(&li->li_tv, rettv);
15776 else
15777 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015779 }
15780 else if (l != NULL)
15781 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 else
15783 {
15784 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015785 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 (varnumber_T)(regmatch.startp[0] - str);
15787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015788 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015790 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 }
15792 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015793 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794 }
15795
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015796 if (type == 4 && l == NULL)
15797 /* matchstrpos() without a list: drop the second item. */
15798 listitem_remove(rettv->vval.v_list,
15799 rettv->vval.v_list->lv_first->li_next);
15800
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015802 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 p_cpo = save_cpo;
15804}
15805
15806/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015807 * "match()" function
15808 */
15809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015810f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015811{
15812 find_some_match(argvars, rettv, 1);
15813}
15814
15815/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015816 * "matchadd()" function
15817 */
15818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015819f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015820{
15821#ifdef FEAT_SEARCH_EXTRA
15822 char_u buf[NUMBUFLEN];
15823 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15824 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15825 int prio = 10; /* default priority */
15826 int id = -1;
15827 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015828 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015829
15830 rettv->vval.v_number = -1;
15831
15832 if (grp == NULL || pat == NULL)
15833 return;
15834 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015835 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015836 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015837 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015838 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015839 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015840 if (argvars[4].v_type != VAR_UNKNOWN)
15841 {
15842 if (argvars[4].v_type != VAR_DICT)
15843 {
15844 EMSG(_(e_dictreq));
15845 return;
15846 }
15847 if (dict_find(argvars[4].vval.v_dict,
15848 (char_u *)"conceal", -1) != NULL)
15849 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15850 (char_u *)"conceal", FALSE);
15851 }
15852 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015853 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015854 if (error == TRUE)
15855 return;
15856 if (id >= 1 && id <= 3)
15857 {
15858 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15859 return;
15860 }
15861
Bram Moolenaar6561d522015-07-21 15:48:27 +020015862 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15863 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015864#endif
15865}
15866
15867/*
15868 * "matchaddpos()" function
15869 */
15870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015871f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015872{
15873#ifdef FEAT_SEARCH_EXTRA
15874 char_u buf[NUMBUFLEN];
15875 char_u *group;
15876 int prio = 10;
15877 int id = -1;
15878 int error = FALSE;
15879 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015880 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015881
15882 rettv->vval.v_number = -1;
15883
15884 group = get_tv_string_buf_chk(&argvars[0], buf);
15885 if (group == NULL)
15886 return;
15887
15888 if (argvars[1].v_type != VAR_LIST)
15889 {
15890 EMSG2(_(e_listarg), "matchaddpos()");
15891 return;
15892 }
15893 l = argvars[1].vval.v_list;
15894 if (l == NULL)
15895 return;
15896
15897 if (argvars[2].v_type != VAR_UNKNOWN)
15898 {
15899 prio = get_tv_number_chk(&argvars[2], &error);
15900 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015901 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015902 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015903 if (argvars[4].v_type != VAR_UNKNOWN)
15904 {
15905 if (argvars[4].v_type != VAR_DICT)
15906 {
15907 EMSG(_(e_dictreq));
15908 return;
15909 }
15910 if (dict_find(argvars[4].vval.v_dict,
15911 (char_u *)"conceal", -1) != NULL)
15912 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15913 (char_u *)"conceal", FALSE);
15914 }
15915 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015916 }
15917 if (error == TRUE)
15918 return;
15919
15920 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15921 if (id == 1 || id == 2)
15922 {
15923 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15924 return;
15925 }
15926
Bram Moolenaar6561d522015-07-21 15:48:27 +020015927 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15928 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015929#endif
15930}
15931
15932/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015933 * "matcharg()" function
15934 */
15935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015936f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015937{
15938 if (rettv_list_alloc(rettv) == OK)
15939 {
15940#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015941 int id = get_tv_number(&argvars[0]);
15942 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015943
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015944 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015945 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015946 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15947 {
15948 list_append_string(rettv->vval.v_list,
15949 syn_id2name(m->hlg_id), -1);
15950 list_append_string(rettv->vval.v_list, m->pattern, -1);
15951 }
15952 else
15953 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015954 list_append_string(rettv->vval.v_list, NULL, -1);
15955 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015956 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015957 }
15958#endif
15959 }
15960}
15961
15962/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015963 * "matchdelete()" function
15964 */
15965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015966f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015967{
15968#ifdef FEAT_SEARCH_EXTRA
15969 rettv->vval.v_number = match_delete(curwin,
15970 (int)get_tv_number(&argvars[0]), TRUE);
15971#endif
15972}
15973
15974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015975 * "matchend()" function
15976 */
15977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015978f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015979{
15980 find_some_match(argvars, rettv, 0);
15981}
15982
15983/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015984 * "matchlist()" function
15985 */
15986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015987f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015988{
15989 find_some_match(argvars, rettv, 3);
15990}
15991
15992/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015993 * "matchstr()" function
15994 */
15995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015996f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015997{
15998 find_some_match(argvars, rettv, 2);
15999}
16000
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016001/*
16002 * "matchstrpos()" function
16003 */
16004 static void
16005f_matchstrpos(typval_T *argvars, typval_T *rettv)
16006{
16007 find_some_match(argvars, rettv, 4);
16008}
16009
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016010static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016011
16012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016013max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016014{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016015 long n = 0;
16016 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016017 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016018
16019 if (argvars[0].v_type == VAR_LIST)
16020 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016021 list_T *l;
16022 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016023
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016024 l = argvars[0].vval.v_list;
16025 if (l != NULL)
16026 {
16027 li = l->lv_first;
16028 if (li != NULL)
16029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016030 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016031 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016032 {
16033 li = li->li_next;
16034 if (li == NULL)
16035 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016036 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016037 if (domax ? i > n : i < n)
16038 n = i;
16039 }
16040 }
16041 }
16042 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016043 else if (argvars[0].v_type == VAR_DICT)
16044 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016045 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016046 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016047 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016048 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016049
16050 d = argvars[0].vval.v_dict;
16051 if (d != NULL)
16052 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016053 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016054 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016055 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016056 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016057 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016058 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016059 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016060 if (first)
16061 {
16062 n = i;
16063 first = FALSE;
16064 }
16065 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016066 n = i;
16067 }
16068 }
16069 }
16070 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016071 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016072 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016073 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016074}
16075
16076/*
16077 * "max()" function
16078 */
16079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016080f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016081{
16082 max_min(argvars, rettv, TRUE);
16083}
16084
16085/*
16086 * "min()" function
16087 */
16088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016089f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016090{
16091 max_min(argvars, rettv, FALSE);
16092}
16093
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016094static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016095
16096/*
16097 * Create the directory in which "dir" is located, and higher levels when
16098 * needed.
16099 */
16100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016101mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016102{
16103 char_u *p;
16104 char_u *updir;
16105 int r = FAIL;
16106
16107 /* Get end of directory name in "dir".
16108 * We're done when it's "/" or "c:/". */
16109 p = gettail_sep(dir);
16110 if (p <= get_past_head(dir))
16111 return OK;
16112
16113 /* If the directory exists we're done. Otherwise: create it.*/
16114 updir = vim_strnsave(dir, (int)(p - dir));
16115 if (updir == NULL)
16116 return FAIL;
16117 if (mch_isdir(updir))
16118 r = OK;
16119 else if (mkdir_recurse(updir, prot) == OK)
16120 r = vim_mkdir_emsg(updir, prot);
16121 vim_free(updir);
16122 return r;
16123}
16124
16125#ifdef vim_mkdir
16126/*
16127 * "mkdir()" function
16128 */
16129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016130f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016131{
16132 char_u *dir;
16133 char_u buf[NUMBUFLEN];
16134 int prot = 0755;
16135
16136 rettv->vval.v_number = FAIL;
16137 if (check_restricted() || check_secure())
16138 return;
16139
16140 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016141 if (*dir == NUL)
16142 rettv->vval.v_number = FAIL;
16143 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016144 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016145 if (*gettail(dir) == NUL)
16146 /* remove trailing slashes */
16147 *gettail_sep(dir) = NUL;
16148
16149 if (argvars[1].v_type != VAR_UNKNOWN)
16150 {
16151 if (argvars[2].v_type != VAR_UNKNOWN)
16152 prot = get_tv_number_chk(&argvars[2], NULL);
16153 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16154 mkdir_recurse(dir, prot);
16155 }
16156 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016157 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016158}
16159#endif
16160
Bram Moolenaar0d660222005-01-07 21:51:51 +000016161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 * "mode()" function
16163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016165f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016167 char_u buf[3];
16168
16169 buf[1] = NUL;
16170 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 if (VIsual_active)
16173 {
16174 if (VIsual_select)
16175 buf[0] = VIsual_mode + 's' - 'v';
16176 else
16177 buf[0] = VIsual_mode;
16178 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016179 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016180 || State == CONFIRM)
16181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016183 if (State == ASKMORE)
16184 buf[1] = 'm';
16185 else if (State == CONFIRM)
16186 buf[1] = '?';
16187 }
16188 else if (State == EXTERNCMD)
16189 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 else if (State & INSERT)
16191 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016192#ifdef FEAT_VREPLACE
16193 if (State & VREPLACE_FLAG)
16194 {
16195 buf[0] = 'R';
16196 buf[1] = 'v';
16197 }
16198 else
16199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200 if (State & REPLACE_FLAG)
16201 buf[0] = 'R';
16202 else
16203 buf[0] = 'i';
16204 }
16205 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016208 if (exmode_active)
16209 buf[1] = 'v';
16210 }
16211 else if (exmode_active)
16212 {
16213 buf[0] = 'c';
16214 buf[1] = 'e';
16215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016219 if (finish_op)
16220 buf[1] = 'o';
16221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016223 /* Clear out the minor mode when the argument is not a non-zero number or
16224 * non-empty string. */
16225 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016226 buf[1] = NUL;
16227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016228 rettv->vval.v_string = vim_strsave(buf);
16229 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230}
16231
Bram Moolenaar429fa852013-04-15 12:27:36 +020016232#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016233/*
16234 * "mzeval()" function
16235 */
16236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016237f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016238{
16239 char_u *str;
16240 char_u buf[NUMBUFLEN];
16241
16242 str = get_tv_string_buf(&argvars[0], buf);
16243 do_mzeval(str, rettv);
16244}
Bram Moolenaar75676462013-01-30 14:55:42 +010016245
16246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016247mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016248{
16249 typval_T argvars[3];
16250
16251 argvars[0].v_type = VAR_STRING;
16252 argvars[0].vval.v_string = name;
16253 copy_tv(args, &argvars[1]);
16254 argvars[2].v_type = VAR_UNKNOWN;
16255 f_call(argvars, rettv);
16256 clear_tv(&argvars[1]);
16257}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016258#endif
16259
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261 * "nextnonblank()" function
16262 */
16263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016264f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016265{
16266 linenr_T lnum;
16267
16268 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016270 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016271 {
16272 lnum = 0;
16273 break;
16274 }
16275 if (*skipwhite(ml_get(lnum)) != NUL)
16276 break;
16277 }
16278 rettv->vval.v_number = lnum;
16279}
16280
16281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 * "nr2char()" function
16283 */
16284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016285f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286{
16287 char_u buf[NUMBUFLEN];
16288
16289#ifdef FEAT_MBYTE
16290 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016291 {
16292 int utf8 = 0;
16293
16294 if (argvars[1].v_type != VAR_UNKNOWN)
16295 utf8 = get_tv_number_chk(&argvars[1], NULL);
16296 if (utf8)
16297 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16298 else
16299 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 else
16302#endif
16303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016304 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305 buf[1] = NUL;
16306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016307 rettv->v_type = VAR_STRING;
16308 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016309}
16310
16311/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016312 * "or(expr, expr)" function
16313 */
16314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016315f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016316{
16317 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16318 | get_tv_number_chk(&argvars[1], NULL);
16319}
16320
16321/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016322 * "pathshorten()" function
16323 */
16324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016325f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016326{
16327 char_u *p;
16328
16329 rettv->v_type = VAR_STRING;
16330 p = get_tv_string_chk(&argvars[0]);
16331 if (p == NULL)
16332 rettv->vval.v_string = NULL;
16333 else
16334 {
16335 p = vim_strsave(p);
16336 rettv->vval.v_string = p;
16337 if (p != NULL)
16338 shorten_dir(p);
16339 }
16340}
16341
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016342#ifdef FEAT_PERL
16343/*
16344 * "perleval()" function
16345 */
16346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016347f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016348{
16349 char_u *str;
16350 char_u buf[NUMBUFLEN];
16351
16352 str = get_tv_string_buf(&argvars[0], buf);
16353 do_perleval(str, rettv);
16354}
16355#endif
16356
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016357#ifdef FEAT_FLOAT
16358/*
16359 * "pow()" function
16360 */
16361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016362f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016363{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016364 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016365
16366 rettv->v_type = VAR_FLOAT;
16367 if (get_float_arg(argvars, &fx) == OK
16368 && get_float_arg(&argvars[1], &fy) == OK)
16369 rettv->vval.v_float = pow(fx, fy);
16370 else
16371 rettv->vval.v_float = 0.0;
16372}
16373#endif
16374
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016375/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376 * "prevnonblank()" function
16377 */
16378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016379f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016380{
16381 linenr_T lnum;
16382
16383 lnum = get_tv_lnum(argvars);
16384 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16385 lnum = 0;
16386 else
16387 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16388 --lnum;
16389 rettv->vval.v_number = lnum;
16390}
16391
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016392/* This dummy va_list is here because:
16393 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16394 * - locally in the function results in a "used before set" warning
16395 * - using va_start() to initialize it gives "function with fixed args" error */
16396static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016397
Bram Moolenaar8c711452005-01-14 21:53:12 +000016398/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016399 * "printf()" function
16400 */
16401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016402f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016403{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016404 char_u buf[NUMBUFLEN];
16405 int len;
16406 char_u *s;
16407 int saved_did_emsg = did_emsg;
16408 char *fmt;
16409
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016410 rettv->v_type = VAR_STRING;
16411 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016412
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016413 /* Get the required length, allocate the buffer and do it for real. */
16414 did_emsg = FALSE;
16415 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16416 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16417 if (!did_emsg)
16418 {
16419 s = alloc(len + 1);
16420 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016421 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016422 rettv->vval.v_string = s;
16423 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016424 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016425 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016426 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016427}
16428
16429/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016430 * "pumvisible()" function
16431 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016433f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016434{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016435#ifdef FEAT_INS_EXPAND
16436 if (pum_visible())
16437 rettv->vval.v_number = 1;
16438#endif
16439}
16440
Bram Moolenaardb913952012-06-29 12:54:53 +020016441#ifdef FEAT_PYTHON3
16442/*
16443 * "py3eval()" function
16444 */
16445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016446f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016447{
16448 char_u *str;
16449 char_u buf[NUMBUFLEN];
16450
16451 str = get_tv_string_buf(&argvars[0], buf);
16452 do_py3eval(str, rettv);
16453}
16454#endif
16455
16456#ifdef FEAT_PYTHON
16457/*
16458 * "pyeval()" function
16459 */
16460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016461f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016462{
16463 char_u *str;
16464 char_u buf[NUMBUFLEN];
16465
16466 str = get_tv_string_buf(&argvars[0], buf);
16467 do_pyeval(str, rettv);
16468}
16469#endif
16470
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016471/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016472 * "range()" function
16473 */
16474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016475f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016476{
16477 long start;
16478 long end;
16479 long stride = 1;
16480 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016481 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016483 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016484 if (argvars[1].v_type == VAR_UNKNOWN)
16485 {
16486 end = start - 1;
16487 start = 0;
16488 }
16489 else
16490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016491 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016492 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016493 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016494 }
16495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016496 if (error)
16497 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016498 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016499 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016500 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016501 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016502 else
16503 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016504 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016505 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016506 if (list_append_number(rettv->vval.v_list,
16507 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016508 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016509 }
16510}
16511
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016512/*
16513 * "readfile()" function
16514 */
16515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016516f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016517{
16518 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016519 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016520 char_u *fname;
16521 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016522 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16523 int io_size = sizeof(buf);
16524 int readlen; /* size of last fread() */
16525 char_u *prev = NULL; /* previously read bytes, if any */
16526 long prevlen = 0; /* length of data in prev */
16527 long prevsize = 0; /* size of prev buffer */
16528 long maxline = MAXLNUM;
16529 long cnt = 0;
16530 char_u *p; /* position in buf */
16531 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016532
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016533 if (argvars[1].v_type != VAR_UNKNOWN)
16534 {
16535 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16536 binary = TRUE;
16537 if (argvars[2].v_type != VAR_UNKNOWN)
16538 maxline = get_tv_number(&argvars[2]);
16539 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016540
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016541 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016542 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016543
16544 /* Always open the file in binary mode, library functions have a mind of
16545 * their own about CR-LF conversion. */
16546 fname = get_tv_string(&argvars[0]);
16547 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16548 {
16549 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16550 return;
16551 }
16552
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016553 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016554 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016555 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016557 /* This for loop processes what was read, but is also entered at end
16558 * of file so that either:
16559 * - an incomplete line gets written
16560 * - a "binary" file gets an empty line at the end if it ends in a
16561 * newline. */
16562 for (p = buf, start = buf;
16563 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16564 ++p)
16565 {
16566 if (*p == '\n' || readlen <= 0)
16567 {
16568 listitem_T *li;
16569 char_u *s = NULL;
16570 long_u len = p - start;
16571
16572 /* Finished a line. Remove CRs before NL. */
16573 if (readlen > 0 && !binary)
16574 {
16575 while (len > 0 && start[len - 1] == '\r')
16576 --len;
16577 /* removal may cross back to the "prev" string */
16578 if (len == 0)
16579 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16580 --prevlen;
16581 }
16582 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016583 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016584 else
16585 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016586 /* Change "prev" buffer to be the right size. This way
16587 * the bytes are only copied once, and very long lines are
16588 * allocated only once. */
16589 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016590 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016591 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016592 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016593 prev = NULL; /* the list will own the string */
16594 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016595 }
16596 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016597 if (s == NULL)
16598 {
16599 do_outofmem_msg((long_u) prevlen + len + 1);
16600 failed = TRUE;
16601 break;
16602 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016603
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016604 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016605 {
16606 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016607 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016608 break;
16609 }
16610 li->li_tv.v_type = VAR_STRING;
16611 li->li_tv.v_lock = 0;
16612 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016613 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016614
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016615 start = p + 1; /* step over newline */
16616 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016617 break;
16618 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016619 else if (*p == NUL)
16620 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016621#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016622 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16623 * when finding the BF and check the previous two bytes. */
16624 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016625 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016626 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16627 * + 1, these may be in the "prev" string. */
16628 char_u back1 = p >= buf + 1 ? p[-1]
16629 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16630 char_u back2 = p >= buf + 2 ? p[-2]
16631 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16632 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016633
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016634 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016635 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016636 char_u *dest = p - 2;
16637
16638 /* Usually a BOM is at the beginning of a file, and so at
16639 * the beginning of a line; then we can just step over it.
16640 */
16641 if (start == dest)
16642 start = p + 1;
16643 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016644 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016645 /* have to shuffle buf to close gap */
16646 int adjust_prevlen = 0;
16647
16648 if (dest < buf)
16649 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016650 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016651 dest = buf;
16652 }
16653 if (readlen > p - buf + 1)
16654 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16655 readlen -= 3 - adjust_prevlen;
16656 prevlen -= adjust_prevlen;
16657 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016658 }
16659 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016660 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016661#endif
16662 } /* for */
16663
16664 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16665 break;
16666 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016667 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016668 /* There's part of a line in buf, store it in "prev". */
16669 if (p - start + prevlen >= prevsize)
16670 {
16671 /* need bigger "prev" buffer */
16672 char_u *newprev;
16673
16674 /* A common use case is ordinary text files and "prev" gets a
16675 * fragment of a line, so the first allocation is made
16676 * small, to avoid repeatedly 'allocing' large and
16677 * 'reallocing' small. */
16678 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016679 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016680 else
16681 {
16682 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016683 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016684 prevsize = grow50pc > growmin ? grow50pc : growmin;
16685 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016686 newprev = prev == NULL ? alloc(prevsize)
16687 : vim_realloc(prev, prevsize);
16688 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016689 {
16690 do_outofmem_msg((long_u)prevsize);
16691 failed = TRUE;
16692 break;
16693 }
16694 prev = newprev;
16695 }
16696 /* Add the line part to end of "prev". */
16697 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016698 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016699 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016700 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016701
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016702 /*
16703 * For a negative line count use only the lines at the end of the file,
16704 * free the rest.
16705 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016706 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016707 while (cnt > -maxline)
16708 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016709 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016710 --cnt;
16711 }
16712
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016713 if (failed)
16714 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016715 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016716 /* readfile doc says an empty list is returned on error */
16717 rettv->vval.v_list = list_alloc();
16718 }
16719
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016720 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016721 fclose(fd);
16722}
16723
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016724#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016725static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016726
16727/*
16728 * Convert a List to proftime_T.
16729 * Return FAIL when there is something wrong.
16730 */
16731 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016732list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016733{
16734 long n1, n2;
16735 int error = FALSE;
16736
16737 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16738 || arg->vval.v_list->lv_len != 2)
16739 return FAIL;
16740 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16741 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16742# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016743 tm->HighPart = n1;
16744 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016745# else
16746 tm->tv_sec = n1;
16747 tm->tv_usec = n2;
16748# endif
16749 return error ? FAIL : OK;
16750}
16751#endif /* FEAT_RELTIME */
16752
16753/*
16754 * "reltime()" function
16755 */
16756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016757f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016758{
16759#ifdef FEAT_RELTIME
16760 proftime_T res;
16761 proftime_T start;
16762
16763 if (argvars[0].v_type == VAR_UNKNOWN)
16764 {
16765 /* No arguments: get current time. */
16766 profile_start(&res);
16767 }
16768 else if (argvars[1].v_type == VAR_UNKNOWN)
16769 {
16770 if (list2proftime(&argvars[0], &res) == FAIL)
16771 return;
16772 profile_end(&res);
16773 }
16774 else
16775 {
16776 /* Two arguments: compute the difference. */
16777 if (list2proftime(&argvars[0], &start) == FAIL
16778 || list2proftime(&argvars[1], &res) == FAIL)
16779 return;
16780 profile_sub(&res, &start);
16781 }
16782
16783 if (rettv_list_alloc(rettv) == OK)
16784 {
16785 long n1, n2;
16786
16787# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016788 n1 = res.HighPart;
16789 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016790# else
16791 n1 = res.tv_sec;
16792 n2 = res.tv_usec;
16793# endif
16794 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16795 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16796 }
16797#endif
16798}
16799
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016800#ifdef FEAT_FLOAT
16801/*
16802 * "reltimefloat()" function
16803 */
16804 static void
16805f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16806{
16807# ifdef FEAT_RELTIME
16808 proftime_T tm;
16809# endif
16810
16811 rettv->v_type = VAR_FLOAT;
16812 rettv->vval.v_float = 0;
16813# ifdef FEAT_RELTIME
16814 if (list2proftime(&argvars[0], &tm) == OK)
16815 rettv->vval.v_float = profile_float(&tm);
16816# endif
16817}
16818#endif
16819
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016820/*
16821 * "reltimestr()" function
16822 */
16823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016824f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016825{
16826#ifdef FEAT_RELTIME
16827 proftime_T tm;
16828#endif
16829
16830 rettv->v_type = VAR_STRING;
16831 rettv->vval.v_string = NULL;
16832#ifdef FEAT_RELTIME
16833 if (list2proftime(&argvars[0], &tm) == OK)
16834 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16835#endif
16836}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016837
Bram Moolenaar0d660222005-01-07 21:51:51 +000016838#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016839static void make_connection(void);
16840static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016841
16842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016843make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016844{
16845 if (X_DISPLAY == NULL
16846# ifdef FEAT_GUI
16847 && !gui.in_use
16848# endif
16849 )
16850 {
16851 x_force_connect = TRUE;
16852 setup_term_clip();
16853 x_force_connect = FALSE;
16854 }
16855}
16856
16857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016858check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859{
16860 make_connection();
16861 if (X_DISPLAY == NULL)
16862 {
16863 EMSG(_("E240: No connection to Vim server"));
16864 return FAIL;
16865 }
16866 return OK;
16867}
16868#endif
16869
16870#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016872remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873{
16874 char_u *server_name;
16875 char_u *keys;
16876 char_u *r = NULL;
16877 char_u buf[NUMBUFLEN];
16878# ifdef WIN32
16879 HWND w;
16880# else
16881 Window w;
16882# endif
16883
16884 if (check_restricted() || check_secure())
16885 return;
16886
16887# ifdef FEAT_X11
16888 if (check_connection() == FAIL)
16889 return;
16890# endif
16891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016892 server_name = get_tv_string_chk(&argvars[0]);
16893 if (server_name == NULL)
16894 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895 keys = get_tv_string_buf(&argvars[1], buf);
16896# ifdef WIN32
16897 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16898# else
16899 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16900 < 0)
16901# endif
16902 {
16903 if (r != NULL)
16904 EMSG(r); /* sending worked but evaluation failed */
16905 else
16906 EMSG2(_("E241: Unable to send to %s"), server_name);
16907 return;
16908 }
16909
16910 rettv->vval.v_string = r;
16911
16912 if (argvars[2].v_type != VAR_UNKNOWN)
16913 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016914 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016915 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016916 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016918 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016919 v.di_tv.v_type = VAR_STRING;
16920 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016921 idvar = get_tv_string_chk(&argvars[2]);
16922 if (idvar != NULL)
16923 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016924 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925 }
16926}
16927#endif
16928
16929/*
16930 * "remote_expr()" function
16931 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016933f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934{
16935 rettv->v_type = VAR_STRING;
16936 rettv->vval.v_string = NULL;
16937#ifdef FEAT_CLIENTSERVER
16938 remote_common(argvars, rettv, TRUE);
16939#endif
16940}
16941
16942/*
16943 * "remote_foreground()" function
16944 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016946f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948#ifdef FEAT_CLIENTSERVER
16949# ifdef WIN32
16950 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016951 {
16952 char_u *server_name = get_tv_string_chk(&argvars[0]);
16953
16954 if (server_name != NULL)
16955 serverForeground(server_name);
16956 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957# else
16958 /* Send a foreground() expression to the server. */
16959 argvars[1].v_type = VAR_STRING;
16960 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16961 argvars[2].v_type = VAR_UNKNOWN;
16962 remote_common(argvars, rettv, TRUE);
16963 vim_free(argvars[1].vval.v_string);
16964# endif
16965#endif
16966}
16967
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016969f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970{
16971#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016972 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 char_u *s = NULL;
16974# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016975 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016977 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978
16979 if (check_restricted() || check_secure())
16980 {
16981 rettv->vval.v_number = -1;
16982 return;
16983 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 serverid = get_tv_string_chk(&argvars[0]);
16985 if (serverid == NULL)
16986 {
16987 rettv->vval.v_number = -1;
16988 return; /* type error; errmsg already given */
16989 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016991 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 if (n == 0)
16993 rettv->vval.v_number = -1;
16994 else
16995 {
16996 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16997 rettv->vval.v_number = (s != NULL);
16998 }
16999# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 if (check_connection() == FAIL)
17001 return;
17002
17003 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017004 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017005# endif
17006
17007 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 char_u *retvar;
17010
Bram Moolenaar33570922005-01-25 22:26:29 +000017011 v.di_tv.v_type = VAR_STRING;
17012 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017013 retvar = get_tv_string_chk(&argvars[1]);
17014 if (retvar != NULL)
17015 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017016 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017 }
17018#else
17019 rettv->vval.v_number = -1;
17020#endif
17021}
17022
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017024f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025{
17026 char_u *r = NULL;
17027
17028#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029 char_u *serverid = get_tv_string_chk(&argvars[0]);
17030
17031 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032 {
17033# ifdef WIN32
17034 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017035 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017036
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017037 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 if (n != 0)
17039 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17040 if (r == NULL)
17041# else
17042 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017043 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017044# endif
17045 EMSG(_("E277: Unable to read a server reply"));
17046 }
17047#endif
17048 rettv->v_type = VAR_STRING;
17049 rettv->vval.v_string = r;
17050}
17051
17052/*
17053 * "remote_send()" function
17054 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017056f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057{
17058 rettv->v_type = VAR_STRING;
17059 rettv->vval.v_string = NULL;
17060#ifdef FEAT_CLIENTSERVER
17061 remote_common(argvars, rettv, FALSE);
17062#endif
17063}
17064
17065/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017066 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017067 */
17068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017069f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017070{
Bram Moolenaar33570922005-01-25 22:26:29 +000017071 list_T *l;
17072 listitem_T *item, *item2;
17073 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017074 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017075 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017076 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017077 dict_T *d;
17078 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017079 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017080
Bram Moolenaar8c711452005-01-14 21:53:12 +000017081 if (argvars[0].v_type == VAR_DICT)
17082 {
17083 if (argvars[2].v_type != VAR_UNKNOWN)
17084 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017085 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017086 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 key = get_tv_string_chk(&argvars[1]);
17089 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017091 di = dict_find(d, key, -1);
17092 if (di == NULL)
17093 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017094 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17095 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017096 {
17097 *rettv = di->di_tv;
17098 init_tv(&di->di_tv);
17099 dictitem_remove(d, di);
17100 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017101 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017102 }
17103 }
17104 else if (argvars[0].v_type != VAR_LIST)
17105 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017106 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017107 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017109 int error = FALSE;
17110
17111 idx = get_tv_number_chk(&argvars[1], &error);
17112 if (error)
17113 ; /* type error: do nothing, errmsg already given */
17114 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017115 EMSGN(_(e_listidx), idx);
17116 else
17117 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017118 if (argvars[2].v_type == VAR_UNKNOWN)
17119 {
17120 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017121 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017122 *rettv = item->li_tv;
17123 vim_free(item);
17124 }
17125 else
17126 {
17127 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017128 end = get_tv_number_chk(&argvars[2], &error);
17129 if (error)
17130 ; /* type error: do nothing */
17131 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017132 EMSGN(_(e_listidx), end);
17133 else
17134 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017135 int cnt = 0;
17136
17137 for (li = item; li != NULL; li = li->li_next)
17138 {
17139 ++cnt;
17140 if (li == item2)
17141 break;
17142 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017143 if (li == NULL) /* didn't find "item2" after "item" */
17144 EMSG(_(e_invrange));
17145 else
17146 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017147 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017148 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017149 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017150 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017151 l->lv_first = item;
17152 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017153 item->li_prev = NULL;
17154 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017155 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017156 }
17157 }
17158 }
17159 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017160 }
17161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162}
17163
17164/*
17165 * "rename({from}, {to})" function
17166 */
17167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017168f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169{
17170 char_u buf[NUMBUFLEN];
17171
17172 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017173 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017175 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17176 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177}
17178
17179/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017180 * "repeat()" function
17181 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017183f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017184{
17185 char_u *p;
17186 int n;
17187 int slen;
17188 int len;
17189 char_u *r;
17190 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017191
17192 n = get_tv_number(&argvars[1]);
17193 if (argvars[0].v_type == VAR_LIST)
17194 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017195 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017196 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017197 if (list_extend(rettv->vval.v_list,
17198 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017199 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017200 }
17201 else
17202 {
17203 p = get_tv_string(&argvars[0]);
17204 rettv->v_type = VAR_STRING;
17205 rettv->vval.v_string = NULL;
17206
17207 slen = (int)STRLEN(p);
17208 len = slen * n;
17209 if (len <= 0)
17210 return;
17211
17212 r = alloc(len + 1);
17213 if (r != NULL)
17214 {
17215 for (i = 0; i < n; i++)
17216 mch_memmove(r + i * slen, p, (size_t)slen);
17217 r[len] = NUL;
17218 }
17219
17220 rettv->vval.v_string = r;
17221 }
17222}
17223
17224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 * "resolve()" function
17226 */
17227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017228f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229{
17230 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017231#ifdef HAVE_READLINK
17232 char_u *buf = NULL;
17233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017235 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236#ifdef FEAT_SHORTCUT
17237 {
17238 char_u *v = NULL;
17239
17240 v = mch_resolve_shortcut(p);
17241 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017242 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017244 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 }
17246#else
17247# ifdef HAVE_READLINK
17248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 char_u *cpy;
17250 int len;
17251 char_u *remain = NULL;
17252 char_u *q;
17253 int is_relative_to_current = FALSE;
17254 int has_trailing_pathsep = FALSE;
17255 int limit = 100;
17256
17257 p = vim_strsave(p);
17258
17259 if (p[0] == '.' && (vim_ispathsep(p[1])
17260 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17261 is_relative_to_current = TRUE;
17262
17263 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017264 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017267 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269
17270 q = getnextcomp(p);
17271 if (*q != NUL)
17272 {
17273 /* Separate the first path component in "p", and keep the
17274 * remainder (beginning with the path separator). */
17275 remain = vim_strsave(q - 1);
17276 q[-1] = NUL;
17277 }
17278
Bram Moolenaard9462e32011-04-11 21:35:11 +020017279 buf = alloc(MAXPATHL + 1);
17280 if (buf == NULL)
17281 goto fail;
17282
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 for (;;)
17284 {
17285 for (;;)
17286 {
17287 len = readlink((char *)p, (char *)buf, MAXPATHL);
17288 if (len <= 0)
17289 break;
17290 buf[len] = NUL;
17291
17292 if (limit-- == 0)
17293 {
17294 vim_free(p);
17295 vim_free(remain);
17296 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 goto fail;
17299 }
17300
17301 /* Ensure that the result will have a trailing path separator
17302 * if the argument has one. */
17303 if (remain == NULL && has_trailing_pathsep)
17304 add_pathsep(buf);
17305
17306 /* Separate the first path component in the link value and
17307 * concatenate the remainders. */
17308 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17309 if (*q != NUL)
17310 {
17311 if (remain == NULL)
17312 remain = vim_strsave(q - 1);
17313 else
17314 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017315 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 if (cpy != NULL)
17317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 vim_free(remain);
17319 remain = cpy;
17320 }
17321 }
17322 q[-1] = NUL;
17323 }
17324
17325 q = gettail(p);
17326 if (q > p && *q == NUL)
17327 {
17328 /* Ignore trailing path separator. */
17329 q[-1] = NUL;
17330 q = gettail(p);
17331 }
17332 if (q > p && !mch_isFullName(buf))
17333 {
17334 /* symlink is relative to directory of argument */
17335 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17336 if (cpy != NULL)
17337 {
17338 STRCPY(cpy, p);
17339 STRCPY(gettail(cpy), buf);
17340 vim_free(p);
17341 p = cpy;
17342 }
17343 }
17344 else
17345 {
17346 vim_free(p);
17347 p = vim_strsave(buf);
17348 }
17349 }
17350
17351 if (remain == NULL)
17352 break;
17353
17354 /* Append the first path component of "remain" to "p". */
17355 q = getnextcomp(remain + 1);
17356 len = q - remain - (*q != NUL);
17357 cpy = vim_strnsave(p, STRLEN(p) + len);
17358 if (cpy != NULL)
17359 {
17360 STRNCAT(cpy, remain, len);
17361 vim_free(p);
17362 p = cpy;
17363 }
17364 /* Shorten "remain". */
17365 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017366 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 else
17368 {
17369 vim_free(remain);
17370 remain = NULL;
17371 }
17372 }
17373
17374 /* If the result is a relative path name, make it explicitly relative to
17375 * the current directory if and only if the argument had this form. */
17376 if (!vim_ispathsep(*p))
17377 {
17378 if (is_relative_to_current
17379 && *p != NUL
17380 && !(p[0] == '.'
17381 && (p[1] == NUL
17382 || vim_ispathsep(p[1])
17383 || (p[1] == '.'
17384 && (p[2] == NUL
17385 || vim_ispathsep(p[2]))))))
17386 {
17387 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017388 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 if (cpy != NULL)
17390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 vim_free(p);
17392 p = cpy;
17393 }
17394 }
17395 else if (!is_relative_to_current)
17396 {
17397 /* Strip leading "./". */
17398 q = p;
17399 while (q[0] == '.' && vim_ispathsep(q[1]))
17400 q += 2;
17401 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017402 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 }
17404 }
17405
17406 /* Ensure that the result will have no trailing path separator
17407 * if the argument had none. But keep "/" or "//". */
17408 if (!has_trailing_pathsep)
17409 {
17410 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017411 if (after_pathsep(p, q))
17412 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 }
17414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017415 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 }
17417# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017418 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419# endif
17420#endif
17421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017422 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423
17424#ifdef HAVE_READLINK
17425fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017426 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017428 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429}
17430
17431/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017432 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433 */
17434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017435f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436{
Bram Moolenaar33570922005-01-25 22:26:29 +000017437 list_T *l;
17438 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439
Bram Moolenaar0d660222005-01-07 21:51:51 +000017440 if (argvars[0].v_type != VAR_LIST)
17441 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017442 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017443 && !tv_check_lock(l->lv_lock,
17444 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017445 {
17446 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017447 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017448 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017449 while (li != NULL)
17450 {
17451 ni = li->li_prev;
17452 list_append(l, li);
17453 li = ni;
17454 }
17455 rettv->vval.v_list = l;
17456 rettv->v_type = VAR_LIST;
17457 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017458 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460}
17461
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017462#define SP_NOMOVE 0x01 /* don't move cursor */
17463#define SP_REPEAT 0x02 /* repeat to find outer pair */
17464#define SP_RETCOUNT 0x04 /* return matchcount */
17465#define SP_SETPCMARK 0x08 /* set previous context mark */
17466#define SP_START 0x10 /* accept match at start position */
17467#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17468#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017469#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017470
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017471static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017472
17473/*
17474 * Get flags for a search function.
17475 * Possibly sets "p_ws".
17476 * Returns BACKWARD, FORWARD or zero (for an error).
17477 */
17478 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017479get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017480{
17481 int dir = FORWARD;
17482 char_u *flags;
17483 char_u nbuf[NUMBUFLEN];
17484 int mask;
17485
17486 if (varp->v_type != VAR_UNKNOWN)
17487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017488 flags = get_tv_string_buf_chk(varp, nbuf);
17489 if (flags == NULL)
17490 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017491 while (*flags != NUL)
17492 {
17493 switch (*flags)
17494 {
17495 case 'b': dir = BACKWARD; break;
17496 case 'w': p_ws = TRUE; break;
17497 case 'W': p_ws = FALSE; break;
17498 default: mask = 0;
17499 if (flagsp != NULL)
17500 switch (*flags)
17501 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017502 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017503 case 'e': mask = SP_END; break;
17504 case 'm': mask = SP_RETCOUNT; break;
17505 case 'n': mask = SP_NOMOVE; break;
17506 case 'p': mask = SP_SUBPAT; break;
17507 case 'r': mask = SP_REPEAT; break;
17508 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017509 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017510 }
17511 if (mask == 0)
17512 {
17513 EMSG2(_(e_invarg2), flags);
17514 dir = 0;
17515 }
17516 else
17517 *flagsp |= mask;
17518 }
17519 if (dir == 0)
17520 break;
17521 ++flags;
17522 }
17523 }
17524 return dir;
17525}
17526
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017528 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017530 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017531search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017533 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 char_u *pat;
17535 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017536 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 int save_p_ws = p_ws;
17538 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017539 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017540 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017541 proftime_T tm;
17542#ifdef FEAT_RELTIME
17543 long time_limit = 0;
17544#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017545 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017546 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017548 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017549 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017550 if (dir == 0)
17551 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017552 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017553 if (flags & SP_START)
17554 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017555 if (flags & SP_END)
17556 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017557 if (flags & SP_COLUMN)
17558 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017559
Bram Moolenaar76929292008-01-06 19:07:36 +000017560 /* Optional arguments: line number to stop searching and timeout. */
17561 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017562 {
17563 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17564 if (lnum_stop < 0)
17565 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017566#ifdef FEAT_RELTIME
17567 if (argvars[3].v_type != VAR_UNKNOWN)
17568 {
17569 time_limit = get_tv_number_chk(&argvars[3], NULL);
17570 if (time_limit < 0)
17571 goto theend;
17572 }
17573#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017574 }
17575
Bram Moolenaar76929292008-01-06 19:07:36 +000017576#ifdef FEAT_RELTIME
17577 /* Set the time limit, if there is one. */
17578 profile_setlimit(time_limit, &tm);
17579#endif
17580
Bram Moolenaar231334e2005-07-25 20:46:57 +000017581 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017582 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017583 * Check to make sure only those flags are set.
17584 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17585 * flags cannot be set. Check for that condition also.
17586 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017587 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017588 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017590 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017591 goto theend;
17592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017594 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017595 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017596 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017597 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017599 if (flags & SP_SUBPAT)
17600 retval = subpatnum;
17601 else
17602 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017603 if (flags & SP_SETPCMARK)
17604 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017606 if (match_pos != NULL)
17607 {
17608 /* Store the match cursor position */
17609 match_pos->lnum = pos.lnum;
17610 match_pos->col = pos.col + 1;
17611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 /* "/$" will put the cursor after the end of the line, may need to
17613 * correct that here */
17614 check_cursor();
17615 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017616
17617 /* If 'n' flag is used: restore cursor position. */
17618 if (flags & SP_NOMOVE)
17619 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017620 else
17621 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017622theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017624
17625 return retval;
17626}
17627
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017628#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017629
17630/*
17631 * round() is not in C90, use ceil() or floor() instead.
17632 */
17633 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017634vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017635{
17636 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17637}
17638
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017639/*
17640 * "round({float})" function
17641 */
17642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017643f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017644{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017645 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017646
17647 rettv->v_type = VAR_FLOAT;
17648 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017649 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017650 else
17651 rettv->vval.v_float = 0.0;
17652}
17653#endif
17654
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017655/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017656 * "screenattr()" function
17657 */
17658 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017659f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017660{
17661 int row;
17662 int col;
17663 int c;
17664
17665 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17666 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17667 if (row < 0 || row >= screen_Rows
17668 || col < 0 || col >= screen_Columns)
17669 c = -1;
17670 else
17671 c = ScreenAttrs[LineOffset[row] + col];
17672 rettv->vval.v_number = c;
17673}
17674
17675/*
17676 * "screenchar()" function
17677 */
17678 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017679f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017680{
17681 int row;
17682 int col;
17683 int off;
17684 int c;
17685
17686 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17687 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17688 if (row < 0 || row >= screen_Rows
17689 || col < 0 || col >= screen_Columns)
17690 c = -1;
17691 else
17692 {
17693 off = LineOffset[row] + col;
17694#ifdef FEAT_MBYTE
17695 if (enc_utf8 && ScreenLinesUC[off] != 0)
17696 c = ScreenLinesUC[off];
17697 else
17698#endif
17699 c = ScreenLines[off];
17700 }
17701 rettv->vval.v_number = c;
17702}
17703
17704/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017705 * "screencol()" function
17706 *
17707 * First column is 1 to be consistent with virtcol().
17708 */
17709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017710f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017711{
17712 rettv->vval.v_number = screen_screencol() + 1;
17713}
17714
17715/*
17716 * "screenrow()" function
17717 */
17718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017719f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017720{
17721 rettv->vval.v_number = screen_screenrow() + 1;
17722}
17723
17724/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017725 * "search()" function
17726 */
17727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017728f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017729{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017730 int flags = 0;
17731
17732 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733}
17734
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017736 * "searchdecl()" function
17737 */
17738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017739f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017740{
17741 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017742 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017743 int error = FALSE;
17744 char_u *name;
17745
17746 rettv->vval.v_number = 1; /* default: FAIL */
17747
17748 name = get_tv_string_chk(&argvars[0]);
17749 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017750 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017751 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017752 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17753 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17754 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017755 if (!error && name != NULL)
17756 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017757 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017758}
17759
17760/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017761 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017764searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765{
17766 char_u *spat, *mpat, *epat;
17767 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 int dir;
17770 int flags = 0;
17771 char_u nbuf1[NUMBUFLEN];
17772 char_u nbuf2[NUMBUFLEN];
17773 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017774 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017775 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017776 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017779 spat = get_tv_string_chk(&argvars[0]);
17780 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17781 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17782 if (spat == NULL || mpat == NULL || epat == NULL)
17783 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785 /* Handle the optional fourth argument: flags */
17786 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017787 if (dir == 0)
17788 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017789
17790 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017791 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17792 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017793 if ((flags & (SP_END | SP_SUBPAT)) != 0
17794 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017795 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017796 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017797 goto theend;
17798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017800 /* Using 'r' implies 'W', otherwise it doesn't work. */
17801 if (flags & SP_REPEAT)
17802 p_ws = FALSE;
17803
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017804 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017805 if (argvars[3].v_type == VAR_UNKNOWN
17806 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 skip = (char_u *)"";
17808 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017810 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017811 if (argvars[5].v_type != VAR_UNKNOWN)
17812 {
17813 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17814 if (lnum_stop < 0)
17815 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017816#ifdef FEAT_RELTIME
17817 if (argvars[6].v_type != VAR_UNKNOWN)
17818 {
17819 time_limit = get_tv_number_chk(&argvars[6], NULL);
17820 if (time_limit < 0)
17821 goto theend;
17822 }
17823#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017824 }
17825 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017826 if (skip == NULL)
17827 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017829 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017830 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017831
17832theend:
17833 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017834
17835 return retval;
17836}
17837
17838/*
17839 * "searchpair()" function
17840 */
17841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017842f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017843{
17844 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17845}
17846
17847/*
17848 * "searchpairpos()" function
17849 */
17850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017851f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017852{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017853 pos_T match_pos;
17854 int lnum = 0;
17855 int col = 0;
17856
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017857 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017858 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017859
17860 if (searchpair_cmn(argvars, &match_pos) > 0)
17861 {
17862 lnum = match_pos.lnum;
17863 col = match_pos.col;
17864 }
17865
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017866 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17867 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017868}
17869
17870/*
17871 * Search for a start/middle/end thing.
17872 * Used by searchpair(), see its documentation for the details.
17873 * Returns 0 or -1 for no match,
17874 */
17875 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017876do_searchpair(
17877 char_u *spat, /* start pattern */
17878 char_u *mpat, /* middle pattern */
17879 char_u *epat, /* end pattern */
17880 int dir, /* BACKWARD or FORWARD */
17881 char_u *skip, /* skip expression */
17882 int flags, /* SP_SETPCMARK and other SP_ values */
17883 pos_T *match_pos,
17884 linenr_T lnum_stop, /* stop at this line if not zero */
17885 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017886{
17887 char_u *save_cpo;
17888 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17889 long retval = 0;
17890 pos_T pos;
17891 pos_T firstpos;
17892 pos_T foundpos;
17893 pos_T save_cursor;
17894 pos_T save_pos;
17895 int n;
17896 int r;
17897 int nest = 1;
17898 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017899 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017900 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017901
17902 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17903 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017904 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017905
Bram Moolenaar76929292008-01-06 19:07:36 +000017906#ifdef FEAT_RELTIME
17907 /* Set the time limit, if there is one. */
17908 profile_setlimit(time_limit, &tm);
17909#endif
17910
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017911 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17912 * start/middle/end (pat3, for the top pair). */
17913 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17914 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17915 if (pat2 == NULL || pat3 == NULL)
17916 goto theend;
17917 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17918 if (*mpat == NUL)
17919 STRCPY(pat3, pat2);
17920 else
17921 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17922 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017923 if (flags & SP_START)
17924 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017925
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 save_cursor = curwin->w_cursor;
17927 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017928 clearpos(&firstpos);
17929 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 pat = pat3;
17931 for (;;)
17932 {
17933 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017934 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17936 /* didn't find it or found the first match again: FAIL */
17937 break;
17938
17939 if (firstpos.lnum == 0)
17940 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017941 if (equalpos(pos, foundpos))
17942 {
17943 /* Found the same position again. Can happen with a pattern that
17944 * has "\zs" at the end and searching backwards. Advance one
17945 * character and try again. */
17946 if (dir == BACKWARD)
17947 decl(&pos);
17948 else
17949 incl(&pos);
17950 }
17951 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017953 /* clear the start flag to avoid getting stuck here */
17954 options &= ~SEARCH_START;
17955
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 /* If the skip pattern matches, ignore this match. */
17957 if (*skip != NUL)
17958 {
17959 save_pos = curwin->w_cursor;
17960 curwin->w_cursor = pos;
17961 r = eval_to_bool(skip, &err, NULL, FALSE);
17962 curwin->w_cursor = save_pos;
17963 if (err)
17964 {
17965 /* Evaluating {skip} caused an error, break here. */
17966 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017967 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 break;
17969 }
17970 if (r)
17971 continue;
17972 }
17973
17974 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17975 {
17976 /* Found end when searching backwards or start when searching
17977 * forward: nested pair. */
17978 ++nest;
17979 pat = pat2; /* nested, don't search for middle */
17980 }
17981 else
17982 {
17983 /* Found end when searching forward or start when searching
17984 * backward: end of (nested) pair; or found middle in outer pair. */
17985 if (--nest == 1)
17986 pat = pat3; /* outer level, search for middle */
17987 }
17988
17989 if (nest == 0)
17990 {
17991 /* Found the match: return matchcount or line number. */
17992 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017993 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017995 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017996 if (flags & SP_SETPCMARK)
17997 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998 curwin->w_cursor = pos;
17999 if (!(flags & SP_REPEAT))
18000 break;
18001 nest = 1; /* search for next unmatched */
18002 }
18003 }
18004
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018005 if (match_pos != NULL)
18006 {
18007 /* Store the match cursor position */
18008 match_pos->lnum = curwin->w_cursor.lnum;
18009 match_pos->col = curwin->w_cursor.col + 1;
18010 }
18011
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018013 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 curwin->w_cursor = save_cursor;
18015
18016theend:
18017 vim_free(pat2);
18018 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018019 if (p_cpo == empty_option)
18020 p_cpo = save_cpo;
18021 else
18022 /* Darn, evaluating the {skip} expression changed the value. */
18023 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018024
18025 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026}
18027
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018028/*
18029 * "searchpos()" function
18030 */
18031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018032f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018033{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018034 pos_T match_pos;
18035 int lnum = 0;
18036 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018037 int n;
18038 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018039
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018040 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018041 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018042
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018043 n = search_cmn(argvars, &match_pos, &flags);
18044 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018045 {
18046 lnum = match_pos.lnum;
18047 col = match_pos.col;
18048 }
18049
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018050 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18051 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018052 if (flags & SP_SUBPAT)
18053 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018054}
18055
Bram Moolenaar0d660222005-01-07 21:51:51 +000018056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018057f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018059#ifdef FEAT_CLIENTSERVER
18060 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018061 char_u *server = get_tv_string_chk(&argvars[0]);
18062 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063
Bram Moolenaar0d660222005-01-07 21:51:51 +000018064 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018065 if (server == NULL || reply == NULL)
18066 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018067 if (check_restricted() || check_secure())
18068 return;
18069# ifdef FEAT_X11
18070 if (check_connection() == FAIL)
18071 return;
18072# endif
18073
18074 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018076 EMSG(_("E258: Unable to send to client"));
18077 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018079 rettv->vval.v_number = 0;
18080#else
18081 rettv->vval.v_number = -1;
18082#endif
18083}
18084
Bram Moolenaar0d660222005-01-07 21:51:51 +000018085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018086f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018087{
18088 char_u *r = NULL;
18089
18090#ifdef FEAT_CLIENTSERVER
18091# ifdef WIN32
18092 r = serverGetVimNames();
18093# else
18094 make_connection();
18095 if (X_DISPLAY != NULL)
18096 r = serverGetVimNames(X_DISPLAY);
18097# endif
18098#endif
18099 rettv->v_type = VAR_STRING;
18100 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101}
18102
18103/*
18104 * "setbufvar()" function
18105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018107f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108{
18109 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018112 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 char_u nbuf[NUMBUFLEN];
18114
18115 if (check_restricted() || check_secure())
18116 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018117 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18118 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018119 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 varp = &argvars[2];
18121
18122 if (buf != NULL && varname != NULL && varp != NULL)
18123 {
18124 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126
18127 if (*varname == '&')
18128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018129 long numval;
18130 char_u *strval;
18131 int error = FALSE;
18132
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018134 numval = get_tv_number_chk(varp, &error);
18135 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018136 if (!error && strval != NULL)
18137 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 }
18139 else
18140 {
18141 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18142 if (bufvarname != NULL)
18143 {
18144 STRCPY(bufvarname, "b:");
18145 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018146 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 vim_free(bufvarname);
18148 }
18149 }
18150
18151 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154}
18155
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018157f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018158{
18159 dict_T *d;
18160 dictitem_T *di;
18161 char_u *csearch;
18162
18163 if (argvars[0].v_type != VAR_DICT)
18164 {
18165 EMSG(_(e_dictreq));
18166 return;
18167 }
18168
18169 if ((d = argvars[0].vval.v_dict) != NULL)
18170 {
18171 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18172 if (csearch != NULL)
18173 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018174#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018175 if (enc_utf8)
18176 {
18177 int pcc[MAX_MCO];
18178 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018179
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018180 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18181 }
18182 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018183#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018184 set_last_csearch(PTR2CHAR(csearch),
18185 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018186 }
18187
18188 di = dict_find(d, (char_u *)"forward", -1);
18189 if (di != NULL)
18190 set_csearch_direction(get_tv_number(&di->di_tv)
18191 ? FORWARD : BACKWARD);
18192
18193 di = dict_find(d, (char_u *)"until", -1);
18194 if (di != NULL)
18195 set_csearch_until(!!get_tv_number(&di->di_tv));
18196 }
18197}
18198
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199/*
18200 * "setcmdpos()" function
18201 */
18202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018203f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018205 int pos = (int)get_tv_number(&argvars[0]) - 1;
18206
18207 if (pos >= 0)
18208 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209}
18210
18211/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018212 * "setfperm({fname}, {mode})" function
18213 */
18214 static void
18215f_setfperm(typval_T *argvars, typval_T *rettv)
18216{
18217 char_u *fname;
18218 char_u modebuf[NUMBUFLEN];
18219 char_u *mode_str;
18220 int i;
18221 int mask;
18222 int mode = 0;
18223
18224 rettv->vval.v_number = 0;
18225 fname = get_tv_string_chk(&argvars[0]);
18226 if (fname == NULL)
18227 return;
18228 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18229 if (mode_str == NULL)
18230 return;
18231 if (STRLEN(mode_str) != 9)
18232 {
18233 EMSG2(_(e_invarg2), mode_str);
18234 return;
18235 }
18236
18237 mask = 1;
18238 for (i = 8; i >= 0; --i)
18239 {
18240 if (mode_str[i] != '-')
18241 mode |= mask;
18242 mask = mask << 1;
18243 }
18244 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18245}
18246
18247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248 * "setline()" function
18249 */
18250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018251f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252{
18253 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018254 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018255 list_T *l = NULL;
18256 listitem_T *li = NULL;
18257 long added = 0;
18258 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018260 lnum = get_tv_lnum(&argvars[0]);
18261 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018263 l = argvars[1].vval.v_list;
18264 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018266 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018267 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018268
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018269 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018270 for (;;)
18271 {
18272 if (l != NULL)
18273 {
18274 /* list argument, get next string */
18275 if (li == NULL)
18276 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018277 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018278 li = li->li_next;
18279 }
18280
18281 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018282 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018283 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018284
18285 /* When coming here from Insert mode, sync undo, so that this can be
18286 * undone separately from what was previously inserted. */
18287 if (u_sync_once == 2)
18288 {
18289 u_sync_once = 1; /* notify that u_sync() was called */
18290 u_sync(TRUE);
18291 }
18292
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018293 if (lnum <= curbuf->b_ml.ml_line_count)
18294 {
18295 /* existing line, replace it */
18296 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18297 {
18298 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018299 if (lnum == curwin->w_cursor.lnum)
18300 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018301 rettv->vval.v_number = 0; /* OK */
18302 }
18303 }
18304 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18305 {
18306 /* lnum is one past the last line, append the line */
18307 ++added;
18308 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18309 rettv->vval.v_number = 0; /* OK */
18310 }
18311
18312 if (l == NULL) /* only one string argument */
18313 break;
18314 ++lnum;
18315 }
18316
18317 if (added > 0)
18318 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319}
18320
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018321static 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 +000018322
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018324 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018325 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018327set_qf_ll_list(
18328 win_T *wp UNUSED,
18329 typval_T *list_arg UNUSED,
18330 typval_T *action_arg UNUSED,
18331 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018332{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018333#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018334 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018335 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018336 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018337#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018338
Bram Moolenaar2641f772005-03-25 21:58:17 +000018339 rettv->vval.v_number = -1;
18340
18341#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018342 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018343 EMSG(_(e_listreq));
18344 else
18345 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018346 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018347
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018348 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018349 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018350 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018351 if (act == NULL)
18352 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018353 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018354 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018355 else
18356 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018357 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018358 else if (action_arg->v_type == VAR_UNKNOWN)
18359 action = ' ';
18360 else
18361 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018362
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018363 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018364 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018365 rettv->vval.v_number = 0;
18366 }
18367#endif
18368}
18369
18370/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018371 * "setloclist()" function
18372 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018374f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018375{
18376 win_T *win;
18377
18378 rettv->vval.v_number = -1;
18379
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018380 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018381 if (win != NULL)
18382 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18383}
18384
18385/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018386 * "setmatches()" function
18387 */
18388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018389f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018390{
18391#ifdef FEAT_SEARCH_EXTRA
18392 list_T *l;
18393 listitem_T *li;
18394 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018395 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018396
18397 rettv->vval.v_number = -1;
18398 if (argvars[0].v_type != VAR_LIST)
18399 {
18400 EMSG(_(e_listreq));
18401 return;
18402 }
18403 if ((l = argvars[0].vval.v_list) != NULL)
18404 {
18405
18406 /* To some extent make sure that we are dealing with a list from
18407 * "getmatches()". */
18408 li = l->lv_first;
18409 while (li != NULL)
18410 {
18411 if (li->li_tv.v_type != VAR_DICT
18412 || (d = li->li_tv.vval.v_dict) == NULL)
18413 {
18414 EMSG(_(e_invarg));
18415 return;
18416 }
18417 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018418 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18419 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018420 && dict_find(d, (char_u *)"priority", -1) != NULL
18421 && dict_find(d, (char_u *)"id", -1) != NULL))
18422 {
18423 EMSG(_(e_invarg));
18424 return;
18425 }
18426 li = li->li_next;
18427 }
18428
18429 clear_matches(curwin);
18430 li = l->lv_first;
18431 while (li != NULL)
18432 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018433 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018434 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018435 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018436 char_u *group;
18437 int priority;
18438 int id;
18439 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018440
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018441 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018442 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18443 {
18444 if (s == NULL)
18445 {
18446 s = list_alloc();
18447 if (s == NULL)
18448 return;
18449 }
18450
18451 /* match from matchaddpos() */
18452 for (i = 1; i < 9; i++)
18453 {
18454 sprintf((char *)buf, (char *)"pos%d", i);
18455 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18456 {
18457 if (di->di_tv.v_type != VAR_LIST)
18458 return;
18459
18460 list_append_tv(s, &di->di_tv);
18461 s->lv_refcount++;
18462 }
18463 else
18464 break;
18465 }
18466 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018467
18468 group = get_dict_string(d, (char_u *)"group", FALSE);
18469 priority = (int)get_dict_number(d, (char_u *)"priority");
18470 id = (int)get_dict_number(d, (char_u *)"id");
18471 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18472 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18473 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018474 if (i == 0)
18475 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018476 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018477 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018478 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018479 }
18480 else
18481 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018482 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018483 list_unref(s);
18484 s = NULL;
18485 }
18486
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018487 li = li->li_next;
18488 }
18489 rettv->vval.v_number = 0;
18490 }
18491#endif
18492}
18493
18494/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018495 * "setpos()" function
18496 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018498f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018499{
18500 pos_T pos;
18501 int fnum;
18502 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018503 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018504
Bram Moolenaar08250432008-02-13 11:42:46 +000018505 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018506 name = get_tv_string_chk(argvars);
18507 if (name != NULL)
18508 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018509 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018510 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018511 if (--pos.col < 0)
18512 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018513 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018514 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018515 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018516 if (fnum == curbuf->b_fnum)
18517 {
18518 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018519 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018520 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018521 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018522 curwin->w_set_curswant = FALSE;
18523 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018524 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018525 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018526 }
18527 else
18528 EMSG(_(e_invarg));
18529 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018530 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18531 {
18532 /* set mark */
18533 if (setmark_pos(name[1], &pos, fnum) == OK)
18534 rettv->vval.v_number = 0;
18535 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018536 else
18537 EMSG(_(e_invarg));
18538 }
18539 }
18540}
18541
18542/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018543 * "setqflist()" function
18544 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018546f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018547{
18548 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18549}
18550
18551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 * "setreg()" function
18553 */
18554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018555f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556{
18557 int regname;
18558 char_u *strregname;
18559 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018560 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 int append;
18562 char_u yank_type;
18563 long block_len;
18564
18565 block_len = -1;
18566 yank_type = MAUTO;
18567 append = FALSE;
18568
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018569 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018572 if (strregname == NULL)
18573 return; /* type error; errmsg already given */
18574 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 if (regname == 0 || regname == '@')
18576 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018578 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018580 stropt = get_tv_string_chk(&argvars[2]);
18581 if (stropt == NULL)
18582 return; /* type error */
18583 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 switch (*stropt)
18585 {
18586 case 'a': case 'A': /* append */
18587 append = TRUE;
18588 break;
18589 case 'v': case 'c': /* character-wise selection */
18590 yank_type = MCHAR;
18591 break;
18592 case 'V': case 'l': /* line-wise selection */
18593 yank_type = MLINE;
18594 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 case 'b': case Ctrl_V: /* block-wise selection */
18596 yank_type = MBLOCK;
18597 if (VIM_ISDIGIT(stropt[1]))
18598 {
18599 ++stropt;
18600 block_len = getdigits(&stropt) - 1;
18601 --stropt;
18602 }
18603 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604 }
18605 }
18606
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018607 if (argvars[1].v_type == VAR_LIST)
18608 {
18609 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018610 char_u **allocval;
18611 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018612 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018613 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018614 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018615 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018616 int len;
18617
18618 /* If the list is NULL handle like an empty list. */
18619 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018620
Bram Moolenaar7d647822014-04-05 21:28:56 +020018621 /* First half: use for pointers to result lines; second half: use for
18622 * pointers to allocated copies. */
18623 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018624 if (lstval == NULL)
18625 return;
18626 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018627 allocval = lstval + len + 2;
18628 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018629
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018630 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018631 li = li->li_next)
18632 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018633 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018634 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018635 goto free_lstval;
18636 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018637 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018638 /* Need to make a copy, next get_tv_string_buf_chk() will
18639 * overwrite the string. */
18640 strval = vim_strsave(buf);
18641 if (strval == NULL)
18642 goto free_lstval;
18643 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018644 }
18645 *curval++ = strval;
18646 }
18647 *curval++ = NULL;
18648
18649 write_reg_contents_lst(regname, lstval, -1,
18650 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018651free_lstval:
18652 while (curallocval > allocval)
18653 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018654 vim_free(lstval);
18655 }
18656 else
18657 {
18658 strval = get_tv_string_chk(&argvars[1]);
18659 if (strval == NULL)
18660 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018661 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018663 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665}
18666
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018667/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018668 * "settabvar()" function
18669 */
18670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018671f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018672{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018673#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018674 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018675 tabpage_T *tp;
18676#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018677 char_u *varname, *tabvarname;
18678 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018679
18680 rettv->vval.v_number = 0;
18681
18682 if (check_restricted() || check_secure())
18683 return;
18684
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018685#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018686 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018687#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018688 varname = get_tv_string_chk(&argvars[1]);
18689 varp = &argvars[2];
18690
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018691 if (varname != NULL && varp != NULL
18692#ifdef FEAT_WINDOWS
18693 && tp != NULL
18694#endif
18695 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018696 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018697#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018698 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018699 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018700#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018701
18702 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18703 if (tabvarname != NULL)
18704 {
18705 STRCPY(tabvarname, "t:");
18706 STRCPY(tabvarname + 2, varname);
18707 set_var(tabvarname, varp, TRUE);
18708 vim_free(tabvarname);
18709 }
18710
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018711#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018712 /* Restore current tabpage */
18713 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018714 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018715#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018716 }
18717}
18718
18719/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018720 * "settabwinvar()" function
18721 */
18722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018723f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018724{
18725 setwinvar(argvars, rettv, 1);
18726}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727
18728/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018729 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018732f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018734 setwinvar(argvars, rettv, 0);
18735}
18736
18737/*
18738 * "setwinvar()" and "settabwinvar()" functions
18739 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018740
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018742setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018743{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 win_T *win;
18745#ifdef FEAT_WINDOWS
18746 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018747 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018748 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749#endif
18750 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018751 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018753 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754
18755 if (check_restricted() || check_secure())
18756 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018757
18758#ifdef FEAT_WINDOWS
18759 if (off == 1)
18760 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18761 else
18762 tp = curtab;
18763#endif
18764 win = find_win_by_nr(&argvars[off], tp);
18765 varname = get_tv_string_chk(&argvars[off + 1]);
18766 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767
18768 if (win != NULL && varname != NULL && varp != NULL)
18769 {
18770#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018771 need_switch_win = !(tp == curtab && win == curwin);
18772 if (!need_switch_win
18773 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018776 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018778 long numval;
18779 char_u *strval;
18780 int error = FALSE;
18781
18782 ++varname;
18783 numval = get_tv_number_chk(varp, &error);
18784 strval = get_tv_string_buf_chk(varp, nbuf);
18785 if (!error && strval != NULL)
18786 set_option_value(varname, numval, strval, OPT_LOCAL);
18787 }
18788 else
18789 {
18790 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18791 if (winvarname != NULL)
18792 {
18793 STRCPY(winvarname, "w:");
18794 STRCPY(winvarname + 2, varname);
18795 set_var(winvarname, varp, TRUE);
18796 vim_free(winvarname);
18797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 }
18799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018801 if (need_switch_win)
18802 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803#endif
18804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805}
18806
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018807#ifdef FEAT_CRYPT
18808/*
18809 * "sha256({string})" function
18810 */
18811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018812f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018813{
18814 char_u *p;
18815
18816 p = get_tv_string(&argvars[0]);
18817 rettv->vval.v_string = vim_strsave(
18818 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18819 rettv->v_type = VAR_STRING;
18820}
18821#endif /* FEAT_CRYPT */
18822
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018824 * "shellescape({string})" function
18825 */
18826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018827f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018828{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018829 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018830 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018831 rettv->v_type = VAR_STRING;
18832}
18833
18834/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018835 * shiftwidth() function
18836 */
18837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018838f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018839{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018840 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018841}
18842
18843/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018844 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 */
18846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018847f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018849 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850
Bram Moolenaar0d660222005-01-07 21:51:51 +000018851 p = get_tv_string(&argvars[0]);
18852 rettv->vval.v_string = vim_strsave(p);
18853 simplify_filename(rettv->vval.v_string); /* simplify in place */
18854 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855}
18856
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018857#ifdef FEAT_FLOAT
18858/*
18859 * "sin()" function
18860 */
18861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018862f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018863{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018864 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018865
18866 rettv->v_type = VAR_FLOAT;
18867 if (get_float_arg(argvars, &f) == OK)
18868 rettv->vval.v_float = sin(f);
18869 else
18870 rettv->vval.v_float = 0.0;
18871}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018872
18873/*
18874 * "sinh()" function
18875 */
18876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018877f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018878{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018879 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018880
18881 rettv->v_type = VAR_FLOAT;
18882 if (get_float_arg(argvars, &f) == OK)
18883 rettv->vval.v_float = sinh(f);
18884 else
18885 rettv->vval.v_float = 0.0;
18886}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018887#endif
18888
Bram Moolenaar0d660222005-01-07 21:51:51 +000018889static int
18890#ifdef __BORLANDC__
18891 _RTLENTRYF
18892#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018893 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018894static int
18895#ifdef __BORLANDC__
18896 _RTLENTRYF
18897#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018898 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018899
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018900/* struct used in the array that's given to qsort() */
18901typedef struct
18902{
18903 listitem_T *item;
18904 int idx;
18905} sortItem_T;
18906
Bram Moolenaar0b962472016-02-22 22:51:33 +010018907/* struct storing information about current sort */
18908typedef struct
18909{
18910 int item_compare_ic;
18911 int item_compare_numeric;
18912 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018913#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018914 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018915#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018916 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018917 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018918 dict_T *item_compare_selfdict;
18919 int item_compare_func_err;
18920 int item_compare_keep_zero;
18921} sortinfo_T;
18922static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018923static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018924#define ITEM_COMPARE_FAIL 999
18925
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018927 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929 static int
18930#ifdef __BORLANDC__
18931_RTLENTRYF
18932#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018933item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018935 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018936 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018937 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018938 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018939 int res;
18940 char_u numbuf1[NUMBUFLEN];
18941 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018943 si1 = (sortItem_T *)s1;
18944 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018945 tv1 = &si1->item->li_tv;
18946 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018947
Bram Moolenaar0b962472016-02-22 22:51:33 +010018948 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018949 {
18950 long v1 = get_tv_number(tv1);
18951 long v2 = get_tv_number(tv2);
18952
18953 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18954 }
18955
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018956#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018957 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018958 {
18959 float_T v1 = get_tv_float(tv1);
18960 float_T v2 = get_tv_float(tv2);
18961
18962 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18963 }
18964#endif
18965
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018966 /* tv2string() puts quotes around a string and allocates memory. Don't do
18967 * that for string variables. Use a single quote when comparing with a
18968 * non-string to do what the docs promise. */
18969 if (tv1->v_type == VAR_STRING)
18970 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018971 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018972 p1 = (char_u *)"'";
18973 else
18974 p1 = tv1->vval.v_string;
18975 }
18976 else
18977 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18978 if (tv2->v_type == VAR_STRING)
18979 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018980 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018981 p2 = (char_u *)"'";
18982 else
18983 p2 = tv2->vval.v_string;
18984 }
18985 else
18986 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018987 if (p1 == NULL)
18988 p1 = (char_u *)"";
18989 if (p2 == NULL)
18990 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018991 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018992 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018993 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018994 res = STRICMP(p1, p2);
18995 else
18996 res = STRCMP(p1, p2);
18997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018999 {
19000 double n1, n2;
19001 n1 = strtod((char *)p1, (char **)&p1);
19002 n2 = strtod((char *)p2, (char **)&p2);
19003 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19004 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019005
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019006 /* When the result would be zero, compare the item indexes. Makes the
19007 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019008 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019009 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019010
Bram Moolenaar0d660222005-01-07 21:51:51 +000019011 vim_free(tofree1);
19012 vim_free(tofree2);
19013 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014}
19015
19016 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019017#ifdef __BORLANDC__
19018_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019020item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019022 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019023 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019024 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019025 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019026 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019027 char_u *func_name;
19028 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019030 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019031 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019032 return 0;
19033
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019034 si1 = (sortItem_T *)s1;
19035 si2 = (sortItem_T *)s2;
19036
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019037 if (partial == NULL)
19038 func_name = sortinfo->item_compare_func;
19039 else
19040 func_name = partial->pt_name;
19041
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019042 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019043 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019044 copy_tv(&si1->item->li_tv, &argv[0]);
19045 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019046
19047 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019048 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019049 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019050 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051 clear_tv(&argv[0]);
19052 clear_tv(&argv[1]);
19053
19054 if (res == FAIL)
19055 res = ITEM_COMPARE_FAIL;
19056 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019057 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19058 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019059 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019060 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019061
19062 /* When the result would be zero, compare the pointers themselves. Makes
19063 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019064 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019065 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019066
Bram Moolenaar0d660222005-01-07 21:51:51 +000019067 return res;
19068}
19069
19070/*
19071 * "sort({list})" function
19072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019074do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075{
Bram Moolenaar33570922005-01-25 22:26:29 +000019076 list_T *l;
19077 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019078 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019079 sortinfo_T *old_sortinfo;
19080 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081 long len;
19082 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 /* Pointer to current info struct used in compare function. Save and
19085 * restore the current one for nested calls. */
19086 old_sortinfo = sortinfo;
19087 sortinfo = &info;
19088
Bram Moolenaar0d660222005-01-07 21:51:51 +000019089 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019090 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 else
19092 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019093 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019094 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019095 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19096 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019097 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019098 rettv->vval.v_list = l;
19099 rettv->v_type = VAR_LIST;
19100 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101
Bram Moolenaar0d660222005-01-07 21:51:51 +000019102 len = list_len(l);
19103 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019104 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105
Bram Moolenaar0b962472016-02-22 22:51:33 +010019106 info.item_compare_ic = FALSE;
19107 info.item_compare_numeric = FALSE;
19108 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019109#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019110 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019111#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019112 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019113 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019115 if (argvars[1].v_type != VAR_UNKNOWN)
19116 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019117 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019118 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019119 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019120 else if (argvars[1].v_type == VAR_PARTIAL)
19121 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019122 else
19123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019124 int error = FALSE;
19125
19126 i = get_tv_number_chk(&argvars[1], &error);
19127 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019128 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019129 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019130 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019131 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019132 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019133 else if (i != 0)
19134 {
19135 EMSG(_(e_invarg));
19136 goto theend;
19137 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019138 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019139 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019140 if (*info.item_compare_func == NUL)
19141 {
19142 /* empty string means default sort */
19143 info.item_compare_func = NULL;
19144 }
19145 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019146 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019147 info.item_compare_func = NULL;
19148 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019149 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019150 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019151 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019152 info.item_compare_func = NULL;
19153 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019154 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019155#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019156 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019157 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019158 info.item_compare_func = NULL;
19159 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019160 }
19161#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019162 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019163 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019164 info.item_compare_func = NULL;
19165 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019166 }
19167 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019168 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019169
19170 if (argvars[2].v_type != VAR_UNKNOWN)
19171 {
19172 /* optional third argument: {dict} */
19173 if (argvars[2].v_type != VAR_DICT)
19174 {
19175 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019176 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019177 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019178 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019179 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181
Bram Moolenaar0d660222005-01-07 21:51:51 +000019182 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019183 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019184 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019185 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186
Bram Moolenaar327aa022014-03-25 18:24:23 +010019187 i = 0;
19188 if (sort)
19189 {
19190 /* sort(): ptrs will be the list to sort */
19191 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019192 {
19193 ptrs[i].item = li;
19194 ptrs[i].idx = i;
19195 ++i;
19196 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019197
Bram Moolenaar0b962472016-02-22 22:51:33 +010019198 info.item_compare_func_err = FALSE;
19199 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019200 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019201 if ((info.item_compare_func != NULL
19202 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019203 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019204 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019205 EMSG(_("E702: Sort compare function failed"));
19206 else
19207 {
19208 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019209 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019210 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019211 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019212 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019213
Bram Moolenaar0b962472016-02-22 22:51:33 +010019214 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019215 {
19216 /* Clear the List and append the items in sorted order. */
19217 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19218 l->lv_len = 0;
19219 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019220 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019221 }
19222 }
19223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019225 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019226 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019227
19228 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019229 info.item_compare_func_err = FALSE;
19230 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019231 item_compare_func_ptr = info.item_compare_func != NULL
19232 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019233 ? item_compare2 : item_compare;
19234
19235 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19236 li = li->li_next)
19237 {
19238 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19239 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019240 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019241 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019242 {
19243 EMSG(_("E882: Uniq compare function failed"));
19244 break;
19245 }
19246 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019247
Bram Moolenaar0b962472016-02-22 22:51:33 +010019248 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019249 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019250 while (--i >= 0)
19251 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019252 li = ptrs[i].item->li_next;
19253 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019254 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019255 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019256 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019257 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019258 list_fix_watch(l, li);
19259 listitem_free(li);
19260 l->lv_len--;
19261 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019262 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019263 }
19264
19265 vim_free(ptrs);
19266 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019267theend:
19268 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019269}
19270
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019271/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019272 * "sort({list})" function
19273 */
19274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019275f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019276{
19277 do_sort_uniq(argvars, rettv, TRUE);
19278}
19279
19280/*
19281 * "uniq({list})" function
19282 */
19283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019284f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019285{
19286 do_sort_uniq(argvars, rettv, FALSE);
19287}
19288
19289/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019290 * "soundfold({word})" function
19291 */
19292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019293f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019294{
19295 char_u *s;
19296
19297 rettv->v_type = VAR_STRING;
19298 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019299#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019300 rettv->vval.v_string = eval_soundfold(s);
19301#else
19302 rettv->vval.v_string = vim_strsave(s);
19303#endif
19304}
19305
19306/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019307 * "spellbadword()" function
19308 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019310f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019311{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019312 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019313 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019314 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019315
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019316 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019317 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019318
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019319#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019320 if (argvars[0].v_type == VAR_UNKNOWN)
19321 {
19322 /* Find the start and length of the badly spelled word. */
19323 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19324 if (len != 0)
19325 word = ml_get_cursor();
19326 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019327 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019328 {
19329 char_u *str = get_tv_string_chk(&argvars[0]);
19330 int capcol = -1;
19331
19332 if (str != NULL)
19333 {
19334 /* Check the argument for spelling. */
19335 while (*str != NUL)
19336 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019337 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019338 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019339 {
19340 word = str;
19341 break;
19342 }
19343 str += len;
19344 }
19345 }
19346 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019347#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019348
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019349 list_append_string(rettv->vval.v_list, word, len);
19350 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019351 attr == HLF_SPB ? "bad" :
19352 attr == HLF_SPR ? "rare" :
19353 attr == HLF_SPL ? "local" :
19354 attr == HLF_SPC ? "caps" :
19355 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019356}
19357
19358/*
19359 * "spellsuggest()" function
19360 */
19361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019362f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019363{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019364#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019365 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019366 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019367 int maxcount;
19368 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019369 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019370 listitem_T *li;
19371 int need_capital = FALSE;
19372#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019373
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019374 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019375 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019376
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019377#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019378 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019379 {
19380 str = get_tv_string(&argvars[0]);
19381 if (argvars[1].v_type != VAR_UNKNOWN)
19382 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019383 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019384 if (maxcount <= 0)
19385 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019386 if (argvars[2].v_type != VAR_UNKNOWN)
19387 {
19388 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19389 if (typeerr)
19390 return;
19391 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019392 }
19393 else
19394 maxcount = 25;
19395
Bram Moolenaar4770d092006-01-12 23:22:24 +000019396 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019397
19398 for (i = 0; i < ga.ga_len; ++i)
19399 {
19400 str = ((char_u **)ga.ga_data)[i];
19401
19402 li = listitem_alloc();
19403 if (li == NULL)
19404 vim_free(str);
19405 else
19406 {
19407 li->li_tv.v_type = VAR_STRING;
19408 li->li_tv.v_lock = 0;
19409 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019410 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019411 }
19412 }
19413 ga_clear(&ga);
19414 }
19415#endif
19416}
19417
Bram Moolenaar0d660222005-01-07 21:51:51 +000019418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019419f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019420{
19421 char_u *str;
19422 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019423 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019424 regmatch_T regmatch;
19425 char_u patbuf[NUMBUFLEN];
19426 char_u *save_cpo;
19427 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019428 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019429 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019430 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019431
19432 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19433 save_cpo = p_cpo;
19434 p_cpo = (char_u *)"";
19435
19436 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019437 if (argvars[1].v_type != VAR_UNKNOWN)
19438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019439 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19440 if (pat == NULL)
19441 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019442 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019443 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019444 }
19445 if (pat == NULL || *pat == NUL)
19446 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019447
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019448 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019450 if (typeerr)
19451 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452
Bram Moolenaar0d660222005-01-07 21:51:51 +000019453 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19454 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019456 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019457 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019458 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019459 if (*str == NUL)
19460 match = FALSE; /* empty item at the end */
19461 else
19462 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019463 if (match)
19464 end = regmatch.startp[0];
19465 else
19466 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019467 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19468 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019469 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019470 if (list_append_string(rettv->vval.v_list, str,
19471 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019472 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019473 }
19474 if (!match)
19475 break;
19476 /* Advance to just after the match. */
19477 if (regmatch.endp[0] > str)
19478 col = 0;
19479 else
19480 {
19481 /* Don't get stuck at the same match. */
19482#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019483 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019484#else
19485 col = 1;
19486#endif
19487 }
19488 str = regmatch.endp[0];
19489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490
Bram Moolenaar473de612013-06-08 18:19:48 +020019491 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493
Bram Moolenaar0d660222005-01-07 21:51:51 +000019494 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495}
19496
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019497#ifdef FEAT_FLOAT
19498/*
19499 * "sqrt()" function
19500 */
19501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019502f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019503{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019504 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019505
19506 rettv->v_type = VAR_FLOAT;
19507 if (get_float_arg(argvars, &f) == OK)
19508 rettv->vval.v_float = sqrt(f);
19509 else
19510 rettv->vval.v_float = 0.0;
19511}
19512
19513/*
19514 * "str2float()" function
19515 */
19516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019517f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019518{
19519 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19520
19521 if (*p == '+')
19522 p = skipwhite(p + 1);
19523 (void)string2float(p, &rettv->vval.v_float);
19524 rettv->v_type = VAR_FLOAT;
19525}
19526#endif
19527
Bram Moolenaar2c932302006-03-18 21:42:09 +000019528/*
19529 * "str2nr()" function
19530 */
19531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019532f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019533{
19534 int base = 10;
19535 char_u *p;
19536 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019537 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019538
19539 if (argvars[1].v_type != VAR_UNKNOWN)
19540 {
19541 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019542 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019543 {
19544 EMSG(_(e_invarg));
19545 return;
19546 }
19547 }
19548
19549 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019550 if (*p == '+')
19551 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019552 switch (base)
19553 {
19554 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19555 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19556 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19557 default: what = 0;
19558 }
19559 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019560 rettv->vval.v_number = n;
19561}
19562
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563#ifdef HAVE_STRFTIME
19564/*
19565 * "strftime({format}[, {time}])" function
19566 */
19567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019568f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569{
19570 char_u result_buf[256];
19571 struct tm *curtime;
19572 time_t seconds;
19573 char_u *p;
19574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019575 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019577 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019578 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579 seconds = time(NULL);
19580 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019581 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 curtime = localtime(&seconds);
19583 /* MSVC returns NULL for an invalid value of seconds. */
19584 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019585 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 else
19587 {
19588# ifdef FEAT_MBYTE
19589 vimconv_T conv;
19590 char_u *enc;
19591
19592 conv.vc_type = CONV_NONE;
19593 enc = enc_locale();
19594 convert_setup(&conv, p_enc, enc);
19595 if (conv.vc_type != CONV_NONE)
19596 p = string_convert(&conv, p, NULL);
19597# endif
19598 if (p != NULL)
19599 (void)strftime((char *)result_buf, sizeof(result_buf),
19600 (char *)p, curtime);
19601 else
19602 result_buf[0] = NUL;
19603
19604# ifdef FEAT_MBYTE
19605 if (conv.vc_type != CONV_NONE)
19606 vim_free(p);
19607 convert_setup(&conv, enc, p_enc);
19608 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 else
19611# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019612 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613
19614# ifdef FEAT_MBYTE
19615 /* Release conversion descriptors */
19616 convert_setup(&conv, NULL, NULL);
19617 vim_free(enc);
19618# endif
19619 }
19620}
19621#endif
19622
19623/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019624 * "strgetchar()" function
19625 */
19626 static void
19627f_strgetchar(typval_T *argvars, typval_T *rettv)
19628{
19629 char_u *str;
19630 int len;
19631 int error = FALSE;
19632 int charidx;
19633
19634 rettv->vval.v_number = -1;
19635 str = get_tv_string_chk(&argvars[0]);
19636 if (str == NULL)
19637 return;
19638 len = (int)STRLEN(str);
19639 charidx = get_tv_number_chk(&argvars[1], &error);
19640 if (error)
19641 return;
19642#ifdef FEAT_MBYTE
19643 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019644 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019645
19646 while (charidx >= 0 && byteidx < len)
19647 {
19648 if (charidx == 0)
19649 {
19650 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19651 break;
19652 }
19653 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019654 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019655 }
19656 }
19657#else
19658 if (charidx < len)
19659 rettv->vval.v_number = str[charidx];
19660#endif
19661}
19662
19663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664 * "stridx()" function
19665 */
19666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019667f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668{
19669 char_u buf[NUMBUFLEN];
19670 char_u *needle;
19671 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019672 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019674 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019676 needle = get_tv_string_chk(&argvars[1]);
19677 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019678 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019679 if (needle == NULL || haystack == NULL)
19680 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681
Bram Moolenaar33570922005-01-25 22:26:29 +000019682 if (argvars[2].v_type != VAR_UNKNOWN)
19683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019684 int error = FALSE;
19685
19686 start_idx = get_tv_number_chk(&argvars[2], &error);
19687 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019688 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019689 if (start_idx >= 0)
19690 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019691 }
19692
19693 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19694 if (pos != NULL)
19695 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696}
19697
19698/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019699 * "string()" function
19700 */
19701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019702f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019703{
19704 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019705 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019707 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019708 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19709 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019710 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019711 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019712 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713}
19714
19715/*
19716 * "strlen()" function
19717 */
19718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019719f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019721 rettv->vval.v_number = (varnumber_T)(STRLEN(
19722 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723}
19724
19725/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019726 * "strchars()" function
19727 */
19728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019729f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019730{
19731 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019732 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019733#ifdef FEAT_MBYTE
19734 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019735 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019736#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019737
19738 if (argvars[1].v_type != VAR_UNKNOWN)
19739 skipcc = get_tv_number_chk(&argvars[1], NULL);
19740 if (skipcc < 0 || skipcc > 1)
19741 EMSG(_(e_invarg));
19742 else
19743 {
19744#ifdef FEAT_MBYTE
19745 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19746 while (*s != NUL)
19747 {
19748 func_mb_ptr2char_adv(&s);
19749 ++len;
19750 }
19751 rettv->vval.v_number = len;
19752#else
19753 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19754#endif
19755 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019756}
19757
19758/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019759 * "strdisplaywidth()" function
19760 */
19761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019762f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019763{
19764 char_u *s = get_tv_string(&argvars[0]);
19765 int col = 0;
19766
19767 if (argvars[1].v_type != VAR_UNKNOWN)
19768 col = get_tv_number(&argvars[1]);
19769
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019770 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019771}
19772
19773/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019774 * "strwidth()" function
19775 */
19776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019777f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019778{
19779 char_u *s = get_tv_string(&argvars[0]);
19780
19781 rettv->vval.v_number = (varnumber_T)(
19782#ifdef FEAT_MBYTE
19783 mb_string2cells(s, -1)
19784#else
19785 STRLEN(s)
19786#endif
19787 );
19788}
19789
19790/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019791 * "strcharpart()" function
19792 */
19793 static void
19794f_strcharpart(typval_T *argvars, typval_T *rettv)
19795{
19796#ifdef FEAT_MBYTE
19797 char_u *p;
19798 int nchar;
19799 int nbyte = 0;
19800 int charlen;
19801 int len = 0;
19802 int slen;
19803 int error = FALSE;
19804
19805 p = get_tv_string(&argvars[0]);
19806 slen = (int)STRLEN(p);
19807
19808 nchar = get_tv_number_chk(&argvars[1], &error);
19809 if (!error)
19810 {
19811 if (nchar > 0)
19812 while (nchar > 0 && nbyte < slen)
19813 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019814 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019815 --nchar;
19816 }
19817 else
19818 nbyte = nchar;
19819 if (argvars[2].v_type != VAR_UNKNOWN)
19820 {
19821 charlen = get_tv_number(&argvars[2]);
19822 while (charlen > 0 && nbyte + len < slen)
19823 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019824 int off = nbyte + len;
19825
19826 if (off < 0)
19827 len += 1;
19828 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019829 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019830 --charlen;
19831 }
19832 }
19833 else
19834 len = slen - nbyte; /* default: all bytes that are available. */
19835 }
19836
19837 /*
19838 * Only return the overlap between the specified part and the actual
19839 * string.
19840 */
19841 if (nbyte < 0)
19842 {
19843 len += nbyte;
19844 nbyte = 0;
19845 }
19846 else if (nbyte > slen)
19847 nbyte = slen;
19848 if (len < 0)
19849 len = 0;
19850 else if (nbyte + len > slen)
19851 len = slen - nbyte;
19852
19853 rettv->v_type = VAR_STRING;
19854 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19855#else
19856 f_strpart(argvars, rettv);
19857#endif
19858}
19859
19860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 * "strpart()" function
19862 */
19863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019864f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865{
19866 char_u *p;
19867 int n;
19868 int len;
19869 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019870 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019872 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019873 slen = (int)STRLEN(p);
19874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019875 n = get_tv_number_chk(&argvars[1], &error);
19876 if (error)
19877 len = 0;
19878 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019879 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 else
19881 len = slen - n; /* default len: all bytes that are available. */
19882
19883 /*
19884 * Only return the overlap between the specified part and the actual
19885 * string.
19886 */
19887 if (n < 0)
19888 {
19889 len += n;
19890 n = 0;
19891 }
19892 else if (n > slen)
19893 n = slen;
19894 if (len < 0)
19895 len = 0;
19896 else if (n + len > slen)
19897 len = slen - n;
19898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019899 rettv->v_type = VAR_STRING;
19900 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901}
19902
19903/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019904 * "strridx()" function
19905 */
19906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019907f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019908{
19909 char_u buf[NUMBUFLEN];
19910 char_u *needle;
19911 char_u *haystack;
19912 char_u *rest;
19913 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019914 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019916 needle = get_tv_string_chk(&argvars[1]);
19917 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019918
19919 rettv->vval.v_number = -1;
19920 if (needle == NULL || haystack == NULL)
19921 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019922
19923 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019924 if (argvars[2].v_type != VAR_UNKNOWN)
19925 {
19926 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019927 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019928 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019929 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019930 }
19931 else
19932 end_idx = haystack_len;
19933
Bram Moolenaar0d660222005-01-07 21:51:51 +000019934 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019935 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019936 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019937 lastmatch = haystack + end_idx;
19938 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019939 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019940 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019941 for (rest = haystack; *rest != '\0'; ++rest)
19942 {
19943 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019944 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019945 break;
19946 lastmatch = rest;
19947 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019948 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019949
19950 if (lastmatch == NULL)
19951 rettv->vval.v_number = -1;
19952 else
19953 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19954}
19955
19956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 * "strtrans()" function
19958 */
19959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019960f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019962 rettv->v_type = VAR_STRING;
19963 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964}
19965
19966/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019967 * "submatch()" function
19968 */
19969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019970f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019971{
Bram Moolenaar41571762014-04-02 19:00:58 +020019972 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019973 int no;
19974 int retList = 0;
19975
19976 no = (int)get_tv_number_chk(&argvars[0], &error);
19977 if (error)
19978 return;
19979 error = FALSE;
19980 if (argvars[1].v_type != VAR_UNKNOWN)
19981 retList = get_tv_number_chk(&argvars[1], &error);
19982 if (error)
19983 return;
19984
19985 if (retList == 0)
19986 {
19987 rettv->v_type = VAR_STRING;
19988 rettv->vval.v_string = reg_submatch(no);
19989 }
19990 else
19991 {
19992 rettv->v_type = VAR_LIST;
19993 rettv->vval.v_list = reg_submatch_list(no);
19994 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019995}
19996
19997/*
19998 * "substitute()" function
19999 */
20000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020001f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020002{
20003 char_u patbuf[NUMBUFLEN];
20004 char_u subbuf[NUMBUFLEN];
20005 char_u flagsbuf[NUMBUFLEN];
20006
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020007 char_u *str = get_tv_string_chk(&argvars[0]);
20008 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20009 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20010 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20011
Bram Moolenaar0d660222005-01-07 21:51:51 +000020012 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020013 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20014 rettv->vval.v_string = NULL;
20015 else
20016 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020017}
20018
20019/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020020 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020023f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024{
20025 int id = 0;
20026#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020027 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 long col;
20029 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020030 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020032 lnum = get_tv_lnum(argvars); /* -1 on type error */
20033 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20034 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020036 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020037 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020038 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039#endif
20040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020041 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042}
20043
20044/*
20045 * "synIDattr(id, what [, mode])" function
20046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020048f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049{
20050 char_u *p = NULL;
20051#ifdef FEAT_SYN_HL
20052 int id;
20053 char_u *what;
20054 char_u *mode;
20055 char_u modebuf[NUMBUFLEN];
20056 int modec;
20057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020058 id = get_tv_number(&argvars[0]);
20059 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020060 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020062 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020064 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 modec = 0; /* replace invalid with current */
20066 }
20067 else
20068 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020069#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020070 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071 modec = 'g';
20072 else
20073#endif
20074 if (t_colors > 1)
20075 modec = 'c';
20076 else
20077 modec = 't';
20078 }
20079
20080
20081 switch (TOLOWER_ASC(what[0]))
20082 {
20083 case 'b':
20084 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20085 p = highlight_color(id, what, modec);
20086 else /* bold */
20087 p = highlight_has_attr(id, HL_BOLD, modec);
20088 break;
20089
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020090 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 p = highlight_color(id, what, modec);
20092 break;
20093
20094 case 'i':
20095 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20096 p = highlight_has_attr(id, HL_INVERSE, modec);
20097 else /* italic */
20098 p = highlight_has_attr(id, HL_ITALIC, modec);
20099 break;
20100
20101 case 'n': /* name */
20102 p = get_highlight_name(NULL, id - 1);
20103 break;
20104
20105 case 'r': /* reverse */
20106 p = highlight_has_attr(id, HL_INVERSE, modec);
20107 break;
20108
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020109 case 's':
20110 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20111 p = highlight_color(id, what, modec);
20112 else /* standout */
20113 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 break;
20115
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020116 case 'u':
20117 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20118 /* underline */
20119 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20120 else
20121 /* undercurl */
20122 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020123 break;
20124 }
20125
20126 if (p != NULL)
20127 p = vim_strsave(p);
20128#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020129 rettv->v_type = VAR_STRING;
20130 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131}
20132
20133/*
20134 * "synIDtrans(id)" function
20135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020137f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138{
20139 int id;
20140
20141#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020142 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143
20144 if (id > 0)
20145 id = syn_get_final_id(id);
20146 else
20147#endif
20148 id = 0;
20149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020150 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151}
20152
20153/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020154 * "synconcealed(lnum, col)" function
20155 */
20156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020157f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020158{
20159#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20160 long lnum;
20161 long col;
20162 int syntax_flags = 0;
20163 int cchar;
20164 int matchid = 0;
20165 char_u str[NUMBUFLEN];
20166#endif
20167
20168 rettv->v_type = VAR_LIST;
20169 rettv->vval.v_list = NULL;
20170
20171#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20172 lnum = get_tv_lnum(argvars); /* -1 on type error */
20173 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20174
20175 vim_memset(str, NUL, sizeof(str));
20176
20177 if (rettv_list_alloc(rettv) != FAIL)
20178 {
20179 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20180 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20181 && curwin->w_p_cole > 0)
20182 {
20183 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20184 syntax_flags = get_syntax_info(&matchid);
20185
20186 /* get the conceal character */
20187 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20188 {
20189 cchar = syn_get_sub_char();
20190 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20191 cchar = lcs_conceal;
20192 if (cchar != NUL)
20193 {
20194# ifdef FEAT_MBYTE
20195 if (has_mbyte)
20196 (*mb_char2bytes)(cchar, str);
20197 else
20198# endif
20199 str[0] = cchar;
20200 }
20201 }
20202 }
20203
20204 list_append_number(rettv->vval.v_list,
20205 (syntax_flags & HL_CONCEAL) != 0);
20206 /* -1 to auto-determine strlen */
20207 list_append_string(rettv->vval.v_list, str, -1);
20208 list_append_number(rettv->vval.v_list, matchid);
20209 }
20210#endif
20211}
20212
20213/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020214 * "synstack(lnum, col)" function
20215 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020217f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020218{
20219#ifdef FEAT_SYN_HL
20220 long lnum;
20221 long col;
20222 int i;
20223 int id;
20224#endif
20225
20226 rettv->v_type = VAR_LIST;
20227 rettv->vval.v_list = NULL;
20228
20229#ifdef FEAT_SYN_HL
20230 lnum = get_tv_lnum(argvars); /* -1 on type error */
20231 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20232
20233 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020234 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020235 && rettv_list_alloc(rettv) != FAIL)
20236 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020237 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020238 for (i = 0; ; ++i)
20239 {
20240 id = syn_get_stack_item(i);
20241 if (id < 0)
20242 break;
20243 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20244 break;
20245 }
20246 }
20247#endif
20248}
20249
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020251get_cmd_output_as_rettv(
20252 typval_T *argvars,
20253 typval_T *rettv,
20254 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020256 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020258 char_u *infile = NULL;
20259 char_u buf[NUMBUFLEN];
20260 int err = FALSE;
20261 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020262 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020263 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020265 rettv->v_type = VAR_STRING;
20266 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020267 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020268 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020269
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020270 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020271 {
20272 /*
20273 * Write the string to a temp file, to be used for input of the shell
20274 * command.
20275 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020276 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020277 {
20278 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020279 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020280 }
20281
20282 fd = mch_fopen((char *)infile, WRITEBIN);
20283 if (fd == NULL)
20284 {
20285 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020286 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020287 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020288 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020289 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020290 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20291 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020292 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020293 else
20294 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020295 size_t len;
20296
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020297 p = get_tv_string_buf_chk(&argvars[1], buf);
20298 if (p == NULL)
20299 {
20300 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020301 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020302 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020303 len = STRLEN(p);
20304 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020305 err = TRUE;
20306 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020307 if (fclose(fd) != 0)
20308 err = TRUE;
20309 if (err)
20310 {
20311 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020312 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020313 }
20314 }
20315
Bram Moolenaar52a72462014-08-29 15:53:52 +020020316 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20317 * echoes typeahead, that messes up the display. */
20318 if (!msg_silent)
20319 flags += SHELL_COOKED;
20320
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020321 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020323 int len;
20324 listitem_T *li;
20325 char_u *s = NULL;
20326 char_u *start;
20327 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020328 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329
Bram Moolenaar52a72462014-08-29 15:53:52 +020020330 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020331 if (res == NULL)
20332 goto errret;
20333
20334 list = list_alloc();
20335 if (list == NULL)
20336 goto errret;
20337
20338 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020340 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020341 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020342 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020343 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020344
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020345 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020346 if (s == NULL)
20347 goto errret;
20348
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020349 for (p = s; start < end; ++p, ++start)
20350 *p = *start == NUL ? NL : *start;
20351 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020352
20353 li = listitem_alloc();
20354 if (li == NULL)
20355 {
20356 vim_free(s);
20357 goto errret;
20358 }
20359 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020360 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020361 li->li_tv.vval.v_string = s;
20362 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020364
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020365 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020366 rettv->v_type = VAR_LIST;
20367 rettv->vval.v_list = list;
20368 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020370 else
20371 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020372 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020373#ifdef USE_CR
20374 /* translate <CR> into <NL> */
20375 if (res != NULL)
20376 {
20377 char_u *s;
20378
20379 for (s = res; *s; ++s)
20380 {
20381 if (*s == CAR)
20382 *s = NL;
20383 }
20384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385#else
20386# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020387 /* translate <CR><NL> into <NL> */
20388 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020390 char_u *s, *d;
20391
20392 d = res;
20393 for (s = res; *s; ++s)
20394 {
20395 if (s[0] == CAR && s[1] == NL)
20396 ++s;
20397 *d++ = *s;
20398 }
20399 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401# endif
20402#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020403 rettv->vval.v_string = res;
20404 res = NULL;
20405 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020406
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020407errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020408 if (infile != NULL)
20409 {
20410 mch_remove(infile);
20411 vim_free(infile);
20412 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020413 if (res != NULL)
20414 vim_free(res);
20415 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020416 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020417}
20418
20419/*
20420 * "system()" function
20421 */
20422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020423f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020424{
20425 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20426}
20427
20428/*
20429 * "systemlist()" function
20430 */
20431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020432f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020433{
20434 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435}
20436
20437/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020438 * "tabpagebuflist()" function
20439 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020441f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020442{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020443#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020444 tabpage_T *tp;
20445 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020446
20447 if (argvars[0].v_type == VAR_UNKNOWN)
20448 wp = firstwin;
20449 else
20450 {
20451 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20452 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020453 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020454 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020455 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020456 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020457 for (; wp != NULL; wp = wp->w_next)
20458 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020459 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020460 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020461 }
20462#endif
20463}
20464
20465
20466/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020467 * "tabpagenr()" function
20468 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020470f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020471{
20472 int nr = 1;
20473#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020474 char_u *arg;
20475
20476 if (argvars[0].v_type != VAR_UNKNOWN)
20477 {
20478 arg = get_tv_string_chk(&argvars[0]);
20479 nr = 0;
20480 if (arg != NULL)
20481 {
20482 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020483 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020484 else
20485 EMSG2(_(e_invexpr2), arg);
20486 }
20487 }
20488 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020489 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020490#endif
20491 rettv->vval.v_number = nr;
20492}
20493
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020494
20495#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020496static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020497
20498/*
20499 * Common code for tabpagewinnr() and winnr().
20500 */
20501 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020502get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020503{
20504 win_T *twin;
20505 int nr = 1;
20506 win_T *wp;
20507 char_u *arg;
20508
20509 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20510 if (argvar->v_type != VAR_UNKNOWN)
20511 {
20512 arg = get_tv_string_chk(argvar);
20513 if (arg == NULL)
20514 nr = 0; /* type error; errmsg already given */
20515 else if (STRCMP(arg, "$") == 0)
20516 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20517 else if (STRCMP(arg, "#") == 0)
20518 {
20519 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20520 if (twin == NULL)
20521 nr = 0;
20522 }
20523 else
20524 {
20525 EMSG2(_(e_invexpr2), arg);
20526 nr = 0;
20527 }
20528 }
20529
20530 if (nr > 0)
20531 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20532 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020533 {
20534 if (wp == NULL)
20535 {
20536 /* didn't find it in this tabpage */
20537 nr = 0;
20538 break;
20539 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020540 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020541 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020542 return nr;
20543}
20544#endif
20545
20546/*
20547 * "tabpagewinnr()" function
20548 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020550f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020551{
20552 int nr = 1;
20553#ifdef FEAT_WINDOWS
20554 tabpage_T *tp;
20555
20556 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20557 if (tp == NULL)
20558 nr = 0;
20559 else
20560 nr = get_winnr(tp, &argvars[1]);
20561#endif
20562 rettv->vval.v_number = nr;
20563}
20564
20565
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020566/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020567 * "tagfiles()" function
20568 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020570f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020571{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020572 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020573 tagname_T tn;
20574 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020575
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020576 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020577 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020578 fname = alloc(MAXPATHL);
20579 if (fname == NULL)
20580 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020581
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020582 for (first = TRUE; ; first = FALSE)
20583 if (get_tagfname(&tn, first, fname) == FAIL
20584 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020585 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020586 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020587 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020588}
20589
20590/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020591 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020592 */
20593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020594f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020595{
20596 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020597
20598 tag_pattern = get_tv_string(&argvars[0]);
20599
20600 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020601 if (*tag_pattern == NUL)
20602 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020603
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020604 if (rettv_list_alloc(rettv) == OK)
20605 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020606}
20607
20608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609 * "tempname()" function
20610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020612f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613{
20614 static int x = 'A';
20615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020616 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020617 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618
20619 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20620 * names. Skip 'I' and 'O', they are used for shell redirection. */
20621 do
20622 {
20623 if (x == 'Z')
20624 x = '0';
20625 else if (x == '9')
20626 x = 'A';
20627 else
20628 {
20629#ifdef EBCDIC
20630 if (x == 'I')
20631 x = 'J';
20632 else if (x == 'R')
20633 x = 'S';
20634 else
20635#endif
20636 ++x;
20637 }
20638 } while (x == 'I' || x == 'O');
20639}
20640
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020641#ifdef FEAT_FLOAT
20642/*
20643 * "tan()" function
20644 */
20645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020646f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020647{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020648 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020649
20650 rettv->v_type = VAR_FLOAT;
20651 if (get_float_arg(argvars, &f) == OK)
20652 rettv->vval.v_float = tan(f);
20653 else
20654 rettv->vval.v_float = 0.0;
20655}
20656
20657/*
20658 * "tanh()" function
20659 */
20660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020661f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020662{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020663 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020664
20665 rettv->v_type = VAR_FLOAT;
20666 if (get_float_arg(argvars, &f) == OK)
20667 rettv->vval.v_float = tanh(f);
20668 else
20669 rettv->vval.v_float = 0.0;
20670}
20671#endif
20672
Bram Moolenaar574860b2016-05-24 17:33:34 +020020673/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020674 * "test_alloc_fail(id, countdown, repeat)" function
20675 */
20676 static void
20677f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20678{
20679 if (argvars[0].v_type != VAR_NUMBER
20680 || argvars[0].vval.v_number <= 0
20681 || argvars[1].v_type != VAR_NUMBER
20682 || argvars[1].vval.v_number < 0
20683 || argvars[2].v_type != VAR_NUMBER)
20684 EMSG(_(e_invarg));
20685 else
20686 {
20687 alloc_fail_id = argvars[0].vval.v_number;
20688 if (alloc_fail_id >= aid_last)
20689 EMSG(_(e_invarg));
20690 alloc_fail_countdown = argvars[1].vval.v_number;
20691 alloc_fail_repeat = argvars[2].vval.v_number;
20692 did_outofmem_msg = FALSE;
20693 }
20694}
20695
20696/*
20697 * "test_disable_char_avail({expr})" function
20698 */
20699 static void
20700f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20701{
20702 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20703}
20704
20705/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020706 * "test_garbagecollect_now()" function
20707 */
20708 static void
20709f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20710{
20711 /* This is dangerous, any Lists and Dicts used internally may be freed
20712 * while still in use. */
20713 garbage_collect(TRUE);
20714}
20715
20716#ifdef FEAT_JOB_CHANNEL
20717 static void
20718f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20719{
20720 rettv->v_type = VAR_CHANNEL;
20721 rettv->vval.v_channel = NULL;
20722}
20723#endif
20724
20725 static void
20726f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20727{
20728 rettv->v_type = VAR_DICT;
20729 rettv->vval.v_dict = NULL;
20730}
20731
20732#ifdef FEAT_JOB_CHANNEL
20733 static void
20734f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20735{
20736 rettv->v_type = VAR_JOB;
20737 rettv->vval.v_job = NULL;
20738}
20739#endif
20740
20741 static void
20742f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20743{
20744 rettv->v_type = VAR_LIST;
20745 rettv->vval.v_list = NULL;
20746}
20747
20748 static void
20749f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20750{
20751 rettv->v_type = VAR_PARTIAL;
20752 rettv->vval.v_partial = NULL;
20753}
20754
20755 static void
20756f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20757{
20758 rettv->v_type = VAR_STRING;
20759 rettv->vval.v_string = NULL;
20760}
20761
Bram Moolenaar975b5272016-03-15 23:10:59 +010020762#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20763/*
20764 * Get a callback from "arg". It can be a Funcref or a function name.
20765 * When "arg" is zero return an empty string.
20766 * Return NULL for an invalid argument.
20767 */
20768 char_u *
20769get_callback(typval_T *arg, partial_T **pp)
20770{
20771 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20772 {
20773 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020774 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020775 return (*pp)->pt_name;
20776 }
20777 *pp = NULL;
20778 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20779 return arg->vval.v_string;
20780 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20781 return (char_u *)"";
20782 EMSG(_("E921: Invalid callback argument"));
20783 return NULL;
20784}
20785#endif
20786
20787#ifdef FEAT_TIMERS
20788/*
20789 * "timer_start(time, callback [, options])" function
20790 */
20791 static void
20792f_timer_start(typval_T *argvars, typval_T *rettv)
20793{
20794 long msec = get_tv_number(&argvars[0]);
20795 timer_T *timer;
20796 int repeat = 0;
20797 char_u *callback;
20798 dict_T *dict;
20799
Bram Moolenaar38499922016-04-22 20:46:52 +020020800 if (check_secure())
20801 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020802 if (argvars[2].v_type != VAR_UNKNOWN)
20803 {
20804 if (argvars[2].v_type != VAR_DICT
20805 || (dict = argvars[2].vval.v_dict) == NULL)
20806 {
20807 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20808 return;
20809 }
20810 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20811 repeat = get_dict_number(dict, (char_u *)"repeat");
20812 }
20813
20814 timer = create_timer(msec, repeat);
20815 callback = get_callback(&argvars[1], &timer->tr_partial);
20816 if (callback == NULL)
20817 {
20818 stop_timer(timer);
20819 rettv->vval.v_number = -1;
20820 }
20821 else
20822 {
20823 timer->tr_callback = vim_strsave(callback);
20824 rettv->vval.v_number = timer->tr_id;
20825 }
20826}
20827
20828/*
20829 * "timer_stop(timer)" function
20830 */
20831 static void
20832f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20833{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020834 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020835
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020836 if (argvars[0].v_type != VAR_NUMBER)
20837 {
20838 EMSG(_(e_number_exp));
20839 return;
20840 }
20841 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020842 if (timer != NULL)
20843 stop_timer(timer);
20844}
20845#endif
20846
Bram Moolenaard52d9742005-08-21 22:20:28 +000020847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 * "tolower(string)" function
20849 */
20850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020851f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852{
20853 char_u *p;
20854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020855 p = vim_strsave(get_tv_string(&argvars[0]));
20856 rettv->v_type = VAR_STRING;
20857 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858
20859 if (p != NULL)
20860 while (*p != NUL)
20861 {
20862#ifdef FEAT_MBYTE
20863 int l;
20864
20865 if (enc_utf8)
20866 {
20867 int c, lc;
20868
20869 c = utf_ptr2char(p);
20870 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020871 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 /* TODO: reallocate string when byte count changes. */
20873 if (utf_char2len(lc) == l)
20874 utf_char2bytes(lc, p);
20875 p += l;
20876 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020877 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878 p += l; /* skip multi-byte character */
20879 else
20880#endif
20881 {
20882 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20883 ++p;
20884 }
20885 }
20886}
20887
20888/*
20889 * "toupper(string)" function
20890 */
20891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020892f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020894 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020895 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896}
20897
20898/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020899 * "tr(string, fromstr, tostr)" function
20900 */
20901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020902f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020903{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020904 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020905 char_u *fromstr;
20906 char_u *tostr;
20907 char_u *p;
20908#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020909 int inlen;
20910 int fromlen;
20911 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020912 int idx;
20913 char_u *cpstr;
20914 int cplen;
20915 int first = TRUE;
20916#endif
20917 char_u buf[NUMBUFLEN];
20918 char_u buf2[NUMBUFLEN];
20919 garray_T ga;
20920
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020921 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020922 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20923 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020924
20925 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020926 rettv->v_type = VAR_STRING;
20927 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020928 if (fromstr == NULL || tostr == NULL)
20929 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020930 ga_init2(&ga, (int)sizeof(char), 80);
20931
20932#ifdef FEAT_MBYTE
20933 if (!has_mbyte)
20934#endif
20935 /* not multi-byte: fromstr and tostr must be the same length */
20936 if (STRLEN(fromstr) != STRLEN(tostr))
20937 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020938#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020939error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020940#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020941 EMSG2(_(e_invarg2), fromstr);
20942 ga_clear(&ga);
20943 return;
20944 }
20945
20946 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020947 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020948 {
20949#ifdef FEAT_MBYTE
20950 if (has_mbyte)
20951 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020952 inlen = (*mb_ptr2len)(in_str);
20953 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020954 cplen = inlen;
20955 idx = 0;
20956 for (p = fromstr; *p != NUL; p += fromlen)
20957 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020958 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020959 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020960 {
20961 for (p = tostr; *p != NUL; p += tolen)
20962 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020963 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020964 if (idx-- == 0)
20965 {
20966 cplen = tolen;
20967 cpstr = p;
20968 break;
20969 }
20970 }
20971 if (*p == NUL) /* tostr is shorter than fromstr */
20972 goto error;
20973 break;
20974 }
20975 ++idx;
20976 }
20977
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020978 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020979 {
20980 /* Check that fromstr and tostr have the same number of
20981 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020982 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020983 first = FALSE;
20984 for (p = tostr; *p != NUL; p += tolen)
20985 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020986 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020987 --idx;
20988 }
20989 if (idx != 0)
20990 goto error;
20991 }
20992
Bram Moolenaarcde88542015-08-11 19:14:00 +020020993 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020994 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020995 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020996
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020997 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020998 }
20999 else
21000#endif
21001 {
21002 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021003 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021004 if (p != NULL)
21005 ga_append(&ga, tostr[p - fromstr]);
21006 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021007 ga_append(&ga, *in_str);
21008 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021009 }
21010 }
21011
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021012 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021013 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021014 ga_append(&ga, NUL);
21015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021016 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021017}
21018
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021019#ifdef FEAT_FLOAT
21020/*
21021 * "trunc({float})" function
21022 */
21023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021024f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021025{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021026 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021027
21028 rettv->v_type = VAR_FLOAT;
21029 if (get_float_arg(argvars, &f) == OK)
21030 /* trunc() is not in C90, use floor() or ceil() instead. */
21031 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21032 else
21033 rettv->vval.v_float = 0.0;
21034}
21035#endif
21036
Bram Moolenaar8299df92004-07-10 09:47:34 +000021037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 * "type(expr)" function
21039 */
21040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021041f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021043 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021044
21045 switch (argvars[0].v_type)
21046 {
21047 case VAR_NUMBER: n = 0; break;
21048 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021049 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021050 case VAR_FUNC: n = 2; break;
21051 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021052 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021053 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021054 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021055 if (argvars[0].vval.v_number == VVAL_FALSE
21056 || argvars[0].vval.v_number == VVAL_TRUE)
21057 n = 6;
21058 else
21059 n = 7;
21060 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021061 case VAR_JOB: n = 8; break;
21062 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021063 case VAR_UNKNOWN:
21064 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21065 n = -1;
21066 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021067 }
21068 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069}
21070
21071/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021072 * "undofile(name)" function
21073 */
21074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021075f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021076{
21077 rettv->v_type = VAR_STRING;
21078#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021079 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021080 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021081
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021082 if (*fname == NUL)
21083 {
21084 /* If there is no file name there will be no undo file. */
21085 rettv->vval.v_string = NULL;
21086 }
21087 else
21088 {
21089 char_u *ffname = FullName_save(fname, FALSE);
21090
21091 if (ffname != NULL)
21092 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21093 vim_free(ffname);
21094 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021095 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021096#else
21097 rettv->vval.v_string = NULL;
21098#endif
21099}
21100
21101/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021102 * "undotree()" function
21103 */
21104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021105f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021106{
21107 if (rettv_dict_alloc(rettv) == OK)
21108 {
21109 dict_T *dict = rettv->vval.v_dict;
21110 list_T *list;
21111
Bram Moolenaar730cde92010-06-27 05:18:54 +020021112 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021113 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021114 dict_add_nr_str(dict, "save_last",
21115 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021116 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21117 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021118 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021119
21120 list = list_alloc();
21121 if (list != NULL)
21122 {
21123 u_eval_tree(curbuf->b_u_oldhead, list);
21124 dict_add_list(dict, "entries", list);
21125 }
21126 }
21127}
21128
21129/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021130 * "values(dict)" function
21131 */
21132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021133f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021134{
21135 dict_list(argvars, rettv, 1);
21136}
21137
21138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 * "virtcol(string)" function
21140 */
21141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021142f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143{
21144 colnr_T vcol = 0;
21145 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021146 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021148 fp = var2fpos(&argvars[0], FALSE, &fnum);
21149 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21150 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 {
21152 getvvcol(curwin, fp, NULL, NULL, &vcol);
21153 ++vcol;
21154 }
21155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021156 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157}
21158
21159/*
21160 * "visualmode()" function
21161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021163f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 char_u str[2];
21166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021167 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 str[0] = curbuf->b_visual_mode_eval;
21169 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021170 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171
21172 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021173 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175}
21176
21177/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021178 * "wildmenumode()" function
21179 */
21180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021181f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021182{
21183#ifdef FEAT_WILDMENU
21184 if (wild_menu_showing)
21185 rettv->vval.v_number = 1;
21186#endif
21187}
21188
21189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 * "winbufnr(nr)" function
21191 */
21192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021193f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194{
21195 win_T *wp;
21196
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021197 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021199 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021201 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202}
21203
21204/*
21205 * "wincol()" function
21206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021208f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209{
21210 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021211 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212}
21213
21214/*
21215 * "winheight(nr)" function
21216 */
21217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021218f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219{
21220 win_T *wp;
21221
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021222 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021224 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021226 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227}
21228
21229/*
21230 * "winline()" function
21231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021233f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234{
21235 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021236 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237}
21238
21239/*
21240 * "winnr()" function
21241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021243f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244{
21245 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021246
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021248 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021250 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251}
21252
21253/*
21254 * "winrestcmd()" function
21255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021257f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258{
21259#ifdef FEAT_WINDOWS
21260 win_T *wp;
21261 int winnr = 1;
21262 garray_T ga;
21263 char_u buf[50];
21264
21265 ga_init2(&ga, (int)sizeof(char), 70);
21266 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21267 {
21268 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21269 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21271 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 ++winnr;
21273 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021274 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021276 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021278 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021280 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281}
21282
21283/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021284 * "winrestview()" function
21285 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021287f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021288{
21289 dict_T *dict;
21290
21291 if (argvars[0].v_type != VAR_DICT
21292 || (dict = argvars[0].vval.v_dict) == NULL)
21293 EMSG(_(e_invarg));
21294 else
21295 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021296 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21297 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21298 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21299 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021300#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021301 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21302 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021303#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021304 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21305 {
21306 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21307 curwin->w_set_curswant = FALSE;
21308 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021309
Bram Moolenaar82c25852014-05-28 16:47:16 +020021310 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21311 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021312#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021313 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21314 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021315#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021316 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21317 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21318 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21319 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021320
21321 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021322 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021323# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021324 win_new_width(curwin, W_WIDTH(curwin));
21325# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021326 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021327
Bram Moolenaarb851a962014-10-31 15:45:52 +010021328 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021329 curwin->w_topline = 1;
21330 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21331 curwin->w_topline = curbuf->b_ml.ml_line_count;
21332#ifdef FEAT_DIFF
21333 check_topfill(curwin, TRUE);
21334#endif
21335 }
21336}
21337
21338/*
21339 * "winsaveview()" function
21340 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021342f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021343{
21344 dict_T *dict;
21345
Bram Moolenaara800b422010-06-27 01:15:55 +020021346 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021347 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021348 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021349
21350 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21351 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21352#ifdef FEAT_VIRTUALEDIT
21353 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21354#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021355 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021356 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21357
21358 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21359#ifdef FEAT_DIFF
21360 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21361#endif
21362 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21363 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21364}
21365
21366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 * "winwidth(nr)" function
21368 */
21369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021370f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371{
21372 win_T *wp;
21373
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021374 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021376 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021378#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021379 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021381 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382#endif
21383}
21384
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021386 * "wordcount()" function
21387 */
21388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021389f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021390{
21391 if (rettv_dict_alloc(rettv) == FAIL)
21392 return;
21393 cursor_pos_info(rettv->vval.v_dict);
21394}
21395
21396/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021397 * Write list of strings to file
21398 */
21399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021400write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021401{
21402 listitem_T *li;
21403 int c;
21404 int ret = OK;
21405 char_u *s;
21406
21407 for (li = list->lv_first; li != NULL; li = li->li_next)
21408 {
21409 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21410 {
21411 if (*s == '\n')
21412 c = putc(NUL, fd);
21413 else
21414 c = putc(*s, fd);
21415 if (c == EOF)
21416 {
21417 ret = FAIL;
21418 break;
21419 }
21420 }
21421 if (!binary || li->li_next != NULL)
21422 if (putc('\n', fd) == EOF)
21423 {
21424 ret = FAIL;
21425 break;
21426 }
21427 if (ret == FAIL)
21428 {
21429 EMSG(_(e_write));
21430 break;
21431 }
21432 }
21433 return ret;
21434}
21435
21436/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021437 * "writefile()" function
21438 */
21439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021440f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021441{
21442 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021443 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021444 char_u *fname;
21445 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021446 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021447
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021448 if (check_restricted() || check_secure())
21449 return;
21450
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021451 if (argvars[0].v_type != VAR_LIST)
21452 {
21453 EMSG2(_(e_listarg), "writefile()");
21454 return;
21455 }
21456 if (argvars[0].vval.v_list == NULL)
21457 return;
21458
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021459 if (argvars[2].v_type != VAR_UNKNOWN)
21460 {
21461 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21462 binary = TRUE;
21463 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21464 append = TRUE;
21465 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021466
21467 /* Always open the file in binary mode, library functions have a mind of
21468 * their own about CR-LF conversion. */
21469 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021470 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21471 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021472 {
21473 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21474 ret = -1;
21475 }
21476 else
21477 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021478 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21479 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021480 fclose(fd);
21481 }
21482
21483 rettv->vval.v_number = ret;
21484}
21485
21486/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021487 * "xor(expr, expr)" function
21488 */
21489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021490f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021491{
21492 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21493 ^ get_tv_number_chk(&argvars[1], NULL);
21494}
21495
21496
21497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021499 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 */
21501 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021502var2fpos(
21503 typval_T *varp,
21504 int dollar_lnum, /* TRUE when $ is last line */
21505 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021507 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021509 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510
Bram Moolenaara5525202006-03-02 22:52:09 +000021511 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021512 if (varp->v_type == VAR_LIST)
21513 {
21514 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021515 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021516 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021517 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021518
21519 l = varp->vval.v_list;
21520 if (l == NULL)
21521 return NULL;
21522
21523 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021524 pos.lnum = list_find_nr(l, 0L, &error);
21525 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021526 return NULL; /* invalid line number */
21527
21528 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021529 pos.col = list_find_nr(l, 1L, &error);
21530 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021531 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021532 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021533
21534 /* We accept "$" for the column number: last column. */
21535 li = list_find(l, 1L);
21536 if (li != NULL && li->li_tv.v_type == VAR_STRING
21537 && li->li_tv.vval.v_string != NULL
21538 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21539 pos.col = len + 1;
21540
Bram Moolenaara5525202006-03-02 22:52:09 +000021541 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021542 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021543 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021544 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021545
Bram Moolenaara5525202006-03-02 22:52:09 +000021546#ifdef FEAT_VIRTUALEDIT
21547 /* Get the virtual offset. Defaults to zero. */
21548 pos.coladd = list_find_nr(l, 2L, &error);
21549 if (error)
21550 pos.coladd = 0;
21551#endif
21552
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021553 return &pos;
21554 }
21555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021556 name = get_tv_string_chk(varp);
21557 if (name == NULL)
21558 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021559 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021561 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21562 {
21563 if (VIsual_active)
21564 return &VIsual;
21565 return &curwin->w_cursor;
21566 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021567 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021569 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21571 return NULL;
21572 return pp;
21573 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021574
21575#ifdef FEAT_VIRTUALEDIT
21576 pos.coladd = 0;
21577#endif
21578
Bram Moolenaar477933c2007-07-17 14:32:23 +000021579 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021580 {
21581 pos.col = 0;
21582 if (name[1] == '0') /* "w0": first visible line */
21583 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021584 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021585 pos.lnum = curwin->w_topline;
21586 return &pos;
21587 }
21588 else if (name[1] == '$') /* "w$": last visible line */
21589 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021590 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021591 pos.lnum = curwin->w_botline - 1;
21592 return &pos;
21593 }
21594 }
21595 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021597 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 {
21599 pos.lnum = curbuf->b_ml.ml_line_count;
21600 pos.col = 0;
21601 }
21602 else
21603 {
21604 pos.lnum = curwin->w_cursor.lnum;
21605 pos.col = (colnr_T)STRLEN(ml_get_curline());
21606 }
21607 return &pos;
21608 }
21609 return NULL;
21610}
21611
21612/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021613 * Convert list in "arg" into a position and optional file number.
21614 * When "fnump" is NULL there is no file number, only 3 items.
21615 * Note that the column is passed on as-is, the caller may want to decrement
21616 * it to use 1 for the first column.
21617 * Return FAIL when conversion is not possible, doesn't check the position for
21618 * validity.
21619 */
21620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021621list2fpos(
21622 typval_T *arg,
21623 pos_T *posp,
21624 int *fnump,
21625 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021626{
21627 list_T *l = arg->vval.v_list;
21628 long i = 0;
21629 long n;
21630
Bram Moolenaar493c1782014-05-28 14:34:46 +020021631 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21632 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021633 if (arg->v_type != VAR_LIST
21634 || l == NULL
21635 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021636 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021637 return FAIL;
21638
21639 if (fnump != NULL)
21640 {
21641 n = list_find_nr(l, i++, NULL); /* fnum */
21642 if (n < 0)
21643 return FAIL;
21644 if (n == 0)
21645 n = curbuf->b_fnum; /* current buffer */
21646 *fnump = n;
21647 }
21648
21649 n = list_find_nr(l, i++, NULL); /* lnum */
21650 if (n < 0)
21651 return FAIL;
21652 posp->lnum = n;
21653
21654 n = list_find_nr(l, i++, NULL); /* col */
21655 if (n < 0)
21656 return FAIL;
21657 posp->col = n;
21658
21659#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021660 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021661 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021662 posp->coladd = 0;
21663 else
21664 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021665#endif
21666
Bram Moolenaar493c1782014-05-28 14:34:46 +020021667 if (curswantp != NULL)
21668 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21669
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021670 return OK;
21671}
21672
21673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 * Get the length of an environment variable name.
21675 * Advance "arg" to the first character after the name.
21676 * Return 0 for error.
21677 */
21678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021679get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680{
21681 char_u *p;
21682 int len;
21683
21684 for (p = *arg; vim_isIDc(*p); ++p)
21685 ;
21686 if (p == *arg) /* no name found */
21687 return 0;
21688
21689 len = (int)(p - *arg);
21690 *arg = p;
21691 return len;
21692}
21693
21694/*
21695 * Get the length of the name of a function or internal variable.
21696 * "arg" is advanced to the first non-white character after the name.
21697 * Return 0 if something is wrong.
21698 */
21699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021700get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701{
21702 char_u *p;
21703 int len;
21704
21705 /* Find the end of the name. */
21706 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021707 {
21708 if (*p == ':')
21709 {
21710 /* "s:" is start of "s:var", but "n:" is not and can be used in
21711 * slice "[n:]". Also "xx:" is not a namespace. */
21712 len = (int)(p - *arg);
21713 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21714 || len > 1)
21715 break;
21716 }
21717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 if (p == *arg) /* no name found */
21719 return 0;
21720
21721 len = (int)(p - *arg);
21722 *arg = skipwhite(p);
21723
21724 return len;
21725}
21726
21727/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021728 * Get the length of the name of a variable or function.
21729 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021731 * Return -1 if curly braces expansion failed.
21732 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 * If the name contains 'magic' {}'s, expand them and return the
21734 * expanded name in an allocated string via 'alias' - caller must free.
21735 */
21736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021737get_name_len(
21738 char_u **arg,
21739 char_u **alias,
21740 int evaluate,
21741 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742{
21743 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 char_u *p;
21745 char_u *expr_start;
21746 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747
21748 *alias = NULL; /* default to no alias */
21749
21750 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21751 && (*arg)[2] == (int)KE_SNR)
21752 {
21753 /* hard coded <SNR>, already translated */
21754 *arg += 3;
21755 return get_id_len(arg) + 3;
21756 }
21757 len = eval_fname_script(*arg);
21758 if (len > 0)
21759 {
21760 /* literal "<SID>", "s:" or "<SNR>" */
21761 *arg += len;
21762 }
21763
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021765 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021767 p = find_name_end(*arg, &expr_start, &expr_end,
21768 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 if (expr_start != NULL)
21770 {
21771 char_u *temp_string;
21772
21773 if (!evaluate)
21774 {
21775 len += (int)(p - *arg);
21776 *arg = skipwhite(p);
21777 return len;
21778 }
21779
21780 /*
21781 * Include any <SID> etc in the expanded string:
21782 * Thus the -len here.
21783 */
21784 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21785 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021786 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 *alias = temp_string;
21788 *arg = skipwhite(p);
21789 return (int)STRLEN(temp_string);
21790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791
21792 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021793 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 EMSG2(_(e_invexpr2), *arg);
21795
21796 return len;
21797}
21798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021799/*
21800 * Find the end of a variable or function name, taking care of magic braces.
21801 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21802 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021803 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021804 * Return a pointer to just after the name. Equal to "arg" if there is no
21805 * valid name.
21806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021808find_name_end(
21809 char_u *arg,
21810 char_u **expr_start,
21811 char_u **expr_end,
21812 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021814 int mb_nest = 0;
21815 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021817 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021819 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021821 *expr_start = NULL;
21822 *expr_end = NULL;
21823 }
21824
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021825 /* Quick check for valid starting character. */
21826 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21827 return arg;
21828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021829 for (p = arg; *p != NUL
21830 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021831 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021832 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021833 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021834 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021835 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021836 if (*p == '\'')
21837 {
21838 /* skip over 'string' to avoid counting [ and ] inside it. */
21839 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21840 ;
21841 if (*p == NUL)
21842 break;
21843 }
21844 else if (*p == '"')
21845 {
21846 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21847 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21848 if (*p == '\\' && p[1] != NUL)
21849 ++p;
21850 if (*p == NUL)
21851 break;
21852 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021853 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21854 {
21855 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021856 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021857 len = (int)(p - arg);
21858 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021859 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021860 break;
21861 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021863 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021865 if (*p == '[')
21866 ++br_nest;
21867 else if (*p == ']')
21868 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021871 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021873 if (*p == '{')
21874 {
21875 mb_nest++;
21876 if (expr_start != NULL && *expr_start == NULL)
21877 *expr_start = p;
21878 }
21879 else if (*p == '}')
21880 {
21881 mb_nest--;
21882 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21883 *expr_end = p;
21884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 }
21887
21888 return p;
21889}
21890
21891/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021892 * Expands out the 'magic' {}'s in a variable/function name.
21893 * Note that this can call itself recursively, to deal with
21894 * constructs like foo{bar}{baz}{bam}
21895 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21896 * "in_start" ^
21897 * "expr_start" ^
21898 * "expr_end" ^
21899 * "in_end" ^
21900 *
21901 * Returns a new allocated string, which the caller must free.
21902 * Returns NULL for failure.
21903 */
21904 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021905make_expanded_name(
21906 char_u *in_start,
21907 char_u *expr_start,
21908 char_u *expr_end,
21909 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021910{
21911 char_u c1;
21912 char_u *retval = NULL;
21913 char_u *temp_result;
21914 char_u *nextcmd = NULL;
21915
21916 if (expr_end == NULL || in_end == NULL)
21917 return NULL;
21918 *expr_start = NUL;
21919 *expr_end = NUL;
21920 c1 = *in_end;
21921 *in_end = NUL;
21922
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021923 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021924 if (temp_result != NULL && nextcmd == NULL)
21925 {
21926 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21927 + (in_end - expr_end) + 1));
21928 if (retval != NULL)
21929 {
21930 STRCPY(retval, in_start);
21931 STRCAT(retval, temp_result);
21932 STRCAT(retval, expr_end + 1);
21933 }
21934 }
21935 vim_free(temp_result);
21936
21937 *in_end = c1; /* put char back for error messages */
21938 *expr_start = '{';
21939 *expr_end = '}';
21940
21941 if (retval != NULL)
21942 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021943 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021944 if (expr_start != NULL)
21945 {
21946 /* Further expansion! */
21947 temp_result = make_expanded_name(retval, expr_start,
21948 expr_end, temp_result);
21949 vim_free(retval);
21950 retval = temp_result;
21951 }
21952 }
21953
21954 return retval;
21955}
21956
21957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021959 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960 */
21961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021962eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021964 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21965}
21966
21967/*
21968 * Return TRUE if character "c" can be used as the first character in a
21969 * variable or function name (excluding '{' and '}').
21970 */
21971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021972eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021973{
21974 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975}
21976
21977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 * Set number v: variable to "val".
21979 */
21980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021981set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021983 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984}
21985
21986/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021987 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 */
21989 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021990get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021992 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993}
21994
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021995/*
21996 * Get string v: variable value. Uses a static buffer, can only be used once.
21997 */
21998 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021999get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022000{
22001 return get_tv_string(&vimvars[idx].vv_tv);
22002}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022003
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022005 * Get List v: variable value. Caller must take care of reference count when
22006 * needed.
22007 */
22008 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022009get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022010{
22011 return vimvars[idx].vv_list;
22012}
22013
22014/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022015 * Set v:char to character "c".
22016 */
22017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022018set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022019{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022020 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022021
22022#ifdef FEAT_MBYTE
22023 if (has_mbyte)
22024 buf[(*mb_char2bytes)(c, buf)] = NUL;
22025 else
22026#endif
22027 {
22028 buf[0] = c;
22029 buf[1] = NUL;
22030 }
22031 set_vim_var_string(VV_CHAR, buf, -1);
22032}
22033
22034/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022035 * Set v:count to "count" and v:count1 to "count1".
22036 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 */
22038 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022039set_vcount(
22040 long count,
22041 long count1,
22042 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022044 if (set_prevcount)
22045 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022046 vimvars[VV_COUNT].vv_nr = count;
22047 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048}
22049
22050/*
22051 * Set string v: variable to a copy of "val".
22052 */
22053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022054set_vim_var_string(
22055 int idx,
22056 char_u *val,
22057 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058{
Bram Moolenaara542c682016-01-31 16:28:04 +010022059 clear_tv(&vimvars[idx].vv_di.di_tv);
22060 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022062 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022064 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022066 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067}
22068
22069/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022070 * Set List v: variable to "val".
22071 */
22072 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022073set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022074{
Bram Moolenaara542c682016-01-31 16:28:04 +010022075 clear_tv(&vimvars[idx].vv_di.di_tv);
22076 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022077 vimvars[idx].vv_list = val;
22078 if (val != NULL)
22079 ++val->lv_refcount;
22080}
22081
22082/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022083 * Set Dictionary v: variable to "val".
22084 */
22085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022086set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022087{
22088 int todo;
22089 hashitem_T *hi;
22090
Bram Moolenaara542c682016-01-31 16:28:04 +010022091 clear_tv(&vimvars[idx].vv_di.di_tv);
22092 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022093 vimvars[idx].vv_dict = val;
22094 if (val != NULL)
22095 {
22096 ++val->dv_refcount;
22097
22098 /* Set readonly */
22099 todo = (int)val->dv_hashtab.ht_used;
22100 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22101 {
22102 if (HASHITEM_EMPTY(hi))
22103 continue;
22104 --todo;
22105 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22106 }
22107 }
22108}
22109
22110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111 * Set v:register if needed.
22112 */
22113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022114set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115{
22116 char_u regname;
22117
22118 if (c == 0 || c == ' ')
22119 regname = '"';
22120 else
22121 regname = c;
22122 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022123 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124 set_vim_var_string(VV_REG, &regname, 1);
22125}
22126
22127/*
22128 * Get or set v:exception. If "oldval" == NULL, return the current value.
22129 * Otherwise, restore the value to "oldval" and return NULL.
22130 * Must always be called in pairs to save and restore v:exception! Does not
22131 * take care of memory allocations.
22132 */
22133 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022134v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135{
22136 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022137 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138
Bram Moolenaare9a41262005-01-15 22:18:47 +000022139 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140 return NULL;
22141}
22142
22143/*
22144 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22145 * Otherwise, restore the value to "oldval" and return NULL.
22146 * Must always be called in pairs to save and restore v:throwpoint! Does not
22147 * take care of memory allocations.
22148 */
22149 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022150v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151{
22152 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022153 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154
Bram Moolenaare9a41262005-01-15 22:18:47 +000022155 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 return NULL;
22157}
22158
22159#if defined(FEAT_AUTOCMD) || defined(PROTO)
22160/*
22161 * Set v:cmdarg.
22162 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22163 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22164 * Must always be called in pairs!
22165 */
22166 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022167set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168{
22169 char_u *oldval;
22170 char_u *newval;
22171 unsigned len;
22172
Bram Moolenaare9a41262005-01-15 22:18:47 +000022173 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022174 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022176 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022177 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022178 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 }
22180
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022181 if (eap->force_bin == FORCE_BIN)
22182 len = 6;
22183 else if (eap->force_bin == FORCE_NOBIN)
22184 len = 8;
22185 else
22186 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022187
22188 if (eap->read_edit)
22189 len += 7;
22190
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022191 if (eap->force_ff != 0)
22192 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22193# ifdef FEAT_MBYTE
22194 if (eap->force_enc != 0)
22195 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022196 if (eap->bad_char != 0)
22197 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022198# endif
22199
22200 newval = alloc(len + 1);
22201 if (newval == NULL)
22202 return NULL;
22203
22204 if (eap->force_bin == FORCE_BIN)
22205 sprintf((char *)newval, " ++bin");
22206 else if (eap->force_bin == FORCE_NOBIN)
22207 sprintf((char *)newval, " ++nobin");
22208 else
22209 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022210
22211 if (eap->read_edit)
22212 STRCAT(newval, " ++edit");
22213
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022214 if (eap->force_ff != 0)
22215 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22216 eap->cmd + eap->force_ff);
22217# ifdef FEAT_MBYTE
22218 if (eap->force_enc != 0)
22219 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22220 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022221 if (eap->bad_char == BAD_KEEP)
22222 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22223 else if (eap->bad_char == BAD_DROP)
22224 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22225 else if (eap->bad_char != 0)
22226 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022227# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022228 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022229 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230}
22231#endif
22232
22233/*
22234 * Get the value of internal variable "name".
22235 * Return OK or FAIL.
22236 */
22237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022238get_var_tv(
22239 char_u *name,
22240 int len, /* length of "name" */
22241 typval_T *rettv, /* NULL when only checking existence */
22242 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22243 int verbose, /* may give error message */
22244 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245{
22246 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022247 typval_T *tv = NULL;
22248 typval_T atv;
22249 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251
22252 /* truncate the name, so that we can use strcmp() */
22253 cc = name[len];
22254 name[len] = NUL;
22255
22256 /*
22257 * Check for "b:changedtick".
22258 */
22259 if (STRCMP(name, "b:changedtick") == 0)
22260 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022261 atv.v_type = VAR_NUMBER;
22262 atv.vval.v_number = curbuf->b_changedtick;
22263 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 }
22265
22266 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 * Check for user-defined variables.
22268 */
22269 else
22270 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022271 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022273 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022274 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022275 if (dip != NULL)
22276 *dip = v;
22277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 }
22279
Bram Moolenaare9a41262005-01-15 22:18:47 +000022280 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022282 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022283 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 ret = FAIL;
22285 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022286 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022287 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288
22289 name[len] = cc;
22290
22291 return ret;
22292}
22293
22294/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022295 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22296 * Also handle function call with Funcref variable: func(expr)
22297 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22298 */
22299 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022300handle_subscript(
22301 char_u **arg,
22302 typval_T *rettv,
22303 int evaluate, /* do more than finding the end */
22304 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022305{
22306 int ret = OK;
22307 dict_T *selfdict = NULL;
22308 char_u *s;
22309 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022310 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022311
22312 while (ret == OK
22313 && (**arg == '['
22314 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022315 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22316 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022317 && !vim_iswhite(*(*arg - 1)))
22318 {
22319 if (**arg == '(')
22320 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022321 partial_T *pt = NULL;
22322
Bram Moolenaard9fba312005-06-26 22:34:35 +000022323 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022324 if (evaluate)
22325 {
22326 functv = *rettv;
22327 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022328
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022329 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022330 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022331 {
22332 pt = functv.vval.v_partial;
22333 s = pt->pt_name;
22334 }
22335 else
22336 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022337 }
22338 else
22339 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022340 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022341 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022342 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022343
22344 /* Clear the funcref afterwards, so that deleting it while
22345 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022346 if (evaluate)
22347 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022348
22349 /* Stop the expression evaluation when immediately aborting on
22350 * error, or when an interrupt occurred or an exception was thrown
22351 * but not caught. */
22352 if (aborting())
22353 {
22354 if (ret == OK)
22355 clear_tv(rettv);
22356 ret = FAIL;
22357 }
22358 dict_unref(selfdict);
22359 selfdict = NULL;
22360 }
22361 else /* **arg == '[' || **arg == '.' */
22362 {
22363 dict_unref(selfdict);
22364 if (rettv->v_type == VAR_DICT)
22365 {
22366 selfdict = rettv->vval.v_dict;
22367 if (selfdict != NULL)
22368 ++selfdict->dv_refcount;
22369 }
22370 else
22371 selfdict = NULL;
22372 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22373 {
22374 clear_tv(rettv);
22375 ret = FAIL;
22376 }
22377 }
22378 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022379
Bram Moolenaar1d429612016-05-24 15:44:17 +020022380 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22381 * Don't do this when "Func" is already a partial that was bound
22382 * explicitly (pt_auto is FALSE). */
22383 if (selfdict != NULL
22384 && (rettv->v_type == VAR_FUNC
22385 || (rettv->v_type == VAR_PARTIAL
22386 && (rettv->vval.v_partial->pt_auto
22387 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022388 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022389 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22390 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022391 char_u *tofree = NULL;
22392 ufunc_T *fp;
22393 char_u fname_buf[FLEN_FIXED + 1];
22394 int error;
22395
22396 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022397 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022398 fp = find_func(fname);
22399 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022400
Bram Moolenaar65639032016-03-16 21:40:30 +010022401 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022402 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022403 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22404
22405 if (pt != NULL)
22406 {
22407 pt->pt_refcount = 1;
22408 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022409 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022410 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022411 if (rettv->v_type == VAR_FUNC)
22412 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022413 /* Just a function: Take over the function name and use
22414 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022415 pt->pt_name = rettv->vval.v_string;
22416 }
22417 else
22418 {
22419 partial_T *ret_pt = rettv->vval.v_partial;
22420 int i;
22421
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022422 /* Partial: copy the function name, use selfdict and copy
22423 * args. Can't take over name or args, the partial might
22424 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022425 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022426 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022427 if (ret_pt->pt_argc > 0)
22428 {
22429 pt->pt_argv = (typval_T *)alloc(
22430 sizeof(typval_T) * ret_pt->pt_argc);
22431 if (pt->pt_argv == NULL)
22432 /* out of memory: drop the arguments */
22433 pt->pt_argc = 0;
22434 else
22435 {
22436 pt->pt_argc = ret_pt->pt_argc;
22437 for (i = 0; i < pt->pt_argc; i++)
22438 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22439 }
22440 }
22441 partial_unref(ret_pt);
22442 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022443 rettv->v_type = VAR_PARTIAL;
22444 rettv->vval.v_partial = pt;
22445 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022446 }
22447 }
22448
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022449 dict_unref(selfdict);
22450 return ret;
22451}
22452
22453/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022454 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022455 * value).
22456 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022457 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022458alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022459{
Bram Moolenaar33570922005-01-25 22:26:29 +000022460 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022461}
22462
22463/*
22464 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 * The string "s" must have been allocated, it is consumed.
22466 * Return NULL for out of memory, the variable otherwise.
22467 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022468 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022469alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470{
Bram Moolenaar33570922005-01-25 22:26:29 +000022471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022473 rettv = alloc_tv();
22474 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022476 rettv->v_type = VAR_STRING;
22477 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 }
22479 else
22480 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022481 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482}
22483
22484/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022485 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022487 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022488free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489{
22490 if (varp != NULL)
22491 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022492 switch (varp->v_type)
22493 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022494 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022495 func_unref(varp->vval.v_string);
22496 /*FALLTHROUGH*/
22497 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022498 vim_free(varp->vval.v_string);
22499 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022500 case VAR_PARTIAL:
22501 partial_unref(varp->vval.v_partial);
22502 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022503 case VAR_LIST:
22504 list_unref(varp->vval.v_list);
22505 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022506 case VAR_DICT:
22507 dict_unref(varp->vval.v_dict);
22508 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022509 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022510#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022511 job_unref(varp->vval.v_job);
22512 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022513#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022514 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022515#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022516 channel_unref(varp->vval.v_channel);
22517 break;
22518#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022519 case VAR_NUMBER:
22520 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022521 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022522 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022523 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 vim_free(varp);
22526 }
22527}
22528
22529/*
22530 * Free the memory for a variable value and set the value to NULL or 0.
22531 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022532 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022533clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534{
22535 if (varp != NULL)
22536 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022537 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022539 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022540 func_unref(varp->vval.v_string);
22541 /*FALLTHROUGH*/
22542 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022543 vim_free(varp->vval.v_string);
22544 varp->vval.v_string = NULL;
22545 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022546 case VAR_PARTIAL:
22547 partial_unref(varp->vval.v_partial);
22548 varp->vval.v_partial = NULL;
22549 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022550 case VAR_LIST:
22551 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022552 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022553 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022554 case VAR_DICT:
22555 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022556 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022557 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022558 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022559 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022560 varp->vval.v_number = 0;
22561 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022562 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022563#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022564 varp->vval.v_float = 0.0;
22565 break;
22566#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022567 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022568#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022569 job_unref(varp->vval.v_job);
22570 varp->vval.v_job = NULL;
22571#endif
22572 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022573 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022574#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022575 channel_unref(varp->vval.v_channel);
22576 varp->vval.v_channel = NULL;
22577#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022578 case VAR_UNKNOWN:
22579 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022581 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 }
22583}
22584
22585/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022586 * Set the value of a variable to NULL without freeing items.
22587 */
22588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022589init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022590{
22591 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022593}
22594
22595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 * Get the number value of a variable.
22597 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022598 * For incompatible types, return 0.
22599 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22600 * caller of incompatible types: it sets *denote to TRUE if "denote"
22601 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022603 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022604get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022606 int error = FALSE;
22607
22608 return get_tv_number_chk(varp, &error); /* return 0L on error */
22609}
22610
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022611 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022612get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022613{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022614 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022616 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022618 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022619 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022620 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022621#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022622 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022623 break;
22624#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022625 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022626 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022627 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022628 break;
22629 case VAR_STRING:
22630 if (varp->vval.v_string != NULL)
22631 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022632 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022633 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022634 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022635 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022636 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022637 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022638 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022639 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022640 case VAR_SPECIAL:
22641 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22642 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022643 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022644#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022645 EMSG(_("E910: Using a Job as a Number"));
22646 break;
22647#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022648 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022649#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022650 EMSG(_("E913: Using a Channel as a Number"));
22651 break;
22652#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022653 case VAR_UNKNOWN:
22654 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022655 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022657 if (denote == NULL) /* useful for values that must be unsigned */
22658 n = -1;
22659 else
22660 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022661 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662}
22663
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022664#ifdef FEAT_FLOAT
22665 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022666get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022667{
22668 switch (varp->v_type)
22669 {
22670 case VAR_NUMBER:
22671 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022672 case VAR_FLOAT:
22673 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022674 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022675 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022676 EMSG(_("E891: Using a Funcref as a Float"));
22677 break;
22678 case VAR_STRING:
22679 EMSG(_("E892: Using a String as a Float"));
22680 break;
22681 case VAR_LIST:
22682 EMSG(_("E893: Using a List as a Float"));
22683 break;
22684 case VAR_DICT:
22685 EMSG(_("E894: Using a Dictionary as a Float"));
22686 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022687 case VAR_SPECIAL:
22688 EMSG(_("E907: Using a special value as a Float"));
22689 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022690 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022691# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022692 EMSG(_("E911: Using a Job as a Float"));
22693 break;
22694# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022695 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022696# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022697 EMSG(_("E914: Using a Channel as a Float"));
22698 break;
22699# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022700 case VAR_UNKNOWN:
22701 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022702 break;
22703 }
22704 return 0;
22705}
22706#endif
22707
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022709 * Get the lnum from the first argument.
22710 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022711 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 */
22713 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022714get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715{
Bram Moolenaar33570922005-01-25 22:26:29 +000022716 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 linenr_T lnum;
22718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022719 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 if (lnum == 0) /* no valid number, try using line() */
22721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022722 rettv.v_type = VAR_NUMBER;
22723 f_line(argvars, &rettv);
22724 lnum = rettv.vval.v_number;
22725 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 }
22727 return lnum;
22728}
22729
22730/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022731 * Get the lnum from the first argument.
22732 * Also accepts "$", then "buf" is used.
22733 * Returns 0 on error.
22734 */
22735 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022736get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022737{
22738 if (argvars[0].v_type == VAR_STRING
22739 && argvars[0].vval.v_string != NULL
22740 && argvars[0].vval.v_string[0] == '$'
22741 && buf != NULL)
22742 return buf->b_ml.ml_line_count;
22743 return get_tv_number_chk(&argvars[0], NULL);
22744}
22745
22746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 * Get the string value of a variable.
22748 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022749 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22750 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 * If the String variable has never been set, return an empty string.
22752 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022753 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22754 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022756 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022757get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758{
22759 static char_u mybuf[NUMBUFLEN];
22760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022761 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762}
22763
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022764 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022765get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022767 char_u *res = get_tv_string_buf_chk(varp, buf);
22768
22769 return res != NULL ? res : (char_u *)"";
22770}
22771
Bram Moolenaar7d647822014-04-05 21:28:56 +020022772/*
22773 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22774 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022775 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022776get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022777{
22778 static char_u mybuf[NUMBUFLEN];
22779
22780 return get_tv_string_buf_chk(varp, mybuf);
22781}
22782
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022783 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022784get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022785{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022786 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022787 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022788 case VAR_NUMBER:
22789 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22790 return buf;
22791 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022792 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022793 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022794 break;
22795 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022796 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022797 break;
22798 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022799 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022800 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022801 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022802#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022803 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022804 break;
22805#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022806 case VAR_STRING:
22807 if (varp->vval.v_string != NULL)
22808 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022809 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022810 case VAR_SPECIAL:
22811 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22812 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022813 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022814#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022815 {
22816 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022817 char *status;
22818
22819 if (job == NULL)
22820 return (char_u *)"no process";
22821 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022822 : job->jv_status == JOB_ENDED ? "dead"
22823 : "run";
22824# ifdef UNIX
22825 vim_snprintf((char *)buf, NUMBUFLEN,
22826 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022827# elif defined(WIN32)
22828 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022829 "process %ld %s",
22830 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022831 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022832# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022833 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022834 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22835# endif
22836 return buf;
22837 }
22838#endif
22839 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022840 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022841#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022842 {
22843 channel_T *channel = varp->vval.v_channel;
22844 char *status = channel_status(channel);
22845
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022846 if (channel == NULL)
22847 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22848 else
22849 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022850 "channel %d %s", channel->ch_id, status);
22851 return buf;
22852 }
22853#endif
22854 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022855 case VAR_UNKNOWN:
22856 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022857 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022859 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860}
22861
22862/*
22863 * Find variable "name" in the list of variables.
22864 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022865 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022866 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022867 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022869 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022870find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022873 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874
Bram Moolenaara7043832005-01-21 11:56:39 +000022875 ht = find_var_ht(name, &varname);
22876 if (htp != NULL)
22877 *htp = ht;
22878 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022880 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881}
22882
22883/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022884 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022885 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022887 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022888find_var_in_ht(
22889 hashtab_T *ht,
22890 int htname,
22891 char_u *varname,
22892 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022893{
Bram Moolenaar33570922005-01-25 22:26:29 +000022894 hashitem_T *hi;
22895
22896 if (*varname == NUL)
22897 {
22898 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022899 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022900 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022901 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022902 case 'g': return &globvars_var;
22903 case 'v': return &vimvars_var;
22904 case 'b': return &curbuf->b_bufvar;
22905 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022906#ifdef FEAT_WINDOWS
22907 case 't': return &curtab->tp_winvar;
22908#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022909 case 'l': return current_funccal == NULL
22910 ? NULL : &current_funccal->l_vars_var;
22911 case 'a': return current_funccal == NULL
22912 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022913 }
22914 return NULL;
22915 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022916
22917 hi = hash_find(ht, varname);
22918 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022919 {
22920 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022921 * worked find the variable again. Don't auto-load a script if it was
22922 * loaded already, otherwise it would be loaded every time when
22923 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022924 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022925 {
22926 /* Note: script_autoload() may make "hi" invalid. It must either
22927 * be obtained again or not used. */
22928 if (!script_autoload(varname, FALSE) || aborting())
22929 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022930 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022931 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022932 if (HASHITEM_EMPTY(hi))
22933 return NULL;
22934 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022935 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022936}
22937
22938/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022939 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022940 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022941 * Set "varname" to the start of name without ':'.
22942 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022943 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022944find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022946 hashitem_T *hi;
22947
Bram Moolenaar73627d02015-08-11 15:46:09 +020022948 if (name[0] == NUL)
22949 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 if (name[1] != ':')
22951 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022952 /* The name must not start with a colon or #. */
22953 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 return NULL;
22955 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022956
22957 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022958 hi = hash_find(&compat_hashtab, name);
22959 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022960 return &compat_hashtab;
22961
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022963 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022964 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 }
22966 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022967 if (*name == 'g') /* global variable */
22968 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022969 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22970 */
22971 if (vim_strchr(name + 2, ':') != NULL
22972 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022973 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022975 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022977 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022978#ifdef FEAT_WINDOWS
22979 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022980 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022981#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022982 if (*name == 'v') /* v: variable */
22983 return &vimvarht;
22984 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022985 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022986 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022987 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 if (*name == 's' /* script variable */
22989 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22990 return &SCRIPT_VARS(current_SID);
22991 return NULL;
22992}
22993
22994/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022995 * Get function call environment based on bactrace debug level
22996 */
22997 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022998get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022999{
23000 int i;
23001 funccall_T *funccal;
23002 funccall_T *temp_funccal;
23003
23004 funccal = current_funccal;
23005 if (debug_backtrace_level > 0)
23006 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023007 for (i = 0; i < debug_backtrace_level; i++)
23008 {
23009 temp_funccal = funccal->caller;
23010 if (temp_funccal)
23011 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023012 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023013 /* backtrace level overflow. reset to max */
23014 debug_backtrace_level = i;
23015 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023016 }
23017 return funccal;
23018}
23019
23020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023022 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023 * Returns NULL when it doesn't exist.
23024 */
23025 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023026get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027{
Bram Moolenaar33570922005-01-25 22:26:29 +000023028 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023030 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031 if (v == NULL)
23032 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023033 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034}
23035
23036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023037 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 * sourcing this script and when executing functions defined in the script.
23039 */
23040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023041new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042{
Bram Moolenaara7043832005-01-21 11:56:39 +000023043 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023044 hashtab_T *ht;
23045 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023046
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23048 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023049 /* Re-allocating ga_data means that an ht_array pointing to
23050 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023051 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023052 for (i = 1; i <= ga_scripts.ga_len; ++i)
23053 {
23054 ht = &SCRIPT_VARS(i);
23055 if (ht->ht_mask == HT_INIT_SIZE - 1)
23056 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023057 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023058 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023059 }
23060
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 while (ga_scripts.ga_len < id)
23062 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023063 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023064 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023065 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 }
23068 }
23069}
23070
23071/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023072 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23073 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 */
23075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023076init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077{
Bram Moolenaar33570922005-01-25 22:26:29 +000023078 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023079 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023080 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023081 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023082 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023083 dict_var->di_tv.vval.v_dict = dict;
23084 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023085 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023086 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23087 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088}
23089
23090/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023091 * Unreference a dictionary initialized by init_var_dict().
23092 */
23093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023094unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023095{
23096 /* Now the dict needs to be freed if no one else is using it, go back to
23097 * normal reference counting. */
23098 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23099 dict_unref(dict);
23100}
23101
23102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023104 * Frees all allocated variables and the value they contain.
23105 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106 */
23107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023108vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023109{
23110 vars_clear_ext(ht, TRUE);
23111}
23112
23113/*
23114 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23115 */
23116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023117vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118{
Bram Moolenaara7043832005-01-21 11:56:39 +000023119 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023120 hashitem_T *hi;
23121 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122
Bram Moolenaar33570922005-01-25 22:26:29 +000023123 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023124 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023125 for (hi = ht->ht_array; todo > 0; ++hi)
23126 {
23127 if (!HASHITEM_EMPTY(hi))
23128 {
23129 --todo;
23130
Bram Moolenaar33570922005-01-25 22:26:29 +000023131 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023132 * ht_array might change then. hash_clear() takes care of it
23133 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023134 v = HI2DI(hi);
23135 if (free_val)
23136 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023137 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023138 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023139 }
23140 }
23141 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023142 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143}
23144
Bram Moolenaara7043832005-01-21 11:56:39 +000023145/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023146 * Delete a variable from hashtab "ht" at item "hi".
23147 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023150delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151{
Bram Moolenaar33570922005-01-25 22:26:29 +000023152 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023153
23154 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023155 clear_tv(&di->di_tv);
23156 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157}
23158
23159/*
23160 * List the value of one internal variable.
23161 */
23162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023163list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023165 char_u *tofree;
23166 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023167 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023168
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023169 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023171 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023172 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173}
23174
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023176list_one_var_a(
23177 char_u *prefix,
23178 char_u *name,
23179 int type,
23180 char_u *string,
23181 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182{
Bram Moolenaar31859182007-08-14 20:41:13 +000023183 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23184 msg_start();
23185 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 if (name != NULL) /* "a:" vars don't have a name stored */
23187 msg_puts(name);
23188 msg_putchar(' ');
23189 msg_advance(22);
23190 if (type == VAR_NUMBER)
23191 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023192 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023193 msg_putchar('*');
23194 else if (type == VAR_LIST)
23195 {
23196 msg_putchar('[');
23197 if (*string == '[')
23198 ++string;
23199 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023200 else if (type == VAR_DICT)
23201 {
23202 msg_putchar('{');
23203 if (*string == '{')
23204 ++string;
23205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 else
23207 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023208
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023210
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023211 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023212 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023213 if (*first)
23214 {
23215 msg_clr_eos();
23216 *first = FALSE;
23217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218}
23219
23220/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023221 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222 * If the variable already exists, the value is updated.
23223 * Otherwise the variable is created.
23224 */
23225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023226set_var(
23227 char_u *name,
23228 typval_T *tv,
23229 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230{
Bram Moolenaar33570922005-01-25 22:26:29 +000023231 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023232 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023233 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023235 ht = find_var_ht(name, &varname);
23236 if (ht == NULL || *varname == NUL)
23237 {
23238 EMSG2(_(e_illvar), name);
23239 return;
23240 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023241 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023242
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023243 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23244 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023245 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023246
Bram Moolenaar33570922005-01-25 22:26:29 +000023247 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023249 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023250 if (var_check_ro(v->di_flags, name, FALSE)
23251 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023252 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023253
23254 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023255 * Handle setting internal v: variables separately where needed to
23256 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023257 */
23258 if (ht == &vimvarht)
23259 {
23260 if (v->di_tv.v_type == VAR_STRING)
23261 {
23262 vim_free(v->di_tv.vval.v_string);
23263 if (copy || tv->v_type != VAR_STRING)
23264 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23265 else
23266 {
23267 /* Take over the string to avoid an extra alloc/free. */
23268 v->di_tv.vval.v_string = tv->vval.v_string;
23269 tv->vval.v_string = NULL;
23270 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023271 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023272 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023273 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023274 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023275 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023276 if (STRCMP(varname, "searchforward") == 0)
23277 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023278#ifdef FEAT_SEARCH_EXTRA
23279 else if (STRCMP(varname, "hlsearch") == 0)
23280 {
23281 no_hlsearch = !v->di_tv.vval.v_number;
23282 redraw_all_later(SOME_VALID);
23283 }
23284#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023285 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023286 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023287 else if (v->di_tv.v_type != tv->v_type)
23288 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023289 }
23290
23291 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 }
23293 else /* add a new variable */
23294 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023295 /* Can't add "v:" variable. */
23296 if (ht == &vimvarht)
23297 {
23298 EMSG2(_(e_illvar), name);
23299 return;
23300 }
23301
Bram Moolenaar92124a32005-06-17 22:03:40 +000023302 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023303 if (!valid_varname(varname))
23304 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023305
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023306 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23307 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023308 if (v == NULL)
23309 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023310 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023311 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023313 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023314 return;
23315 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023316 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023318
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023319 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023320 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023321 else
23322 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023323 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023324 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023325 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327}
23328
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023329/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023330 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023331 * Also give an error message.
23332 */
23333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023334var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023335{
23336 if (flags & DI_FLAGS_RO)
23337 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023338 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023339 return TRUE;
23340 }
23341 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23342 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023343 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023344 return TRUE;
23345 }
23346 return FALSE;
23347}
23348
23349/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023350 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23351 * Also give an error message.
23352 */
23353 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023354var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023355{
23356 if (flags & DI_FLAGS_FIX)
23357 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023358 EMSG2(_("E795: Cannot delete variable %s"),
23359 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023360 return TRUE;
23361 }
23362 return FALSE;
23363}
23364
23365/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023366 * Check if a funcref is assigned to a valid variable name.
23367 * Return TRUE and give an error if not.
23368 */
23369 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023370var_check_func_name(
23371 char_u *name, /* points to start of variable name */
23372 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023373{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023374 /* Allow for w: b: s: and t:. */
23375 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023376 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23377 ? name[2] : name[0]))
23378 {
23379 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23380 name);
23381 return TRUE;
23382 }
23383 /* Don't allow hiding a function. When "v" is not NULL we might be
23384 * assigning another function to the same var, the type is checked
23385 * below. */
23386 if (new_var && function_exists(name))
23387 {
23388 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23389 name);
23390 return TRUE;
23391 }
23392 return FALSE;
23393}
23394
23395/*
23396 * Check if a variable name is valid.
23397 * Return FALSE and give an error if not.
23398 */
23399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023400valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023401{
23402 char_u *p;
23403
23404 for (p = varname; *p != NUL; ++p)
23405 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23406 && *p != AUTOLOAD_CHAR)
23407 {
23408 EMSG2(_(e_illvar), varname);
23409 return FALSE;
23410 }
23411 return TRUE;
23412}
23413
23414/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023415 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023416 * Also give an error message, using "name" or _("name") when use_gettext is
23417 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023418 */
23419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023420tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023421{
23422 if (lock & VAR_LOCKED)
23423 {
23424 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023425 name == NULL ? (char_u *)_("Unknown")
23426 : use_gettext ? (char_u *)_(name)
23427 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023428 return TRUE;
23429 }
23430 if (lock & VAR_FIXED)
23431 {
23432 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023433 name == NULL ? (char_u *)_("Unknown")
23434 : use_gettext ? (char_u *)_(name)
23435 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023436 return TRUE;
23437 }
23438 return FALSE;
23439}
23440
23441/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023442 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023443 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023444 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023445 * It is OK for "from" and "to" to point to the same item. This is used to
23446 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023447 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023449copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023451 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023452 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023453 switch (from->v_type)
23454 {
23455 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023456 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023457 to->vval.v_number = from->vval.v_number;
23458 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023459 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023460#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023461 to->vval.v_float = from->vval.v_float;
23462 break;
23463#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023464 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023465#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023466 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023467 if (to->vval.v_job != NULL)
23468 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023469 break;
23470#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023471 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023472#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023473 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023474 if (to->vval.v_channel != NULL)
23475 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023476 break;
23477#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023478 case VAR_STRING:
23479 case VAR_FUNC:
23480 if (from->vval.v_string == NULL)
23481 to->vval.v_string = NULL;
23482 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023483 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023484 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023485 if (from->v_type == VAR_FUNC)
23486 func_ref(to->vval.v_string);
23487 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023488 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023489 case VAR_PARTIAL:
23490 if (from->vval.v_partial == NULL)
23491 to->vval.v_partial = NULL;
23492 else
23493 {
23494 to->vval.v_partial = from->vval.v_partial;
23495 ++to->vval.v_partial->pt_refcount;
23496 }
23497 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023498 case VAR_LIST:
23499 if (from->vval.v_list == NULL)
23500 to->vval.v_list = NULL;
23501 else
23502 {
23503 to->vval.v_list = from->vval.v_list;
23504 ++to->vval.v_list->lv_refcount;
23505 }
23506 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023507 case VAR_DICT:
23508 if (from->vval.v_dict == NULL)
23509 to->vval.v_dict = NULL;
23510 else
23511 {
23512 to->vval.v_dict = from->vval.v_dict;
23513 ++to->vval.v_dict->dv_refcount;
23514 }
23515 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023516 case VAR_UNKNOWN:
23517 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023518 break;
23519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520}
23521
23522/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023523 * Make a copy of an item.
23524 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023525 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23526 * reference to an already copied list/dict can be used.
23527 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023528 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023529 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023530item_copy(
23531 typval_T *from,
23532 typval_T *to,
23533 int deep,
23534 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023535{
23536 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023537 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023538
Bram Moolenaar33570922005-01-25 22:26:29 +000023539 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023540 {
23541 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023542 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023543 }
23544 ++recurse;
23545
23546 switch (from->v_type)
23547 {
23548 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023549 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023550 case VAR_STRING:
23551 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023552 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023553 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023554 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023555 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023556 copy_tv(from, to);
23557 break;
23558 case VAR_LIST:
23559 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023560 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023561 if (from->vval.v_list == NULL)
23562 to->vval.v_list = NULL;
23563 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23564 {
23565 /* use the copy made earlier */
23566 to->vval.v_list = from->vval.v_list->lv_copylist;
23567 ++to->vval.v_list->lv_refcount;
23568 }
23569 else
23570 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23571 if (to->vval.v_list == NULL)
23572 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023573 break;
23574 case VAR_DICT:
23575 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023576 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023577 if (from->vval.v_dict == NULL)
23578 to->vval.v_dict = NULL;
23579 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23580 {
23581 /* use the copy made earlier */
23582 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23583 ++to->vval.v_dict->dv_refcount;
23584 }
23585 else
23586 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23587 if (to->vval.v_dict == NULL)
23588 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023589 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023590 case VAR_UNKNOWN:
23591 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023592 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023593 }
23594 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023595 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023596}
23597
23598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 * ":echo expr1 ..." print each argument separated with a space, add a
23600 * newline at the end.
23601 * ":echon expr1 ..." print each argument plain.
23602 */
23603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023604ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605{
23606 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023607 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023608 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023609 char_u *p;
23610 int needclr = TRUE;
23611 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023612 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613
23614 if (eap->skip)
23615 ++emsg_skip;
23616 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23617 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023618 /* If eval1() causes an error message the text from the command may
23619 * still need to be cleared. E.g., "echo 22,44". */
23620 need_clr_eos = needclr;
23621
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023623 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 {
23625 /*
23626 * Report the invalid expression unless the expression evaluation
23627 * has been cancelled due to an aborting error, an interrupt, or an
23628 * exception.
23629 */
23630 if (!aborting())
23631 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023632 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 break;
23634 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023635 need_clr_eos = FALSE;
23636
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637 if (!eap->skip)
23638 {
23639 if (atstart)
23640 {
23641 atstart = FALSE;
23642 /* Call msg_start() after eval1(), evaluating the expression
23643 * may cause a message to appear. */
23644 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023645 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023646 /* Mark the saved text as finishing the line, so that what
23647 * follows is displayed on a new line when scrolling back
23648 * at the more prompt. */
23649 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 }
23653 else if (eap->cmdidx == CMD_echo)
23654 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023655 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023656 if (p != NULL)
23657 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023659 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023661 if (*p != TAB && needclr)
23662 {
23663 /* remove any text still there from the command */
23664 msg_clr_eos();
23665 needclr = FALSE;
23666 }
23667 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 }
23669 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023670 {
23671#ifdef FEAT_MBYTE
23672 if (has_mbyte)
23673 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023674 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023675
23676 (void)msg_outtrans_len_attr(p, i, echo_attr);
23677 p += i - 1;
23678 }
23679 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023681 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023684 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687 arg = skipwhite(arg);
23688 }
23689 eap->nextcmd = check_nextcmd(arg);
23690
23691 if (eap->skip)
23692 --emsg_skip;
23693 else
23694 {
23695 /* remove text that may still be there from the command */
23696 if (needclr)
23697 msg_clr_eos();
23698 if (eap->cmdidx == CMD_echo)
23699 msg_end();
23700 }
23701}
23702
23703/*
23704 * ":echohl {name}".
23705 */
23706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023707ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708{
23709 int id;
23710
23711 id = syn_name2id(eap->arg);
23712 if (id == 0)
23713 echo_attr = 0;
23714 else
23715 echo_attr = syn_id2attr(id);
23716}
23717
23718/*
23719 * ":execute expr1 ..." execute the result of an expression.
23720 * ":echomsg expr1 ..." Print a message
23721 * ":echoerr expr1 ..." Print an error
23722 * Each gets spaces around each argument and a newline at the end for
23723 * echo commands
23724 */
23725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023726ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023727{
23728 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023729 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730 int ret = OK;
23731 char_u *p;
23732 garray_T ga;
23733 int len;
23734 int save_did_emsg;
23735
23736 ga_init2(&ga, 1, 80);
23737
23738 if (eap->skip)
23739 ++emsg_skip;
23740 while (*arg != NUL && *arg != '|' && *arg != '\n')
23741 {
23742 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023743 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023744 {
23745 /*
23746 * Report the invalid expression unless the expression evaluation
23747 * has been cancelled due to an aborting error, an interrupt, or an
23748 * exception.
23749 */
23750 if (!aborting())
23751 EMSG2(_(e_invexpr2), p);
23752 ret = FAIL;
23753 break;
23754 }
23755
23756 if (!eap->skip)
23757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023758 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 len = (int)STRLEN(p);
23760 if (ga_grow(&ga, len + 2) == FAIL)
23761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023762 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 ret = FAIL;
23764 break;
23765 }
23766 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 ga.ga_len += len;
23770 }
23771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023772 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 arg = skipwhite(arg);
23774 }
23775
23776 if (ret != FAIL && ga.ga_data != NULL)
23777 {
23778 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023781 out_flush();
23782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783 else if (eap->cmdidx == CMD_echoerr)
23784 {
23785 /* We don't want to abort following commands, restore did_emsg. */
23786 save_did_emsg = did_emsg;
23787 EMSG((char_u *)ga.ga_data);
23788 if (!force_abort)
23789 did_emsg = save_did_emsg;
23790 }
23791 else if (eap->cmdidx == CMD_execute)
23792 do_cmdline((char_u *)ga.ga_data,
23793 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23794 }
23795
23796 ga_clear(&ga);
23797
23798 if (eap->skip)
23799 --emsg_skip;
23800
23801 eap->nextcmd = check_nextcmd(arg);
23802}
23803
23804/*
23805 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23806 * "arg" points to the "&" or '+' when called, to "option" when returning.
23807 * Returns NULL when no option name found. Otherwise pointer to the char
23808 * after the option name.
23809 */
23810 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023811find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812{
23813 char_u *p = *arg;
23814
23815 ++p;
23816 if (*p == 'g' && p[1] == ':')
23817 {
23818 *opt_flags = OPT_GLOBAL;
23819 p += 2;
23820 }
23821 else if (*p == 'l' && p[1] == ':')
23822 {
23823 *opt_flags = OPT_LOCAL;
23824 p += 2;
23825 }
23826 else
23827 *opt_flags = 0;
23828
23829 if (!ASCII_ISALPHA(*p))
23830 return NULL;
23831 *arg = p;
23832
23833 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23834 p += 4; /* termcap option */
23835 else
23836 while (ASCII_ISALPHA(*p))
23837 ++p;
23838 return p;
23839}
23840
23841/*
23842 * ":function"
23843 */
23844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023845ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846{
23847 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023848 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 int j;
23850 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023852 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853 char_u *name = NULL;
23854 char_u *p;
23855 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023856 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 garray_T newargs;
23858 garray_T newlines;
23859 int varargs = FALSE;
23860 int mustend = FALSE;
23861 int flags = 0;
23862 ufunc_T *fp;
23863 int indent;
23864 int nesting;
23865 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023866 dictitem_T *v;
23867 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023868 static int func_nr = 0; /* number for nameless function */
23869 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023870 hashtab_T *ht;
23871 int todo;
23872 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023873 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874
23875 /*
23876 * ":function" without argument: list functions.
23877 */
23878 if (ends_excmd(*eap->arg))
23879 {
23880 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023881 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023882 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023883 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023884 {
23885 if (!HASHITEM_EMPTY(hi))
23886 {
23887 --todo;
23888 fp = HI2UF(hi);
23889 if (!isdigit(*fp->uf_name))
23890 list_func_head(fp, FALSE);
23891 }
23892 }
23893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 eap->nextcmd = check_nextcmd(eap->arg);
23895 return;
23896 }
23897
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023898 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023899 * ":function /pat": list functions matching pattern.
23900 */
23901 if (*eap->arg == '/')
23902 {
23903 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23904 if (!eap->skip)
23905 {
23906 regmatch_T regmatch;
23907
23908 c = *p;
23909 *p = NUL;
23910 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23911 *p = c;
23912 if (regmatch.regprog != NULL)
23913 {
23914 regmatch.rm_ic = p_ic;
23915
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023916 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023917 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23918 {
23919 if (!HASHITEM_EMPTY(hi))
23920 {
23921 --todo;
23922 fp = HI2UF(hi);
23923 if (!isdigit(*fp->uf_name)
23924 && vim_regexec(&regmatch, fp->uf_name, 0))
23925 list_func_head(fp, FALSE);
23926 }
23927 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023928 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023929 }
23930 }
23931 if (*p == '/')
23932 ++p;
23933 eap->nextcmd = check_nextcmd(p);
23934 return;
23935 }
23936
23937 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023938 * Get the function name. There are these situations:
23939 * func normal function name
23940 * "name" == func, "fudi.fd_dict" == NULL
23941 * dict.func new dictionary entry
23942 * "name" == NULL, "fudi.fd_dict" set,
23943 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23944 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023945 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023946 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23947 * dict.func existing dict entry that's not a Funcref
23948 * "name" == NULL, "fudi.fd_dict" set,
23949 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023950 * s:func script-local function name
23951 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023954 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023955 paren = (vim_strchr(p, '(') != NULL);
23956 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023957 {
23958 /*
23959 * Return on an invalid expression in braces, unless the expression
23960 * evaluation has been cancelled due to an aborting error, an
23961 * interrupt, or an exception.
23962 */
23963 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023964 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023965 if (!eap->skip && fudi.fd_newkey != NULL)
23966 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023967 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970 else
23971 eap->skip = TRUE;
23972 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023973
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 /* An error in a function call during evaluation of an expression in magic
23975 * braces should not cause the function not to be defined. */
23976 saved_did_emsg = did_emsg;
23977 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978
23979 /*
23980 * ":function func" with only function name: list function.
23981 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023982 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983 {
23984 if (!ends_excmd(*skipwhite(p)))
23985 {
23986 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023987 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 }
23989 eap->nextcmd = check_nextcmd(p);
23990 if (eap->nextcmd != NULL)
23991 *p = NUL;
23992 if (!eap->skip && !got_int)
23993 {
23994 fp = find_func(name);
23995 if (fp != NULL)
23996 {
23997 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023998 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024000 if (FUNCLINE(fp, j) == NULL)
24001 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 msg_putchar('\n');
24003 msg_outnum((long)(j + 1));
24004 if (j < 9)
24005 msg_putchar(' ');
24006 if (j < 99)
24007 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024008 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 out_flush(); /* show a line at a time */
24010 ui_breakcheck();
24011 }
24012 if (!got_int)
24013 {
24014 msg_putchar('\n');
24015 msg_puts((char_u *)" endfunction");
24016 }
24017 }
24018 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024019 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024021 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 }
24023
24024 /*
24025 * ":function name(arg1, arg2)" Define function.
24026 */
24027 p = skipwhite(p);
24028 if (*p != '(')
24029 {
24030 if (!eap->skip)
24031 {
24032 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024033 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 }
24035 /* attempt to continue by skipping some text */
24036 if (vim_strchr(p, '(') != NULL)
24037 p = vim_strchr(p, '(');
24038 }
24039 p = skipwhite(p + 1);
24040
24041 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24042 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24043
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024044 if (!eap->skip)
24045 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024046 /* Check the name of the function. Unless it's a dictionary function
24047 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024048 if (name != NULL)
24049 arg = name;
24050 else
24051 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024052 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024053 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24054 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024055 {
24056 if (*arg == K_SPECIAL)
24057 j = 3;
24058 else
24059 j = 0;
24060 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24061 : eval_isnamec(arg[j])))
24062 ++j;
24063 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024064 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024065 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024066 /* Disallow using the g: dict. */
24067 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24068 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024069 }
24070
Bram Moolenaar071d4272004-06-13 20:20:40 +000024071 /*
24072 * Isolate the arguments: "arg1, arg2, ...)"
24073 */
24074 while (*p != ')')
24075 {
24076 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24077 {
24078 varargs = TRUE;
24079 p += 3;
24080 mustend = TRUE;
24081 }
24082 else
24083 {
24084 arg = p;
24085 while (ASCII_ISALNUM(*p) || *p == '_')
24086 ++p;
24087 if (arg == p || isdigit(*arg)
24088 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24089 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24090 {
24091 if (!eap->skip)
24092 EMSG2(_("E125: Illegal argument: %s"), arg);
24093 break;
24094 }
24095 if (ga_grow(&newargs, 1) == FAIL)
24096 goto erret;
24097 c = *p;
24098 *p = NUL;
24099 arg = vim_strsave(arg);
24100 if (arg == NULL)
24101 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024102
24103 /* Check for duplicate argument name. */
24104 for (i = 0; i < newargs.ga_len; ++i)
24105 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24106 {
24107 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024108 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024109 goto erret;
24110 }
24111
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24113 *p = c;
24114 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 if (*p == ',')
24116 ++p;
24117 else
24118 mustend = TRUE;
24119 }
24120 p = skipwhite(p);
24121 if (mustend && *p != ')')
24122 {
24123 if (!eap->skip)
24124 EMSG2(_(e_invarg2), eap->arg);
24125 break;
24126 }
24127 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024128 if (*p != ')')
24129 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 ++p; /* skip the ')' */
24131
Bram Moolenaare9a41262005-01-15 22:18:47 +000024132 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 for (;;)
24134 {
24135 p = skipwhite(p);
24136 if (STRNCMP(p, "range", 5) == 0)
24137 {
24138 flags |= FC_RANGE;
24139 p += 5;
24140 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024141 else if (STRNCMP(p, "dict", 4) == 0)
24142 {
24143 flags |= FC_DICT;
24144 p += 4;
24145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024146 else if (STRNCMP(p, "abort", 5) == 0)
24147 {
24148 flags |= FC_ABORT;
24149 p += 5;
24150 }
24151 else
24152 break;
24153 }
24154
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024155 /* When there is a line break use what follows for the function body.
24156 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24157 if (*p == '\n')
24158 line_arg = p + 1;
24159 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160 EMSG(_(e_trailing));
24161
24162 /*
24163 * Read the body of the function, until ":endfunction" is found.
24164 */
24165 if (KeyTyped)
24166 {
24167 /* Check if the function already exists, don't let the user type the
24168 * whole function before telling him it doesn't work! For a script we
24169 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024170 if (!eap->skip && !eap->forceit)
24171 {
24172 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24173 EMSG(_(e_funcdict));
24174 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024175 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024177
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024178 if (!eap->skip && did_emsg)
24179 goto erret;
24180
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 msg_putchar('\n'); /* don't overwrite the function name */
24182 cmdline_row = msg_row;
24183 }
24184
24185 indent = 2;
24186 nesting = 0;
24187 for (;;)
24188 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024189 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024190 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024191 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024192 saved_wait_return = FALSE;
24193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024195 sourcing_lnum_off = sourcing_lnum;
24196
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024197 if (line_arg != NULL)
24198 {
24199 /* Use eap->arg, split up in parts by line breaks. */
24200 theline = line_arg;
24201 p = vim_strchr(theline, '\n');
24202 if (p == NULL)
24203 line_arg += STRLEN(line_arg);
24204 else
24205 {
24206 *p = NUL;
24207 line_arg = p + 1;
24208 }
24209 }
24210 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 theline = getcmdline(':', 0L, indent);
24212 else
24213 theline = eap->getline(':', eap->cookie, indent);
24214 if (KeyTyped)
24215 lines_left = Rows - 1;
24216 if (theline == NULL)
24217 {
24218 EMSG(_("E126: Missing :endfunction"));
24219 goto erret;
24220 }
24221
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024222 /* Detect line continuation: sourcing_lnum increased more than one. */
24223 if (sourcing_lnum > sourcing_lnum_off + 1)
24224 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24225 else
24226 sourcing_lnum_off = 0;
24227
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 if (skip_until != NULL)
24229 {
24230 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24231 * don't check for ":endfunc". */
24232 if (STRCMP(theline, skip_until) == 0)
24233 {
24234 vim_free(skip_until);
24235 skip_until = NULL;
24236 }
24237 }
24238 else
24239 {
24240 /* skip ':' and blanks*/
24241 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24242 ;
24243
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024244 /* Check for "endfunction". */
24245 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024247 if (line_arg == NULL)
24248 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 break;
24250 }
24251
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024252 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 * at "end". */
24254 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24255 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024256 else if (STRNCMP(p, "if", 2) == 0
24257 || STRNCMP(p, "wh", 2) == 0
24258 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 || STRNCMP(p, "try", 3) == 0)
24260 indent += 2;
24261
24262 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024263 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024265 if (*p == '!')
24266 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024268 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024269 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024271 ++nesting;
24272 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273 }
24274 }
24275
24276 /* Check for ":append" or ":insert". */
24277 p = skip_range(p, NULL);
24278 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24279 || (p[0] == 'i'
24280 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24281 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24282 skip_until = vim_strsave((char_u *)".");
24283
24284 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24285 arg = skipwhite(skiptowhite(p));
24286 if (arg[0] == '<' && arg[1] =='<'
24287 && ((p[0] == 'p' && p[1] == 'y'
24288 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24289 || (p[0] == 'p' && p[1] == 'e'
24290 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24291 || (p[0] == 't' && p[1] == 'c'
24292 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024293 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24294 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24296 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024297 || (p[0] == 'm' && p[1] == 'z'
24298 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 ))
24300 {
24301 /* ":python <<" continues until a dot, like ":append" */
24302 p = skipwhite(arg + 2);
24303 if (*p == NUL)
24304 skip_until = vim_strsave((char_u *)".");
24305 else
24306 skip_until = vim_strsave(p);
24307 }
24308 }
24309
24310 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024311 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024312 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024313 if (line_arg == NULL)
24314 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024316 }
24317
24318 /* Copy the line to newly allocated memory. get_one_sourceline()
24319 * allocates 250 bytes per line, this saves 80% on average. The cost
24320 * is an extra alloc/free. */
24321 p = vim_strsave(theline);
24322 if (p != NULL)
24323 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024324 if (line_arg == NULL)
24325 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024326 theline = p;
24327 }
24328
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024329 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24330
24331 /* Add NULL lines for continuation lines, so that the line count is
24332 * equal to the index in the growarray. */
24333 while (sourcing_lnum_off-- > 0)
24334 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024335
24336 /* Check for end of eap->arg. */
24337 if (line_arg != NULL && *line_arg == NUL)
24338 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339 }
24340
24341 /* Don't define the function when skipping commands or when an error was
24342 * detected. */
24343 if (eap->skip || did_emsg)
24344 goto erret;
24345
24346 /*
24347 * If there are no errors, add the function
24348 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024349 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024350 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024351 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024352 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024353 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024354 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024355 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024356 goto erret;
24357 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024358
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024359 fp = find_func(name);
24360 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024362 if (!eap->forceit)
24363 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024364 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024365 goto erret;
24366 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024367 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024368 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024369 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024370 name);
24371 goto erret;
24372 }
24373 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024374 ga_clear_strings(&(fp->uf_args));
24375 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024376 vim_free(name);
24377 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 }
24380 else
24381 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024382 char numbuf[20];
24383
24384 fp = NULL;
24385 if (fudi.fd_newkey == NULL && !eap->forceit)
24386 {
24387 EMSG(_(e_funcdict));
24388 goto erret;
24389 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024390 if (fudi.fd_di == NULL)
24391 {
24392 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024393 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024394 goto erret;
24395 }
24396 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024397 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024398 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024399
24400 /* Give the function a sequential number. Can only be used with a
24401 * Funcref! */
24402 vim_free(name);
24403 sprintf(numbuf, "%d", ++func_nr);
24404 name = vim_strsave((char_u *)numbuf);
24405 if (name == NULL)
24406 goto erret;
24407 }
24408
24409 if (fp == NULL)
24410 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024411 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024412 {
24413 int slen, plen;
24414 char_u *scriptname;
24415
24416 /* Check that the autoload name matches the script name. */
24417 j = FAIL;
24418 if (sourcing_name != NULL)
24419 {
24420 scriptname = autoload_name(name);
24421 if (scriptname != NULL)
24422 {
24423 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024424 plen = (int)STRLEN(p);
24425 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024426 if (slen > plen && fnamecmp(p,
24427 sourcing_name + slen - plen) == 0)
24428 j = OK;
24429 vim_free(scriptname);
24430 }
24431 }
24432 if (j == FAIL)
24433 {
24434 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24435 goto erret;
24436 }
24437 }
24438
24439 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440 if (fp == NULL)
24441 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024442
24443 if (fudi.fd_dict != NULL)
24444 {
24445 if (fudi.fd_di == NULL)
24446 {
24447 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024448 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024449 if (fudi.fd_di == NULL)
24450 {
24451 vim_free(fp);
24452 goto erret;
24453 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024454 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24455 {
24456 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024457 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024458 goto erret;
24459 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024460 }
24461 else
24462 /* overwrite existing dict entry */
24463 clear_tv(&fudi.fd_di->di_tv);
24464 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024465 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024466 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024467 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024468
24469 /* behave like "dict" was used */
24470 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024471 }
24472
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024474 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024475 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24476 {
24477 vim_free(fp);
24478 goto erret;
24479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024481 fp->uf_args = newargs;
24482 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024483#ifdef FEAT_PROFILE
24484 fp->uf_tml_count = NULL;
24485 fp->uf_tml_total = NULL;
24486 fp->uf_tml_self = NULL;
24487 fp->uf_profiling = FALSE;
24488 if (prof_def_func())
24489 func_do_profile(fp);
24490#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024491 fp->uf_varargs = varargs;
24492 fp->uf_flags = flags;
24493 fp->uf_calls = 0;
24494 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024495 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496
24497erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498 ga_clear_strings(&newargs);
24499 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024500ret_free:
24501 vim_free(skip_until);
24502 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024505 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506}
24507
24508/*
24509 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024510 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024512 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024513 * TFN_INT: internal function name OK
24514 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024515 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516 * Advances "pp" to just after the function name (if no error).
24517 */
24518 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024519trans_function_name(
24520 char_u **pp,
24521 int skip, /* only find the end, don't evaluate */
24522 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024523 funcdict_T *fdp, /* return: info about dictionary used */
24524 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024526 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 char_u *start;
24528 char_u *end;
24529 int lead;
24530 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024532 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024533
24534 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024535 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024536 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024537
24538 /* Check for hard coded <SNR>: already translated function ID (from a user
24539 * command). */
24540 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24541 && (*pp)[2] == (int)KE_SNR)
24542 {
24543 *pp += 3;
24544 len = get_id_len(pp) + 3;
24545 return vim_strnsave(start, len);
24546 }
24547
24548 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24549 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024551 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024552 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024553
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024554 /* Note that TFN_ flags use the same values as GLV_ flags. */
24555 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024556 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024557 if (end == start)
24558 {
24559 if (!skip)
24560 EMSG(_("E129: Function name required"));
24561 goto theend;
24562 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024563 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024564 {
24565 /*
24566 * Report an invalid expression in braces, unless the expression
24567 * evaluation has been cancelled due to an aborting error, an
24568 * interrupt, or an exception.
24569 */
24570 if (!aborting())
24571 {
24572 if (end != NULL)
24573 EMSG2(_(e_invarg2), start);
24574 }
24575 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024576 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024577 goto theend;
24578 }
24579
24580 if (lv.ll_tv != NULL)
24581 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024582 if (fdp != NULL)
24583 {
24584 fdp->fd_dict = lv.ll_dict;
24585 fdp->fd_newkey = lv.ll_newkey;
24586 lv.ll_newkey = NULL;
24587 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024588 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024589 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24590 {
24591 name = vim_strsave(lv.ll_tv->vval.v_string);
24592 *pp = end;
24593 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024594 else if (lv.ll_tv->v_type == VAR_PARTIAL
24595 && lv.ll_tv->vval.v_partial != NULL)
24596 {
24597 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24598 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024599 if (partial != NULL)
24600 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024601 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024602 else
24603 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024604 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24605 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024606 EMSG(_(e_funcref));
24607 else
24608 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024609 name = NULL;
24610 }
24611 goto theend;
24612 }
24613
24614 if (lv.ll_name == NULL)
24615 {
24616 /* Error found, but continue after the function name. */
24617 *pp = end;
24618 goto theend;
24619 }
24620
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024621 /* Check if the name is a Funcref. If so, use the value. */
24622 if (lv.ll_exp_name != NULL)
24623 {
24624 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024625 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024626 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024627 if (name == lv.ll_exp_name)
24628 name = NULL;
24629 }
24630 else
24631 {
24632 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024633 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024634 if (name == *pp)
24635 name = NULL;
24636 }
24637 if (name != NULL)
24638 {
24639 name = vim_strsave(name);
24640 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024641 if (STRNCMP(name, "<SNR>", 5) == 0)
24642 {
24643 /* Change "<SNR>" to the byte sequence. */
24644 name[0] = K_SPECIAL;
24645 name[1] = KS_EXTRA;
24646 name[2] = (int)KE_SNR;
24647 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24648 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024649 goto theend;
24650 }
24651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024652 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024653 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024654 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024655 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24656 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24657 {
24658 /* When there was "s:" already or the name expanded to get a
24659 * leading "s:" then remove it. */
24660 lv.ll_name += 2;
24661 len -= 2;
24662 lead = 2;
24663 }
24664 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024665 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024666 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024667 /* skip over "s:" and "g:" */
24668 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024669 lv.ll_name += 2;
24670 len = (int)(end - lv.ll_name);
24671 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024672
24673 /*
24674 * Copy the function name to allocated memory.
24675 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24676 * Accept <SNR>123_name() outside a script.
24677 */
24678 if (skip)
24679 lead = 0; /* do nothing */
24680 else if (lead > 0)
24681 {
24682 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024683 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24684 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024685 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024686 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024687 if (current_SID <= 0)
24688 {
24689 EMSG(_(e_usingsid));
24690 goto theend;
24691 }
24692 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24693 lead += (int)STRLEN(sid_buf);
24694 }
24695 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024696 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024697 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024698 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024699 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024700 goto theend;
24701 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024702 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024703 {
24704 char_u *cp = vim_strchr(lv.ll_name, ':');
24705
24706 if (cp != NULL && cp < end)
24707 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024708 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024709 goto theend;
24710 }
24711 }
24712
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024713 name = alloc((unsigned)(len + lead + 1));
24714 if (name != NULL)
24715 {
24716 if (lead > 0)
24717 {
24718 name[0] = K_SPECIAL;
24719 name[1] = KS_EXTRA;
24720 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024721 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024722 STRCPY(name + 3, sid_buf);
24723 }
24724 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024725 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024726 }
24727 *pp = end;
24728
24729theend:
24730 clear_lval(&lv);
24731 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024732}
24733
24734/*
24735 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24736 * Return 2 if "p" starts with "s:".
24737 * Return 0 otherwise.
24738 */
24739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024740eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024742 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24743 * the standard library function. */
24744 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24745 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024746 return 5;
24747 if (p[0] == 's' && p[1] == ':')
24748 return 2;
24749 return 0;
24750}
24751
24752/*
24753 * Return TRUE if "p" starts with "<SID>" or "s:".
24754 * Only works if eval_fname_script() returned non-zero for "p"!
24755 */
24756 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024757eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758{
24759 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24760}
24761
24762/*
24763 * List the head of the function: "name(arg1, arg2)".
24764 */
24765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024766list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024767{
24768 int j;
24769
24770 msg_start();
24771 if (indent)
24772 MSG_PUTS(" ");
24773 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024774 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775 {
24776 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024777 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024778 }
24779 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024780 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024782 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783 {
24784 if (j)
24785 MSG_PUTS(", ");
24786 msg_puts(FUNCARG(fp, j));
24787 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024788 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789 {
24790 if (j)
24791 MSG_PUTS(", ");
24792 MSG_PUTS("...");
24793 }
24794 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024795 if (fp->uf_flags & FC_ABORT)
24796 MSG_PUTS(" abort");
24797 if (fp->uf_flags & FC_RANGE)
24798 MSG_PUTS(" range");
24799 if (fp->uf_flags & FC_DICT)
24800 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024801 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024802 if (p_verbose > 0)
24803 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804}
24805
24806/*
24807 * Find a function by name, return pointer to it in ufuncs.
24808 * Return NULL for unknown function.
24809 */
24810 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024811find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024813 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024815 hi = hash_find(&func_hashtab, name);
24816 if (!HASHITEM_EMPTY(hi))
24817 return HI2UF(hi);
24818 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024819}
24820
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024821#if defined(EXITFREE) || defined(PROTO)
24822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024823free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024824{
24825 hashitem_T *hi;
24826
24827 /* Need to start all over every time, because func_free() may change the
24828 * hash table. */
24829 while (func_hashtab.ht_used > 0)
24830 for (hi = func_hashtab.ht_array; ; ++hi)
24831 if (!HASHITEM_EMPTY(hi))
24832 {
24833 func_free(HI2UF(hi));
24834 break;
24835 }
24836}
24837#endif
24838
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024840translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024841{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024842 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024843 return find_internal_func(name) >= 0;
24844 return find_func(name) != NULL;
24845}
24846
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024847/*
24848 * Return TRUE if a function "name" exists.
24849 */
24850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024851function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024852{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024853 char_u *nm = name;
24854 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024855 int n = FALSE;
24856
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024857 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024858 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024859 nm = skipwhite(nm);
24860
24861 /* Only accept "funcname", "funcname ", "funcname (..." and
24862 * "funcname(...", not "funcname!...". */
24863 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024864 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024865 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024866 return n;
24867}
24868
Bram Moolenaara1544c02013-05-30 12:35:52 +020024869 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024870get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024871{
24872 char_u *nm = name;
24873 char_u *p;
24874
Bram Moolenaar65639032016-03-16 21:40:30 +010024875 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024876
24877 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024878 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024879 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024880
Bram Moolenaara1544c02013-05-30 12:35:52 +020024881 vim_free(p);
24882 return NULL;
24883}
24884
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024885/*
24886 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024887 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24888 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024889 */
24890 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024891builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024892{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024893 char_u *p;
24894
24895 if (!ASCII_ISLOWER(name[0]))
24896 return FALSE;
24897 p = vim_strchr(name, AUTOLOAD_CHAR);
24898 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024899}
24900
Bram Moolenaar05159a02005-02-26 23:04:13 +000024901#if defined(FEAT_PROFILE) || defined(PROTO)
24902/*
24903 * Start profiling function "fp".
24904 */
24905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024906func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024907{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024908 int len = fp->uf_lines.ga_len;
24909
24910 if (len == 0)
24911 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024912 fp->uf_tm_count = 0;
24913 profile_zero(&fp->uf_tm_self);
24914 profile_zero(&fp->uf_tm_total);
24915 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024916 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024917 if (fp->uf_tml_total == NULL)
24918 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024919 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024920 if (fp->uf_tml_self == NULL)
24921 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024922 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024923 fp->uf_tml_idx = -1;
24924 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24925 || fp->uf_tml_self == NULL)
24926 return; /* out of memory */
24927
24928 fp->uf_profiling = TRUE;
24929}
24930
24931/*
24932 * Dump the profiling results for all functions in file "fd".
24933 */
24934 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024935func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024936{
24937 hashitem_T *hi;
24938 int todo;
24939 ufunc_T *fp;
24940 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024941 ufunc_T **sorttab;
24942 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024944 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024945 if (todo == 0)
24946 return; /* nothing to dump */
24947
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024948 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024949
Bram Moolenaar05159a02005-02-26 23:04:13 +000024950 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24951 {
24952 if (!HASHITEM_EMPTY(hi))
24953 {
24954 --todo;
24955 fp = HI2UF(hi);
24956 if (fp->uf_profiling)
24957 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024958 if (sorttab != NULL)
24959 sorttab[st_len++] = fp;
24960
Bram Moolenaar05159a02005-02-26 23:04:13 +000024961 if (fp->uf_name[0] == K_SPECIAL)
24962 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24963 else
24964 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24965 if (fp->uf_tm_count == 1)
24966 fprintf(fd, "Called 1 time\n");
24967 else
24968 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24969 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24970 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24971 fprintf(fd, "\n");
24972 fprintf(fd, "count total (s) self (s)\n");
24973
24974 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24975 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024976 if (FUNCLINE(fp, i) == NULL)
24977 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024978 prof_func_line(fd, fp->uf_tml_count[i],
24979 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024980 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24981 }
24982 fprintf(fd, "\n");
24983 }
24984 }
24985 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024986
24987 if (sorttab != NULL && st_len > 0)
24988 {
24989 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24990 prof_total_cmp);
24991 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24992 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24993 prof_self_cmp);
24994 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24995 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024996
24997 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024998}
Bram Moolenaar73830342005-02-28 22:48:19 +000024999
25000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025001prof_sort_list(
25002 FILE *fd,
25003 ufunc_T **sorttab,
25004 int st_len,
25005 char *title,
25006 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025007{
25008 int i;
25009 ufunc_T *fp;
25010
25011 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25012 fprintf(fd, "count total (s) self (s) function\n");
25013 for (i = 0; i < 20 && i < st_len; ++i)
25014 {
25015 fp = sorttab[i];
25016 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25017 prefer_self);
25018 if (fp->uf_name[0] == K_SPECIAL)
25019 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25020 else
25021 fprintf(fd, " %s()\n", fp->uf_name);
25022 }
25023 fprintf(fd, "\n");
25024}
25025
25026/*
25027 * Print the count and times for one function or function line.
25028 */
25029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025030prof_func_line(
25031 FILE *fd,
25032 int count,
25033 proftime_T *total,
25034 proftime_T *self,
25035 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025036{
25037 if (count > 0)
25038 {
25039 fprintf(fd, "%5d ", count);
25040 if (prefer_self && profile_equal(total, self))
25041 fprintf(fd, " ");
25042 else
25043 fprintf(fd, "%s ", profile_msg(total));
25044 if (!prefer_self && profile_equal(total, self))
25045 fprintf(fd, " ");
25046 else
25047 fprintf(fd, "%s ", profile_msg(self));
25048 }
25049 else
25050 fprintf(fd, " ");
25051}
25052
25053/*
25054 * Compare function for total time sorting.
25055 */
25056 static int
25057#ifdef __BORLANDC__
25058_RTLENTRYF
25059#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025060prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025061{
25062 ufunc_T *p1, *p2;
25063
25064 p1 = *(ufunc_T **)s1;
25065 p2 = *(ufunc_T **)s2;
25066 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25067}
25068
25069/*
25070 * Compare function for self time sorting.
25071 */
25072 static int
25073#ifdef __BORLANDC__
25074_RTLENTRYF
25075#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025076prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025077{
25078 ufunc_T *p1, *p2;
25079
25080 p1 = *(ufunc_T **)s1;
25081 p2 = *(ufunc_T **)s2;
25082 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25083}
25084
Bram Moolenaar05159a02005-02-26 23:04:13 +000025085#endif
25086
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025087/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025088 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025089 * Return TRUE if a package was loaded.
25090 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025091 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025092script_autoload(
25093 char_u *name,
25094 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025095{
25096 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025097 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025098 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025099 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025100
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025101 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025102 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025103 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025104 return FALSE;
25105
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025106 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025107
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025108 /* Find the name in the list of previously loaded package names. Skip
25109 * "autoload/", it's always the same. */
25110 for (i = 0; i < ga_loaded.ga_len; ++i)
25111 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25112 break;
25113 if (!reload && i < ga_loaded.ga_len)
25114 ret = FALSE; /* was loaded already */
25115 else
25116 {
25117 /* Remember the name if it wasn't loaded already. */
25118 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25119 {
25120 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25121 tofree = NULL;
25122 }
25123
25124 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025125 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025126 ret = TRUE;
25127 }
25128
25129 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025130 return ret;
25131}
25132
25133/*
25134 * Return the autoload script name for a function or variable name.
25135 * Returns NULL when out of memory.
25136 */
25137 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025138autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025139{
25140 char_u *p;
25141 char_u *scriptname;
25142
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025143 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025144 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25145 if (scriptname == NULL)
25146 return FALSE;
25147 STRCPY(scriptname, "autoload/");
25148 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025149 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025150 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025151 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025152 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025153 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025154}
25155
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25157
25158/*
25159 * Function given to ExpandGeneric() to obtain the list of user defined
25160 * function names.
25161 */
25162 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025163get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025165 static long_u done;
25166 static hashitem_T *hi;
25167 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025168
25169 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025170 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025171 done = 0;
25172 hi = func_hashtab.ht_array;
25173 }
25174 if (done < func_hashtab.ht_used)
25175 {
25176 if (done++ > 0)
25177 ++hi;
25178 while (HASHITEM_EMPTY(hi))
25179 ++hi;
25180 fp = HI2UF(hi);
25181
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025182 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025183 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025184
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025185 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25186 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025187
25188 cat_func_name(IObuff, fp);
25189 if (xp->xp_context != EXPAND_USER_FUNC)
25190 {
25191 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025192 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193 STRCAT(IObuff, ")");
25194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025195 return IObuff;
25196 }
25197 return NULL;
25198}
25199
25200#endif /* FEAT_CMDL_COMPL */
25201
25202/*
25203 * Copy the function name of "fp" to buffer "buf".
25204 * "buf" must be able to hold the function name plus three bytes.
25205 * Takes care of script-local function names.
25206 */
25207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025208cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025210 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025211 {
25212 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025213 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 }
25215 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025216 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217}
25218
25219/*
25220 * ":delfunction {name}"
25221 */
25222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025223ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025224{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025225 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025226 char_u *p;
25227 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025228 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025229
25230 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025231 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025232 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025234 {
25235 if (fudi.fd_dict != NULL && !eap->skip)
25236 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025237 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239 if (!ends_excmd(*skipwhite(p)))
25240 {
25241 vim_free(name);
25242 EMSG(_(e_trailing));
25243 return;
25244 }
25245 eap->nextcmd = check_nextcmd(p);
25246 if (eap->nextcmd != NULL)
25247 *p = NUL;
25248
25249 if (!eap->skip)
25250 fp = find_func(name);
25251 vim_free(name);
25252
25253 if (!eap->skip)
25254 {
25255 if (fp == NULL)
25256 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025257 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258 return;
25259 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025260 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261 {
25262 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25263 return;
25264 }
25265
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025266 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025268 /* Delete the dict item that refers to the function, it will
25269 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025270 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025272 else
25273 func_free(fp);
25274 }
25275}
25276
25277/*
25278 * Free a function and remove it from the list of functions.
25279 */
25280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025281func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025282{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025283 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025284
25285 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025286 ga_clear_strings(&(fp->uf_args));
25287 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025288#ifdef FEAT_PROFILE
25289 vim_free(fp->uf_tml_count);
25290 vim_free(fp->uf_tml_total);
25291 vim_free(fp->uf_tml_self);
25292#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025293
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025294 /* remove the function from the function hashtable */
25295 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25296 if (HASHITEM_EMPTY(hi))
25297 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025298 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025299 hash_remove(&func_hashtab, hi);
25300
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025301 vim_free(fp);
25302}
25303
25304/*
25305 * Unreference a Function: decrement the reference count and free it when it
25306 * becomes zero. Only for numbered functions.
25307 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025309func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025310{
25311 ufunc_T *fp;
25312
25313 if (name != NULL && isdigit(*name))
25314 {
25315 fp = find_func(name);
25316 if (fp == NULL)
25317 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025318 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025319 {
25320 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025321 * when "uf_calls" becomes zero. */
25322 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025323 func_free(fp);
25324 }
25325 }
25326}
25327
25328/*
25329 * Count a reference to a Function.
25330 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025332func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025333{
25334 ufunc_T *fp;
25335
25336 if (name != NULL && isdigit(*name))
25337 {
25338 fp = find_func(name);
25339 if (fp == NULL)
25340 EMSG2(_(e_intern2), "func_ref()");
25341 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025342 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 }
25344}
25345
25346/*
25347 * Call a user function.
25348 */
25349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025350call_user_func(
25351 ufunc_T *fp, /* pointer to function */
25352 int argcount, /* nr of args */
25353 typval_T *argvars, /* arguments */
25354 typval_T *rettv, /* return value */
25355 linenr_T firstline, /* first line of range */
25356 linenr_T lastline, /* last line of range */
25357 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025358{
Bram Moolenaar33570922005-01-25 22:26:29 +000025359 char_u *save_sourcing_name;
25360 linenr_T save_sourcing_lnum;
25361 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025362 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025363 int save_did_emsg;
25364 static int depth = 0;
25365 dictitem_T *v;
25366 int fixvar_idx = 0; /* index in fixvar[] */
25367 int i;
25368 int ai;
25369 char_u numbuf[NUMBUFLEN];
25370 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025371 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025372#ifdef FEAT_PROFILE
25373 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025374 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025376
25377 /* If depth of calling is getting too high, don't execute the function */
25378 if (depth >= p_mfd)
25379 {
25380 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025381 rettv->v_type = VAR_NUMBER;
25382 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383 return;
25384 }
25385 ++depth;
25386
25387 line_breakcheck(); /* check for CTRL-C hit */
25388
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025389 fc = (funccall_T *)alloc(sizeof(funccall_T));
25390 fc->caller = current_funccal;
25391 current_funccal = fc;
25392 fc->func = fp;
25393 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025394 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025395 fc->linenr = 0;
25396 fc->returned = FALSE;
25397 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025399 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25400 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025401
Bram Moolenaar33570922005-01-25 22:26:29 +000025402 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025403 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025404 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25405 * each argument variable and saves a lot of time.
25406 */
25407 /*
25408 * Init l: variables.
25409 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025410 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025411 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025412 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025413 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25414 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025415 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025416 name = v->di_key;
25417 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025418 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025419 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025420 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025421 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025422 v->di_tv.vval.v_dict = selfdict;
25423 ++selfdict->dv_refcount;
25424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025425
Bram Moolenaar33570922005-01-25 22:26:29 +000025426 /*
25427 * Init a: variables.
25428 * Set a:0 to "argcount".
25429 * Set a:000 to a list with room for the "..." arguments.
25430 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025431 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025432 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025433 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025434 /* Use "name" to avoid a warning from some compiler that checks the
25435 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025436 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025437 name = v->di_key;
25438 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025439 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025440 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025441 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025442 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025443 v->di_tv.vval.v_list = &fc->l_varlist;
25444 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25445 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25446 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025447
25448 /*
25449 * Set a:firstline to "firstline" and a:lastline to "lastline".
25450 * Set a:name to named arguments.
25451 * Set a:N to the "..." arguments.
25452 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025453 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025454 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025455 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025456 (varnumber_T)lastline);
25457 for (i = 0; i < argcount; ++i)
25458 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025459 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025460 if (ai < 0)
25461 /* named argument a:name */
25462 name = FUNCARG(fp, i);
25463 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025464 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025465 /* "..." argument a:1, a:2, etc. */
25466 sprintf((char *)numbuf, "%d", ai + 1);
25467 name = numbuf;
25468 }
25469 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25470 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025471 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025472 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25473 }
25474 else
25475 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025476 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25477 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025478 if (v == NULL)
25479 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025480 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025481 }
25482 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025483 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025484
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025485 /* Note: the values are copied directly to avoid alloc/free.
25486 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025487 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025488 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025489
25490 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25491 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025492 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25493 fc->l_listitems[ai].li_tv = argvars[i];
25494 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025495 }
25496 }
25497
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498 /* Don't redraw while executing the function. */
25499 ++RedrawingDisabled;
25500 save_sourcing_name = sourcing_name;
25501 save_sourcing_lnum = sourcing_lnum;
25502 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025503 /* need space for function name + ("function " + 3) or "[number]" */
25504 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25505 + STRLEN(fp->uf_name) + 20;
25506 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025507 if (sourcing_name != NULL)
25508 {
25509 if (save_sourcing_name != NULL
25510 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025511 sprintf((char *)sourcing_name, "%s[%d]..",
25512 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 else
25514 STRCPY(sourcing_name, "function ");
25515 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25516
25517 if (p_verbose >= 12)
25518 {
25519 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025520 verbose_enter_scroll();
25521
Bram Moolenaar555b2802005-05-19 21:08:39 +000025522 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523 if (p_verbose >= 14)
25524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025526 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025527 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025528 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529
25530 msg_puts((char_u *)"(");
25531 for (i = 0; i < argcount; ++i)
25532 {
25533 if (i > 0)
25534 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025535 if (argvars[i].v_type == VAR_NUMBER)
25536 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025537 else
25538 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025539 /* Do not want errors such as E724 here. */
25540 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025541 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025542 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025543 if (s != NULL)
25544 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025545 if (vim_strsize(s) > MSG_BUF_CLEN)
25546 {
25547 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25548 s = buf;
25549 }
25550 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025551 vim_free(tofree);
25552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553 }
25554 }
25555 msg_puts((char_u *)")");
25556 }
25557 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025558
25559 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025560 --no_wait_return;
25561 }
25562 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025563#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025564 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025565 {
25566 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25567 func_do_profile(fp);
25568 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025569 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025570 {
25571 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025572 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025573 profile_zero(&fp->uf_tm_children);
25574 }
25575 script_prof_save(&wait_start);
25576 }
25577#endif
25578
Bram Moolenaar071d4272004-06-13 20:20:40 +000025579 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025580 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581 save_did_emsg = did_emsg;
25582 did_emsg = FALSE;
25583
25584 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025585 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25587
25588 --RedrawingDisabled;
25589
25590 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025591 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025593 clear_tv(rettv);
25594 rettv->v_type = VAR_NUMBER;
25595 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596 }
25597
Bram Moolenaar05159a02005-02-26 23:04:13 +000025598#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025599 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025600 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025601 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025602 profile_end(&call_start);
25603 profile_sub_wait(&wait_start, &call_start);
25604 profile_add(&fp->uf_tm_total, &call_start);
25605 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025606 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025607 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025608 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25609 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025610 }
25611 }
25612#endif
25613
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 /* when being verbose, mention the return value */
25615 if (p_verbose >= 12)
25616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025618 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025621 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025622 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025623 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025624 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025625 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025626 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025627 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025628 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025629 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025630 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025631
Bram Moolenaar555b2802005-05-19 21:08:39 +000025632 /* The value may be very long. Skip the middle part, so that we
25633 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025634 * truncate it at the end. Don't want errors such as E724 here. */
25635 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025636 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025637 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025638 if (s != NULL)
25639 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025640 if (vim_strsize(s) > MSG_BUF_CLEN)
25641 {
25642 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25643 s = buf;
25644 }
25645 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025646 vim_free(tofree);
25647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 }
25649 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025650
25651 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 --no_wait_return;
25653 }
25654
25655 vim_free(sourcing_name);
25656 sourcing_name = save_sourcing_name;
25657 sourcing_lnum = save_sourcing_lnum;
25658 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025659#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025660 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025661 script_prof_restore(&wait_start);
25662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663
25664 if (p_verbose >= 12 && sourcing_name != NULL)
25665 {
25666 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025667 verbose_enter_scroll();
25668
Bram Moolenaar555b2802005-05-19 21:08:39 +000025669 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025671
25672 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025673 --no_wait_return;
25674 }
25675
25676 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025677 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025678 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025679
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025680 /* If the a:000 list and the l: and a: dicts are not referenced we can
25681 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025682 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25683 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25684 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25685 {
25686 free_funccal(fc, FALSE);
25687 }
25688 else
25689 {
25690 hashitem_T *hi;
25691 listitem_T *li;
25692 int todo;
25693
25694 /* "fc" is still in use. This can happen when returning "a:000" or
25695 * assigning "l:" to a global variable.
25696 * Link "fc" in the list for garbage collection later. */
25697 fc->caller = previous_funccal;
25698 previous_funccal = fc;
25699
25700 /* Make a copy of the a: variables, since we didn't do that above. */
25701 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25702 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25703 {
25704 if (!HASHITEM_EMPTY(hi))
25705 {
25706 --todo;
25707 v = HI2DI(hi);
25708 copy_tv(&v->di_tv, &v->di_tv);
25709 }
25710 }
25711
25712 /* Make a copy of the a:000 items, since we didn't do that above. */
25713 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25714 copy_tv(&li->li_tv, &li->li_tv);
25715 }
25716}
25717
25718/*
25719 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025720 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025721 */
25722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025723can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025724{
25725 return (fc->l_varlist.lv_copyID != copyID
25726 && fc->l_vars.dv_copyID != copyID
25727 && fc->l_avars.dv_copyID != copyID);
25728}
25729
25730/*
25731 * Free "fc" and what it contains.
25732 */
25733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025734free_funccal(
25735 funccall_T *fc,
25736 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025737{
25738 listitem_T *li;
25739
25740 /* The a: variables typevals may not have been allocated, only free the
25741 * allocated variables. */
25742 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25743
25744 /* free all l: variables */
25745 vars_clear(&fc->l_vars.dv_hashtab);
25746
25747 /* Free the a:000 variables if they were allocated. */
25748 if (free_val)
25749 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25750 clear_tv(&li->li_tv);
25751
25752 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753}
25754
25755/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025756 * Add a number variable "name" to dict "dp" with value "nr".
25757 */
25758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025759add_nr_var(
25760 dict_T *dp,
25761 dictitem_T *v,
25762 char *name,
25763 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025764{
25765 STRCPY(v->di_key, name);
25766 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25767 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25768 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025769 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025770 v->di_tv.vval.v_number = nr;
25771}
25772
25773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 * ":return [expr]"
25775 */
25776 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025777ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778{
25779 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025780 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025781 int returning = FALSE;
25782
25783 if (current_funccal == NULL)
25784 {
25785 EMSG(_("E133: :return not inside a function"));
25786 return;
25787 }
25788
25789 if (eap->skip)
25790 ++emsg_skip;
25791
25792 eap->nextcmd = NULL;
25793 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025794 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795 {
25796 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025797 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025799 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025800 }
25801 /* It's safer to return also on error. */
25802 else if (!eap->skip)
25803 {
25804 /*
25805 * Return unless the expression evaluation has been cancelled due to an
25806 * aborting error, an interrupt, or an exception.
25807 */
25808 if (!aborting())
25809 returning = do_return(eap, FALSE, TRUE, NULL);
25810 }
25811
25812 /* When skipping or the return gets pending, advance to the next command
25813 * in this line (!returning). Otherwise, ignore the rest of the line.
25814 * Following lines will be ignored by get_func_line(). */
25815 if (returning)
25816 eap->nextcmd = NULL;
25817 else if (eap->nextcmd == NULL) /* no argument */
25818 eap->nextcmd = check_nextcmd(arg);
25819
25820 if (eap->skip)
25821 --emsg_skip;
25822}
25823
25824/*
25825 * Return from a function. Possibly makes the return pending. Also called
25826 * for a pending return at the ":endtry" or after returning from an extra
25827 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025828 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025829 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 * FALSE when the return gets pending.
25831 */
25832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025833do_return(
25834 exarg_T *eap,
25835 int reanimate,
25836 int is_cmd,
25837 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838{
25839 int idx;
25840 struct condstack *cstack = eap->cstack;
25841
25842 if (reanimate)
25843 /* Undo the return. */
25844 current_funccal->returned = FALSE;
25845
25846 /*
25847 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25848 * not in its finally clause (which then is to be executed next) is found.
25849 * In this case, make the ":return" pending for execution at the ":endtry".
25850 * Otherwise, return normally.
25851 */
25852 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25853 if (idx >= 0)
25854 {
25855 cstack->cs_pending[idx] = CSTP_RETURN;
25856
25857 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025858 /* A pending return again gets pending. "rettv" points to an
25859 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025861 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862 else
25863 {
25864 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025865 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025867 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025869 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025870 {
25871 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025872 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025873 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874 else
25875 EMSG(_(e_outofmem));
25876 }
25877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025878 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025879
25880 if (reanimate)
25881 {
25882 /* The pending return value could be overwritten by a ":return"
25883 * without argument in a finally clause; reset the default
25884 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025885 current_funccal->rettv->v_type = VAR_NUMBER;
25886 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025887 }
25888 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025889 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025890 }
25891 else
25892 {
25893 current_funccal->returned = TRUE;
25894
25895 /* If the return is carried out now, store the return value. For
25896 * a return immediately after reanimation, the value is already
25897 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025898 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025900 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025901 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025903 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904 }
25905 }
25906
25907 return idx < 0;
25908}
25909
25910/*
25911 * Free the variable with a pending return value.
25912 */
25913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025914discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025915{
Bram Moolenaar33570922005-01-25 22:26:29 +000025916 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025917}
25918
25919/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025920 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025921 * is an allocated string. Used by report_pending() for verbose messages.
25922 */
25923 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025924get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025925{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025926 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025927 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025928 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025930 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025931 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025932 if (s == NULL)
25933 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025934
25935 STRCPY(IObuff, ":return ");
25936 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25937 if (STRLEN(s) + 8 >= IOSIZE)
25938 STRCPY(IObuff + IOSIZE - 4, "...");
25939 vim_free(tofree);
25940 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941}
25942
25943/*
25944 * Get next function line.
25945 * Called by do_cmdline() to get the next line.
25946 * Returns allocated string, or NULL for end of function.
25947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025949get_func_line(
25950 int c UNUSED,
25951 void *cookie,
25952 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953{
Bram Moolenaar33570922005-01-25 22:26:29 +000025954 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025955 ufunc_T *fp = fcp->func;
25956 char_u *retval;
25957 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025958
25959 /* If breakpoints have been added/deleted need to check for it. */
25960 if (fcp->dbg_tick != debug_tick)
25961 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025962 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025963 sourcing_lnum);
25964 fcp->dbg_tick = debug_tick;
25965 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025966#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025967 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025968 func_line_end(cookie);
25969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970
Bram Moolenaar05159a02005-02-26 23:04:13 +000025971 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025972 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25973 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025974 retval = NULL;
25975 else
25976 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025977 /* Skip NULL lines (continuation lines). */
25978 while (fcp->linenr < gap->ga_len
25979 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25980 ++fcp->linenr;
25981 if (fcp->linenr >= gap->ga_len)
25982 retval = NULL;
25983 else
25984 {
25985 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25986 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025987#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025988 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025989 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025990#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025992 }
25993
25994 /* Did we encounter a breakpoint? */
25995 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25996 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025997 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025999 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026000 sourcing_lnum);
26001 fcp->dbg_tick = debug_tick;
26002 }
26003
26004 return retval;
26005}
26006
Bram Moolenaar05159a02005-02-26 23:04:13 +000026007#if defined(FEAT_PROFILE) || defined(PROTO)
26008/*
26009 * Called when starting to read a function line.
26010 * "sourcing_lnum" must be correct!
26011 * When skipping lines it may not actually be executed, but we won't find out
26012 * until later and we need to store the time now.
26013 */
26014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026015func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026016{
26017 funccall_T *fcp = (funccall_T *)cookie;
26018 ufunc_T *fp = fcp->func;
26019
26020 if (fp->uf_profiling && sourcing_lnum >= 1
26021 && sourcing_lnum <= fp->uf_lines.ga_len)
26022 {
26023 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026024 /* Skip continuation lines. */
26025 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26026 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026027 fp->uf_tml_execed = FALSE;
26028 profile_start(&fp->uf_tml_start);
26029 profile_zero(&fp->uf_tml_children);
26030 profile_get_wait(&fp->uf_tml_wait);
26031 }
26032}
26033
26034/*
26035 * Called when actually executing a function line.
26036 */
26037 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026038func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026039{
26040 funccall_T *fcp = (funccall_T *)cookie;
26041 ufunc_T *fp = fcp->func;
26042
26043 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26044 fp->uf_tml_execed = TRUE;
26045}
26046
26047/*
26048 * Called when done with a function line.
26049 */
26050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026051func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026052{
26053 funccall_T *fcp = (funccall_T *)cookie;
26054 ufunc_T *fp = fcp->func;
26055
26056 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26057 {
26058 if (fp->uf_tml_execed)
26059 {
26060 ++fp->uf_tml_count[fp->uf_tml_idx];
26061 profile_end(&fp->uf_tml_start);
26062 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026063 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026064 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26065 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026066 }
26067 fp->uf_tml_idx = -1;
26068 }
26069}
26070#endif
26071
Bram Moolenaar071d4272004-06-13 20:20:40 +000026072/*
26073 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026074 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026075 */
26076 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026077func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026078{
Bram Moolenaar33570922005-01-25 22:26:29 +000026079 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026080
26081 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26082 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026083 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026084 || fcp->returned);
26085}
26086
26087/*
26088 * return TRUE if cookie indicates a function which "abort"s on errors.
26089 */
26090 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026091func_has_abort(
26092 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026093{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026094 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026095}
26096
26097#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26098typedef enum
26099{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026100 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26101 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26102 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103} var_flavour_T;
26104
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026105static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026106
26107 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026108var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026109{
26110 char_u *p = varname;
26111
26112 if (ASCII_ISUPPER(*p))
26113 {
26114 while (*(++p))
26115 if (ASCII_ISLOWER(*p))
26116 return VAR_FLAVOUR_SESSION;
26117 return VAR_FLAVOUR_VIMINFO;
26118 }
26119 else
26120 return VAR_FLAVOUR_DEFAULT;
26121}
26122#endif
26123
26124#if defined(FEAT_VIMINFO) || defined(PROTO)
26125/*
26126 * Restore global vars that start with a capital from the viminfo file
26127 */
26128 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026129read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026130{
26131 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026132 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026133 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026134 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026135
26136 if (!writing && (find_viminfo_parameter('!') != NULL))
26137 {
26138 tab = vim_strchr(virp->vir_line + 1, '\t');
26139 if (tab != NULL)
26140 {
26141 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026142 switch (*tab)
26143 {
26144 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026145#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026146 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026147#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026148 case 'D': type = VAR_DICT; break;
26149 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026150 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026152
26153 tab = vim_strchr(tab, '\t');
26154 if (tab != NULL)
26155 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026156 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026157 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026158 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026159 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026160#ifdef FEAT_FLOAT
26161 else if (type == VAR_FLOAT)
26162 (void)string2float(tab + 1, &tv.vval.v_float);
26163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026164 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026165 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026166 if (type == VAR_DICT || type == VAR_LIST)
26167 {
26168 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26169
26170 if (etv == NULL)
26171 /* Failed to parse back the dict or list, use it as a
26172 * string. */
26173 tv.v_type = VAR_STRING;
26174 else
26175 {
26176 vim_free(tv.vval.v_string);
26177 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026178 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026179 }
26180 }
26181
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026182 /* when in a function use global variables */
26183 save_funccal = current_funccal;
26184 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026185 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026186 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026187
26188 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026189 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026190 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26191 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192 }
26193 }
26194 }
26195
26196 return viminfo_readline(virp);
26197}
26198
26199/*
26200 * Write global vars that start with a capital to the viminfo file
26201 */
26202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026203write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026204{
Bram Moolenaar33570922005-01-25 22:26:29 +000026205 hashitem_T *hi;
26206 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026207 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026208 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026209 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026210 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026211 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026212
26213 if (find_viminfo_parameter('!') == NULL)
26214 return;
26215
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026216 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026218 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026219 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026220 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026221 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026222 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026223 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026224 this_var = HI2DI(hi);
26225 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026226 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026227 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026228 {
26229 case VAR_STRING: s = "STR"; break;
26230 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026231 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026232 case VAR_DICT: s = "DIC"; break;
26233 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026234 case VAR_SPECIAL: s = "XPL"; break;
26235
26236 case VAR_UNKNOWN:
26237 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026238 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026239 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026240 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026241 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026242 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026243 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026244 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026245 if (p != NULL)
26246 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026247 vim_free(tofree);
26248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026249 }
26250 }
26251}
26252#endif
26253
26254#if defined(FEAT_SESSION) || defined(PROTO)
26255 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026256store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026257{
Bram Moolenaar33570922005-01-25 22:26:29 +000026258 hashitem_T *hi;
26259 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026260 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026261 char_u *p, *t;
26262
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026263 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026264 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026265 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026266 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026267 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026268 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026269 this_var = HI2DI(hi);
26270 if ((this_var->di_tv.v_type == VAR_NUMBER
26271 || this_var->di_tv.v_type == VAR_STRING)
26272 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026273 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026274 /* Escape special characters with a backslash. Turn a LF and
26275 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026276 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026277 (char_u *)"\\\"\n\r");
26278 if (p == NULL) /* out of memory */
26279 break;
26280 for (t = p; *t != NUL; ++t)
26281 if (*t == '\n')
26282 *t = 'n';
26283 else if (*t == '\r')
26284 *t = 'r';
26285 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026286 this_var->di_key,
26287 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26288 : ' ',
26289 p,
26290 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26291 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026292 || put_eol(fd) == FAIL)
26293 {
26294 vim_free(p);
26295 return FAIL;
26296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026297 vim_free(p);
26298 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026299#ifdef FEAT_FLOAT
26300 else if (this_var->di_tv.v_type == VAR_FLOAT
26301 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26302 {
26303 float_T f = this_var->di_tv.vval.v_float;
26304 int sign = ' ';
26305
26306 if (f < 0)
26307 {
26308 f = -f;
26309 sign = '-';
26310 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026311 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026312 this_var->di_key, sign, f) < 0)
26313 || put_eol(fd) == FAIL)
26314 return FAIL;
26315 }
26316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317 }
26318 }
26319 return OK;
26320}
26321#endif
26322
Bram Moolenaar661b1822005-07-28 22:36:45 +000026323/*
26324 * Display script name where an item was last set.
26325 * Should only be invoked when 'verbose' is non-zero.
26326 */
26327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026328last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026329{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026330 char_u *p;
26331
Bram Moolenaar661b1822005-07-28 22:36:45 +000026332 if (scriptID != 0)
26333 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026334 p = home_replace_save(NULL, get_scriptname(scriptID));
26335 if (p != NULL)
26336 {
26337 verbose_enter();
26338 MSG_PUTS(_("\n\tLast set from "));
26339 MSG_PUTS(p);
26340 vim_free(p);
26341 verbose_leave();
26342 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026343 }
26344}
26345
Bram Moolenaard812df62008-11-09 12:46:09 +000026346/*
26347 * List v:oldfiles in a nice way.
26348 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026350ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026351{
26352 list_T *l = vimvars[VV_OLDFILES].vv_list;
26353 listitem_T *li;
26354 int nr = 0;
26355
26356 if (l == NULL)
26357 msg((char_u *)_("No old files"));
26358 else
26359 {
26360 msg_start();
26361 msg_scroll = TRUE;
26362 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26363 {
26364 msg_outnum((long)++nr);
26365 MSG_PUTS(": ");
26366 msg_outtrans(get_tv_string(&li->li_tv));
26367 msg_putchar('\n');
26368 out_flush(); /* output one line at a time */
26369 ui_breakcheck();
26370 }
26371 /* Assume "got_int" was set to truncate the listing. */
26372 got_int = FALSE;
26373
26374#ifdef FEAT_BROWSE_CMD
26375 if (cmdmod.browse)
26376 {
26377 quit_more = FALSE;
26378 nr = prompt_for_number(FALSE);
26379 msg_starthere();
26380 if (nr > 0)
26381 {
26382 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26383 (long)nr);
26384
26385 if (p != NULL)
26386 {
26387 p = expand_env_save(p);
26388 eap->arg = p;
26389 eap->cmdidx = CMD_edit;
26390 cmdmod.browse = FALSE;
26391 do_exedit(eap, NULL);
26392 vim_free(p);
26393 }
26394 }
26395 }
26396#endif
26397 }
26398}
26399
Bram Moolenaar53744302015-07-17 17:38:22 +020026400/* reset v:option_new, v:option_old and v:option_type */
26401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026402reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026403{
26404 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26405 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26406 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26407}
26408
26409
Bram Moolenaar071d4272004-06-13 20:20:40 +000026410#endif /* FEAT_EVAL */
26411
Bram Moolenaar071d4272004-06-13 20:20:40 +000026412
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026413#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026414
26415#ifdef WIN3264
26416/*
26417 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26418 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026419static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26420static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26421static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026422
26423/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026424 * Get the short path (8.3) for the filename in "fnamep".
26425 * Only works for a valid file name.
26426 * When the path gets longer "fnamep" is changed and the allocated buffer
26427 * is put in "bufp".
26428 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26429 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026430 */
26431 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026432get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026433{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026434 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026435 char_u *newbuf;
26436
26437 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026438 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026439 if (l > len - 1)
26440 {
26441 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026442 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026443 newbuf = vim_strnsave(*fnamep, l);
26444 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446
26447 vim_free(*bufp);
26448 *fnamep = *bufp = newbuf;
26449
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026450 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026451 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452 }
26453
26454 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026455 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026456}
26457
26458/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026459 * Get the short path (8.3) for the filename in "fname". The converted
26460 * path is returned in "bufp".
26461 *
26462 * Some of the directories specified in "fname" may not exist. This function
26463 * will shorten the existing directories at the beginning of the path and then
26464 * append the remaining non-existing path.
26465 *
26466 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026467 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026468 * bufp - Pointer to an allocated buffer for the filename.
26469 * fnamelen - Length of the filename pointed to by fname
26470 *
26471 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026472 */
26473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026474shortpath_for_invalid_fname(
26475 char_u **fname,
26476 char_u **bufp,
26477 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026478{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026479 char_u *short_fname, *save_fname, *pbuf_unused;
26480 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026481 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026482 int old_len, len;
26483 int new_len, sfx_len;
26484 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026485
26486 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026487 old_len = *fnamelen;
26488 save_fname = vim_strnsave(*fname, old_len);
26489 pbuf_unused = NULL;
26490 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026491
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026492 endp = save_fname + old_len - 1; /* Find the end of the copy */
26493 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026494
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026495 /*
26496 * Try shortening the supplied path till it succeeds by removing one
26497 * directory at a time from the tail of the path.
26498 */
26499 len = 0;
26500 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026501 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026502 /* go back one path-separator */
26503 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26504 --endp;
26505 if (endp <= save_fname)
26506 break; /* processed the complete path */
26507
26508 /*
26509 * Replace the path separator with a NUL and try to shorten the
26510 * resulting path.
26511 */
26512 ch = *endp;
26513 *endp = 0;
26514 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026515 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026516 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26517 {
26518 retval = FAIL;
26519 goto theend;
26520 }
26521 *endp = ch; /* preserve the string */
26522
26523 if (len > 0)
26524 break; /* successfully shortened the path */
26525
26526 /* failed to shorten the path. Skip the path separator */
26527 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026528 }
26529
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026530 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026531 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026532 /*
26533 * Succeeded in shortening the path. Now concatenate the shortened
26534 * path with the remaining path at the tail.
26535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026536
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026537 /* Compute the length of the new path. */
26538 sfx_len = (int)(save_endp - endp) + 1;
26539 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026540
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026541 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026542 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026543 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026544 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026545 /* There is not enough space in the currently allocated string,
26546 * copy it to a buffer big enough. */
26547 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026548 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026549 {
26550 retval = FAIL;
26551 goto theend;
26552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026553 }
26554 else
26555 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026556 /* Transfer short_fname to the main buffer (it's big enough),
26557 * unless get_short_pathname() did its work in-place. */
26558 *fname = *bufp = save_fname;
26559 if (short_fname != save_fname)
26560 vim_strncpy(save_fname, short_fname, len);
26561 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026563
26564 /* concat the not-shortened part of the path */
26565 vim_strncpy(*fname + len, endp, sfx_len);
26566 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026567 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026568
26569theend:
26570 vim_free(pbuf_unused);
26571 vim_free(save_fname);
26572
26573 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026574}
26575
26576/*
26577 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026578 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026579 */
26580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026581shortpath_for_partial(
26582 char_u **fnamep,
26583 char_u **bufp,
26584 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026585{
26586 int sepcount, len, tflen;
26587 char_u *p;
26588 char_u *pbuf, *tfname;
26589 int hasTilde;
26590
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026591 /* Count up the path separators from the RHS.. so we know which part
26592 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026593 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026594 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026595 if (vim_ispathsep(*p))
26596 ++sepcount;
26597
26598 /* Need full path first (use expand_env() to remove a "~/") */
26599 hasTilde = (**fnamep == '~');
26600 if (hasTilde)
26601 pbuf = tfname = expand_env_save(*fnamep);
26602 else
26603 pbuf = tfname = FullName_save(*fnamep, FALSE);
26604
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026605 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026606
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026607 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26608 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026609
26610 if (len == 0)
26611 {
26612 /* Don't have a valid filename, so shorten the rest of the
26613 * path if we can. This CAN give us invalid 8.3 filenames, but
26614 * there's not a lot of point in guessing what it might be.
26615 */
26616 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026617 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26618 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026619 }
26620
26621 /* Count the paths backward to find the beginning of the desired string. */
26622 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026623 {
26624#ifdef FEAT_MBYTE
26625 if (has_mbyte)
26626 p -= mb_head_off(tfname, p);
26627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026628 if (vim_ispathsep(*p))
26629 {
26630 if (sepcount == 0 || (hasTilde && sepcount == 1))
26631 break;
26632 else
26633 sepcount --;
26634 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026636 if (hasTilde)
26637 {
26638 --p;
26639 if (p >= tfname)
26640 *p = '~';
26641 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026642 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026643 }
26644 else
26645 ++p;
26646
26647 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26648 vim_free(*bufp);
26649 *fnamelen = (int)STRLEN(p);
26650 *bufp = pbuf;
26651 *fnamep = p;
26652
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026653 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026654}
26655#endif /* WIN3264 */
26656
26657/*
26658 * Adjust a filename, according to a string of modifiers.
26659 * *fnamep must be NUL terminated when called. When returning, the length is
26660 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026661 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026662 * When there is an error, *fnamep is set to NULL.
26663 */
26664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026665modify_fname(
26666 char_u *src, /* string with modifiers */
26667 int *usedlen, /* characters after src that are used */
26668 char_u **fnamep, /* file name so far */
26669 char_u **bufp, /* buffer for allocated file name or NULL */
26670 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026671{
26672 int valid = 0;
26673 char_u *tail;
26674 char_u *s, *p, *pbuf;
26675 char_u dirname[MAXPATHL];
26676 int c;
26677 int has_fullname = 0;
26678#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026679 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026680 int has_shortname = 0;
26681#endif
26682
26683repeat:
26684 /* ":p" - full path/file_name */
26685 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26686 {
26687 has_fullname = 1;
26688
26689 valid |= VALID_PATH;
26690 *usedlen += 2;
26691
26692 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26693 if ((*fnamep)[0] == '~'
26694#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26695 && ((*fnamep)[1] == '/'
26696# ifdef BACKSLASH_IN_FILENAME
26697 || (*fnamep)[1] == '\\'
26698# endif
26699 || (*fnamep)[1] == NUL)
26700
26701#endif
26702 )
26703 {
26704 *fnamep = expand_env_save(*fnamep);
26705 vim_free(*bufp); /* free any allocated file name */
26706 *bufp = *fnamep;
26707 if (*fnamep == NULL)
26708 return -1;
26709 }
26710
26711 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026712 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026713 {
26714 if (vim_ispathsep(*p)
26715 && p[1] == '.'
26716 && (p[2] == NUL
26717 || vim_ispathsep(p[2])
26718 || (p[2] == '.'
26719 && (p[3] == NUL || vim_ispathsep(p[3])))))
26720 break;
26721 }
26722
26723 /* FullName_save() is slow, don't use it when not needed. */
26724 if (*p != NUL || !vim_isAbsName(*fnamep))
26725 {
26726 *fnamep = FullName_save(*fnamep, *p != NUL);
26727 vim_free(*bufp); /* free any allocated file name */
26728 *bufp = *fnamep;
26729 if (*fnamep == NULL)
26730 return -1;
26731 }
26732
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026733#ifdef WIN3264
26734# if _WIN32_WINNT >= 0x0500
26735 if (vim_strchr(*fnamep, '~') != NULL)
26736 {
26737 /* Expand 8.3 filename to full path. Needed to make sure the same
26738 * file does not have two different names.
26739 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26740 p = alloc(_MAX_PATH + 1);
26741 if (p != NULL)
26742 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026743 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026744 {
26745 vim_free(*bufp);
26746 *bufp = *fnamep = p;
26747 }
26748 else
26749 vim_free(p);
26750 }
26751 }
26752# endif
26753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026754 /* Append a path separator to a directory. */
26755 if (mch_isdir(*fnamep))
26756 {
26757 /* Make room for one or two extra characters. */
26758 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26759 vim_free(*bufp); /* free any allocated file name */
26760 *bufp = *fnamep;
26761 if (*fnamep == NULL)
26762 return -1;
26763 add_pathsep(*fnamep);
26764 }
26765 }
26766
26767 /* ":." - path relative to the current directory */
26768 /* ":~" - path relative to the home directory */
26769 /* ":8" - shortname path - postponed till after */
26770 while (src[*usedlen] == ':'
26771 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26772 {
26773 *usedlen += 2;
26774 if (c == '8')
26775 {
26776#ifdef WIN3264
26777 has_shortname = 1; /* Postpone this. */
26778#endif
26779 continue;
26780 }
26781 pbuf = NULL;
26782 /* Need full path first (use expand_env() to remove a "~/") */
26783 if (!has_fullname)
26784 {
26785 if (c == '.' && **fnamep == '~')
26786 p = pbuf = expand_env_save(*fnamep);
26787 else
26788 p = pbuf = FullName_save(*fnamep, FALSE);
26789 }
26790 else
26791 p = *fnamep;
26792
26793 has_fullname = 0;
26794
26795 if (p != NULL)
26796 {
26797 if (c == '.')
26798 {
26799 mch_dirname(dirname, MAXPATHL);
26800 s = shorten_fname(p, dirname);
26801 if (s != NULL)
26802 {
26803 *fnamep = s;
26804 if (pbuf != NULL)
26805 {
26806 vim_free(*bufp); /* free any allocated file name */
26807 *bufp = pbuf;
26808 pbuf = NULL;
26809 }
26810 }
26811 }
26812 else
26813 {
26814 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26815 /* Only replace it when it starts with '~' */
26816 if (*dirname == '~')
26817 {
26818 s = vim_strsave(dirname);
26819 if (s != NULL)
26820 {
26821 *fnamep = s;
26822 vim_free(*bufp);
26823 *bufp = s;
26824 }
26825 }
26826 }
26827 vim_free(pbuf);
26828 }
26829 }
26830
26831 tail = gettail(*fnamep);
26832 *fnamelen = (int)STRLEN(*fnamep);
26833
26834 /* ":h" - head, remove "/file_name", can be repeated */
26835 /* Don't remove the first "/" or "c:\" */
26836 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26837 {
26838 valid |= VALID_HEAD;
26839 *usedlen += 2;
26840 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026841 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026842 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026843 *fnamelen = (int)(tail - *fnamep);
26844#ifdef VMS
26845 if (*fnamelen > 0)
26846 *fnamelen += 1; /* the path separator is part of the path */
26847#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026848 if (*fnamelen == 0)
26849 {
26850 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26851 p = vim_strsave((char_u *)".");
26852 if (p == NULL)
26853 return -1;
26854 vim_free(*bufp);
26855 *bufp = *fnamep = tail = p;
26856 *fnamelen = 1;
26857 }
26858 else
26859 {
26860 while (tail > s && !after_pathsep(s, tail))
26861 mb_ptr_back(*fnamep, tail);
26862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026863 }
26864
26865 /* ":8" - shortname */
26866 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26867 {
26868 *usedlen += 2;
26869#ifdef WIN3264
26870 has_shortname = 1;
26871#endif
26872 }
26873
26874#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026875 /*
26876 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026877 */
26878 if (has_shortname)
26879 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026880 /* Copy the string if it is shortened by :h and when it wasn't copied
26881 * yet, because we are going to change it in place. Avoids changing
26882 * the buffer name for "%:8". */
26883 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026884 {
26885 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026886 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026887 return -1;
26888 vim_free(*bufp);
26889 *bufp = *fnamep = p;
26890 }
26891
26892 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026893 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026894 if (!has_fullname && !vim_isAbsName(*fnamep))
26895 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026896 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026897 return -1;
26898 }
26899 else
26900 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026901 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026902
Bram Moolenaardc935552011-08-17 15:23:23 +020026903 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026904 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026905 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026906 return -1;
26907
26908 if (l == 0)
26909 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026910 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026911 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026912 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026913 return -1;
26914 }
26915 *fnamelen = l;
26916 }
26917 }
26918#endif /* WIN3264 */
26919
26920 /* ":t" - tail, just the basename */
26921 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26922 {
26923 *usedlen += 2;
26924 *fnamelen -= (int)(tail - *fnamep);
26925 *fnamep = tail;
26926 }
26927
26928 /* ":e" - extension, can be repeated */
26929 /* ":r" - root, without extension, can be repeated */
26930 while (src[*usedlen] == ':'
26931 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26932 {
26933 /* find a '.' in the tail:
26934 * - for second :e: before the current fname
26935 * - otherwise: The last '.'
26936 */
26937 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26938 s = *fnamep - 2;
26939 else
26940 s = *fnamep + *fnamelen - 1;
26941 for ( ; s > tail; --s)
26942 if (s[0] == '.')
26943 break;
26944 if (src[*usedlen + 1] == 'e') /* :e */
26945 {
26946 if (s > tail)
26947 {
26948 *fnamelen += (int)(*fnamep - (s + 1));
26949 *fnamep = s + 1;
26950#ifdef VMS
26951 /* cut version from the extension */
26952 s = *fnamep + *fnamelen - 1;
26953 for ( ; s > *fnamep; --s)
26954 if (s[0] == ';')
26955 break;
26956 if (s > *fnamep)
26957 *fnamelen = s - *fnamep;
26958#endif
26959 }
26960 else if (*fnamep <= tail)
26961 *fnamelen = 0;
26962 }
26963 else /* :r */
26964 {
26965 if (s > tail) /* remove one extension */
26966 *fnamelen = (int)(s - *fnamep);
26967 }
26968 *usedlen += 2;
26969 }
26970
26971 /* ":s?pat?foo?" - substitute */
26972 /* ":gs?pat?foo?" - global substitute */
26973 if (src[*usedlen] == ':'
26974 && (src[*usedlen + 1] == 's'
26975 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26976 {
26977 char_u *str;
26978 char_u *pat;
26979 char_u *sub;
26980 int sep;
26981 char_u *flags;
26982 int didit = FALSE;
26983
26984 flags = (char_u *)"";
26985 s = src + *usedlen + 2;
26986 if (src[*usedlen + 1] == 'g')
26987 {
26988 flags = (char_u *)"g";
26989 ++s;
26990 }
26991
26992 sep = *s++;
26993 if (sep)
26994 {
26995 /* find end of pattern */
26996 p = vim_strchr(s, sep);
26997 if (p != NULL)
26998 {
26999 pat = vim_strnsave(s, (int)(p - s));
27000 if (pat != NULL)
27001 {
27002 s = p + 1;
27003 /* find end of substitution */
27004 p = vim_strchr(s, sep);
27005 if (p != NULL)
27006 {
27007 sub = vim_strnsave(s, (int)(p - s));
27008 str = vim_strnsave(*fnamep, *fnamelen);
27009 if (sub != NULL && str != NULL)
27010 {
27011 *usedlen = (int)(p + 1 - src);
27012 s = do_string_sub(str, pat, sub, flags);
27013 if (s != NULL)
27014 {
27015 *fnamep = s;
27016 *fnamelen = (int)STRLEN(s);
27017 vim_free(*bufp);
27018 *bufp = s;
27019 didit = TRUE;
27020 }
27021 }
27022 vim_free(sub);
27023 vim_free(str);
27024 }
27025 vim_free(pat);
27026 }
27027 }
27028 /* after using ":s", repeat all the modifiers */
27029 if (didit)
27030 goto repeat;
27031 }
27032 }
27033
Bram Moolenaar26df0922014-02-23 23:39:13 +010027034 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27035 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027036 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027037 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027038 if (c != NUL)
27039 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027040 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027041 if (c != NUL)
27042 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027043 if (p == NULL)
27044 return -1;
27045 vim_free(*bufp);
27046 *bufp = *fnamep = p;
27047 *fnamelen = (int)STRLEN(p);
27048 *usedlen += 2;
27049 }
27050
Bram Moolenaar071d4272004-06-13 20:20:40 +000027051 return valid;
27052}
27053
27054/*
27055 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27056 * "flags" can be "g" to do a global substitute.
27057 * Returns an allocated string, NULL for error.
27058 */
27059 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027060do_string_sub(
27061 char_u *str,
27062 char_u *pat,
27063 char_u *sub,
27064 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027065{
27066 int sublen;
27067 regmatch_T regmatch;
27068 int i;
27069 int do_all;
27070 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027071 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027072 garray_T ga;
27073 char_u *ret;
27074 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027075 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027076
27077 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27078 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027079 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027080
27081 ga_init2(&ga, 1, 200);
27082
27083 do_all = (flags[0] == 'g');
27084
27085 regmatch.rm_ic = p_ic;
27086 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27087 if (regmatch.regprog != NULL)
27088 {
27089 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027090 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027091 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27092 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027093 /* Skip empty match except for first match. */
27094 if (regmatch.startp[0] == regmatch.endp[0])
27095 {
27096 if (zero_width == regmatch.startp[0])
27097 {
27098 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027099 i = MB_PTR2LEN(tail);
27100 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27101 (size_t)i);
27102 ga.ga_len += i;
27103 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027104 continue;
27105 }
27106 zero_width = regmatch.startp[0];
27107 }
27108
Bram Moolenaar071d4272004-06-13 20:20:40 +000027109 /*
27110 * Get some space for a temporary buffer to do the substitution
27111 * into. It will contain:
27112 * - The text up to where the match is.
27113 * - The substituted text.
27114 * - The text after the match.
27115 */
27116 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027117 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027118 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27119 {
27120 ga_clear(&ga);
27121 break;
27122 }
27123
27124 /* copy the text up to where the match is */
27125 i = (int)(regmatch.startp[0] - tail);
27126 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27127 /* add the substituted text */
27128 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27129 + ga.ga_len + i, TRUE, TRUE, FALSE);
27130 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027131 tail = regmatch.endp[0];
27132 if (*tail == NUL)
27133 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027134 if (!do_all)
27135 break;
27136 }
27137
27138 if (ga.ga_data != NULL)
27139 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27140
Bram Moolenaar473de612013-06-08 18:19:48 +020027141 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027142 }
27143
27144 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27145 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027146 if (p_cpo == empty_option)
27147 p_cpo = save_cpo;
27148 else
27149 /* Darn, evaluating {sub} expression changed the value. */
27150 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027151
27152 return ret;
27153}
27154
27155#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */