blob: eadb802b3046ec263fd63d7bfd77c5377870f26d [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 Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
103static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
104static char *e_funcdict = N_("E717: Dictionary entry already exists");
105static char *e_funcref = N_("E718: Funcref required");
106static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
107static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000108static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000109static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200110#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200111static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200112#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000113
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100114#define NAMESPACE_CHAR (char_u *)"abglstvw"
115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000128 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 */
130static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131#define COPYID_INC 2
132#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133
Bram Moolenaar8502c702014-06-17 12:51:16 +0200134/* Abort conversion to string after a recursion error. */
135static int did_echo_string_emsg = FALSE;
136
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200365 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200366 {VV_NAME("option_new", VAR_STRING), VV_RO},
367 {VV_NAME("option_old", VAR_STRING), VV_RO},
368 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100369 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100370 {VV_NAME("false", VAR_SPECIAL), VV_RO},
371 {VV_NAME("true", VAR_SPECIAL), VV_RO},
372 {VV_NAME("null", VAR_SPECIAL), VV_RO},
373 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000374};
375
376/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000377#define vv_type vv_di.di_tv.v_type
378#define vv_nr vv_di.di_tv.vval.v_number
379#define vv_float vv_di.di_tv.vval.v_float
380#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000381#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200382#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000384
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200385static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000386#define vimvarht vimvardict.dv_hashtab
387
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100388static void prepare_vimvar(int idx, typval_T *save_tv);
389static void restore_vimvar(int idx, typval_T *save_tv);
390static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
391static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
392static char_u *skip_var_one(char_u *arg);
393static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
394static void list_glob_vars(int *first);
395static void list_buf_vars(int *first);
396static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100398static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100400static void list_vim_vars(int *first);
401static void list_script_vars(int *first);
402static void list_func_vars(int *first);
403static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
404static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
405static int check_changedtick(char_u *arg);
406static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
407static void clear_lval(lval_T *lp);
408static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
409static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
410static void list_fix_watch(list_T *l, listitem_T *item);
411static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
412static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
413static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
414static void item_lock(typval_T *tv, int deep, int lock);
415static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000416
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100417static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
418static int eval1(char_u **arg, typval_T *rettv, int evaluate);
419static int eval2(char_u **arg, typval_T *rettv, int evaluate);
420static int eval3(char_u **arg, typval_T *rettv, int evaluate);
421static int eval4(char_u **arg, typval_T *rettv, int evaluate);
422static int eval5(char_u **arg, typval_T *rettv, int evaluate);
423static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
424static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100426static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
427static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
428static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
429static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
431static long list_len(list_T *l);
432static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
433static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
434static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
435static long list_find_nr(list_T *l, long idx, int *errorp);
436static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100437static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
438static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
439static list_T *list_copy(list_T *orig, int deep, int copyID);
440static char_u *list2string(typval_T *tv, int copyID);
441static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
442static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
443static int free_unref_items(int copyID);
444static dictitem_T *dictitem_copy(dictitem_T *org);
445static void dictitem_remove(dict_T *dict, dictitem_T *item);
446static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
447static long dict_len(dict_T *d);
448static char_u *dict2string(typval_T *tv, int copyID);
449static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
450static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
451static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *string_quote(char_u *str, int function);
453static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
454static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100455static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
456static 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 +0100457static void emsg_funcname(char *ermsg, char_u *name);
458static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void f_abs(typval_T *argvars, typval_T *rettv);
462static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void f_add(typval_T *argvars, typval_T *rettv);
465static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
466static void f_and(typval_T *argvars, typval_T *rettv);
467static void f_append(typval_T *argvars, typval_T *rettv);
468static void f_argc(typval_T *argvars, typval_T *rettv);
469static void f_argidx(typval_T *argvars, typval_T *rettv);
470static void f_arglistid(typval_T *argvars, typval_T *rettv);
471static void f_argv(typval_T *argvars, typval_T *rettv);
472static void f_assert_equal(typval_T *argvars, typval_T *rettv);
473static void f_assert_exception(typval_T *argvars, typval_T *rettv);
474static void f_assert_fails(typval_T *argvars, typval_T *rettv);
475static void f_assert_false(typval_T *argvars, typval_T *rettv);
476static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_asin(typval_T *argvars, typval_T *rettv);
479static void f_atan(typval_T *argvars, typval_T *rettv);
480static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_browse(typval_T *argvars, typval_T *rettv);
483static void f_browsedir(typval_T *argvars, typval_T *rettv);
484static void f_bufexists(typval_T *argvars, typval_T *rettv);
485static void f_buflisted(typval_T *argvars, typval_T *rettv);
486static void f_bufloaded(typval_T *argvars, typval_T *rettv);
487static void f_bufname(typval_T *argvars, typval_T *rettv);
488static void f_bufnr(typval_T *argvars, typval_T *rettv);
489static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
490static void f_byte2line(typval_T *argvars, typval_T *rettv);
491static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
492static void f_byteidx(typval_T *argvars, typval_T *rettv);
493static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
494static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000495#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100496static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100499static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100500static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
501static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100502static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100503static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100504static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100505static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
506static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100507static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100509static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
510static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100511static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100512static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100513#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_changenr(typval_T *argvars, typval_T *rettv);
515static void f_char2nr(typval_T *argvars, typval_T *rettv);
516static void f_cindent(typval_T *argvars, typval_T *rettv);
517static void f_clearmatches(typval_T *argvars, typval_T *rettv);
518static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000519#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100520static void f_complete(typval_T *argvars, typval_T *rettv);
521static void f_complete_add(typval_T *argvars, typval_T *rettv);
522static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_confirm(typval_T *argvars, typval_T *rettv);
525static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_cos(typval_T *argvars, typval_T *rettv);
528static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_count(typval_T *argvars, typval_T *rettv);
531static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
532static void f_cursor(typval_T *argsvars, typval_T *rettv);
533static void f_deepcopy(typval_T *argvars, typval_T *rettv);
534static void f_delete(typval_T *argvars, typval_T *rettv);
535static void f_did_filetype(typval_T *argvars, typval_T *rettv);
536static void f_diff_filler(typval_T *argvars, typval_T *rettv);
537static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100538static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_empty(typval_T *argvars, typval_T *rettv);
540static void f_escape(typval_T *argvars, typval_T *rettv);
541static void f_eval(typval_T *argvars, typval_T *rettv);
542static void f_eventhandler(typval_T *argvars, typval_T *rettv);
543static void f_executable(typval_T *argvars, typval_T *rettv);
544static void f_exepath(typval_T *argvars, typval_T *rettv);
545static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_expand(typval_T *argvars, typval_T *rettv);
550static void f_extend(typval_T *argvars, typval_T *rettv);
551static void f_feedkeys(typval_T *argvars, typval_T *rettv);
552static void f_filereadable(typval_T *argvars, typval_T *rettv);
553static void f_filewritable(typval_T *argvars, typval_T *rettv);
554static void f_filter(typval_T *argvars, typval_T *rettv);
555static void f_finddir(typval_T *argvars, typval_T *rettv);
556static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_float2nr(typval_T *argvars, typval_T *rettv);
559static void f_floor(typval_T *argvars, typval_T *rettv);
560static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_fnameescape(typval_T *argvars, typval_T *rettv);
563static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
564static void f_foldclosed(typval_T *argvars, typval_T *rettv);
565static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
566static void f_foldlevel(typval_T *argvars, typval_T *rettv);
567static void f_foldtext(typval_T *argvars, typval_T *rettv);
568static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
569static void f_foreground(typval_T *argvars, typval_T *rettv);
570static void f_function(typval_T *argvars, typval_T *rettv);
571static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
572static void f_get(typval_T *argvars, typval_T *rettv);
573static void f_getbufline(typval_T *argvars, typval_T *rettv);
574static void f_getbufvar(typval_T *argvars, typval_T *rettv);
575static void f_getchar(typval_T *argvars, typval_T *rettv);
576static void f_getcharmod(typval_T *argvars, typval_T *rettv);
577static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
578static void f_getcmdline(typval_T *argvars, typval_T *rettv);
579static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
580static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
581static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
582static void f_getcwd(typval_T *argvars, typval_T *rettv);
583static void f_getfontname(typval_T *argvars, typval_T *rettv);
584static void f_getfperm(typval_T *argvars, typval_T *rettv);
585static void f_getfsize(typval_T *argvars, typval_T *rettv);
586static void f_getftime(typval_T *argvars, typval_T *rettv);
587static void f_getftype(typval_T *argvars, typval_T *rettv);
588static void f_getline(typval_T *argvars, typval_T *rettv);
589static void f_getmatches(typval_T *argvars, typval_T *rettv);
590static void f_getpid(typval_T *argvars, typval_T *rettv);
591static void f_getcurpos(typval_T *argvars, typval_T *rettv);
592static void f_getpos(typval_T *argvars, typval_T *rettv);
593static void f_getqflist(typval_T *argvars, typval_T *rettv);
594static void f_getreg(typval_T *argvars, typval_T *rettv);
595static void f_getregtype(typval_T *argvars, typval_T *rettv);
596static void f_gettabvar(typval_T *argvars, typval_T *rettv);
597static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
598static void f_getwinposx(typval_T *argvars, typval_T *rettv);
599static void f_getwinposy(typval_T *argvars, typval_T *rettv);
600static void f_getwinvar(typval_T *argvars, typval_T *rettv);
601static void f_glob(typval_T *argvars, typval_T *rettv);
602static void f_globpath(typval_T *argvars, typval_T *rettv);
603static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
604static void f_has(typval_T *argvars, typval_T *rettv);
605static void f_has_key(typval_T *argvars, typval_T *rettv);
606static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
607static void f_hasmapto(typval_T *argvars, typval_T *rettv);
608static void f_histadd(typval_T *argvars, typval_T *rettv);
609static void f_histdel(typval_T *argvars, typval_T *rettv);
610static void f_histget(typval_T *argvars, typval_T *rettv);
611static void f_histnr(typval_T *argvars, typval_T *rettv);
612static void f_hlID(typval_T *argvars, typval_T *rettv);
613static void f_hlexists(typval_T *argvars, typval_T *rettv);
614static void f_hostname(typval_T *argvars, typval_T *rettv);
615static void f_iconv(typval_T *argvars, typval_T *rettv);
616static void f_indent(typval_T *argvars, typval_T *rettv);
617static void f_index(typval_T *argvars, typval_T *rettv);
618static void f_input(typval_T *argvars, typval_T *rettv);
619static void f_inputdialog(typval_T *argvars, typval_T *rettv);
620static void f_inputlist(typval_T *argvars, typval_T *rettv);
621static void f_inputrestore(typval_T *argvars, typval_T *rettv);
622static void f_inputsave(typval_T *argvars, typval_T *rettv);
623static void f_inputsecret(typval_T *argvars, typval_T *rettv);
624static void f_insert(typval_T *argvars, typval_T *rettv);
625static void f_invert(typval_T *argvars, typval_T *rettv);
626static void f_isdirectory(typval_T *argvars, typval_T *rettv);
627static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100628#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
629static void f_isnan(typval_T *argvars, typval_T *rettv);
630#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100631static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100632#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100633static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100634static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100635static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100636static void f_job_start(typval_T *argvars, typval_T *rettv);
637static void f_job_stop(typval_T *argvars, typval_T *rettv);
638static void f_job_status(typval_T *argvars, typval_T *rettv);
639#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100640static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100641static void f_js_decode(typval_T *argvars, typval_T *rettv);
642static void f_js_encode(typval_T *argvars, typval_T *rettv);
643static void f_json_decode(typval_T *argvars, typval_T *rettv);
644static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_keys(typval_T *argvars, typval_T *rettv);
646static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
647static void f_len(typval_T *argvars, typval_T *rettv);
648static void f_libcall(typval_T *argvars, typval_T *rettv);
649static void f_libcallnr(typval_T *argvars, typval_T *rettv);
650static void f_line(typval_T *argvars, typval_T *rettv);
651static void f_line2byte(typval_T *argvars, typval_T *rettv);
652static void f_lispindent(typval_T *argvars, typval_T *rettv);
653static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_log(typval_T *argvars, typval_T *rettv);
656static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200658#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_map(typval_T *argvars, typval_T *rettv);
662static void f_maparg(typval_T *argvars, typval_T *rettv);
663static void f_mapcheck(typval_T *argvars, typval_T *rettv);
664static void f_match(typval_T *argvars, typval_T *rettv);
665static void f_matchadd(typval_T *argvars, typval_T *rettv);
666static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
667static void f_matcharg(typval_T *argvars, typval_T *rettv);
668static void f_matchdelete(typval_T *argvars, typval_T *rettv);
669static void f_matchend(typval_T *argvars, typval_T *rettv);
670static void f_matchlist(typval_T *argvars, typval_T *rettv);
671static void f_matchstr(typval_T *argvars, typval_T *rettv);
672static void f_max(typval_T *argvars, typval_T *rettv);
673static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000674#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000676#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100678#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
682static void f_nr2char(typval_T *argvars, typval_T *rettv);
683static void f_or(typval_T *argvars, typval_T *rettv);
684static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100685#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100687#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
692static void f_printf(typval_T *argvars, typval_T *rettv);
693static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200694#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200696#endif
697#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200699#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_range(typval_T *argvars, typval_T *rettv);
701static void f_readfile(typval_T *argvars, typval_T *rettv);
702static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100703#ifdef FEAT_FLOAT
704static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
705#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100706static void f_reltimestr(typval_T *argvars, typval_T *rettv);
707static void f_remote_expr(typval_T *argvars, typval_T *rettv);
708static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
709static void f_remote_peek(typval_T *argvars, typval_T *rettv);
710static void f_remote_read(typval_T *argvars, typval_T *rettv);
711static void f_remote_send(typval_T *argvars, typval_T *rettv);
712static void f_remove(typval_T *argvars, typval_T *rettv);
713static void f_rename(typval_T *argvars, typval_T *rettv);
714static void f_repeat(typval_T *argvars, typval_T *rettv);
715static void f_resolve(typval_T *argvars, typval_T *rettv);
716static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000717#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000719#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_screenattr(typval_T *argvars, typval_T *rettv);
721static void f_screenchar(typval_T *argvars, typval_T *rettv);
722static void f_screencol(typval_T *argvars, typval_T *rettv);
723static void f_screenrow(typval_T *argvars, typval_T *rettv);
724static void f_search(typval_T *argvars, typval_T *rettv);
725static void f_searchdecl(typval_T *argvars, typval_T *rettv);
726static void f_searchpair(typval_T *argvars, typval_T *rettv);
727static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
728static void f_searchpos(typval_T *argvars, typval_T *rettv);
729static void f_server2client(typval_T *argvars, typval_T *rettv);
730static void f_serverlist(typval_T *argvars, typval_T *rettv);
731static void f_setbufvar(typval_T *argvars, typval_T *rettv);
732static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
733static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100734static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_setline(typval_T *argvars, typval_T *rettv);
736static void f_setloclist(typval_T *argvars, typval_T *rettv);
737static void f_setmatches(typval_T *argvars, typval_T *rettv);
738static void f_setpos(typval_T *argvars, typval_T *rettv);
739static void f_setqflist(typval_T *argvars, typval_T *rettv);
740static void f_setreg(typval_T *argvars, typval_T *rettv);
741static void f_settabvar(typval_T *argvars, typval_T *rettv);
742static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
743static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100744#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100746#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_shellescape(typval_T *argvars, typval_T *rettv);
748static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
749static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_sin(typval_T *argvars, typval_T *rettv);
752static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sort(typval_T *argvars, typval_T *rettv);
755static void f_soundfold(typval_T *argvars, typval_T *rettv);
756static void f_spellbadword(typval_T *argvars, typval_T *rettv);
757static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
758static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sqrt(typval_T *argvars, typval_T *rettv);
761static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_str2nr(typval_T *argvars, typval_T *rettv);
764static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000765#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_stridx(typval_T *argvars, typval_T *rettv);
769static void f_string(typval_T *argvars, typval_T *rettv);
770static void f_strlen(typval_T *argvars, typval_T *rettv);
771static void f_strpart(typval_T *argvars, typval_T *rettv);
772static void f_strridx(typval_T *argvars, typval_T *rettv);
773static void f_strtrans(typval_T *argvars, typval_T *rettv);
774static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
775static void f_strwidth(typval_T *argvars, typval_T *rettv);
776static void f_submatch(typval_T *argvars, typval_T *rettv);
777static void f_substitute(typval_T *argvars, typval_T *rettv);
778static void f_synID(typval_T *argvars, typval_T *rettv);
779static void f_synIDattr(typval_T *argvars, typval_T *rettv);
780static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
781static void f_synstack(typval_T *argvars, typval_T *rettv);
782static void f_synconcealed(typval_T *argvars, typval_T *rettv);
783static void f_system(typval_T *argvars, typval_T *rettv);
784static void f_systemlist(typval_T *argvars, typval_T *rettv);
785static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
787static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
788static void f_taglist(typval_T *argvars, typval_T *rettv);
789static void f_tagfiles(typval_T *argvars, typval_T *rettv);
790static void f_tempname(typval_T *argvars, typval_T *rettv);
791static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200792#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100793static void f_tan(typval_T *argvars, typval_T *rettv);
794static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tolower(typval_T *argvars, typval_T *rettv);
797static void f_toupper(typval_T *argvars, typval_T *rettv);
798static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000799#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000801#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100802static void f_type(typval_T *argvars, typval_T *rettv);
803static void f_undofile(typval_T *argvars, typval_T *rettv);
804static void f_undotree(typval_T *argvars, typval_T *rettv);
805static void f_uniq(typval_T *argvars, typval_T *rettv);
806static void f_values(typval_T *argvars, typval_T *rettv);
807static void f_virtcol(typval_T *argvars, typval_T *rettv);
808static void f_visualmode(typval_T *argvars, typval_T *rettv);
809static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100810static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100811static void f_win_getid(typval_T *argvars, typval_T *rettv);
812static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
813static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
814static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100815static void f_winbufnr(typval_T *argvars, typval_T *rettv);
816static void f_wincol(typval_T *argvars, typval_T *rettv);
817static void f_winheight(typval_T *argvars, typval_T *rettv);
818static void f_winline(typval_T *argvars, typval_T *rettv);
819static void f_winnr(typval_T *argvars, typval_T *rettv);
820static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
821static void f_winrestview(typval_T *argvars, typval_T *rettv);
822static void f_winsaveview(typval_T *argvars, typval_T *rettv);
823static void f_winwidth(typval_T *argvars, typval_T *rettv);
824static void f_writefile(typval_T *argvars, typval_T *rettv);
825static void f_wordcount(typval_T *argvars, typval_T *rettv);
826static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000827
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100828static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
829static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
830static int get_env_len(char_u **arg);
831static int get_id_len(char_u **arg);
832static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
833static 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 +0000834#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
835#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
836 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
838static int eval_isnamec(int c);
839static int eval_isnamec1(int c);
840static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
841static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100842static typval_T *alloc_string_tv(char_u *string);
843static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100844#ifdef FEAT_FLOAT
845static float_T get_tv_float(typval_T *varp);
846#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static linenr_T get_tv_lnum(typval_T *argvars);
848static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100849static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
850static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
851static hashtab_T *find_var_ht(char_u *name, char_u **varname);
852static funccall_T *get_funccal(void);
853static void vars_clear_ext(hashtab_T *ht, int free_val);
854static void delete_var(hashtab_T *ht, hashitem_T *hi);
855static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
856static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
857static void set_var(char_u *name, typval_T *varp, int copy);
858static int var_check_ro(int flags, char_u *name, int use_gettext);
859static int var_check_fixed(int flags, char_u *name, int use_gettext);
860static int var_check_func_name(char_u *name, int new_var);
861static int valid_varname(char_u *varname);
862static int tv_check_lock(int lock, char_u *name, int use_gettext);
863static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
864static char_u *find_option_end(char_u **arg, int *opt_flags);
865static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
866static int eval_fname_script(char_u *p);
867static int eval_fname_sid(char_u *p);
868static void list_func_head(ufunc_T *fp, int indent);
869static ufunc_T *find_func(char_u *name);
870static int function_exists(char_u *name);
871static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000872#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873static void func_do_profile(ufunc_T *fp);
874static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
875static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000876static int
877# ifdef __BORLANDC__
878 _RTLENTRYF
879# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000881static int
882# ifdef __BORLANDC__
883 _RTLENTRYF
884# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000886#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100887static int script_autoload(char_u *name, int reload);
888static char_u *autoload_name(char_u *name);
889static void cat_func_name(char_u *buf, ufunc_T *fp);
890static void func_free(ufunc_T *fp);
891static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
892static int can_free_funccal(funccall_T *fc, int copyID) ;
893static void free_funccal(funccall_T *fc, int free_val);
894static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
895static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
896static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
897static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
898static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
899static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
900static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
901static int write_list(FILE *fd, list_T *list, int binary);
902static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000903
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200904
905#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100906static int compare_func_name(const void *s1, const void *s2);
907static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200908#endif
909
Bram Moolenaar33570922005-01-25 22:26:29 +0000910/*
911 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000912 */
913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100914eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000915{
Bram Moolenaar33570922005-01-25 22:26:29 +0000916 int i;
917 struct vimvar *p;
918
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200919 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
920 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200921 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000922 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000923 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000924
925 for (i = 0; i < VV_LEN; ++i)
926 {
927 p = &vimvars[i];
928 STRCPY(p->vv_di.di_key, p->vv_name);
929 if (p->vv_flags & VV_RO)
930 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
931 else if (p->vv_flags & VV_RO_SBX)
932 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
933 else
934 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000935
936 /* add to v: scope dict, unless the value is not always available */
937 if (p->vv_type != VAR_UNKNOWN)
938 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000939 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000940 /* add to compat scope dict */
941 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000942 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100943 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
944
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000945 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100946 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200947 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100948 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100949
950 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
951 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
952 set_vim_var_nr(VV_NONE, VVAL_NONE);
953 set_vim_var_nr(VV_NULL, VVAL_NULL);
954
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200955 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200956
957#ifdef EBCDIC
958 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100959 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200960 */
961 sortFunctions();
962#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000963}
964
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000965#if defined(EXITFREE) || defined(PROTO)
966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100967eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000968{
969 int i;
970 struct vimvar *p;
971
972 for (i = 0; i < VV_LEN; ++i)
973 {
974 p = &vimvars[i];
975 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000976 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000977 vim_free(p->vv_str);
978 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000979 }
980 else if (p->vv_di.di_tv.v_type == VAR_LIST)
981 {
982 list_unref(p->vv_list);
983 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000984 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000985 }
986 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000987 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000988 hash_clear(&compat_hashtab);
989
Bram Moolenaard9fba312005-06-26 22:34:35 +0000990 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100991# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200992 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100993# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000994
995 /* global variables */
996 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000997
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000998 /* autoloaded script names */
999 ga_clear_strings(&ga_loaded);
1000
Bram Moolenaarcca74132013-09-25 21:00:28 +02001001 /* Script-local variables. First clear all the variables and in a second
1002 * loop free the scriptvar_T, because a variable in one script might hold
1003 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001004 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001005 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001006 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001007 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001008 ga_clear(&ga_scripts);
1009
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001010 /* unreferenced lists and dicts */
1011 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001012
1013 /* functions */
1014 free_all_functions();
1015 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001016}
1017#endif
1018
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 * Return the name of the executed function.
1021 */
1022 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001023func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026}
1027
1028/*
1029 * Return the address holding the next breakpoint line for a funccall cookie.
1030 */
1031 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001032func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035}
1036
1037/*
1038 * Return the address holding the debug tick for a funccall cookie.
1039 */
1040 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001041func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
Bram Moolenaar33570922005-01-25 22:26:29 +00001043 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044}
1045
1046/*
1047 * Return the nesting level for a funccall cookie.
1048 */
1049 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001050func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
Bram Moolenaar33570922005-01-25 22:26:29 +00001052 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053}
1054
1055/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001056funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001058/* pointer to list of previously used funccal, still around because some
1059 * item in it is still being used. */
1060funccall_T *previous_funccal = NULL;
1061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062/*
1063 * Return TRUE when a function was ended by a ":return" command.
1064 */
1065 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001066current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
1068 return current_funccal->returned;
1069}
1070
1071
1072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 * Set an internal variable to a string value. Creates the variable if it does
1074 * not already exist.
1075 */
1076 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001077set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001079 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001080 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
1082 val = vim_strsave(value);
1083 if (val != NULL)
1084 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001085 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001086 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001088 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001089 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091 }
1092}
1093
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001095static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096static char_u *redir_endp = NULL;
1097static char_u *redir_varname = NULL;
1098
1099/*
1100 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001101 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 * Returns OK if successfully completed the setup. FAIL otherwise.
1103 */
1104 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001105var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106{
1107 int save_emsg;
1108 int err;
1109 typval_T tv;
1110
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001111 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001112 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 {
1114 EMSG(_(e_invarg));
1115 return FAIL;
1116 }
1117
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 redir_varname = vim_strsave(name);
1120 if (redir_varname == NULL)
1121 return FAIL;
1122
1123 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1124 if (redir_lval == NULL)
1125 {
1126 var_redir_stop();
1127 return FAIL;
1128 }
1129
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 /* The output is stored in growarray "redir_ga" until redirection ends. */
1131 ga_init2(&redir_ga, (int)sizeof(char), 500);
1132
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001134 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001135 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1137 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001138 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 if (redir_endp != NULL && *redir_endp != NUL)
1140 /* Trailing characters are present after the variable name */
1141 EMSG(_(e_trailing));
1142 else
1143 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001144 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 var_redir_stop();
1146 return FAIL;
1147 }
1148
1149 /* check if we can write to the variable: set it to or append an empty
1150 * string */
1151 save_emsg = did_emsg;
1152 did_emsg = FALSE;
1153 tv.v_type = VAR_STRING;
1154 tv.vval.v_string = (char_u *)"";
1155 if (append)
1156 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1157 else
1158 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001159 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001161 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 if (err)
1163 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001164 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 var_redir_stop();
1166 return FAIL;
1167 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168
1169 return OK;
1170}
1171
1172/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173 * Append "value[value_len]" to the variable set by var_redir_start().
1174 * The actual appending is postponed until redirection ends, because the value
1175 * appended may in fact be the string we write to, changing it may cause freed
1176 * memory to be used:
1177 * :redir => foo
1178 * :let foo
1179 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 */
1181 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001182var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001184 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185
1186 if (redir_lval == NULL)
1187 return;
1188
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001190 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001192 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001194 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001195 {
1196 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001197 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001198 }
1199 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201}
1202
1203/*
1204 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001205 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206 */
1207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001208var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001210 typval_T tv;
1211
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212 if (redir_lval != NULL)
1213 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001214 /* If there was no error: assign the text to the variable. */
1215 if (redir_endp != NULL)
1216 {
1217 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1218 tv.v_type = VAR_STRING;
1219 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001220 /* Call get_lval() again, if it's inside a Dict or List it may
1221 * have changed. */
1222 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001223 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001224 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1225 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1226 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001227 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001228
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001229 /* free the collected output */
1230 vim_free(redir_ga.ga_data);
1231 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001233 vim_free(redir_lval);
1234 redir_lval = NULL;
1235 }
1236 vim_free(redir_varname);
1237 redir_varname = NULL;
1238}
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240# if defined(FEAT_MBYTE) || defined(PROTO)
1241 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001242eval_charconvert(
1243 char_u *enc_from,
1244 char_u *enc_to,
1245 char_u *fname_from,
1246 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 int err = FALSE;
1249
1250 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1251 set_vim_var_string(VV_CC_TO, enc_to, -1);
1252 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1253 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1254 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1255 err = TRUE;
1256 set_vim_var_string(VV_CC_FROM, NULL, -1);
1257 set_vim_var_string(VV_CC_TO, NULL, -1);
1258 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1259 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1260
1261 if (err)
1262 return FAIL;
1263 return OK;
1264}
1265# endif
1266
1267# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001269eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270{
1271 int err = FALSE;
1272
1273 set_vim_var_string(VV_FNAME_IN, fname, -1);
1274 set_vim_var_string(VV_CMDARG, args, -1);
1275 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1276 err = TRUE;
1277 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1278 set_vim_var_string(VV_CMDARG, NULL, -1);
1279
1280 if (err)
1281 {
1282 mch_remove(fname);
1283 return FAIL;
1284 }
1285 return OK;
1286}
1287# endif
1288
1289# if defined(FEAT_DIFF) || defined(PROTO)
1290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001291eval_diff(
1292 char_u *origfile,
1293 char_u *newfile,
1294 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 int err = FALSE;
1297
1298 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1299 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1300 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1301 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1302 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1303 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1304 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1305}
1306
1307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001308eval_patch(
1309 char_u *origfile,
1310 char_u *difffile,
1311 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
1313 int err;
1314
1315 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1316 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1317 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1318 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1319 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1320 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1321 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1322}
1323# endif
1324
1325/*
1326 * Top level evaluation function, returning a boolean.
1327 * Sets "error" to TRUE if there was an error.
1328 * Return TRUE or FALSE.
1329 */
1330 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001331eval_to_bool(
1332 char_u *arg,
1333 int *error,
1334 char_u **nextcmd,
1335 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
Bram Moolenaar33570922005-01-25 22:26:29 +00001337 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 int retval = FALSE;
1339
1340 if (skip)
1341 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 else
1345 {
1346 *error = FALSE;
1347 if (!skip)
1348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001349 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001350 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 }
1352 }
1353 if (skip)
1354 --emsg_skip;
1355
1356 return retval;
1357}
1358
1359/*
1360 * Top level evaluation function, returning a string. If "skip" is TRUE,
1361 * only parsing to "nextcmd" is done, without reporting errors. Return
1362 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1363 */
1364 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001365eval_to_string_skip(
1366 char_u *arg,
1367 char_u **nextcmd,
1368 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
Bram Moolenaar33570922005-01-25 22:26:29 +00001370 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 char_u *retval;
1372
1373 if (skip)
1374 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 retval = NULL;
1377 else
1378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 retval = vim_strsave(get_tv_string(&tv));
1380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382 if (skip)
1383 --emsg_skip;
1384
1385 return retval;
1386}
1387
1388/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001389 * Skip over an expression at "*pp".
1390 * Return FAIL for an error, OK otherwise.
1391 */
1392 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001393skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001394{
Bram Moolenaar33570922005-01-25 22:26:29 +00001395 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001396
1397 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001398 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001399}
1400
1401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001403 * When "convert" is TRUE convert a List into a sequence of lines and convert
1404 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 * Return pointer to allocated memory, or NULL for failure.
1406 */
1407 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001408eval_to_string(
1409 char_u *arg,
1410 char_u **nextcmd,
1411 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001415 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001416#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001417 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 retval = NULL;
1422 else
1423 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001424 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001425 {
1426 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001427 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001428 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001429 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001430 if (tv.vval.v_list->lv_len > 0)
1431 ga_append(&ga, NL);
1432 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001433 ga_append(&ga, NUL);
1434 retval = (char_u *)ga.ga_data;
1435 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001436#ifdef FEAT_FLOAT
1437 else if (convert && tv.v_type == VAR_FLOAT)
1438 {
1439 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1440 retval = vim_strsave(numbuf);
1441 }
1442#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001443 else
1444 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001445 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447
1448 return retval;
1449}
1450
1451/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001452 * Call eval_to_string() without using current local variables and using
1453 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 */
1455 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001456eval_to_string_safe(
1457 char_u *arg,
1458 char_u **nextcmd,
1459 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
1461 char_u *retval;
1462 void *save_funccalp;
1463
1464 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001465 if (use_sandbox)
1466 ++sandbox;
1467 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001468 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001469 if (use_sandbox)
1470 --sandbox;
1471 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 restore_funccal(save_funccalp);
1473 return retval;
1474}
1475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476/*
1477 * Top level evaluation function, returning a number.
1478 * Evaluates "expr" silently.
1479 * Returns -1 for an error.
1480 */
1481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
Bram Moolenaar33570922005-01-25 22:26:29 +00001484 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001486 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
1488 ++emsg_off;
1489
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001490 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 retval = -1;
1492 else
1493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001494 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 }
1497 --emsg_off;
1498
1499 return retval;
1500}
1501
Bram Moolenaara40058a2005-07-11 22:42:07 +00001502/*
1503 * Prepare v: variable "idx" to be used.
1504 * Save the current typeval in "save_tv".
1505 * When not used yet add the variable to the v: hashtable.
1506 */
1507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001509{
1510 *save_tv = vimvars[idx].vv_tv;
1511 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1512 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1513}
1514
1515/*
1516 * Restore v: variable "idx" to typeval "save_tv".
1517 * When no longer defined, remove the variable from the v: hashtable.
1518 */
1519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001520restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001521{
1522 hashitem_T *hi;
1523
Bram Moolenaara40058a2005-07-11 22:42:07 +00001524 vimvars[idx].vv_tv = *save_tv;
1525 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1526 {
1527 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1528 if (HASHITEM_EMPTY(hi))
1529 EMSG2(_(e_intern2), "restore_vimvar()");
1530 else
1531 hash_remove(&vimvarht, hi);
1532 }
1533}
1534
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001535#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001536/*
1537 * Evaluate an expression to a list with suggestions.
1538 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001539 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540 */
1541 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001542eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001543{
1544 typval_T save_val;
1545 typval_T rettv;
1546 list_T *list = NULL;
1547 char_u *p = skipwhite(expr);
1548
1549 /* Set "v:val" to the bad word. */
1550 prepare_vimvar(VV_VAL, &save_val);
1551 vimvars[VV_VAL].vv_type = VAR_STRING;
1552 vimvars[VV_VAL].vv_str = badword;
1553 if (p_verbose == 0)
1554 ++emsg_off;
1555
1556 if (eval1(&p, &rettv, TRUE) == OK)
1557 {
1558 if (rettv.v_type != VAR_LIST)
1559 clear_tv(&rettv);
1560 else
1561 list = rettv.vval.v_list;
1562 }
1563
1564 if (p_verbose == 0)
1565 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001566 restore_vimvar(VV_VAL, &save_val);
1567
1568 return list;
1569}
1570
1571/*
1572 * "list" is supposed to contain two items: a word and a number. Return the
1573 * word in "pp" and the number as the return value.
1574 * Return -1 if anything isn't right.
1575 * Used to get the good word and score from the eval_spell_expr() result.
1576 */
1577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001579{
1580 listitem_T *li;
1581
1582 li = list->lv_first;
1583 if (li == NULL)
1584 return -1;
1585 *pp = get_tv_string(&li->li_tv);
1586
1587 li = li->li_next;
1588 if (li == NULL)
1589 return -1;
1590 return get_tv_number(&li->li_tv);
1591}
1592#endif
1593
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001594/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001595 * Top level evaluation function.
1596 * Returns an allocated typval_T with the result.
1597 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001598 */
1599 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001600eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001601{
1602 typval_T *tv;
1603
1604 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001605 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001606 {
1607 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001608 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001609 }
1610
1611 return tv;
1612}
1613
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001616 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001617 * Uses argv[argc] for the function arguments. Only Number and String
1618 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001621 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001622call_vim_function(
1623 char_u *func,
1624 int argc,
1625 char_u **argv,
1626 int safe, /* use the sandbox */
1627 int str_arg_only, /* all arguments are strings */
1628 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
Bram Moolenaar33570922005-01-25 22:26:29 +00001630 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 long n;
1632 int len;
1633 int i;
1634 int doesrange;
1635 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001638 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
1642 for (i = 0; i < argc; i++)
1643 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001644 /* Pass a NULL or empty argument as an empty string */
1645 if (argv[i] == NULL || *argv[i] == NUL)
1646 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001647 argvars[i].v_type = VAR_STRING;
1648 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001649 continue;
1650 }
1651
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001652 if (str_arg_only)
1653 len = 0;
1654 else
1655 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001656 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (len != 0 && len == (int)STRLEN(argv[i]))
1658 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001659 argvars[i].v_type = VAR_NUMBER;
1660 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
1662 else
1663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001664 argvars[i].v_type = VAR_STRING;
1665 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 }
1667 }
1668
1669 if (safe)
1670 {
1671 save_funccalp = save_funccal();
1672 ++sandbox;
1673 }
1674
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1676 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001678 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (safe)
1680 {
1681 --sandbox;
1682 restore_funccal(save_funccalp);
1683 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001684 vim_free(argvars);
1685
1686 if (ret == FAIL)
1687 clear_tv(rettv);
1688
1689 return ret;
1690}
1691
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001692/*
1693 * Call vimL function "func" and return the result as a number.
1694 * Returns -1 when calling the function fails.
1695 * Uses argv[argc] for the function arguments.
1696 */
1697 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001698call_func_retnr(
1699 char_u *func,
1700 int argc,
1701 char_u **argv,
1702 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001703{
1704 typval_T rettv;
1705 long retval;
1706
1707 /* All arguments are passed as strings, no conversion to number. */
1708 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1709 return -1;
1710
1711 retval = get_tv_number_chk(&rettv, NULL);
1712 clear_tv(&rettv);
1713 return retval;
1714}
1715
1716#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1717 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1718
Bram Moolenaar4f688582007-07-24 12:34:30 +00001719# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001721 * Call vimL function "func" and return the result as a string.
1722 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 * Uses argv[argc] for the function arguments.
1724 */
1725 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001726call_func_retstr(
1727 char_u *func,
1728 int argc,
1729 char_u **argv,
1730 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731{
1732 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001733 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001735 /* All arguments are passed as strings, no conversion to number. */
1736 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737 return NULL;
1738
1739 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 return retval;
1742}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001743# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001745/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001746 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001748 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001749 */
1750 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001751call_func_retlist(
1752 char_u *func,
1753 int argc,
1754 char_u **argv,
1755 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756{
1757 typval_T rettv;
1758
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001759 /* All arguments are passed as strings, no conversion to number. */
1760 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761 return NULL;
1762
1763 if (rettv.v_type != VAR_LIST)
1764 {
1765 clear_tv(&rettv);
1766 return NULL;
1767 }
1768
1769 return rettv.vval.v_list;
1770}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#endif
1772
1773/*
1774 * Save the current function call pointer, and set it to NULL.
1775 * Used when executing autocommands and for ":source".
1776 */
1777 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001780 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 current_funccal = NULL;
1783 return (void *)fc;
1784}
1785
1786 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001787restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001789 funccall_T *fc = (funccall_T *)vfc;
1790
1791 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792}
1793
Bram Moolenaar05159a02005-02-26 23:04:13 +00001794#if defined(FEAT_PROFILE) || defined(PROTO)
1795/*
1796 * Prepare profiling for entering a child or something else that is not
1797 * counted for the script/function itself.
1798 * Should always be called in pair with prof_child_exit().
1799 */
1800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001801prof_child_enter(
1802 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001803{
1804 funccall_T *fc = current_funccal;
1805
1806 if (fc != NULL && fc->func->uf_profiling)
1807 profile_start(&fc->prof_child);
1808 script_prof_save(tm);
1809}
1810
1811/*
1812 * Take care of time spent in a child.
1813 * Should always be called after prof_child_enter().
1814 */
1815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816prof_child_exit(
1817 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001818{
1819 funccall_T *fc = current_funccal;
1820
1821 if (fc != NULL && fc->func->uf_profiling)
1822 {
1823 profile_end(&fc->prof_child);
1824 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1825 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1826 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1827 }
1828 script_prof_restore(tm);
1829}
1830#endif
1831
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#ifdef FEAT_FOLDING
1834/*
1835 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1836 * it in "*cp". Doesn't give error messages.
1837 */
1838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001839eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 int retval;
1843 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001844 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1845 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001848 if (use_sandbox)
1849 ++sandbox;
1850 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 retval = 0;
1854 else
1855 {
1856 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 if (tv.v_type == VAR_NUMBER)
1858 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001859 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 retval = 0;
1861 else
1862 {
1863 /* If the result is a string, check if there is a non-digit before
1864 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 if (!VIM_ISDIGIT(*s) && *s != '-')
1867 *cp = *s++;
1868 retval = atol((char *)s);
1869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001873 if (use_sandbox)
1874 --sandbox;
1875 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
1877 return retval;
1878}
1879#endif
1880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 * ":let" list all variable values
1883 * ":let var1 var2" list variable values
1884 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001885 * ":let var += expr" assignment command.
1886 * ":let var -= expr" assignment command.
1887 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 */
1890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001891ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892{
1893 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001895 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 int var_count = 0;
1898 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001900 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001901 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
Bram Moolenaardb552d602006-03-23 22:59:57 +00001903 argend = skip_var_list(arg, &var_count, &semicolon);
1904 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001906 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1907 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 expr = skipwhite(argend);
1909 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1910 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001912 /*
1913 * ":let" without "=": list variables
1914 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 if (*arg == '[')
1916 EMSG(_(e_invarg));
1917 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001919 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001921 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 list_glob_vars(&first);
1924 list_buf_vars(&first);
1925 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001926#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001927 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001928#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001929 list_script_vars(&first);
1930 list_func_vars(&first);
1931 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 eap->nextcmd = check_nextcmd(arg);
1934 }
1935 else
1936 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 op[0] = '=';
1938 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001939 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001941 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1942 op[0] = *expr; /* +=, -= or .= */
1943 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001945 else
1946 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (eap->skip)
1949 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (eap->skip)
1952 {
1953 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 --emsg_skip;
1956 }
1957 else if (i != FAIL)
1958 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963 }
1964}
1965
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966/*
1967 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1968 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 * When "nextchars" is not NULL it points to a string with characters that
1970 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1971 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 * Returns OK or FAIL;
1973 */
1974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001975ex_let_vars(
1976 char_u *arg_start,
1977 typval_T *tv,
1978 int copy, /* copy values from "tv", don't move */
1979 int semicolon, /* from skip_var_list() */
1980 int var_count, /* from skip_var_list() */
1981 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982{
1983 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001984 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001986 listitem_T *item;
1987 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988
1989 if (*arg != '[')
1990 {
1991 /*
1992 * ":let var = expr" or ":for var in list"
1993 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001994 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 return FAIL;
1996 return OK;
1997 }
1998
1999 /*
2000 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2001 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002002 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 {
2004 EMSG(_(e_listreq));
2005 return FAIL;
2006 }
2007
2008 i = list_len(l);
2009 if (semicolon == 0 && var_count < i)
2010 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002011 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 return FAIL;
2013 }
2014 if (var_count - semicolon > i)
2015 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002016 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 return FAIL;
2018 }
2019
2020 item = l->lv_first;
2021 while (*arg != ']')
2022 {
2023 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002024 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 item = item->li_next;
2026 if (arg == NULL)
2027 return FAIL;
2028
2029 arg = skipwhite(arg);
2030 if (*arg == ';')
2031 {
2032 /* Put the rest of the list (may be empty) in the var after ';'.
2033 * Create a new list for this. */
2034 l = list_alloc();
2035 if (l == NULL)
2036 return FAIL;
2037 while (item != NULL)
2038 {
2039 list_append_tv(l, &item->li_tv);
2040 item = item->li_next;
2041 }
2042
2043 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002044 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002045 ltv.vval.v_list = l;
2046 l->lv_refcount = 1;
2047
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002048 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2049 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050 clear_tv(&ltv);
2051 if (arg == NULL)
2052 return FAIL;
2053 break;
2054 }
2055 else if (*arg != ',' && *arg != ']')
2056 {
2057 EMSG2(_(e_intern2), "ex_let_vars()");
2058 return FAIL;
2059 }
2060 }
2061
2062 return OK;
2063}
2064
2065/*
2066 * Skip over assignable variable "var" or list of variables "[var, var]".
2067 * Used for ":let varvar = expr" and ":for varvar in expr".
2068 * For "[var, var]" increment "*var_count" for each variable.
2069 * for "[var, var; var]" set "semicolon".
2070 * Return NULL for an error.
2071 */
2072 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002073skip_var_list(
2074 char_u *arg,
2075 int *var_count,
2076 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077{
2078 char_u *p, *s;
2079
2080 if (*arg == '[')
2081 {
2082 /* "[var, var]": find the matching ']'. */
2083 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002084 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085 {
2086 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2087 s = skip_var_one(p);
2088 if (s == p)
2089 {
2090 EMSG2(_(e_invarg2), p);
2091 return NULL;
2092 }
2093 ++*var_count;
2094
2095 p = skipwhite(s);
2096 if (*p == ']')
2097 break;
2098 else if (*p == ';')
2099 {
2100 if (*semicolon == 1)
2101 {
2102 EMSG(_("Double ; in list of variables"));
2103 return NULL;
2104 }
2105 *semicolon = 1;
2106 }
2107 else if (*p != ',')
2108 {
2109 EMSG2(_(e_invarg2), p);
2110 return NULL;
2111 }
2112 }
2113 return p + 1;
2114 }
2115 else
2116 return skip_var_one(arg);
2117}
2118
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002120 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002121 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002123 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002124skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002125{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002126 if (*arg == '@' && arg[1] != NUL)
2127 return arg + 2;
2128 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2129 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130}
2131
Bram Moolenaara7043832005-01-21 11:56:39 +00002132/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002133 * List variables for hashtab "ht" with prefix "prefix".
2134 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002137list_hashtable_vars(
2138 hashtab_T *ht,
2139 char_u *prefix,
2140 int empty,
2141 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002142{
Bram Moolenaar33570922005-01-25 22:26:29 +00002143 hashitem_T *hi;
2144 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002145 int todo;
2146
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002147 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002148 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2149 {
2150 if (!HASHITEM_EMPTY(hi))
2151 {
2152 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002153 di = HI2DI(hi);
2154 if (empty || di->di_tv.v_type != VAR_STRING
2155 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002157 }
2158 }
2159}
2160
2161/*
2162 * List global variables.
2163 */
2164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002165list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002166{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002168}
2169
2170/*
2171 * List buffer variables.
2172 */
2173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002175{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002176 char_u numbuf[NUMBUFLEN];
2177
Bram Moolenaar429fa852013-04-15 12:27:36 +02002178 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002180
2181 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2183 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002184}
2185
2186/*
2187 * List window variables.
2188 */
2189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002191{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002192 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002194}
2195
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196#ifdef FEAT_WINDOWS
2197/*
2198 * List tab page variables.
2199 */
2200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002202{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002203 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002205}
2206#endif
2207
Bram Moolenaara7043832005-01-21 11:56:39 +00002208/*
2209 * List Vim variables.
2210 */
2211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002212list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215}
2216
2217/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002218 * List script-local variables, if there is a script.
2219 */
2220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002221list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002222{
2223 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002224 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2225 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002226}
2227
2228/*
2229 * List function variables, if there is a function.
2230 */
2231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002232list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002233{
2234 if (current_funccal != NULL)
2235 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002236 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002237}
2238
2239/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 * List variables in "arg".
2241 */
2242 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002243list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244{
2245 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 char_u *name_start;
2249 char_u *arg_subsc;
2250 char_u *tofree;
2251 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252
2253 while (!ends_excmd(*arg) && !got_int)
2254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002257 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2259 {
2260 emsg_severe = TRUE;
2261 EMSG(_(e_trailing));
2262 break;
2263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 /* get_name_len() takes care of expanding curly braces */
2268 name_start = name = arg;
2269 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2270 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 /* This is mainly to keep test 49 working: when expanding
2273 * curly braces fails overrule the exception error message. */
2274 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 emsg_severe = TRUE;
2277 EMSG2(_(e_invarg2), arg);
2278 break;
2279 }
2280 error = TRUE;
2281 }
2282 else
2283 {
2284 if (tofree != NULL)
2285 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002286 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 else
2289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290 /* handle d.key, l[idx], f(expr) */
2291 arg_subsc = arg;
2292 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002300 case 'g': list_glob_vars(first); break;
2301 case 'b': list_buf_vars(first); break;
2302 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002303#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002305#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002306 case 'v': list_vim_vars(first); break;
2307 case 's': list_script_vars(first); break;
2308 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309 default:
2310 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002311 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002312 }
2313 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 {
2315 char_u numbuf[NUMBUFLEN];
2316 char_u *tf;
2317 int c;
2318 char_u *s;
2319
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002320 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 c = *arg;
2322 *arg = NUL;
2323 list_one_var_a((char_u *)"",
2324 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002325 tv.v_type,
2326 s == NULL ? (char_u *)"" : s,
2327 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328 *arg = c;
2329 vim_free(tf);
2330 }
2331 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002332 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 }
2334 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335
2336 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002338
2339 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
2341
2342 return arg;
2343}
2344
2345/*
2346 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2347 * Returns a pointer to the char just after the var name.
2348 * Returns NULL if there is an error.
2349 */
2350 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002351ex_let_one(
2352 char_u *arg, /* points to variable name */
2353 typval_T *tv, /* value to assign to variable */
2354 int copy, /* copy value from "tv" */
2355 char_u *endchars, /* valid chars after variable name or NULL */
2356 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357{
2358 int c1;
2359 char_u *name;
2360 char_u *p;
2361 char_u *arg_end = NULL;
2362 int len;
2363 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365
2366 /*
2367 * ":let $VAR = expr": Set environment variable.
2368 */
2369 if (*arg == '$')
2370 {
2371 /* Find the end of the name. */
2372 ++arg;
2373 name = arg;
2374 len = get_env_len(&arg);
2375 if (len == 0)
2376 EMSG2(_(e_invarg2), name - 1);
2377 else
2378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 if (op != NULL && (*op == '+' || *op == '-'))
2380 EMSG2(_(e_letwrong), op);
2381 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002382 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002384 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 {
2386 c1 = name[len];
2387 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 p = get_tv_string_chk(tv);
2389 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 {
2391 int mustfree = FALSE;
2392 char_u *s = vim_getenv(name, &mustfree);
2393
2394 if (s != NULL)
2395 {
2396 p = tofree = concat_str(s, p);
2397 if (mustfree)
2398 vim_free(s);
2399 }
2400 }
2401 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002402 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002404 if (STRICMP(name, "HOME") == 0)
2405 init_homedir();
2406 else if (didset_vim && STRICMP(name, "VIM") == 0)
2407 didset_vim = FALSE;
2408 else if (didset_vimruntime
2409 && STRICMP(name, "VIMRUNTIME") == 0)
2410 didset_vimruntime = FALSE;
2411 arg_end = arg;
2412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002414 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 }
2416 }
2417 }
2418
2419 /*
2420 * ":let &option = expr": Set option value.
2421 * ":let &l:option = expr": Set local option value.
2422 * ":let &g:option = expr": Set global option value.
2423 */
2424 else if (*arg == '&')
2425 {
2426 /* Find the end of the name. */
2427 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002428 if (p == NULL || (endchars != NULL
2429 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 EMSG(_(e_letunexp));
2431 else
2432 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 long n;
2434 int opt_type;
2435 long numval;
2436 char_u *stringval = NULL;
2437 char_u *s;
2438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 c1 = *p;
2440 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441
2442 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002443 s = get_tv_string_chk(tv); /* != NULL if number or string */
2444 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 {
2446 opt_type = get_option_value(arg, &numval,
2447 &stringval, opt_flags);
2448 if ((opt_type == 1 && *op == '.')
2449 || (opt_type == 0 && *op != '.'))
2450 EMSG2(_(e_letwrong), op);
2451 else
2452 {
2453 if (opt_type == 1) /* number */
2454 {
2455 if (*op == '+')
2456 n = numval + n;
2457 else
2458 n = numval - n;
2459 }
2460 else if (opt_type == 0 && stringval != NULL) /* string */
2461 {
2462 s = concat_str(stringval, s);
2463 vim_free(stringval);
2464 stringval = s;
2465 }
2466 }
2467 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 if (s != NULL)
2469 {
2470 set_option_value(arg, n, s, opt_flags);
2471 arg_end = p;
2472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 }
2476 }
2477
2478 /*
2479 * ":let @r = expr": Set register contents.
2480 */
2481 else if (*arg == '@')
2482 {
2483 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 if (op != NULL && (*op == '+' || *op == '-'))
2485 EMSG2(_(e_letwrong), op);
2486 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002487 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 EMSG(_(e_letunexp));
2489 else
2490 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002491 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 char_u *s;
2493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002494 p = get_tv_string_chk(tv);
2495 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002497 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 if (s != NULL)
2499 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002500 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 vim_free(s);
2502 }
2503 }
2504 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002507 arg_end = arg + 1;
2508 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002509 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 }
2511 }
2512
2513 /*
2514 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002519 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002521 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2525 EMSG(_(e_letunexp));
2526 else
2527 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002528 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 arg_end = p;
2530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 }
2534
2535 else
2536 EMSG2(_(e_invarg2), arg);
2537
2538 return arg_end;
2539}
2540
2541/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2543 */
2544 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002545check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546{
2547 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2548 {
2549 EMSG2(_(e_readonlyvar), arg);
2550 return TRUE;
2551 }
2552 return FALSE;
2553}
2554
2555/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 * Get an lval: variable, Dict item or List item that can be assigned a value
2557 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2558 * "name.key", "name.key[expr]" etc.
2559 * Indexing only works if "name" is an existing List or Dictionary.
2560 * "name" points to the start of the name.
2561 * If "rettv" is not NULL it points to the value to be assigned.
2562 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2563 * wrong; must end in space or cmd separator.
2564 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002565 * flags:
2566 * GLV_QUIET: do not give error messages
2567 * GLV_NO_AUTOLOAD: do not use script autoloading
2568 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002570 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 */
2573 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002574get_lval(
2575 char_u *name,
2576 typval_T *rettv,
2577 lval_T *lp,
2578 int unlet,
2579 int skip,
2580 int flags, /* GLV_ values */
2581 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 char_u *expr_start, *expr_end;
2585 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 dictitem_T *v;
2587 typval_T var1;
2588 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002594 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598
2599 if (skip)
2600 {
2601 /* When skipping just find the end of the name. */
2602 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002603 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 }
2605
2606 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002607 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (expr_start != NULL)
2609 {
2610 /* Don't expand the name when we already know there is an error. */
2611 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2612 && *p != '[' && *p != '.')
2613 {
2614 EMSG(_(e_trailing));
2615 return NULL;
2616 }
2617
2618 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2619 if (lp->ll_exp_name == NULL)
2620 {
2621 /* Report an invalid expression in braces, unless the
2622 * expression evaluation has been cancelled due to an
2623 * aborting error, an interrupt, or an exception. */
2624 if (!aborting() && !quiet)
2625 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002626 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 EMSG2(_(e_invarg2), name);
2628 return NULL;
2629 }
2630 }
2631 lp->ll_name = lp->ll_exp_name;
2632 }
2633 else
2634 lp->ll_name = name;
2635
2636 /* Without [idx] or .key we are done. */
2637 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2638 return p;
2639
2640 cc = *p;
2641 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002642 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (v == NULL && !quiet)
2644 EMSG2(_(e_undefvar), lp->ll_name);
2645 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002646 if (v == NULL)
2647 return NULL;
2648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 /*
2650 * Loop until no more [idx] or .key is following.
2651 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002652 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2656 && !(lp->ll_tv->v_type == VAR_DICT
2657 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E689: Can only index a List or Dictionary"));
2661 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!quiet)
2666 EMSG(_("E708: [:] must come last"));
2667 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 len = -1;
2671 if (*p == '.')
2672 {
2673 key = p + 1;
2674 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2675 ;
2676 if (len == 0)
2677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 if (!quiet)
2679 EMSG(_(e_emptykey));
2680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
2682 p = key + len;
2683 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 else
2685 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (*p == ':')
2689 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690 else
2691 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 empty1 = FALSE;
2693 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002695 if (get_tv_string_chk(&var1) == NULL)
2696 {
2697 /* not a number or string */
2698 clear_tv(&var1);
2699 return NULL;
2700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 }
2702
2703 /* Optionally get the second index [ :expr]. */
2704 if (*p == ':')
2705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002709 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 if (!empty1)
2711 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (rettv != NULL && (rettv->v_type != VAR_LIST
2715 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (!quiet)
2718 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 if (!empty1)
2720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 p = skipwhite(p + 1);
2724 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 else
2727 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (!empty1)
2732 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002735 if (get_tv_string_chk(&var2) == NULL)
2736 {
2737 /* not a number or string */
2738 if (!empty1)
2739 clear_tv(&var1);
2740 clear_tv(&var2);
2741 return NULL;
2742 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002745 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (!quiet)
2752 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (!empty1)
2754 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 }
2759
2760 /* Skip to past ']'. */
2761 ++p;
2762 }
2763
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 {
2766 if (len == -1)
2767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002769 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 if (*key == NUL)
2771 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (!quiet)
2773 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 }
2777 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002778 lp->ll_list = NULL;
2779 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002780 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002781
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002782 /* When assigning to a scope dictionary check that a function and
2783 * variable name is valid (only variable name unless it is l: or
2784 * g: dictionary). Disallow overwriting a builtin function. */
2785 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002787 int prevval;
2788 int wrong;
2789
2790 if (len != -1)
2791 {
2792 prevval = key[len];
2793 key[len] = NUL;
2794 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002795 else
2796 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002797 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2798 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002799 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002800 || !valid_varname(key);
2801 if (len != -1)
2802 key[len] = prevval;
2803 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002804 return NULL;
2805 }
2806
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 /* Can't add "v:" variable. */
2810 if (lp->ll_dict == &vimvardict)
2811 {
2812 EMSG2(_(e_illvar), name);
2813 return NULL;
2814 }
2815
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002816 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 if (len == -1)
2822 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 }
2825 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 if (len == -1)
2830 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 p = NULL;
2833 break;
2834 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002835 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002836 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002837 return NULL;
2838
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 if (len == -1)
2840 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 }
2843 else
2844 {
2845 /*
2846 * Get the number and item for the only or first index of the List.
2847 */
2848 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 else
2851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002852 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 clear_tv(&var1);
2854 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002855 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 lp->ll_list = lp->ll_tv->vval.v_list;
2857 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2858 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002860 if (lp->ll_n1 < 0)
2861 {
2862 lp->ll_n1 = 0;
2863 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2864 }
2865 }
2866 if (lp->ll_li == NULL)
2867 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002870 if (!quiet)
2871 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 }
2874
2875 /*
2876 * May need to find the item or absolute index for the second
2877 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 * When no index given: "lp->ll_empty2" is TRUE.
2879 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002883 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 {
2890 if (!quiet)
2891 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 }
2896
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2898 if (lp->ll_n1 < 0)
2899 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2900 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002901 {
2902 if (!quiet)
2903 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002905 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002906 }
2907
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002910 }
2911
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 return p;
2913}
2914
2915/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 */
2918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002919clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920{
2921 vim_free(lp->ll_exp_name);
2922 vim_free(lp->ll_newkey);
2923}
2924
2925/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 */
2930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002931set_var_lval(
2932 lval_T *lp,
2933 char_u *endp,
2934 typval_T *rettv,
2935 int copy,
2936 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937{
2938 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002939 listitem_T *ri;
2940 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941
2942 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 cc = *endp;
2947 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 if (op != NULL && *op != '=')
2949 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002952 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002953 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002954 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002955 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002957 if ((di == NULL
2958 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2959 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2960 FALSE)))
2961 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 set_var(lp->ll_name, &tv, FALSE);
2963 clear_tv(&tv);
2964 }
2965 }
2966 else
2967 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002969 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002971 else if (tv_check_lock(lp->ll_newkey == NULL
2972 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002973 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002974 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 else if (lp->ll_range)
2976 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002977 listitem_T *ll_li = lp->ll_li;
2978 int ll_n1 = lp->ll_n1;
2979
2980 /*
2981 * Check whether any of the list items is locked
2982 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002983 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002984 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002985 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002986 return;
2987 ri = ri->li_next;
2988 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2989 break;
2990 ll_li = ll_li->li_next;
2991 ++ll_n1;
2992 }
2993
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 /*
2995 * Assign the List values to the list items.
2996 */
2997 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 if (op != NULL && *op != '=')
3000 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3001 else
3002 {
3003 clear_tv(&lp->ll_li->li_tv);
3004 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 ri = ri->li_next;
3007 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3008 break;
3009 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003012 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003013 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 ri = NULL;
3015 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003016 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 lp->ll_li = lp->ll_li->li_next;
3019 ++lp->ll_n1;
3020 }
3021 if (ri != NULL)
3022 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 else if (lp->ll_empty2
3024 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 : lp->ll_n1 != lp->ll_n2)
3026 EMSG(_("E711: List value has not enough items"));
3027 }
3028 else
3029 {
3030 /*
3031 * Assign to a List or Dictionary item.
3032 */
3033 if (lp->ll_newkey != NULL)
3034 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 if (op != NULL && *op != '=')
3036 {
3037 EMSG2(_(e_letwrong), op);
3038 return;
3039 }
3040
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003042 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 if (di == NULL)
3044 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003045 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3046 {
3047 vim_free(di);
3048 return;
3049 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 else if (op != NULL && *op != '=')
3053 {
3054 tv_op(lp->ll_tv, rettv, op);
3055 return;
3056 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003057 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003059
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 /*
3061 * Assign the value to the variable or list item.
3062 */
3063 if (copy)
3064 copy_tv(rettv, lp->ll_tv);
3065 else
3066 {
3067 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003068 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003069 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070 }
3071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072}
3073
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003074/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3076 * Returns OK or FAIL.
3077 */
3078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003079tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080{
3081 long n;
3082 char_u numbuf[NUMBUFLEN];
3083 char_u *s;
3084
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003085 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3086 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3087 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 {
3089 switch (tv1->v_type)
3090 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003091 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003092 case VAR_DICT:
3093 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003094 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003095 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003096 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003097 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 break;
3099
3100 case VAR_LIST:
3101 if (*op != '+' || tv2->v_type != VAR_LIST)
3102 break;
3103 /* List += List */
3104 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3105 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3106 return OK;
3107
3108 case VAR_NUMBER:
3109 case VAR_STRING:
3110 if (tv2->v_type == VAR_LIST)
3111 break;
3112 if (*op == '+' || *op == '-')
3113 {
3114 /* nr += nr or nr -= nr*/
3115 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116#ifdef FEAT_FLOAT
3117 if (tv2->v_type == VAR_FLOAT)
3118 {
3119 float_T f = n;
3120
3121 if (*op == '+')
3122 f += tv2->vval.v_float;
3123 else
3124 f -= tv2->vval.v_float;
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_FLOAT;
3127 tv1->vval.v_float = f;
3128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#endif
3131 {
3132 if (*op == '+')
3133 n += get_tv_number(tv2);
3134 else
3135 n -= get_tv_number(tv2);
3136 clear_tv(tv1);
3137 tv1->v_type = VAR_NUMBER;
3138 tv1->vval.v_number = n;
3139 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003140 }
3141 else
3142 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143 if (tv2->v_type == VAR_FLOAT)
3144 break;
3145
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 /* str .= str */
3147 s = get_tv_string(tv1);
3148 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3149 clear_tv(tv1);
3150 tv1->v_type = VAR_STRING;
3151 tv1->vval.v_string = s;
3152 }
3153 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003156#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157 {
3158 float_T f;
3159
3160 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3161 && tv2->v_type != VAR_NUMBER
3162 && tv2->v_type != VAR_STRING))
3163 break;
3164 if (tv2->v_type == VAR_FLOAT)
3165 f = tv2->vval.v_float;
3166 else
3167 f = get_tv_number(tv2);
3168 if (*op == '+')
3169 tv1->vval.v_float += f;
3170 else
3171 tv1->vval.v_float -= f;
3172 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003173#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003174 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003175 }
3176 }
3177
3178 EMSG2(_(e_letwrong), op);
3179 return FAIL;
3180}
3181
3182/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 * Add a watcher to a list.
3184 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003186list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187{
3188 lw->lw_next = l->lv_watch;
3189 l->lv_watch = lw;
3190}
3191
3192/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003193 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 * No warning when it isn't found...
3195 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003197list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198{
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200
3201 lwp = &l->lv_watch;
3202 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3203 {
3204 if (lw == lwrem)
3205 {
3206 *lwp = lw->lw_next;
3207 break;
3208 }
3209 lwp = &lw->lw_next;
3210 }
3211}
3212
3213/*
3214 * Just before removing an item from a list: advance watchers to the next
3215 * item.
3216 */
3217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219{
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221
3222 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3223 if (lw->lw_item == item)
3224 lw->lw_item = item->li_next;
3225}
3226
3227/*
3228 * Evaluate the expression used in a ":for var in expr" command.
3229 * "arg" points to "var".
3230 * Set "*errp" to TRUE for an error, FALSE otherwise;
3231 * Return a pointer that holds the info. Null when there is an error.
3232 */
3233 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003234eval_for_line(
3235 char_u *arg,
3236 int *errp,
3237 char_u **nextcmdp,
3238 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239{
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 typval_T tv;
3243 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244
3245 *errp = TRUE; /* default: there is an error */
3246
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 if (fi == NULL)
3249 return NULL;
3250
3251 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3252 if (expr == NULL)
3253 return fi;
3254
3255 expr = skipwhite(expr);
3256 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3257 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003258 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 return fi;
3260 }
3261
3262 if (skip)
3263 ++emsg_skip;
3264 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3265 {
3266 *errp = FALSE;
3267 if (!skip)
3268 {
3269 l = tv.vval.v_list;
3270 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003273 clear_tv(&tv);
3274 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 else
3276 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003277 /* No need to increment the refcount, it's already set for the
3278 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 fi->fi_list = l;
3280 list_add_watch(l, &fi->fi_lw);
3281 fi->fi_lw.lw_item = l->lv_first;
3282 }
3283 }
3284 }
3285 if (skip)
3286 --emsg_skip;
3287
3288 return fi;
3289}
3290
3291/*
3292 * Use the first item in a ":for" list. Advance to the next.
3293 * Assign the values to the variable (list). "arg" points to the first one.
3294 * Return TRUE when a valid item was found, FALSE when at end of list or
3295 * something wrong.
3296 */
3297 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003298next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003300 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303
3304 item = fi->fi_lw.lw_item;
3305 if (item == NULL)
3306 result = FALSE;
3307 else
3308 {
3309 fi->fi_lw.lw_item = item->li_next;
3310 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3311 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3312 }
3313 return result;
3314}
3315
3316/*
3317 * Free the structure used to store info used by ":for".
3318 */
3319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321{
Bram Moolenaar33570922005-01-25 22:26:29 +00003322 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003324 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003327 list_unref(fi->fi_list);
3328 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 vim_free(fi);
3330}
3331
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3333
3334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003335set_context_for_expression(
3336 expand_T *xp,
3337 char_u *arg,
3338 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 int got_eq = FALSE;
3341 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 if (cmdidx == CMD_let)
3345 {
3346 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003347 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 {
3349 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003350 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 {
3352 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003353 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003354 if (vim_iswhite(*p))
3355 break;
3356 }
3357 return;
3358 }
3359 }
3360 else
3361 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3362 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 while ((xp->xp_pattern = vim_strpbrk(arg,
3364 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3365 {
3366 c = *xp->xp_pattern;
3367 if (c == '&')
3368 {
3369 c = xp->xp_pattern[1];
3370 if (c == '&')
3371 {
3372 ++xp->xp_pattern;
3373 xp->xp_context = cmdidx != CMD_let || got_eq
3374 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3375 }
3376 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003379 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3380 xp->xp_pattern += 2;
3381
3382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 else if (c == '$')
3385 {
3386 /* environment variable */
3387 xp->xp_context = EXPAND_ENV_VARS;
3388 }
3389 else if (c == '=')
3390 {
3391 got_eq = TRUE;
3392 xp->xp_context = EXPAND_EXPRESSION;
3393 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003394 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 && xp->xp_context == EXPAND_FUNCTIONS
3396 && vim_strchr(xp->xp_pattern, '(') == NULL)
3397 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003398 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 break;
3400 }
3401 else if (cmdidx != CMD_let || got_eq)
3402 {
3403 if (c == '"') /* string */
3404 {
3405 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3406 if (c == '\\' && xp->xp_pattern[1] != NUL)
3407 ++xp->xp_pattern;
3408 xp->xp_context = EXPAND_NOTHING;
3409 }
3410 else if (c == '\'') /* literal string */
3411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003412 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3414 /* skip */ ;
3415 xp->xp_context = EXPAND_NOTHING;
3416 }
3417 else if (c == '|')
3418 {
3419 if (xp->xp_pattern[1] == '|')
3420 {
3421 ++xp->xp_pattern;
3422 xp->xp_context = EXPAND_EXPRESSION;
3423 }
3424 else
3425 xp->xp_context = EXPAND_COMMANDS;
3426 }
3427 else
3428 xp->xp_context = EXPAND_EXPRESSION;
3429 }
3430 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003431 /* Doesn't look like something valid, expand as an expression
3432 * anyway. */
3433 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 arg = xp->xp_pattern;
3435 if (*arg != NUL)
3436 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3437 /* skip */ ;
3438 }
3439 xp->xp_pattern = arg;
3440}
3441
3442#endif /* FEAT_CMDL_COMPL */
3443
3444/*
3445 * ":1,25call func(arg1, arg2)" function call.
3446 */
3447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 char_u *arg = eap->arg;
3451 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003453 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003455 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 linenr_T lnum;
3457 int doesrange;
3458 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 funcdict_T fudi;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003460 partial_T *partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003462 if (eap->skip)
3463 {
3464 /* trans_function_name() doesn't work well when skipping, use eval0()
3465 * instead to skip to any following command, e.g. for:
3466 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003467 ++emsg_skip;
3468 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3469 clear_tv(&rettv);
3470 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003471 return;
3472 }
3473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003474 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003475 if (fudi.fd_newkey != NULL)
3476 {
3477 /* Still need to give an error message for missing key. */
3478 EMSG2(_(e_dictkey), fudi.fd_newkey);
3479 vim_free(fudi.fd_newkey);
3480 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003481 if (tofree == NULL)
3482 return;
3483
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003484 /* Increase refcount on dictionary, it could get deleted when evaluating
3485 * the arguments. */
3486 if (fudi.fd_dict != NULL)
3487 ++fudi.fd_dict->dv_refcount;
3488
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003489 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003490 len = (int)STRLEN(tofree);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003491 name = deref_func_name(tofree, &len, &partial, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar532c7802005-01-27 14:44:31 +00003493 /* Skip white space to allow ":call func ()". Not good, but required for
3494 * backward compatibility. */
3495 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498 if (*startarg != '(')
3499 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003500 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 goto end;
3502 }
3503
3504 /*
3505 * When skipping, evaluate the function once, to find the end of the
3506 * arguments.
3507 * When the function takes a range, this is discovered after the first
3508 * call, and the loop is broken.
3509 */
3510 if (eap->skip)
3511 {
3512 ++emsg_skip;
3513 lnum = eap->line2; /* do it once, also with an invalid range */
3514 }
3515 else
3516 lnum = eap->line1;
3517 for ( ; lnum <= eap->line2; ++lnum)
3518 {
3519 if (!eap->skip && eap->addr_count > 0)
3520 {
3521 curwin->w_cursor.lnum = lnum;
3522 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003523#ifdef FEAT_VIRTUALEDIT
3524 curwin->w_cursor.coladd = 0;
3525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003529 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003530 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
3532 failed = TRUE;
3533 break;
3534 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003535
3536 /* Handle a function returning a Funcref, Dictionary or List. */
3537 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3538 {
3539 failed = TRUE;
3540 break;
3541 }
3542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (doesrange || eap->skip)
3545 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003549 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003550 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (aborting())
3552 break;
3553 }
3554 if (eap->skip)
3555 --emsg_skip;
3556
3557 if (!failed)
3558 {
3559 /* Check for trailing illegal characters and a following command. */
3560 if (!ends_excmd(*arg))
3561 {
3562 emsg_severe = TRUE;
3563 EMSG(_(e_trailing));
3564 }
3565 else
3566 eap->nextcmd = check_nextcmd(arg);
3567 }
3568
3569end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003570 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003571 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572}
3573
3574/*
3575 * ":unlet[!] var1 ... " command.
3576 */
3577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003578ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580 ex_unletlock(eap, eap->arg, 0);
3581}
3582
3583/*
3584 * ":lockvar" and ":unlockvar" commands
3585 */
3586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003588{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 int deep = 2;
3591
3592 if (eap->forceit)
3593 deep = -1;
3594 else if (vim_isdigit(*arg))
3595 {
3596 deep = getdigits(&arg);
3597 arg = skipwhite(arg);
3598 }
3599
3600 ex_unletlock(eap, arg, deep);
3601}
3602
3603/*
3604 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3605 */
3606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003607ex_unletlock(
3608 exarg_T *eap,
3609 char_u *argstart,
3610 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611{
3612 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 do
3618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003620 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003621 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 if (lv.ll_name == NULL)
3623 error = TRUE; /* error but continue parsing */
3624 if (name_end == NULL || (!vim_iswhite(*name_end)
3625 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (name_end != NULL)
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 if (!(eap->skip || error))
3633 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 break;
3635 }
3636
3637 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 {
3639 if (eap->cmdidx == CMD_unlet)
3640 {
3641 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3642 error = TRUE;
3643 }
3644 else
3645 {
3646 if (do_lock_var(&lv, name_end, deep,
3647 eap->cmdidx == CMD_lockvar) == FAIL)
3648 error = TRUE;
3649 }
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (!eap->skip)
3653 clear_lval(&lv);
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 arg = skipwhite(name_end);
3656 } while (!ends_excmd(*arg));
3657
3658 eap->nextcmd = check_nextcmd(arg);
3659}
3660
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003662do_unlet_var(
3663 lval_T *lp,
3664 char_u *name_end,
3665 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 int ret = OK;
3668 int cc;
3669
3670 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 cc = *name_end;
3673 *name_end = NUL;
3674
3675 /* Normal name or expanded name. */
3676 if (check_changedtick(lp->ll_name))
3677 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003684 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003685 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 else if (lp->ll_range)
3688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003691 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003692
3693 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3694 {
3695 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003696 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 return FAIL;
3698 ll_li = li;
3699 ++ll_n1;
3700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701
3702 /* Delete a range of List items. */
3703 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3704 {
3705 li = lp->ll_li->li_next;
3706 listitem_remove(lp->ll_list, lp->ll_li);
3707 lp->ll_li = li;
3708 ++lp->ll_n1;
3709 }
3710 }
3711 else
3712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a List item. */
3715 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003718 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 }
3720
3721 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003722}
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724/*
3725 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003729do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 hashtab_T *ht;
3732 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003733 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003735 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 ht = find_var_ht(name, &varname);
3738 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003740 if (ht == &globvarht)
3741 d = &globvardict;
3742 else if (current_funccal != NULL
3743 && ht == &current_funccal->l_vars.dv_hashtab)
3744 d = &current_funccal->l_vars;
3745 else if (ht == &compat_hashtab)
3746 d = &vimvardict;
3747 else
3748 {
3749 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3750 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3751 }
3752 if (d == NULL)
3753 {
3754 EMSG2(_(e_intern2), "do_unlet()");
3755 return FAIL;
3756 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003757 hi = hash_find(ht, varname);
3758 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003759 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003760 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003761 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003762 || var_check_ro(di->di_flags, name, FALSE)
3763 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003764 return FAIL;
3765
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766 delete_var(ht, hi);
3767 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003770 if (forceit)
3771 return OK;
3772 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 return FAIL;
3774}
3775
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003776/*
3777 * Lock or unlock variable indicated by "lp".
3778 * "deep" is the levels to go (-1 for unlimited);
3779 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3780 */
3781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782do_lock_var(
3783 lval_T *lp,
3784 char_u *name_end,
3785 int deep,
3786 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003787{
3788 int ret = OK;
3789 int cc;
3790 dictitem_T *di;
3791
3792 if (deep == 0) /* nothing to do */
3793 return OK;
3794
3795 if (lp->ll_tv == NULL)
3796 {
3797 cc = *name_end;
3798 *name_end = NUL;
3799
3800 /* Normal name or expanded name. */
3801 if (check_changedtick(lp->ll_name))
3802 ret = FAIL;
3803 else
3804 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003805 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003806 if (di == NULL)
3807 ret = FAIL;
3808 else
3809 {
3810 if (lock)
3811 di->di_flags |= DI_FLAGS_LOCK;
3812 else
3813 di->di_flags &= ~DI_FLAGS_LOCK;
3814 item_lock(&di->di_tv, deep, lock);
3815 }
3816 }
3817 *name_end = cc;
3818 }
3819 else if (lp->ll_range)
3820 {
3821 listitem_T *li = lp->ll_li;
3822
3823 /* (un)lock a range of List items. */
3824 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3825 {
3826 item_lock(&li->li_tv, deep, lock);
3827 li = li->li_next;
3828 ++lp->ll_n1;
3829 }
3830 }
3831 else if (lp->ll_list != NULL)
3832 /* (un)lock a List item. */
3833 item_lock(&lp->ll_li->li_tv, deep, lock);
3834 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003835 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003836 item_lock(&lp->ll_di->di_tv, deep, lock);
3837
3838 return ret;
3839}
3840
3841/*
3842 * Lock or unlock an item. "deep" is nr of levels to go.
3843 */
3844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003845item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003846{
3847 static int recurse = 0;
3848 list_T *l;
3849 listitem_T *li;
3850 dict_T *d;
3851 hashitem_T *hi;
3852 int todo;
3853
3854 if (recurse >= DICT_MAXNEST)
3855 {
3856 EMSG(_("E743: variable nested too deep for (un)lock"));
3857 return;
3858 }
3859 if (deep == 0)
3860 return;
3861 ++recurse;
3862
3863 /* lock/unlock the item itself */
3864 if (lock)
3865 tv->v_lock |= VAR_LOCKED;
3866 else
3867 tv->v_lock &= ~VAR_LOCKED;
3868
3869 switch (tv->v_type)
3870 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003871 case VAR_UNKNOWN:
3872 case VAR_NUMBER:
3873 case VAR_STRING:
3874 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003875 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003876 case VAR_FLOAT:
3877 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003878 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003879 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003880 break;
3881
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003882 case VAR_LIST:
3883 if ((l = tv->vval.v_list) != NULL)
3884 {
3885 if (lock)
3886 l->lv_lock |= VAR_LOCKED;
3887 else
3888 l->lv_lock &= ~VAR_LOCKED;
3889 if (deep < 0 || deep > 1)
3890 /* recursive: lock/unlock the items the List contains */
3891 for (li = l->lv_first; li != NULL; li = li->li_next)
3892 item_lock(&li->li_tv, deep - 1, lock);
3893 }
3894 break;
3895 case VAR_DICT:
3896 if ((d = tv->vval.v_dict) != NULL)
3897 {
3898 if (lock)
3899 d->dv_lock |= VAR_LOCKED;
3900 else
3901 d->dv_lock &= ~VAR_LOCKED;
3902 if (deep < 0 || deep > 1)
3903 {
3904 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003905 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003906 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3907 {
3908 if (!HASHITEM_EMPTY(hi))
3909 {
3910 --todo;
3911 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3912 }
3913 }
3914 }
3915 }
3916 }
3917 --recurse;
3918}
3919
Bram Moolenaara40058a2005-07-11 22:42:07 +00003920/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003921 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3922 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003923 */
3924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003925tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003926{
3927 return (tv->v_lock & VAR_LOCKED)
3928 || (tv->v_type == VAR_LIST
3929 && tv->vval.v_list != NULL
3930 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3931 || (tv->v_type == VAR_DICT
3932 && tv->vval.v_dict != NULL
3933 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3934}
3935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3937/*
3938 * Delete all "menutrans_" variables.
3939 */
3940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003941del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003947 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 {
3950 if (!HASHITEM_EMPTY(hi))
3951 {
3952 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3954 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003955 }
3956 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958}
3959#endif
3960
3961#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3962
3963/*
3964 * Local string buffer for the next two functions to store a variable name
3965 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3966 * get_user_var_name().
3967 */
3968
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003969static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
3971static char_u *varnamebuf = NULL;
3972static int varnamebuflen = 0;
3973
3974/*
3975 * Function to concatenate a prefix and a variable name.
3976 */
3977 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003978cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979{
3980 int len;
3981
3982 len = (int)STRLEN(name) + 3;
3983 if (len > varnamebuflen)
3984 {
3985 vim_free(varnamebuf);
3986 len += 10; /* some additional space */
3987 varnamebuf = alloc(len);
3988 if (varnamebuf == NULL)
3989 {
3990 varnamebuflen = 0;
3991 return NULL;
3992 }
3993 varnamebuflen = len;
3994 }
3995 *varnamebuf = prefix;
3996 varnamebuf[1] = ':';
3997 STRCPY(varnamebuf + 2, name);
3998 return varnamebuf;
3999}
4000
4001/*
4002 * Function given to ExpandGeneric() to obtain the list of user defined
4003 * (global/buffer/window/built-in) variable names.
4004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004006get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004008 static long_u gdone;
4009 static long_u bdone;
4010 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004011#ifdef FEAT_WINDOWS
4012 static long_u tdone;
4013#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004014 static int vidx;
4015 static hashitem_T *hi;
4016 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
4018 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004020 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004021#ifdef FEAT_WINDOWS
4022 tdone = 0;
4023#endif
4024 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004025
4026 /* Global variables */
4027 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004031 else
4032 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004033 while (HASHITEM_EMPTY(hi))
4034 ++hi;
4035 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4036 return cat_prefix_varname('g', hi->hi_key);
4037 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004039
4040 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004041 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004042 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004044 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004046 else
4047 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004048 while (HASHITEM_EMPTY(hi))
4049 ++hi;
4050 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004054 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return (char_u *)"b:changedtick";
4056 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004057
4058 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004059 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004062 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004063 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004064 else
4065 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004066 while (HASHITEM_EMPTY(hi))
4067 ++hi;
4068 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004070
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004071#ifdef FEAT_WINDOWS
4072 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004073 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004074 if (tdone < ht->ht_used)
4075 {
4076 if (tdone++ == 0)
4077 hi = ht->ht_array;
4078 else
4079 ++hi;
4080 while (HASHITEM_EMPTY(hi))
4081 ++hi;
4082 return cat_prefix_varname('t', hi->hi_key);
4083 }
4084#endif
4085
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 /* v: variables */
4087 if (vidx < VV_LEN)
4088 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090 vim_free(varnamebuf);
4091 varnamebuf = NULL;
4092 varnamebuflen = 0;
4093 return NULL;
4094}
4095
4096#endif /* FEAT_CMDL_COMPL */
4097
4098/*
4099 * types for expressions.
4100 */
4101typedef enum
4102{
4103 TYPE_UNKNOWN = 0
4104 , TYPE_EQUAL /* == */
4105 , TYPE_NEQUAL /* != */
4106 , TYPE_GREATER /* > */
4107 , TYPE_GEQUAL /* >= */
4108 , TYPE_SMALLER /* < */
4109 , TYPE_SEQUAL /* <= */
4110 , TYPE_MATCH /* =~ */
4111 , TYPE_NOMATCH /* !~ */
4112} exptype_T;
4113
4114/*
4115 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4118 */
4119
4120/*
4121 * Handle zero level expression.
4122 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004124 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 * Return OK or FAIL.
4126 */
4127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004128eval0(
4129 char_u *arg,
4130 typval_T *rettv,
4131 char_u **nextcmd,
4132 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133{
4134 int ret;
4135 char_u *p;
4136
4137 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (ret == FAIL || !ends_excmd(*p))
4140 {
4141 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 /*
4144 * Report the invalid expression unless the expression evaluation has
4145 * been cancelled due to an aborting error, an interrupt, or an
4146 * exception.
4147 */
4148 if (!aborting())
4149 EMSG2(_(e_invexpr2), arg);
4150 ret = FAIL;
4151 }
4152 if (nextcmd != NULL)
4153 *nextcmd = check_nextcmd(p);
4154
4155 return ret;
4156}
4157
4158/*
4159 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004160 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 *
4162 * "arg" must point to the first non-white of the expression.
4163 * "arg" is advanced to the next non-white after the recognized expression.
4164 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004165 * Note: "rettv.v_lock" is not set.
4166 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 * Return OK or FAIL.
4168 */
4169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004170eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171{
4172 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004173 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174
4175 /*
4176 * Get the first variable.
4177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return FAIL;
4180
4181 if ((*arg)[0] == '?')
4182 {
4183 result = FALSE;
4184 if (evaluate)
4185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 int error = FALSE;
4187
4188 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 if (error)
4192 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194
4195 /*
4196 * Get the second variable.
4197 */
4198 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 return FAIL;
4201
4202 /*
4203 * Check for the ":".
4204 */
4205 if ((*arg)[0] != ':')
4206 {
4207 EMSG(_("E109: Missing ':' after '?'"));
4208 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004209 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return FAIL;
4211 }
4212
4213 /*
4214 * Get the third variable.
4215 */
4216 *arg = skipwhite(*arg + 1);
4217 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4218 {
4219 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 return FAIL;
4222 }
4223 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226
4227 return OK;
4228}
4229
4230/*
4231 * Handle first level expression:
4232 * expr2 || expr2 || expr2 logical OR
4233 *
4234 * "arg" must point to the first non-white of the expression.
4235 * "arg" is advanced to the next non-white after the recognized expression.
4236 *
4237 * Return OK or FAIL.
4238 */
4239 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004240eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
Bram Moolenaar33570922005-01-25 22:26:29 +00004242 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 long result;
4244 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
4247 /*
4248 * Get the first variable.
4249 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004250 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 return FAIL;
4252
4253 /*
4254 * Repeat until there is no following "||".
4255 */
4256 first = TRUE;
4257 result = FALSE;
4258 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4259 {
4260 if (evaluate && first)
4261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (error)
4266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 first = FALSE;
4268 }
4269
4270 /*
4271 * Get the second variable.
4272 */
4273 *arg = skipwhite(*arg + 2);
4274 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4275 return FAIL;
4276
4277 /*
4278 * Compute the result.
4279 */
4280 if (evaluate && !result)
4281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004285 if (error)
4286 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 if (evaluate)
4289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 rettv->v_type = VAR_NUMBER;
4291 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 }
4294
4295 return OK;
4296}
4297
4298/*
4299 * Handle second level expression:
4300 * expr3 && expr3 && expr3 logical AND
4301 *
4302 * "arg" must point to the first non-white of the expression.
4303 * "arg" is advanced to the next non-white after the recognized expression.
4304 *
4305 * Return OK or FAIL.
4306 */
4307 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004308eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309{
Bram Moolenaar33570922005-01-25 22:26:29 +00004310 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 long result;
4312 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
4315 /*
4316 * Get the first variable.
4317 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return FAIL;
4320
4321 /*
4322 * Repeat until there is no following "&&".
4323 */
4324 first = TRUE;
4325 result = TRUE;
4326 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4327 {
4328 if (evaluate && first)
4329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 if (error)
4334 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 first = FALSE;
4336 }
4337
4338 /*
4339 * Get the second variable.
4340 */
4341 *arg = skipwhite(*arg + 2);
4342 if (eval4(arg, &var2, evaluate && result) == FAIL)
4343 return FAIL;
4344
4345 /*
4346 * Compute the result.
4347 */
4348 if (evaluate && result)
4349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004352 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004353 if (error)
4354 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 if (evaluate)
4357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 rettv->v_type = VAR_NUMBER;
4359 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 }
4362
4363 return OK;
4364}
4365
4366/*
4367 * Handle third level expression:
4368 * var1 == var2
4369 * var1 =~ var2
4370 * var1 != var2
4371 * var1 !~ var2
4372 * var1 > var2
4373 * var1 >= var2
4374 * var1 < var2
4375 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004376 * var1 is var2
4377 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 *
4379 * "arg" must point to the first non-white of the expression.
4380 * "arg" is advanced to the next non-white after the recognized expression.
4381 *
4382 * Return OK or FAIL.
4383 */
4384 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004385eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386{
Bram Moolenaar33570922005-01-25 22:26:29 +00004387 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 char_u *p;
4389 int i;
4390 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004391 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 int len = 2;
4393 long n1, n2;
4394 char_u *s1, *s2;
4395 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4396 regmatch_T regmatch;
4397 int ic;
4398 char_u *save_cpo;
4399
4400 /*
4401 * Get the first variable.
4402 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004403 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 return FAIL;
4405
4406 p = *arg;
4407 switch (p[0])
4408 {
4409 case '=': if (p[1] == '=')
4410 type = TYPE_EQUAL;
4411 else if (p[1] == '~')
4412 type = TYPE_MATCH;
4413 break;
4414 case '!': if (p[1] == '=')
4415 type = TYPE_NEQUAL;
4416 else if (p[1] == '~')
4417 type = TYPE_NOMATCH;
4418 break;
4419 case '>': if (p[1] != '=')
4420 {
4421 type = TYPE_GREATER;
4422 len = 1;
4423 }
4424 else
4425 type = TYPE_GEQUAL;
4426 break;
4427 case '<': if (p[1] != '=')
4428 {
4429 type = TYPE_SMALLER;
4430 len = 1;
4431 }
4432 else
4433 type = TYPE_SEQUAL;
4434 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 case 'i': if (p[1] == 's')
4436 {
4437 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4438 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004439 i = p[len];
4440 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 {
4442 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4443 type_is = TRUE;
4444 }
4445 }
4446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448
4449 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004450 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 */
4452 if (type != TYPE_UNKNOWN)
4453 {
4454 /* extra question mark appended: ignore case */
4455 if (p[len] == '?')
4456 {
4457 ic = TRUE;
4458 ++len;
4459 }
4460 /* extra '#' appended: match case */
4461 else if (p[len] == '#')
4462 {
4463 ic = FALSE;
4464 ++len;
4465 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004466 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 else
4468 ic = p_ic;
4469
4470 /*
4471 * Get the second variable.
4472 */
4473 *arg = skipwhite(p + len);
4474 if (eval5(arg, &var2, evaluate) == FAIL)
4475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 return FAIL;
4478 }
4479
4480 if (evaluate)
4481 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004482 if (type_is && rettv->v_type != var2.v_type)
4483 {
4484 /* For "is" a different type always means FALSE, for "notis"
4485 * it means TRUE. */
4486 n1 = (type == TYPE_NEQUAL);
4487 }
4488 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4489 {
4490 if (type_is)
4491 {
4492 n1 = (rettv->v_type == var2.v_type
4493 && rettv->vval.v_list == var2.vval.v_list);
4494 if (type == TYPE_NEQUAL)
4495 n1 = !n1;
4496 }
4497 else if (rettv->v_type != var2.v_type
4498 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4499 {
4500 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004501 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004503 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004504 clear_tv(rettv);
4505 clear_tv(&var2);
4506 return FAIL;
4507 }
4508 else
4509 {
4510 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004511 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4512 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 if (type == TYPE_NEQUAL)
4514 n1 = !n1;
4515 }
4516 }
4517
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004518 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4519 {
4520 if (type_is)
4521 {
4522 n1 = (rettv->v_type == var2.v_type
4523 && rettv->vval.v_dict == var2.vval.v_dict);
4524 if (type == TYPE_NEQUAL)
4525 n1 = !n1;
4526 }
4527 else if (rettv->v_type != var2.v_type
4528 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4529 {
4530 if (rettv->v_type != var2.v_type)
4531 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4532 else
4533 EMSG(_("E736: Invalid operation for Dictionary"));
4534 clear_tv(rettv);
4535 clear_tv(&var2);
4536 return FAIL;
4537 }
4538 else
4539 {
4540 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004541 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4542 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004543 if (type == TYPE_NEQUAL)
4544 n1 = !n1;
4545 }
4546 }
4547
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004548 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4549 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 {
4551 if (rettv->v_type != var2.v_type
4552 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4553 {
4554 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004555 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004556 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004557 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004558 clear_tv(rettv);
4559 clear_tv(&var2);
4560 return FAIL;
4561 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004562 else if (rettv->v_type == VAR_PARTIAL)
4563 {
4564 /* Partials are only equal when identical. */
4565 n1 = rettv->vval.v_partial != NULL
4566 && rettv->vval.v_partial == var2.vval.v_partial;
4567 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 else
4569 {
4570 /* Compare two Funcrefs for being equal or unequal. */
4571 if (rettv->vval.v_string == NULL
4572 || var2.vval.v_string == NULL)
4573 n1 = FALSE;
4574 else
4575 n1 = STRCMP(rettv->vval.v_string,
4576 var2.vval.v_string) == 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004577 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004578 if (type == TYPE_NEQUAL)
4579 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004580 }
4581
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004582#ifdef FEAT_FLOAT
4583 /*
4584 * If one of the two variables is a float, compare as a float.
4585 * When using "=~" or "!~", always compare as string.
4586 */
4587 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4588 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4589 {
4590 float_T f1, f2;
4591
4592 if (rettv->v_type == VAR_FLOAT)
4593 f1 = rettv->vval.v_float;
4594 else
4595 f1 = get_tv_number(rettv);
4596 if (var2.v_type == VAR_FLOAT)
4597 f2 = var2.vval.v_float;
4598 else
4599 f2 = get_tv_number(&var2);
4600 n1 = FALSE;
4601 switch (type)
4602 {
4603 case TYPE_EQUAL: n1 = (f1 == f2); break;
4604 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4605 case TYPE_GREATER: n1 = (f1 > f2); break;
4606 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4607 case TYPE_SMALLER: n1 = (f1 < f2); break;
4608 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4609 case TYPE_UNKNOWN:
4610 case TYPE_MATCH:
4611 case TYPE_NOMATCH: break; /* avoid gcc warning */
4612 }
4613 }
4614#endif
4615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 /*
4617 * If one of the two variables is a number, compare as a number.
4618 * When using "=~" or "!~", always compare as string.
4619 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004620 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4622 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623 n1 = get_tv_number(rettv);
4624 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 switch (type)
4626 {
4627 case TYPE_EQUAL: n1 = (n1 == n2); break;
4628 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4629 case TYPE_GREATER: n1 = (n1 > n2); break;
4630 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4631 case TYPE_SMALLER: n1 = (n1 < n2); break;
4632 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4633 case TYPE_UNKNOWN:
4634 case TYPE_MATCH:
4635 case TYPE_NOMATCH: break; /* avoid gcc warning */
4636 }
4637 }
4638 else
4639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 s1 = get_tv_string_buf(rettv, buf1);
4641 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4643 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4644 else
4645 i = 0;
4646 n1 = FALSE;
4647 switch (type)
4648 {
4649 case TYPE_EQUAL: n1 = (i == 0); break;
4650 case TYPE_NEQUAL: n1 = (i != 0); break;
4651 case TYPE_GREATER: n1 = (i > 0); break;
4652 case TYPE_GEQUAL: n1 = (i >= 0); break;
4653 case TYPE_SMALLER: n1 = (i < 0); break;
4654 case TYPE_SEQUAL: n1 = (i <= 0); break;
4655
4656 case TYPE_MATCH:
4657 case TYPE_NOMATCH:
4658 /* avoid 'l' flag in 'cpoptions' */
4659 save_cpo = p_cpo;
4660 p_cpo = (char_u *)"";
4661 regmatch.regprog = vim_regcomp(s2,
4662 RE_MAGIC + RE_STRING);
4663 regmatch.rm_ic = ic;
4664 if (regmatch.regprog != NULL)
4665 {
4666 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004667 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (type == TYPE_NOMATCH)
4669 n1 = !n1;
4670 }
4671 p_cpo = save_cpo;
4672 break;
4673
4674 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4675 }
4676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 clear_tv(rettv);
4678 clear_tv(&var2);
4679 rettv->v_type = VAR_NUMBER;
4680 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682 }
4683
4684 return OK;
4685}
4686
4687/*
4688 * Handle fourth level expression:
4689 * + number addition
4690 * - number subtraction
4691 * . string concatenation
4692 *
4693 * "arg" must point to the first non-white of the expression.
4694 * "arg" is advanced to the next non-white after the recognized expression.
4695 *
4696 * Return OK or FAIL.
4697 */
4698 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004699eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700{
Bram Moolenaar33570922005-01-25 22:26:29 +00004701 typval_T var2;
4702 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 int op;
4704 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705#ifdef FEAT_FLOAT
4706 float_T f1 = 0, f2 = 0;
4707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 char_u *s1, *s2;
4709 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4710 char_u *p;
4711
4712 /*
4713 * Get the first variable.
4714 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004715 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return FAIL;
4717
4718 /*
4719 * Repeat computing, until no '+', '-' or '.' is following.
4720 */
4721 for (;;)
4722 {
4723 op = **arg;
4724 if (op != '+' && op != '-' && op != '.')
4725 break;
4726
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004727 if ((op != '+' || rettv->v_type != VAR_LIST)
4728#ifdef FEAT_FLOAT
4729 && (op == '.' || rettv->v_type != VAR_FLOAT)
4730#endif
4731 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004732 {
4733 /* For "list + ...", an illegal use of the first operand as
4734 * a number cannot be determined before evaluating the 2nd
4735 * operand: if this is also a list, all is ok.
4736 * For "something . ...", "something - ..." or "non-list + ...",
4737 * we know that the first operand needs to be a string or number
4738 * without evaluating the 2nd operand. So check before to avoid
4739 * side effects after an error. */
4740 if (evaluate && get_tv_string_chk(rettv) == NULL)
4741 {
4742 clear_tv(rettv);
4743 return FAIL;
4744 }
4745 }
4746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 /*
4748 * Get the second variable.
4749 */
4750 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004751 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004753 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 return FAIL;
4755 }
4756
4757 if (evaluate)
4758 {
4759 /*
4760 * Compute the result.
4761 */
4762 if (op == '.')
4763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4765 s2 = get_tv_string_buf_chk(&var2, buf2);
4766 if (s2 == NULL) /* type error ? */
4767 {
4768 clear_tv(rettv);
4769 clear_tv(&var2);
4770 return FAIL;
4771 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004772 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004773 clear_tv(rettv);
4774 rettv->v_type = VAR_STRING;
4775 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004777 else if (op == '+' && rettv->v_type == VAR_LIST
4778 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004779 {
4780 /* concatenate Lists */
4781 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4782 &var3) == FAIL)
4783 {
4784 clear_tv(rettv);
4785 clear_tv(&var2);
4786 return FAIL;
4787 }
4788 clear_tv(rettv);
4789 *rettv = var3;
4790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 else
4792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 int error = FALSE;
4794
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795#ifdef FEAT_FLOAT
4796 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004798 f1 = rettv->vval.v_float;
4799 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 else
4802#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004803 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804 n1 = get_tv_number_chk(rettv, &error);
4805 if (error)
4806 {
4807 /* This can only happen for "list + non-list". For
4808 * "non-list + ..." or "something - ...", we returned
4809 * before evaluating the 2nd operand. */
4810 clear_tv(rettv);
4811 return FAIL;
4812 }
4813#ifdef FEAT_FLOAT
4814 if (var2.v_type == VAR_FLOAT)
4815 f1 = n1;
4816#endif
4817 }
4818#ifdef FEAT_FLOAT
4819 if (var2.v_type == VAR_FLOAT)
4820 {
4821 f2 = var2.vval.v_float;
4822 n2 = 0;
4823 }
4824 else
4825#endif
4826 {
4827 n2 = get_tv_number_chk(&var2, &error);
4828 if (error)
4829 {
4830 clear_tv(rettv);
4831 clear_tv(&var2);
4832 return FAIL;
4833 }
4834#ifdef FEAT_FLOAT
4835 if (rettv->v_type == VAR_FLOAT)
4836 f2 = n2;
4837#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840
4841#ifdef FEAT_FLOAT
4842 /* If there is a float on either side the result is a float. */
4843 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4844 {
4845 if (op == '+')
4846 f1 = f1 + f2;
4847 else
4848 f1 = f1 - f2;
4849 rettv->v_type = VAR_FLOAT;
4850 rettv->vval.v_float = f1;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004853#endif
4854 {
4855 if (op == '+')
4856 n1 = n1 + n2;
4857 else
4858 n1 = n1 - n2;
4859 rettv->v_type = VAR_NUMBER;
4860 rettv->vval.v_number = n1;
4861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
4865 }
4866 return OK;
4867}
4868
4869/*
4870 * Handle fifth level expression:
4871 * * number multiplication
4872 * / number division
4873 * % number modulo
4874 *
4875 * "arg" must point to the first non-white of the expression.
4876 * "arg" is advanced to the next non-white after the recognized expression.
4877 *
4878 * Return OK or FAIL.
4879 */
4880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004881eval6(
4882 char_u **arg,
4883 typval_T *rettv,
4884 int evaluate,
4885 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886{
Bram Moolenaar33570922005-01-25 22:26:29 +00004887 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 int op;
4889 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890#ifdef FEAT_FLOAT
4891 int use_float = FALSE;
4892 float_T f1 = 0, f2;
4893#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004894 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896 /*
4897 * Get the first variable.
4898 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004899 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 return FAIL;
4901
4902 /*
4903 * Repeat computing, until no '*', '/' or '%' is following.
4904 */
4905 for (;;)
4906 {
4907 op = **arg;
4908 if (op != '*' && op != '/' && op != '%')
4909 break;
4910
4911 if (evaluate)
4912 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913#ifdef FEAT_FLOAT
4914 if (rettv->v_type == VAR_FLOAT)
4915 {
4916 f1 = rettv->vval.v_float;
4917 use_float = TRUE;
4918 n1 = 0;
4919 }
4920 else
4921#endif
4922 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004924 if (error)
4925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927 else
4928 n1 = 0;
4929
4930 /*
4931 * Get the second variable.
4932 */
4933 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004934 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 return FAIL;
4936
4937 if (evaluate)
4938 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939#ifdef FEAT_FLOAT
4940 if (var2.v_type == VAR_FLOAT)
4941 {
4942 if (!use_float)
4943 {
4944 f1 = n1;
4945 use_float = TRUE;
4946 }
4947 f2 = var2.vval.v_float;
4948 n2 = 0;
4949 }
4950 else
4951#endif
4952 {
4953 n2 = get_tv_number_chk(&var2, &error);
4954 clear_tv(&var2);
4955 if (error)
4956 return FAIL;
4957#ifdef FEAT_FLOAT
4958 if (use_float)
4959 f2 = n2;
4960#endif
4961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
4963 /*
4964 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004967#ifdef FEAT_FLOAT
4968 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 if (op == '*')
4971 f1 = f1 * f2;
4972 else if (op == '/')
4973 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004974# ifdef VMS
4975 /* VMS crashes on divide by zero, work around it */
4976 if (f2 == 0.0)
4977 {
4978 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004979 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004980 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004981 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004983 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004984 }
4985 else
4986 f1 = f1 / f2;
4987# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 /* We rely on the floating point library to handle divide
4989 * by zero to result in "inf" and not a crash. */
4990 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004991# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004995 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 return FAIL;
4997 }
4998 rettv->v_type = VAR_FLOAT;
4999 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
5001 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 if (op == '*')
5005 n1 = n1 * n2;
5006 else if (op == '/')
5007 {
5008 if (n2 == 0) /* give an error message? */
5009 {
5010 if (n1 == 0)
5011 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5012 else if (n1 < 0)
5013 n1 = -0x7fffffffL;
5014 else
5015 n1 = 0x7fffffffL;
5016 }
5017 else
5018 n1 = n1 / n2;
5019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005021 {
5022 if (n2 == 0) /* give an error message? */
5023 n1 = 0;
5024 else
5025 n1 = n1 % n2;
5026 }
5027 rettv->v_type = VAR_NUMBER;
5028 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031 }
5032
5033 return OK;
5034}
5035
5036/*
5037 * Handle sixth level expression:
5038 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005039 * "string" string constant
5040 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 * &option-name option value
5042 * @r register contents
5043 * identifier variable value
5044 * function() function call
5045 * $VAR environment variable
5046 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005047 * [expr, expr] List
5048 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 *
5050 * Also handle:
5051 * ! in front logical NOT
5052 * - in front unary minus
5053 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 * trailing [] subscript in String or List
5055 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 *
5057 * "arg" must point to the first non-white of the expression.
5058 * "arg" is advanced to the next non-white after the recognized expression.
5059 *
5060 * Return OK or FAIL.
5061 */
5062 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005063eval7(
5064 char_u **arg,
5065 typval_T *rettv,
5066 int evaluate,
5067 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 long n;
5070 int len;
5071 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 char_u *start_leader, *end_leader;
5073 int ret = OK;
5074 char_u *alias;
5075
5076 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005078 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005080 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
5082 /*
5083 * Skip '!' and '-' characters. They are handled later.
5084 */
5085 start_leader = *arg;
5086 while (**arg == '!' || **arg == '-' || **arg == '+')
5087 *arg = skipwhite(*arg + 1);
5088 end_leader = *arg;
5089
5090 switch (**arg)
5091 {
5092 /*
5093 * Number constant.
5094 */
5095 case '0':
5096 case '1':
5097 case '2':
5098 case '3':
5099 case '4':
5100 case '5':
5101 case '6':
5102 case '7':
5103 case '8':
5104 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 {
5106#ifdef FEAT_FLOAT
5107 char_u *p = skipdigits(*arg + 1);
5108 int get_float = FALSE;
5109
5110 /* We accept a float when the format matches
5111 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005112 * strict to avoid backwards compatibility problems.
5113 * Don't look for a float after the "." operator, so that
5114 * ":let vers = 1.2.3" doesn't fail. */
5115 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005117 get_float = TRUE;
5118 p = skipdigits(p + 2);
5119 if (*p == 'e' || *p == 'E')
5120 {
5121 ++p;
5122 if (*p == '-' || *p == '+')
5123 ++p;
5124 if (!vim_isdigit(*p))
5125 get_float = FALSE;
5126 else
5127 p = skipdigits(p + 1);
5128 }
5129 if (ASCII_ISALPHA(*p) || *p == '.')
5130 get_float = FALSE;
5131 }
5132 if (get_float)
5133 {
5134 float_T f;
5135
5136 *arg += string2float(*arg, &f);
5137 if (evaluate)
5138 {
5139 rettv->v_type = VAR_FLOAT;
5140 rettv->vval.v_float = f;
5141 }
5142 }
5143 else
5144#endif
5145 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005146 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 *arg += len;
5148 if (evaluate)
5149 {
5150 rettv->v_type = VAR_NUMBER;
5151 rettv->vval.v_number = n;
5152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
5157 /*
5158 * String constant: "string".
5159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005166 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005167 break;
5168
5169 /*
5170 * List: [expr, expr]
5171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005176 * Dictionary: {key: val, key: val}
5177 */
5178 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5179 break;
5180
5181 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005182 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005184 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 break;
5186
5187 /*
5188 * Environment variable: $VAR.
5189 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 break;
5192
5193 /*
5194 * Register contents: @r.
5195 */
5196 case '@': ++*arg;
5197 if (evaluate)
5198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005200 rettv->vval.v_string = get_reg_contents(**arg,
5201 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203 if (**arg != NUL)
5204 ++*arg;
5205 break;
5206
5207 /*
5208 * nested expression: (expression).
5209 */
5210 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005211 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (**arg == ')')
5213 ++*arg;
5214 else if (ret == OK)
5215 {
5216 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005217 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 ret = FAIL;
5219 }
5220 break;
5221
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 break;
5224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225
5226 if (ret == NOTDONE)
5227 {
5228 /*
5229 * Must be a variable or function name.
5230 * Can also be a curly-braces kind of name: {expr}.
5231 */
5232 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 if (alias != NULL)
5235 s = alias;
5236
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005237 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 ret = FAIL;
5239 else
5240 {
5241 if (**arg == '(') /* recursive! */
5242 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005243 partial_T *partial;
5244
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 /* If "s" is the name of a variable of type VAR_FUNC
5246 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005247 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248
5249 /* Invoke the function. */
5250 ret = get_func_tv(s, len, rettv, arg,
5251 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005252 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005253
5254 /* If evaluate is FALSE rettv->v_type was not set in
5255 * get_func_tv, but it's needed in handle_subscript() to parse
5256 * what follows. So set it here. */
5257 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5258 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005259 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005260 rettv->v_type = VAR_FUNC;
5261 }
5262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 /* Stop the expression evaluation when immediately
5264 * aborting on error, or when an interrupt occurred or
5265 * an exception was thrown but not caught. */
5266 if (aborting())
5267 {
5268 if (ret == OK)
5269 clear_tv(rettv);
5270 ret = FAIL;
5271 }
5272 }
5273 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005274 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005275 else
5276 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005278 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 }
5280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 *arg = skipwhite(*arg);
5282
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005283 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5284 * expr(expr). */
5285 if (ret == OK)
5286 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 /*
5289 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5290 */
5291 if (ret == OK && evaluate && end_leader > start_leader)
5292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005294 int val = 0;
5295#ifdef FEAT_FLOAT
5296 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005298 if (rettv->v_type == VAR_FLOAT)
5299 f = rettv->vval.v_float;
5300 else
5301#endif
5302 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005305 clear_tv(rettv);
5306 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 else
5309 {
5310 while (end_leader > start_leader)
5311 {
5312 --end_leader;
5313 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005314 {
5315#ifdef FEAT_FLOAT
5316 if (rettv->v_type == VAR_FLOAT)
5317 f = !f;
5318 else
5319#endif
5320 val = !val;
5321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005323 {
5324#ifdef FEAT_FLOAT
5325 if (rettv->v_type == VAR_FLOAT)
5326 f = -f;
5327 else
5328#endif
5329 val = -val;
5330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005332#ifdef FEAT_FLOAT
5333 if (rettv->v_type == VAR_FLOAT)
5334 {
5335 clear_tv(rettv);
5336 rettv->vval.v_float = f;
5337 }
5338 else
5339#endif
5340 {
5341 clear_tv(rettv);
5342 rettv->v_type = VAR_NUMBER;
5343 rettv->vval.v_number = val;
5344 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
5347
5348 return ret;
5349}
5350
5351/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005352 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5353 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5355 */
5356 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005357eval_index(
5358 char_u **arg,
5359 typval_T *rettv,
5360 int evaluate,
5361 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362{
5363 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005364 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366 long len = -1;
5367 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005369 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370
Bram Moolenaara03f2332016-02-06 18:09:59 +01005371 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005373 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005374 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005375 if (verbose)
5376 EMSG(_("E695: Cannot index a Funcref"));
5377 return FAIL;
5378 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005379#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005380 if (verbose)
5381 EMSG(_(e_float_as_string));
5382 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005383#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005384 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005385 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005386 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005387 if (verbose)
5388 EMSG(_("E909: Cannot index a special variable"));
5389 return FAIL;
5390 case VAR_UNKNOWN:
5391 if (evaluate)
5392 return FAIL;
5393 /* FALLTHROUGH */
5394
5395 case VAR_STRING:
5396 case VAR_NUMBER:
5397 case VAR_LIST:
5398 case VAR_DICT:
5399 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005400 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005402 init_tv(&var1);
5403 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 /*
5407 * dict.name
5408 */
5409 key = *arg + 1;
5410 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5411 ;
5412 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005414 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 }
5416 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005418 /*
5419 * something[idx]
5420 *
5421 * Get the (first) variable from inside the [].
5422 */
5423 *arg = skipwhite(*arg + 1);
5424 if (**arg == ':')
5425 empty1 = TRUE;
5426 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5427 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005428 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5429 {
5430 /* not a number or string */
5431 clear_tv(&var1);
5432 return FAIL;
5433 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434
5435 /*
5436 * Get the second variable from inside the [:].
5437 */
5438 if (**arg == ':')
5439 {
5440 range = TRUE;
5441 *arg = skipwhite(*arg + 1);
5442 if (**arg == ']')
5443 empty2 = TRUE;
5444 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005446 if (!empty1)
5447 clear_tv(&var1);
5448 return FAIL;
5449 }
5450 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5451 {
5452 /* not a number or string */
5453 if (!empty1)
5454 clear_tv(&var1);
5455 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 return FAIL;
5457 }
5458 }
5459
5460 /* Check for the ']'. */
5461 if (**arg != ']')
5462 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005463 if (verbose)
5464 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465 clear_tv(&var1);
5466 if (range)
5467 clear_tv(&var2);
5468 return FAIL;
5469 }
5470 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 }
5472
5473 if (evaluate)
5474 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475 n1 = 0;
5476 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 n1 = get_tv_number(&var1);
5479 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 }
5481 if (range)
5482 {
5483 if (empty2)
5484 n2 = -1;
5485 else
5486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 n2 = get_tv_number(&var2);
5488 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 }
5491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005494 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005495 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005496 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005497 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005498 case VAR_SPECIAL:
5499 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005500 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005501 break; /* not evaluating, skipping over subscript */
5502
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 case VAR_NUMBER:
5504 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005506 len = (long)STRLEN(s);
5507 if (range)
5508 {
5509 /* The resulting variable is a substring. If the indexes
5510 * are out of range the result is empty. */
5511 if (n1 < 0)
5512 {
5513 n1 = len + n1;
5514 if (n1 < 0)
5515 n1 = 0;
5516 }
5517 if (n2 < 0)
5518 n2 = len + n2;
5519 else if (n2 >= len)
5520 n2 = len;
5521 if (n1 >= len || n2 < 0 || n1 > n2)
5522 s = NULL;
5523 else
5524 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5525 }
5526 else
5527 {
5528 /* The resulting variable is a string of a single
5529 * character. If the index is too big or negative the
5530 * result is empty. */
5531 if (n1 >= len || n1 < 0)
5532 s = NULL;
5533 else
5534 s = vim_strnsave(s + n1, 1);
5535 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005536 clear_tv(rettv);
5537 rettv->v_type = VAR_STRING;
5538 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005539 break;
5540
5541 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 if (n1 < 0)
5544 n1 = len + n1;
5545 if (!empty1 && (n1 < 0 || n1 >= len))
5546 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005547 /* For a range we allow invalid values and return an empty
5548 * list. A list index out of range is an error. */
5549 if (!range)
5550 {
5551 if (verbose)
5552 EMSGN(_(e_listidx), n1);
5553 return FAIL;
5554 }
5555 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 }
5557 if (range)
5558 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005559 list_T *l;
5560 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561
5562 if (n2 < 0)
5563 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005564 else if (n2 >= len)
5565 n2 = len - 1;
5566 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005567 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 l = list_alloc();
5569 if (l == NULL)
5570 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 n1 <= n2; ++n1)
5573 {
5574 if (list_append_tv(l, &item->li_tv) == FAIL)
5575 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005576 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 return FAIL;
5578 }
5579 item = item->li_next;
5580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 clear_tv(rettv);
5582 rettv->v_type = VAR_LIST;
5583 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005584 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 }
5586 else
5587 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005588 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 clear_tv(rettv);
5590 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 }
5592 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593
5594 case VAR_DICT:
5595 if (range)
5596 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005597 if (verbose)
5598 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 if (len == -1)
5600 clear_tv(&var1);
5601 return FAIL;
5602 }
5603 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005604 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605
5606 if (len == -1)
5607 {
5608 key = get_tv_string(&var1);
5609 if (*key == NUL)
5610 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005611 if (verbose)
5612 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613 clear_tv(&var1);
5614 return FAIL;
5615 }
5616 }
5617
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005618 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005620 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005621 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 if (len == -1)
5623 clear_tv(&var1);
5624 if (item == NULL)
5625 return FAIL;
5626
5627 copy_tv(&item->di_tv, &var1);
5628 clear_tv(rettv);
5629 *rettv = var1;
5630 }
5631 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 }
5633 }
5634
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 return OK;
5636}
5637
5638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 * Get an option value.
5640 * "arg" points to the '&' or '+' before the option name.
5641 * "arg" is advanced to character after the option name.
5642 * Return OK or FAIL.
5643 */
5644 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005645get_option_tv(
5646 char_u **arg,
5647 typval_T *rettv, /* when NULL, only check if option exists */
5648 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 char_u *option_end;
5651 long numval;
5652 char_u *stringval;
5653 int opt_type;
5654 int c;
5655 int working = (**arg == '+'); /* has("+option") */
5656 int ret = OK;
5657 int opt_flags;
5658
5659 /*
5660 * Isolate the option name and find its value.
5661 */
5662 option_end = find_option_end(arg, &opt_flags);
5663 if (option_end == NULL)
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 EMSG2(_("E112: Option name missing: %s"), *arg);
5667 return FAIL;
5668 }
5669
5670 if (!evaluate)
5671 {
5672 *arg = option_end;
5673 return OK;
5674 }
5675
5676 c = *option_end;
5677 *option_end = NUL;
5678 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
5681 if (opt_type == -3) /* invalid name */
5682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 EMSG2(_("E113: Unknown option: %s"), *arg);
5685 ret = FAIL;
5686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 {
5689 if (opt_type == -2) /* hidden string option */
5690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005691 rettv->v_type = VAR_STRING;
5692 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
5694 else if (opt_type == -1) /* hidden number option */
5695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696 rettv->v_type = VAR_NUMBER;
5697 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699 else if (opt_type == 1) /* number option */
5700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701 rettv->v_type = VAR_NUMBER;
5702 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
5704 else /* string option */
5705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005706 rettv->v_type = VAR_STRING;
5707 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
5709 }
5710 else if (working && (opt_type == -2 || opt_type == -1))
5711 ret = FAIL;
5712
5713 *option_end = c; /* put back for error messages */
5714 *arg = option_end;
5715
5716 return ret;
5717}
5718
5719/*
5720 * Allocate a variable for a string constant.
5721 * Return OK or FAIL.
5722 */
5723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005724get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
5726 char_u *p;
5727 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 int extra = 0;
5729
5730 /*
5731 * Find the end of the string, skipping backslashed characters.
5732 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005733 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {
5735 if (*p == '\\' && p[1] != NUL)
5736 {
5737 ++p;
5738 /* A "\<x>" form occupies at least 4 characters, and produces up
5739 * to 6 characters: reserve space for 2 extra */
5740 if (*p == '<')
5741 extra += 2;
5742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 }
5744
5745 if (*p != '"')
5746 {
5747 EMSG2(_("E114: Missing quote: %s"), *arg);
5748 return FAIL;
5749 }
5750
5751 /* If only parsing, set *arg and return here */
5752 if (!evaluate)
5753 {
5754 *arg = p + 1;
5755 return OK;
5756 }
5757
5758 /*
5759 * Copy the string into allocated memory, handling backslashed
5760 * characters.
5761 */
5762 name = alloc((unsigned)(p - *arg + extra));
5763 if (name == NULL)
5764 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 rettv->v_type = VAR_STRING;
5766 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
5770 if (*p == '\\')
5771 {
5772 switch (*++p)
5773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 case 'b': *name++ = BS; ++p; break;
5775 case 'e': *name++ = ESC; ++p; break;
5776 case 'f': *name++ = FF; ++p; break;
5777 case 'n': *name++ = NL; ++p; break;
5778 case 'r': *name++ = CAR; ++p; break;
5779 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781 case 'X': /* hex: "\x1", "\x12" */
5782 case 'x':
5783 case 'u': /* Unicode: "\u0023" */
5784 case 'U':
5785 if (vim_isxdigit(p[1]))
5786 {
5787 int n, nr;
5788 int c = toupper(*p);
5789
5790 if (c == 'X')
5791 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005792 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005794 else
5795 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 nr = 0;
5797 while (--n >= 0 && vim_isxdigit(p[1]))
5798 {
5799 ++p;
5800 nr = (nr << 4) + hex2nr(*p);
5801 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803#ifdef FEAT_MBYTE
5804 /* For "\u" store the number according to
5805 * 'encoding'. */
5806 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 else
5809#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 break;
5813
5814 /* octal: "\1", "\12", "\123" */
5815 case '0':
5816 case '1':
5817 case '2':
5818 case '3':
5819 case '4':
5820 case '5':
5821 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 case '7': *name = *p++ - '0';
5823 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 *name = (*name << 3) + *p++ - '0';
5826 if (*p >= '0' && *p <= '7')
5827 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831
5832 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (extra != 0)
5835 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 break;
5838 }
5839 /* FALLTHROUGH */
5840
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 break;
5843 }
5844 }
5845 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 *arg = p + 1;
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 return OK;
5853}
5854
5855/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 * Return OK or FAIL.
5858 */
5859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005860get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
5862 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005864 int reduce = 0;
5865
5866 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 */
5869 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 break;
5875 ++reduce;
5876 ++p;
5877 }
5878 }
5879
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 return FAIL;
5884 }
5885
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 if (!evaluate)
5888 {
5889 *arg = p + 1;
5890 return OK;
5891 }
5892
5893 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 */
5896 str = alloc((unsigned)((p - *arg) - reduce));
5897 if (str == NULL)
5898 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 rettv->v_type = VAR_STRING;
5900 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901
Bram Moolenaar8c711452005-01-14 21:53:12 +00005902 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005905 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 break;
5908 ++p;
5909 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 *arg = p + 1;
5914
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005915 return OK;
5916}
5917
5918/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 * Allocate a variable for a List and fill it from "*arg".
5920 * Return OK or FAIL.
5921 */
5922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005923get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924{
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 list_T *l = NULL;
5926 typval_T tv;
5927 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928
5929 if (evaluate)
5930 {
5931 l = list_alloc();
5932 if (l == NULL)
5933 return FAIL;
5934 }
5935
5936 *arg = skipwhite(*arg + 1);
5937 while (**arg != ']' && **arg != NUL)
5938 {
5939 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5940 goto failret;
5941 if (evaluate)
5942 {
5943 item = listitem_alloc();
5944 if (item != NULL)
5945 {
5946 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005947 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 list_append(l, item);
5949 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005950 else
5951 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 }
5953
5954 if (**arg == ']')
5955 break;
5956 if (**arg != ',')
5957 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005958 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 goto failret;
5960 }
5961 *arg = skipwhite(*arg + 1);
5962 }
5963
5964 if (**arg != ']')
5965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005966 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967failret:
5968 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005969 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 return FAIL;
5971 }
5972
5973 *arg = skipwhite(*arg + 1);
5974 if (evaluate)
5975 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005976 rettv->v_type = VAR_LIST;
5977 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978 ++l->lv_refcount;
5979 }
5980
5981 return OK;
5982}
5983
5984/*
5985 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005986 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005988 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005989list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005991 list_T *l;
5992
5993 l = (list_T *)alloc_clear(sizeof(list_T));
5994 if (l != NULL)
5995 {
5996 /* Prepend the list to the list of lists for garbage collection. */
5997 if (first_list != NULL)
5998 first_list->lv_used_prev = l;
5999 l->lv_used_prev = NULL;
6000 l->lv_used_next = first_list;
6001 first_list = l;
6002 }
6003 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004}
6005
6006/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006007 * Allocate an empty list for a return value.
6008 * Returns OK or FAIL.
6009 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006010 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006011rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006012{
6013 list_T *l = list_alloc();
6014
6015 if (l == NULL)
6016 return FAIL;
6017
6018 rettv->vval.v_list = l;
6019 rettv->v_type = VAR_LIST;
6020 ++l->lv_refcount;
6021 return OK;
6022}
6023
6024/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 * Unreference a list: decrement the reference count and free it when it
6026 * becomes zero.
6027 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006029list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006031 if (l != NULL && --l->lv_refcount <= 0)
6032 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033}
6034
6035/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006036 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 * Ignores the reference count.
6038 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006040list_free(
6041 list_T *l,
6042 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043{
Bram Moolenaar33570922005-01-25 22:26:29 +00006044 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006046 /* Remove the list from the list of lists for garbage collection. */
6047 if (l->lv_used_prev == NULL)
6048 first_list = l->lv_used_next;
6049 else
6050 l->lv_used_prev->lv_used_next = l->lv_used_next;
6051 if (l->lv_used_next != NULL)
6052 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6053
Bram Moolenaard9fba312005-06-26 22:34:35 +00006054 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006056 /* Remove the item before deleting it. */
6057 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006058 if (recurse || (item->li_tv.v_type != VAR_LIST
6059 && item->li_tv.v_type != VAR_DICT))
6060 clear_tv(&item->li_tv);
6061 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 }
6063 vim_free(l);
6064}
6065
6066/*
6067 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006068 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006070 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006071listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072{
Bram Moolenaar33570922005-01-25 22:26:29 +00006073 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074}
6075
6076/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006077 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006082 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 vim_free(item);
6084}
6085
6086/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006087 * Remove a list item from a List and free it. Also clears the value.
6088 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006090listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006091{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006092 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006093 listitem_free(item);
6094}
6095
6096/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 * Get the number of items in a list.
6098 */
6099 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006101{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 if (l == NULL)
6103 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006104 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006105}
6106
6107/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108 * Return TRUE when two lists have exactly the same values.
6109 */
6110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006111list_equal(
6112 list_T *l1,
6113 list_T *l2,
6114 int ic, /* ignore case for strings */
6115 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006116{
Bram Moolenaar33570922005-01-25 22:26:29 +00006117 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006119 if (l1 == NULL || l2 == NULL)
6120 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006121 if (l1 == l2)
6122 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006123 if (list_len(l1) != list_len(l2))
6124 return FALSE;
6125
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126 for (item1 = l1->lv_first, item2 = l2->lv_first;
6127 item1 != NULL && item2 != NULL;
6128 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130 return FALSE;
6131 return item1 == NULL && item2 == NULL;
6132}
6133
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006134/*
6135 * Return the dictitem that an entry in a hashtable points to.
6136 */
6137 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006138dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006139{
6140 return HI2DI(hi);
6141}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006142
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144 * Return TRUE when two dictionaries have exactly the same key/values.
6145 */
6146 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006147dict_equal(
6148 dict_T *d1,
6149 dict_T *d2,
6150 int ic, /* ignore case for strings */
6151 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006152{
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 hashitem_T *hi;
6154 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006156
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006157 if (d1 == NULL || d2 == NULL)
6158 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006159 if (d1 == d2)
6160 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006161 if (dict_len(d1) != dict_len(d2))
6162 return FALSE;
6163
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006164 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006166 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006167 if (!HASHITEM_EMPTY(hi))
6168 {
6169 item2 = dict_find(d2, hi->hi_key, -1);
6170 if (item2 == NULL)
6171 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006172 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006173 return FALSE;
6174 --todo;
6175 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006176 }
6177 return TRUE;
6178}
6179
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006180static int tv_equal_recurse_limit;
6181
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006182/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006184 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006185 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186 */
6187 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006188tv_equal(
6189 typval_T *tv1,
6190 typval_T *tv2,
6191 int ic, /* ignore case */
6192 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193{
6194 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006195 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006197 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006198
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006199 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006200 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006202 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203 * recursiveness to a limit. We guess they are equal then.
6204 * A fixed limit has the problem of still taking an awful long time.
6205 * Reduce the limit every time running into it. That should work fine for
6206 * deeply linked structures that are not recursively linked and catch
6207 * recursiveness quickly. */
6208 if (!recursive)
6209 tv_equal_recurse_limit = 1000;
6210 if (recursive_cnt >= tv_equal_recurse_limit)
6211 {
6212 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006213 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006214 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006215
6216 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006217 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006218 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006219 ++recursive_cnt;
6220 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6221 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006222 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006223
6224 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006225 ++recursive_cnt;
6226 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6227 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006228 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006229
6230 case VAR_FUNC:
6231 return (tv1->vval.v_string != NULL
6232 && tv2->vval.v_string != NULL
6233 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6234
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006235 case VAR_PARTIAL:
6236 return tv1->vval.v_partial != NULL
6237 && tv1->vval.v_partial == tv2->vval.v_partial;
6238
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006239 case VAR_NUMBER:
6240 return tv1->vval.v_number == tv2->vval.v_number;
6241
6242 case VAR_STRING:
6243 s1 = get_tv_string_buf(tv1, buf1);
6244 s2 = get_tv_string_buf(tv2, buf2);
6245 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006246
6247 case VAR_SPECIAL:
6248 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006249
6250 case VAR_FLOAT:
6251#ifdef FEAT_FLOAT
6252 return tv1->vval.v_float == tv2->vval.v_float;
6253#endif
6254 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006255#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006256 return tv1->vval.v_job == tv2->vval.v_job;
6257#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006258 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006259#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006260 return tv1->vval.v_channel == tv2->vval.v_channel;
6261#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006262 case VAR_UNKNOWN:
6263 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006264 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006265
Bram Moolenaara03f2332016-02-06 18:09:59 +01006266 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6267 * does not equal anything, not even itself. */
6268 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006269}
6270
6271/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272 * Locate item with index "n" in list "l" and return it.
6273 * A negative index is counted from the end; -1 is the last item.
6274 * Returns NULL when "n" is out of range.
6275 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006276 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006277list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278{
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006280 long idx;
6281
6282 if (l == NULL)
6283 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284
6285 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006287 n = l->lv_len + n;
6288
6289 /* Check for index out of range. */
6290 if (n < 0 || n >= l->lv_len)
6291 return NULL;
6292
6293 /* When there is a cached index may start search from there. */
6294 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006296 if (n < l->lv_idx / 2)
6297 {
6298 /* closest to the start of the list */
6299 item = l->lv_first;
6300 idx = 0;
6301 }
6302 else if (n > (l->lv_idx + l->lv_len) / 2)
6303 {
6304 /* closest to the end of the list */
6305 item = l->lv_last;
6306 idx = l->lv_len - 1;
6307 }
6308 else
6309 {
6310 /* closest to the cached index */
6311 item = l->lv_idx_item;
6312 idx = l->lv_idx;
6313 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 }
6315 else
6316 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006317 if (n < l->lv_len / 2)
6318 {
6319 /* closest to the start of the list */
6320 item = l->lv_first;
6321 idx = 0;
6322 }
6323 else
6324 {
6325 /* closest to the end of the list */
6326 item = l->lv_last;
6327 idx = l->lv_len - 1;
6328 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006330
6331 while (n > idx)
6332 {
6333 /* search forward */
6334 item = item->li_next;
6335 ++idx;
6336 }
6337 while (n < idx)
6338 {
6339 /* search backward */
6340 item = item->li_prev;
6341 --idx;
6342 }
6343
6344 /* cache the used index */
6345 l->lv_idx = idx;
6346 l->lv_idx_item = item;
6347
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 return item;
6349}
6350
6351/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006352 * Get list item "l[idx]" as a number.
6353 */
6354 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006355list_find_nr(
6356 list_T *l,
6357 long idx,
6358 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006359{
6360 listitem_T *li;
6361
6362 li = list_find(l, idx);
6363 if (li == NULL)
6364 {
6365 if (errorp != NULL)
6366 *errorp = TRUE;
6367 return -1L;
6368 }
6369 return get_tv_number_chk(&li->li_tv, errorp);
6370}
6371
6372/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006373 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6374 */
6375 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006376list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006377{
6378 listitem_T *li;
6379
6380 li = list_find(l, idx - 1);
6381 if (li == NULL)
6382 {
6383 EMSGN(_(e_listidx), idx);
6384 return NULL;
6385 }
6386 return get_tv_string(&li->li_tv);
6387}
6388
6389/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006390 * Locate "item" list "l" and return its index.
6391 * Returns -1 when "item" is not in the list.
6392 */
6393 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006394list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006395{
6396 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006398
6399 if (l == NULL)
6400 return -1;
6401 idx = 0;
6402 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6403 ++idx;
6404 if (li == NULL)
6405 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006406 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006407}
6408
6409/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410 * Append item "item" to the end of list "l".
6411 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006412 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006413list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414{
6415 if (l->lv_last == NULL)
6416 {
6417 /* empty list */
6418 l->lv_first = item;
6419 l->lv_last = item;
6420 item->li_prev = NULL;
6421 }
6422 else
6423 {
6424 l->lv_last->li_next = item;
6425 item->li_prev = l->lv_last;
6426 l->lv_last = item;
6427 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006428 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429 item->li_next = NULL;
6430}
6431
6432/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006436 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006437list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006439 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440
Bram Moolenaar05159a02005-02-26 23:04:13 +00006441 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006443 copy_tv(tv, &li->li_tv);
6444 list_append(l, li);
6445 return OK;
6446}
6447
6448/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006449 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006450 * Return FAIL when out of memory.
6451 */
6452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006453list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006454{
6455 listitem_T *li = listitem_alloc();
6456
6457 if (li == NULL)
6458 return FAIL;
6459 li->li_tv.v_type = VAR_DICT;
6460 li->li_tv.v_lock = 0;
6461 li->li_tv.vval.v_dict = dict;
6462 list_append(list, li);
6463 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006464 return OK;
6465}
6466
6467/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006468 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006469 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470 * Returns FAIL when out of memory.
6471 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006472 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006473list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006474{
6475 listitem_T *li = listitem_alloc();
6476
6477 if (li == NULL)
6478 return FAIL;
6479 list_append(l, li);
6480 li->li_tv.v_type = VAR_STRING;
6481 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006482 if (str == NULL)
6483 li->li_tv.vval.v_string = NULL;
6484 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006485 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006486 return FAIL;
6487 return OK;
6488}
6489
6490/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006491 * Append "n" to list "l".
6492 * Returns FAIL when out of memory.
6493 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006496{
6497 listitem_T *li;
6498
6499 li = listitem_alloc();
6500 if (li == NULL)
6501 return FAIL;
6502 li->li_tv.v_type = VAR_NUMBER;
6503 li->li_tv.v_lock = 0;
6504 li->li_tv.vval.v_number = n;
6505 list_append(l, li);
6506 return OK;
6507}
6508
6509/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006510 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511 * If "item" is NULL append at the end.
6512 * Return FAIL when out of memory.
6513 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006514 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006515list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516{
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518
6519 if (ni == NULL)
6520 return FAIL;
6521 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006522 list_insert(l, ni, item);
6523 return OK;
6524}
6525
6526 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006527list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006528{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 if (item == NULL)
6530 /* Append new item at end of list. */
6531 list_append(l, ni);
6532 else
6533 {
6534 /* Insert new item before existing item. */
6535 ni->li_prev = item->li_prev;
6536 ni->li_next = item;
6537 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006538 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006540 ++l->lv_idx;
6541 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006543 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006545 l->lv_idx_item = NULL;
6546 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006548 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550}
6551
6552/*
6553 * Extend "l1" with "l2".
6554 * If "bef" is NULL append at the end, otherwise insert before this item.
6555 * Returns FAIL when out of memory.
6556 */
6557 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006558list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559{
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006561 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006563 /* We also quit the loop when we have inserted the original item count of
6564 * the list, avoid a hang when we extend a list with itself. */
6565 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6567 return FAIL;
6568 return OK;
6569}
6570
6571/*
6572 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6573 * Return FAIL when out of memory.
6574 */
6575 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006576list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577{
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006580 if (l1 == NULL || l2 == NULL)
6581 return FAIL;
6582
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006584 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006585 if (l == NULL)
6586 return FAIL;
6587 tv->v_type = VAR_LIST;
6588 tv->vval.v_list = l;
6589
6590 /* append all items from the second list */
6591 return list_extend(l, l2, NULL);
6592}
6593
6594/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006595 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 * Returns NULL when out of memory.
6599 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006600 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006601list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602{
Bram Moolenaar33570922005-01-25 22:26:29 +00006603 list_T *copy;
6604 listitem_T *item;
6605 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606
6607 if (orig == NULL)
6608 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609
6610 copy = list_alloc();
6611 if (copy != NULL)
6612 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006613 if (copyID != 0)
6614 {
6615 /* Do this before adding the items, because one of the items may
6616 * refer back to this list. */
6617 orig->lv_copyID = copyID;
6618 orig->lv_copylist = copy;
6619 }
6620 for (item = orig->lv_first; item != NULL && !got_int;
6621 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 {
6623 ni = listitem_alloc();
6624 if (ni == NULL)
6625 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006626 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006627 {
6628 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6629 {
6630 vim_free(ni);
6631 break;
6632 }
6633 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006635 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 list_append(copy, ni);
6637 }
6638 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006639 if (item != NULL)
6640 {
6641 list_unref(copy);
6642 copy = NULL;
6643 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644 }
6645
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 return copy;
6647}
6648
6649/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006650 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006651 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006652 * This used to be called list_remove, but that conflicts with a Sun header
6653 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006655 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006656vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657{
Bram Moolenaar33570922005-01-25 22:26:29 +00006658 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006660 /* notify watchers */
6661 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006663 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664 list_fix_watch(l, ip);
6665 if (ip == item2)
6666 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006668
6669 if (item2->li_next == NULL)
6670 l->lv_last = item->li_prev;
6671 else
6672 item2->li_next->li_prev = item->li_prev;
6673 if (item->li_prev == NULL)
6674 l->lv_first = item2->li_next;
6675 else
6676 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006677 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678}
6679
6680/*
6681 * Return an allocated string with the string representation of a list.
6682 * May return NULL.
6683 */
6684 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006686{
6687 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688
6689 if (tv->vval.v_list == NULL)
6690 return NULL;
6691 ga_init2(&ga, (int)sizeof(char), 80);
6692 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006693 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006694 {
6695 vim_free(ga.ga_data);
6696 return NULL;
6697 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006698 ga_append(&ga, ']');
6699 ga_append(&ga, NUL);
6700 return (char_u *)ga.ga_data;
6701}
6702
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006703typedef struct join_S {
6704 char_u *s;
6705 char_u *tofree;
6706} join_T;
6707
6708 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006709list_join_inner(
6710 garray_T *gap, /* to store the result in */
6711 list_T *l,
6712 char_u *sep,
6713 int echo_style,
6714 int copyID,
6715 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006716{
6717 int i;
6718 join_T *p;
6719 int len;
6720 int sumlen = 0;
6721 int first = TRUE;
6722 char_u *tofree;
6723 char_u numbuf[NUMBUFLEN];
6724 listitem_T *item;
6725 char_u *s;
6726
6727 /* Stringify each item in the list. */
6728 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6729 {
6730 if (echo_style)
6731 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6732 else
6733 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6734 if (s == NULL)
6735 return FAIL;
6736
6737 len = (int)STRLEN(s);
6738 sumlen += len;
6739
Bram Moolenaarcde88542015-08-11 19:14:00 +02006740 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006741 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6742 if (tofree != NULL || s != numbuf)
6743 {
6744 p->s = s;
6745 p->tofree = tofree;
6746 }
6747 else
6748 {
6749 p->s = vim_strnsave(s, len);
6750 p->tofree = p->s;
6751 }
6752
6753 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006754 if (did_echo_string_emsg) /* recursion error, bail out */
6755 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006756 }
6757
6758 /* Allocate result buffer with its total size, avoid re-allocation and
6759 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6760 if (join_gap->ga_len >= 2)
6761 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6762 if (ga_grow(gap, sumlen + 2) == FAIL)
6763 return FAIL;
6764
6765 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6766 {
6767 if (first)
6768 first = FALSE;
6769 else
6770 ga_concat(gap, sep);
6771 p = ((join_T *)join_gap->ga_data) + i;
6772
6773 if (p->s != NULL)
6774 ga_concat(gap, p->s);
6775 line_breakcheck();
6776 }
6777
6778 return OK;
6779}
6780
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006783 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006784 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006785 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006786 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006787list_join(
6788 garray_T *gap,
6789 list_T *l,
6790 char_u *sep,
6791 int echo_style,
6792 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006793{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006794 garray_T join_ga;
6795 int retval;
6796 join_T *p;
6797 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798
Bram Moolenaard39a7512015-04-16 22:51:22 +02006799 if (l->lv_len < 1)
6800 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006801 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6802 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6803
6804 /* Dispose each item in join_ga. */
6805 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006807 p = (join_T *)join_ga.ga_data;
6808 for (i = 0; i < join_ga.ga_len; ++i)
6809 {
6810 vim_free(p->tofree);
6811 ++p;
6812 }
6813 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006815
6816 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006817}
6818
6819/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006820 * Return the next (unique) copy ID.
6821 * Used for serializing nested structures.
6822 */
6823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006824get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006825{
6826 current_copyID += COPYID_INC;
6827 return current_copyID;
6828}
6829
6830/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831 * Garbage collection for lists and dictionaries.
6832 *
6833 * We use reference counts to be able to free most items right away when they
6834 * are no longer used. But for composite items it's possible that it becomes
6835 * unused while the reference count is > 0: When there is a recursive
6836 * reference. Example:
6837 * :let l = [1, 2, 3]
6838 * :let d = {9: l}
6839 * :let l[1] = d
6840 *
6841 * Since this is quite unusual we handle this with garbage collection: every
6842 * once in a while find out which lists and dicts are not referenced from any
6843 * variable.
6844 *
6845 * Here is a good reference text about garbage collection (refers to Python
6846 * but it applies to all reference-counting mechanisms):
6847 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006848 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849
6850/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 * Do garbage collection for lists and dicts.
6852 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006853 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006855garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006858 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 buf_T *buf;
6860 win_T *wp;
6861 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006862 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006863 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006865#ifdef FEAT_WINDOWS
6866 tabpage_T *tp;
6867#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006869 /* Only do this once. */
6870 want_garbage_collect = FALSE;
6871 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006872 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006873
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 /* We advance by two because we add one for items referenced through
6875 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006876 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006877
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878 /*
6879 * 1. Go through all accessible variables and mark all lists and dicts
6880 * with copyID.
6881 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006882
6883 /* Don't free variables in the previous_funccal list unless they are only
6884 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006885 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6887 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6889 NULL);
6890 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6891 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006892 }
6893
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894 /* script-local variables */
6895 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897
6898 /* buffer-local variables */
6899 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6901 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902
6903 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006904 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6906 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006907#ifdef FEAT_AUTOCMD
6908 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6910 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006911#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006912
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006913#ifdef FEAT_WINDOWS
6914 /* tabpage-local variables */
6915 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6917 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006918#endif
6919
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922
6923 /* function-local variables */
6924 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6925 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6927 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 }
6929
Bram Moolenaard812df62008-11-09 12:46:09 +00006930 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006932
Bram Moolenaar1dced572012-04-05 16:54:08 +02006933#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006935#endif
6936
Bram Moolenaardb913952012-06-29 12:54:53 +02006937#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006939#endif
6940
6941#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006943#endif
6944
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006945#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006946 abort = abort || set_ref_in_channel(copyID);
6947#endif
6948
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006950 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006951 /*
6952 * 2. Free lists and dictionaries that are not referenced.
6953 */
6954 did_free = free_unref_items(copyID);
6955
6956 /*
6957 * 3. Check if any funccal can be freed now.
6958 */
6959 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006960 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 if (can_free_funccal(*pfc, copyID))
6962 {
6963 fc = *pfc;
6964 *pfc = fc->caller;
6965 free_funccal(fc, TRUE);
6966 did_free = TRUE;
6967 did_free_funccal = TRUE;
6968 }
6969 else
6970 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006972 if (did_free_funccal)
6973 /* When a funccal was freed some more items might be garbage
6974 * collected, so run again. */
6975 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 else if (p_verbose > 0)
6978 {
6979 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6980 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981
6982 return did_free;
6983}
6984
6985/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006986 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987 */
6988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006989free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006991 dict_T *dd, *dd_next;
6992 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006993 int did_free = FALSE;
6994
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 */
6998 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006999 {
7000 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007003 /* Free the Dictionary and ordinary items it contains, but don't
7004 * recurse into Lists and Dictionaries, they will be in the list
7005 * of dicts or list of lists. */
7006 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007009 dd = dd_next;
7010 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011
7012 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007013 * Go through the list of lists and free items without the copyID.
7014 * But don't free a list that has a watcher (used in a for loop), these
7015 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 */
7017 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007018 {
7019 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007020 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7021 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007023 /* Free the List and ordinary items it contains, but don't recurse
7024 * into Lists and Dictionaries, they will be in the list of dicts
7025 * or list of lists. */
7026 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007029 ll = ll_next;
7030 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007031
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 return did_free;
7033}
7034
7035/*
7036 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 * "list_stack" is used to add lists to be marked. Can be NULL.
7038 *
7039 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007042set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043{
7044 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 hashtab_T *cur_ht;
7048 ht_stack_T *ht_stack = NULL;
7049 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051 cur_ht = ht;
7052 for (;;)
7053 {
7054 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056 /* Mark each item in the hashtab. If the item contains a hashtab
7057 * it is added to ht_stack, if it contains a list it is added to
7058 * list_stack. */
7059 todo = (int)cur_ht->ht_used;
7060 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7061 if (!HASHITEM_EMPTY(hi))
7062 {
7063 --todo;
7064 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7065 &ht_stack, list_stack);
7066 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068
7069 if (ht_stack == NULL)
7070 break;
7071
7072 /* take an item from the stack */
7073 cur_ht = ht_stack->ht;
7074 tempitem = ht_stack;
7075 ht_stack = ht_stack->prev;
7076 free(tempitem);
7077 }
7078
7079 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080}
7081
7082/*
7083 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7085 *
7086 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007089set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 listitem_T *li;
7092 int abort = FALSE;
7093 list_T *cur_l;
7094 list_stack_T *list_stack = NULL;
7095 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 cur_l = l;
7098 for (;;)
7099 {
7100 if (!abort)
7101 /* Mark each item in the list. If the item contains a hashtab
7102 * it is added to ht_stack, if it contains a list it is added to
7103 * list_stack. */
7104 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7105 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7106 ht_stack, &list_stack);
7107 if (list_stack == NULL)
7108 break;
7109
7110 /* take an item from the stack */
7111 cur_l = list_stack->list;
7112 tempitem = list_stack;
7113 list_stack = list_stack->prev;
7114 free(tempitem);
7115 }
7116
7117 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007118}
7119
7120/*
7121 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007122 * "list_stack" is used to add lists to be marked. Can be NULL.
7123 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7124 *
7125 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007126 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007128set_ref_in_item(
7129 typval_T *tv,
7130 int copyID,
7131 ht_stack_T **ht_stack,
7132 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007133{
7134 dict_T *dd;
7135 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007136 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007137
Bram Moolenaara03f2332016-02-06 18:09:59 +01007138 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007139 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007140 dd = tv->vval.v_dict;
7141 if (dd != NULL && dd->dv_copyID != copyID)
7142 {
7143 /* Didn't see this dict yet. */
7144 dd->dv_copyID = copyID;
7145 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007146 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007147 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7148 }
7149 else
7150 {
7151 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7152 if (newitem == NULL)
7153 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007154 else
7155 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007156 newitem->ht = &dd->dv_hashtab;
7157 newitem->prev = *ht_stack;
7158 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007159 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007160 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007161 }
7162 }
7163 else if (tv->v_type == VAR_LIST)
7164 {
7165 ll = tv->vval.v_list;
7166 if (ll != NULL && ll->lv_copyID != copyID)
7167 {
7168 /* Didn't see this list yet. */
7169 ll->lv_copyID = copyID;
7170 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007171 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007172 abort = set_ref_in_list(ll, copyID, ht_stack);
7173 }
7174 else
7175 {
7176 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007177 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007178 if (newitem == NULL)
7179 abort = TRUE;
7180 else
7181 {
7182 newitem->list = ll;
7183 newitem->prev = *list_stack;
7184 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007185 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007186 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007187 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007188 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007189 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007190}
7191
7192/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193 * Allocate an empty header for a dictionary.
7194 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007195 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007196dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197{
Bram Moolenaar33570922005-01-25 22:26:29 +00007198 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007201 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007202 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007203 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007204 if (first_dict != NULL)
7205 first_dict->dv_used_prev = d;
7206 d->dv_used_next = first_dict;
7207 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007208 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007209
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007211 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007212 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007213 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007214 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007215 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217}
7218
7219/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007220 * Allocate an empty dict for a return value.
7221 * Returns OK or FAIL.
7222 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007223 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007224rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007225{
7226 dict_T *d = dict_alloc();
7227
7228 if (d == NULL)
7229 return FAIL;
7230
7231 rettv->vval.v_dict = d;
7232 rettv->v_type = VAR_DICT;
7233 ++d->dv_refcount;
7234 return OK;
7235}
7236
7237
7238/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239 * Unreference a Dictionary: decrement the reference count and free it when it
7240 * becomes zero.
7241 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007242 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007243dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007245 if (d != NULL && --d->dv_refcount <= 0)
7246 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247}
7248
7249/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007250 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251 * Ignores the reference count.
7252 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007253 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007254dict_free(
7255 dict_T *d,
7256 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007258 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007260 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007262 /* Remove the dict from the list of dicts for garbage collection. */
7263 if (d->dv_used_prev == NULL)
7264 first_dict = d->dv_used_next;
7265 else
7266 d->dv_used_prev->dv_used_next = d->dv_used_next;
7267 if (d->dv_used_next != NULL)
7268 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7269
7270 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007271 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007272 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007273 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275 if (!HASHITEM_EMPTY(hi))
7276 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007277 /* Remove the item before deleting it, just in case there is
7278 * something recursive causing trouble. */
7279 di = HI2DI(hi);
7280 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007281 if (recurse || (di->di_tv.v_type != VAR_LIST
7282 && di->di_tv.v_type != VAR_DICT))
7283 clear_tv(&di->di_tv);
7284 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 --todo;
7286 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 vim_free(d);
7290}
7291
7292/*
7293 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007294 * The "key" is copied to the new item.
7295 * Note that the value of the item "di_tv" still needs to be initialized!
7296 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007298 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007299dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300{
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007307 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007308 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310}
7311
7312/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 * Make a copy of a Dictionary item.
7314 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007316dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317{
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007320 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7321 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322 if (di != NULL)
7323 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007325 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 copy_tv(&org->di_tv, &di->di_tv);
7327 }
7328 return di;
7329}
7330
7331/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332 * Remove item "item" from Dictionary "dict" and free it.
7333 */
7334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007335dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336{
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340 if (HASHITEM_EMPTY(hi))
7341 EMSG2(_(e_intern2), "dictitem_remove()");
7342 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344 dictitem_free(item);
7345}
7346
7347/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348 * Free a dict item. Also clears the value.
7349 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007350 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007351dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007354 if (item->di_flags & DI_FLAGS_ALLOC)
7355 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356}
7357
7358/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007359 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7360 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007362 * Returns NULL when out of memory.
7363 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007365dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366{
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 dict_T *copy;
7368 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371
7372 if (orig == NULL)
7373 return NULL;
7374
7375 copy = dict_alloc();
7376 if (copy != NULL)
7377 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378 if (copyID != 0)
7379 {
7380 orig->dv_copyID = copyID;
7381 orig->dv_copydict = copy;
7382 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007383 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007384 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007386 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007388 --todo;
7389
7390 di = dictitem_alloc(hi->hi_key);
7391 if (di == NULL)
7392 break;
7393 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007394 {
7395 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7396 copyID) == FAIL)
7397 {
7398 vim_free(di);
7399 break;
7400 }
7401 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402 else
7403 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7404 if (dict_add(copy, di) == FAIL)
7405 {
7406 dictitem_free(di);
7407 break;
7408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007413 if (todo > 0)
7414 {
7415 dict_unref(copy);
7416 copy = NULL;
7417 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007418 }
7419
7420 return copy;
7421}
7422
7423/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007425 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007428dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429{
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431}
7432
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007434 * Add a number or string entry to dictionary "d".
7435 * When "str" is NULL use number "nr", otherwise use "str".
7436 * Returns FAIL when out of memory and when key already exists.
7437 */
7438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007439dict_add_nr_str(
7440 dict_T *d,
7441 char *key,
7442 long nr,
7443 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007444{
7445 dictitem_T *item;
7446
7447 item = dictitem_alloc((char_u *)key);
7448 if (item == NULL)
7449 return FAIL;
7450 item->di_tv.v_lock = 0;
7451 if (str == NULL)
7452 {
7453 item->di_tv.v_type = VAR_NUMBER;
7454 item->di_tv.vval.v_number = nr;
7455 }
7456 else
7457 {
7458 item->di_tv.v_type = VAR_STRING;
7459 item->di_tv.vval.v_string = vim_strsave(str);
7460 }
7461 if (dict_add(d, item) == FAIL)
7462 {
7463 dictitem_free(item);
7464 return FAIL;
7465 }
7466 return OK;
7467}
7468
7469/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007470 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007471 * Returns FAIL when out of memory and when key already exists.
7472 */
7473 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007474dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007475{
7476 dictitem_T *item;
7477
7478 item = dictitem_alloc((char_u *)key);
7479 if (item == NULL)
7480 return FAIL;
7481 item->di_tv.v_lock = 0;
7482 item->di_tv.v_type = VAR_LIST;
7483 item->di_tv.vval.v_list = list;
7484 if (dict_add(d, item) == FAIL)
7485 {
7486 dictitem_free(item);
7487 return FAIL;
7488 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007489 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007490 return OK;
7491}
7492
7493/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007494 * Get the number of items in a Dictionary.
7495 */
7496 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007497dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007498{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 if (d == NULL)
7500 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007501 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007502}
7503
7504/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 * Find item "key[len]" in Dictionary "d".
7506 * If "len" is negative use strlen(key).
7507 * Returns NULL when not found.
7508 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007509 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007510dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512#define AKEYLEN 200
7513 char_u buf[AKEYLEN];
7514 char_u *akey;
7515 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 if (len < 0)
7519 akey = key;
7520 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007521 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007522 tofree = akey = vim_strnsave(key, len);
7523 if (akey == NULL)
7524 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007525 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 else
7527 {
7528 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007529 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530 akey = buf;
7531 }
7532
Bram Moolenaar33570922005-01-25 22:26:29 +00007533 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534 vim_free(tofree);
7535 if (HASHITEM_EMPTY(hi))
7536 return NULL;
7537 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538}
7539
7540/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007541 * Get a string item from a dictionary.
7542 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007543 * Returns NULL if the entry doesn't exist or out of memory.
7544 */
7545 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007546get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007547{
7548 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007549 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007550
7551 di = dict_find(d, key, -1);
7552 if (di == NULL)
7553 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007554 s = get_tv_string(&di->di_tv);
7555 if (save && s != NULL)
7556 s = vim_strsave(s);
7557 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007558}
7559
7560/*
7561 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007562 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007563 */
7564 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007565get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007566{
7567 dictitem_T *di;
7568
7569 di = dict_find(d, key, -1);
7570 if (di == NULL)
7571 return 0;
7572 return get_tv_number(&di->di_tv);
7573}
7574
7575/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 * Return an allocated string with the string representation of a Dictionary.
7577 * May return NULL.
7578 */
7579 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007580dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581{
7582 garray_T ga;
7583 int first = TRUE;
7584 char_u *tofree;
7585 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007586 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007589 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007590
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007591 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 return NULL;
7593 ga_init2(&ga, (int)sizeof(char), 80);
7594 ga_append(&ga, '{');
7595
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007596 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007597 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007601 --todo;
7602
7603 if (first)
7604 first = FALSE;
7605 else
7606 ga_concat(&ga, (char_u *)", ");
7607
7608 tofree = string_quote(hi->hi_key, FALSE);
7609 if (tofree != NULL)
7610 {
7611 ga_concat(&ga, tofree);
7612 vim_free(tofree);
7613 }
7614 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007615 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007616 if (s != NULL)
7617 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007619 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007620 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007621 line_breakcheck();
7622
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007625 if (todo > 0)
7626 {
7627 vim_free(ga.ga_data);
7628 return NULL;
7629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630
7631 ga_append(&ga, '}');
7632 ga_append(&ga, NUL);
7633 return (char_u *)ga.ga_data;
7634}
7635
7636/*
7637 * Allocate a variable for a Dictionary and fill it from "*arg".
7638 * Return OK or FAIL. Returns NOTDONE for {expr}.
7639 */
7640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007641get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642{
Bram Moolenaar33570922005-01-25 22:26:29 +00007643 dict_T *d = NULL;
7644 typval_T tvkey;
7645 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007646 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007647 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007649 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650
7651 /*
7652 * First check if it's not a curly-braces thing: {expr}.
7653 * Must do this without evaluating, otherwise a function may be called
7654 * twice. Unfortunately this means we need to call eval1() twice for the
7655 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007656 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007658 if (*start != '}')
7659 {
7660 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7661 return FAIL;
7662 if (*start == '}')
7663 return NOTDONE;
7664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665
7666 if (evaluate)
7667 {
7668 d = dict_alloc();
7669 if (d == NULL)
7670 return FAIL;
7671 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007672 tvkey.v_type = VAR_UNKNOWN;
7673 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674
7675 *arg = skipwhite(*arg + 1);
7676 while (**arg != '}' && **arg != NUL)
7677 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007678 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679 goto failret;
7680 if (**arg != ':')
7681 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007682 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007683 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 goto failret;
7685 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007686 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007688 key = get_tv_string_buf_chk(&tvkey, buf);
7689 if (key == NULL || *key == NUL)
7690 {
7691 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7692 if (key != NULL)
7693 EMSG(_(e_emptykey));
7694 clear_tv(&tvkey);
7695 goto failret;
7696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698
7699 *arg = skipwhite(*arg + 1);
7700 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7701 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007702 if (evaluate)
7703 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 goto failret;
7705 }
7706 if (evaluate)
7707 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007708 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709 if (item != NULL)
7710 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007711 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007712 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713 clear_tv(&tv);
7714 goto failret;
7715 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007716 item = dictitem_alloc(key);
7717 clear_tv(&tvkey);
7718 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007721 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007722 if (dict_add(d, item) == FAIL)
7723 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 }
7725 }
7726
7727 if (**arg == '}')
7728 break;
7729 if (**arg != ',')
7730 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007731 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 goto failret;
7733 }
7734 *arg = skipwhite(*arg + 1);
7735 }
7736
7737 if (**arg != '}')
7738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007739 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740failret:
7741 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007742 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 return FAIL;
7744 }
7745
7746 *arg = skipwhite(*arg + 1);
7747 if (evaluate)
7748 {
7749 rettv->v_type = VAR_DICT;
7750 rettv->vval.v_dict = d;
7751 ++d->dv_refcount;
7752 }
7753
7754 return OK;
7755}
7756
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007757#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007758#endif
7759
Bram Moolenaar17a13432016-01-24 14:22:10 +01007760 static char *
7761get_var_special_name(int nr)
7762{
7763 switch (nr)
7764 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007765 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007766 case VVAL_TRUE: return "v:true";
7767 case VVAL_NONE: return "v:none";
7768 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007769 }
7770 EMSG2(_(e_intern2), "get_var_special_name()");
7771 return "42";
7772}
7773
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775 * Return a string with the string representation of a variable.
7776 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007777 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007778 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007779 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007780 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007781 */
7782 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007783echo_string(
7784 typval_T *tv,
7785 char_u **tofree,
7786 char_u *numbuf,
7787 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007788{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007789 static int recurse = 0;
7790 char_u *r = NULL;
7791
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007793 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007794 if (!did_echo_string_emsg)
7795 {
7796 /* Only give this message once for a recursive call to avoid
7797 * flooding the user with errors. And stop iterating over lists
7798 * and dicts. */
7799 did_echo_string_emsg = TRUE;
7800 EMSG(_("E724: variable nested too deep for displaying"));
7801 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007803 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007804 }
7805 ++recurse;
7806
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007807 switch (tv->v_type)
7808 {
7809 case VAR_FUNC:
7810 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 r = tv->vval.v_string;
7812 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007813
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007814 case VAR_PARTIAL:
7815 *tofree = NULL;
7816 /* TODO: arguments */
7817 r = tv->vval.v_partial == NULL ? NULL : tv->vval.v_partial->pt_name;
7818 break;
7819
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007821 if (tv->vval.v_list == NULL)
7822 {
7823 *tofree = NULL;
7824 r = NULL;
7825 }
7826 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7827 {
7828 *tofree = NULL;
7829 r = (char_u *)"[...]";
7830 }
7831 else
7832 {
7833 tv->vval.v_list->lv_copyID = copyID;
7834 *tofree = list2string(tv, copyID);
7835 r = *tofree;
7836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007838
Bram Moolenaar8c711452005-01-14 21:53:12 +00007839 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007840 if (tv->vval.v_dict == NULL)
7841 {
7842 *tofree = NULL;
7843 r = NULL;
7844 }
7845 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7846 {
7847 *tofree = NULL;
7848 r = (char_u *)"{...}";
7849 }
7850 else
7851 {
7852 tv->vval.v_dict->dv_copyID = copyID;
7853 *tofree = dict2string(tv, copyID);
7854 r = *tofree;
7855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007856 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858 case VAR_STRING:
7859 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007860 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007861 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007862 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 *tofree = NULL;
7864 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007866
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007867 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007868#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869 *tofree = NULL;
7870 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7871 r = numbuf;
7872 break;
7873#endif
7874
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007875 case VAR_SPECIAL:
7876 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007877 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007878 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007879 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007880
Bram Moolenaar8502c702014-06-17 12:51:16 +02007881 if (--recurse == 0)
7882 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884}
7885
7886/*
7887 * Return a string with the string representation of a variable.
7888 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7889 * "numbuf" is used for a number.
7890 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007891 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 */
7893 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007894tv2string(
7895 typval_T *tv,
7896 char_u **tofree,
7897 char_u *numbuf,
7898 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899{
7900 switch (tv->v_type)
7901 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007902 case VAR_FUNC:
7903 *tofree = string_quote(tv->vval.v_string, TRUE);
7904 return *tofree;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007905 case VAR_PARTIAL:
7906 *tofree = string_quote(tv->vval.v_partial == NULL ? NULL
7907 : tv->vval.v_partial->pt_name, TRUE);
7908 return *tofree;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 case VAR_STRING:
7910 *tofree = string_quote(tv->vval.v_string, FALSE);
7911 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007912 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007913#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007914 *tofree = NULL;
7915 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7916 return numbuf;
7917#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007918 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007920 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007921 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007922 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007923 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007924 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007925 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007927 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928}
7929
7930/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 * Return string "str" in ' quotes, doubling ' characters.
7932 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007934 */
7935 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007936string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007937{
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007939 char_u *p, *r, *s;
7940
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 len = (function ? 13 : 3);
7942 if (str != NULL)
7943 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007944 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 for (p = str; *p != NUL; mb_ptr_adv(p))
7946 if (*p == '\'')
7947 ++len;
7948 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007949 s = r = alloc(len);
7950 if (r != NULL)
7951 {
7952 if (function)
7953 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007954 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007955 r += 10;
7956 }
7957 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007958 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007959 if (str != NULL)
7960 for (p = str; *p != NUL; )
7961 {
7962 if (*p == '\'')
7963 *r++ = '\'';
7964 MB_COPY_CHAR(p, r);
7965 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007967 if (function)
7968 *r++ = ')';
7969 *r++ = NUL;
7970 }
7971 return s;
7972}
7973
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007974#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007975/*
7976 * Convert the string "text" to a floating point number.
7977 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7978 * this always uses a decimal point.
7979 * Returns the length of the text that was consumed.
7980 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007982string2float(
7983 char_u *text,
7984 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985{
7986 char *s = (char *)text;
7987 float_T f;
7988
7989 f = strtod(s, &s);
7990 *value = f;
7991 return (int)((char_u *)s - text);
7992}
7993#endif
7994
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 * Get the value of an environment variable.
7997 * "arg" is pointing to the '$'. It is advanced to after the name.
7998 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007999 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 */
8001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008002get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003{
8004 char_u *string = NULL;
8005 int len;
8006 int cc;
8007 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008008 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009
8010 ++*arg;
8011 name = *arg;
8012 len = get_env_len(arg);
8013 if (evaluate)
8014 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008015 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008016 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008017
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008018 cc = name[len];
8019 name[len] = NUL;
8020 /* first try vim_getenv(), fast for normal environment vars */
8021 string = vim_getenv(name, &mustfree);
8022 if (string != NULL && *string != NUL)
8023 {
8024 if (!mustfree)
8025 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008027 else
8028 {
8029 if (mustfree)
8030 vim_free(string);
8031
8032 /* next try expanding things like $VIM and ${HOME} */
8033 string = expand_env_save(name - 1);
8034 if (string != NULL && *string == '$')
8035 {
8036 vim_free(string);
8037 string = NULL;
8038 }
8039 }
8040 name[len] = cc;
8041
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008042 rettv->v_type = VAR_STRING;
8043 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
8045
8046 return OK;
8047}
8048
8049/*
8050 * Array with names and number of arguments of all internal functions
8051 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8052 */
8053static struct fst
8054{
8055 char *f_name; /* function name */
8056 char f_min_argc; /* minimal number of arguments */
8057 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008058 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008059 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060} functions[] =
8061{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008062#ifdef FEAT_FLOAT
8063 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008064 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008065#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008066 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008067 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008068 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"append", 2, 2, f_append},
8070 {"argc", 0, 0, f_argc},
8071 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008072 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008073 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008074#ifdef FEAT_FLOAT
8075 {"asin", 1, 1, f_asin}, /* WJMc */
8076#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008077 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008078 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008079 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008080 {"assert_false", 1, 2, f_assert_false},
8081 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082#ifdef FEAT_FLOAT
8083 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008084 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008087 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"bufexists", 1, 1, f_bufexists},
8089 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8090 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8091 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8092 {"buflisted", 1, 1, f_buflisted},
8093 {"bufloaded", 1, 1, f_bufloaded},
8094 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008095 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"bufwinnr", 1, 1, f_bufwinnr},
8097 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008098 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008099 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008100 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008101#ifdef FEAT_FLOAT
8102 {"ceil", 1, 1, f_ceil},
8103#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008104#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008105 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008106 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8107 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008108 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008109 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008110 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008111 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008112 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008113 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008114 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008115 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8116 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008117 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008118 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008119#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008120 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008121 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008123 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008125#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008126 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008127 {"complete_add", 1, 1, f_complete_add},
8128 {"complete_check", 0, 0, f_complete_check},
8129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008131 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132#ifdef FEAT_FLOAT
8133 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008134 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008135#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008136 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008138 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008139 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008140 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008142 {"diff_filler", 1, 1, f_diff_filler},
8143 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008144 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008145 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008147 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"eventhandler", 0, 0, f_eventhandler},
8149 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008150 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008152#ifdef FEAT_FLOAT
8153 {"exp", 1, 1, f_exp},
8154#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008155 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008156 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008157 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8159 {"filereadable", 1, 1, f_filereadable},
8160 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008161 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008162 {"finddir", 1, 3, f_finddir},
8163 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164#ifdef FEAT_FLOAT
8165 {"float2nr", 1, 1, f_float2nr},
8166 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008167 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008169 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"fnamemodify", 2, 2, f_fnamemodify},
8171 {"foldclosed", 1, 1, f_foldclosed},
8172 {"foldclosedend", 1, 1, f_foldclosedend},
8173 {"foldlevel", 1, 1, f_foldlevel},
8174 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008175 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008177 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008178 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008179 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008180 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008181 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"getchar", 0, 1, f_getchar},
8183 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008184 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"getcmdline", 0, 0, f_getcmdline},
8186 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008187 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008188 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008189 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008190 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008191 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008192 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {"getfsize", 1, 1, f_getfsize},
8194 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008195 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008196 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008197 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008198 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008199 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008200 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008201 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008202 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008204 {"gettabvar", 2, 3, f_gettabvar},
8205 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"getwinposx", 0, 0, f_getwinposx},
8207 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008208 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008209 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008210 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008211 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008213 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008214 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008215 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8217 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8218 {"histadd", 2, 2, f_histadd},
8219 {"histdel", 1, 2, f_histdel},
8220 {"histget", 1, 2, f_histget},
8221 {"histnr", 1, 1, f_histnr},
8222 {"hlID", 1, 1, f_hlID},
8223 {"hlexists", 1, 1, f_hlexists},
8224 {"hostname", 0, 0, f_hostname},
8225 {"iconv", 3, 3, f_iconv},
8226 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008227 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008228 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008230 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"inputrestore", 0, 0, f_inputrestore},
8232 {"inputsave", 0, 0, f_inputsave},
8233 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008234 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008235 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008237 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008238#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8239 {"isnan", 1, 1, f_isnan},
8240#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008241 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008242#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008243 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008244 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008245 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008246 {"job_start", 1, 2, f_job_start},
8247 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008248 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008249#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008250 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008251 {"js_decode", 1, 1, f_js_decode},
8252 {"js_encode", 1, 1, f_js_encode},
8253 {"json_decode", 1, 1, f_json_decode},
8254 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008255 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008257 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {"libcall", 3, 3, f_libcall},
8259 {"libcallnr", 3, 3, f_libcallnr},
8260 {"line", 1, 1, f_line},
8261 {"line2byte", 1, 1, f_line2byte},
8262 {"lispindent", 1, 1, f_lispindent},
8263 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008264#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008265 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008266 {"log10", 1, 1, f_log10},
8267#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008268#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008269 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008270#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008271 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008272 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008273 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008274 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008275 {"matchadd", 2, 5, f_matchadd},
8276 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008277 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008278 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008279 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008280 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008281 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008282 {"max", 1, 1, f_max},
8283 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008284#ifdef vim_mkdir
8285 {"mkdir", 1, 3, f_mkdir},
8286#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008287 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008288#ifdef FEAT_MZSCHEME
8289 {"mzeval", 1, 1, f_mzeval},
8290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008292 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008293 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008294 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008295#ifdef FEAT_PERL
8296 {"perleval", 1, 1, f_perleval},
8297#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298#ifdef FEAT_FLOAT
8299 {"pow", 2, 2, f_pow},
8300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008302 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008303 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008304#ifdef FEAT_PYTHON3
8305 {"py3eval", 1, 1, f_py3eval},
8306#endif
8307#ifdef FEAT_PYTHON
8308 {"pyeval", 1, 1, f_pyeval},
8309#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008310 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008311 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008312 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008313#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008314 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008315#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008316 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"remote_expr", 2, 3, f_remote_expr},
8318 {"remote_foreground", 1, 1, f_remote_foreground},
8319 {"remote_peek", 1, 2, f_remote_peek},
8320 {"remote_read", 1, 1, f_remote_read},
8321 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008322 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008324 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008326 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008327#ifdef FEAT_FLOAT
8328 {"round", 1, 1, f_round},
8329#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008330 {"screenattr", 2, 2, f_screenattr},
8331 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008332 {"screencol", 0, 0, f_screencol},
8333 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008334 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008335 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008336 {"searchpair", 3, 7, f_searchpair},
8337 {"searchpairpos", 3, 7, f_searchpairpos},
8338 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"server2client", 2, 2, f_server2client},
8340 {"serverlist", 0, 0, f_serverlist},
8341 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008342 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008344 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008346 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008347 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008348 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008349 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008351 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008352 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008354#ifdef FEAT_CRYPT
8355 {"sha256", 1, 1, f_sha256},
8356#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008357 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008358 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008360#ifdef FEAT_FLOAT
8361 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008362 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008363#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008364 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008365 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008366 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008367 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008368 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008369#ifdef FEAT_FLOAT
8370 {"sqrt", 1, 1, f_sqrt},
8371 {"str2float", 1, 1, f_str2float},
8372#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008373 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008374 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008375 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376#ifdef HAVE_STRFTIME
8377 {"strftime", 1, 2, f_strftime},
8378#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008380 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {"strlen", 1, 1, f_strlen},
8382 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008383 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008385 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008386 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"substitute", 4, 4, f_substitute},
8388 {"synID", 3, 3, f_synID},
8389 {"synIDattr", 2, 3, f_synIDattr},
8390 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008391 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008392 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008393 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008394 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008395 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008396 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008397 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008398 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008399 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008400#ifdef FEAT_FLOAT
8401 {"tan", 1, 1, f_tan},
8402 {"tanh", 1, 1, f_tanh},
8403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008405 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"tolower", 1, 1, f_tolower},
8407 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008408 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008409#ifdef FEAT_FLOAT
8410 {"trunc", 1, 1, f_trunc},
8411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008413 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008414 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008415 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008416 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"virtcol", 1, 1, f_virtcol},
8418 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008419 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008420 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008421 {"win_getid", 0, 2, f_win_getid},
8422 {"win_gotoid", 1, 1, f_win_gotoid},
8423 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8424 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {"winbufnr", 1, 1, f_winbufnr},
8426 {"wincol", 0, 0, f_wincol},
8427 {"winheight", 1, 1, f_winheight},
8428 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008429 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008431 {"winrestview", 1, 1, f_winrestview},
8432 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008434 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008435 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008436 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437};
8438
8439#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8440
8441/*
8442 * Function given to ExpandGeneric() to obtain the list of internal
8443 * or user defined function names.
8444 */
8445 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008446get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 static int intidx = -1;
8449 char_u *name;
8450
8451 if (idx == 0)
8452 intidx = -1;
8453 if (intidx < 0)
8454 {
8455 name = get_user_func_name(xp, idx);
8456 if (name != NULL)
8457 return name;
8458 }
8459 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8460 {
8461 STRCPY(IObuff, functions[intidx].f_name);
8462 STRCAT(IObuff, "(");
8463 if (functions[intidx].f_max_argc == 0)
8464 STRCAT(IObuff, ")");
8465 return IObuff;
8466 }
8467
8468 return NULL;
8469}
8470
8471/*
8472 * Function given to ExpandGeneric() to obtain the list of internal or
8473 * user defined variable or function names.
8474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008476get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
8478 static int intidx = -1;
8479 char_u *name;
8480
8481 if (idx == 0)
8482 intidx = -1;
8483 if (intidx < 0)
8484 {
8485 name = get_function_name(xp, idx);
8486 if (name != NULL)
8487 return name;
8488 }
8489 return get_user_var_name(xp, ++intidx);
8490}
8491
8492#endif /* FEAT_CMDL_COMPL */
8493
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008494#if defined(EBCDIC) || defined(PROTO)
8495/*
8496 * Compare struct fst by function name.
8497 */
8498 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008499compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008500{
8501 struct fst *p1 = (struct fst *)s1;
8502 struct fst *p2 = (struct fst *)s2;
8503
8504 return STRCMP(p1->f_name, p2->f_name);
8505}
8506
8507/*
8508 * Sort the function table by function name.
8509 * The sorting of the table above is ASCII dependant.
8510 * On machines using EBCDIC we have to sort it.
8511 */
8512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008513sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008514{
8515 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8516
8517 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8518}
8519#endif
8520
8521
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522/*
8523 * Find internal function in table above.
8524 * Return index, or -1 if not found
8525 */
8526 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008527find_internal_func(
8528 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
8530 int first = 0;
8531 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8532 int cmp;
8533 int x;
8534
8535 /*
8536 * Find the function name in the table. Binary search.
8537 */
8538 while (first <= last)
8539 {
8540 x = first + ((unsigned)(last - first) >> 1);
8541 cmp = STRCMP(name, functions[x].f_name);
8542 if (cmp < 0)
8543 last = x - 1;
8544 else if (cmp > 0)
8545 first = x + 1;
8546 else
8547 return x;
8548 }
8549 return -1;
8550}
8551
8552/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008553 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8554 * name it contains, otherwise return "name".
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008555 * If "name" is of type VAR_PARTIAL also return "partial"
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008556 */
8557 static char_u *
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008558deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008559{
Bram Moolenaar33570922005-01-25 22:26:29 +00008560 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008561 int cc;
8562
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008563 *partial = NULL;
8564
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008565 cc = name[*lenp];
8566 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008567 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008568 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008569 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008570 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008572 {
8573 *lenp = 0;
8574 return (char_u *)""; /* just in case */
8575 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008576 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008577 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008578 }
8579
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008580 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8581 {
8582 *partial = v->di_tv.vval.v_partial;
8583 if (*partial == NULL)
8584 {
8585 *lenp = 0;
8586 return (char_u *)""; /* just in case */
8587 }
8588 *lenp = (int)STRLEN((*partial)->pt_name);
8589 return (*partial)->pt_name;
8590 }
8591
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008592 return name;
8593}
8594
8595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 * Allocate a variable for the result of a function.
8597 * Return OK or FAIL.
8598 */
8599 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008600get_func_tv(
8601 char_u *name, /* name of the function */
8602 int len, /* length of "name" */
8603 typval_T *rettv,
8604 char_u **arg, /* argument, pointing to the '(' */
8605 linenr_T firstline, /* first line of range */
8606 linenr_T lastline, /* last line of range */
8607 int *doesrange, /* return: function handled range */
8608 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008609 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008610 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
8612 char_u *argp;
8613 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008614 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 int argcount = 0; /* number of arguments found */
8616
8617 /*
8618 * Get the arguments.
8619 */
8620 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008621 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 {
8623 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8624 if (*argp == ')' || *argp == ',' || *argp == NUL)
8625 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8627 {
8628 ret = FAIL;
8629 break;
8630 }
8631 ++argcount;
8632 if (*argp != ',')
8633 break;
8634 }
8635 if (*argp == ')')
8636 ++argp;
8637 else
8638 ret = FAIL;
8639
8640 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008642 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008644 {
8645 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008646 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008648 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
8651 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008652 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654 *arg = skipwhite(argp);
8655 return ret;
8656}
8657
8658
8659/*
8660 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008661 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008662 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008665call_func(
8666 char_u *funcname, /* name of the function */
8667 int len, /* length of "name" */
8668 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008669 int argcount_in, /* number of "argvars" */
8670 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008671 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008672 linenr_T firstline, /* first line of range */
8673 linenr_T lastline, /* last line of range */
8674 int *doesrange, /* return: function handled range */
8675 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008676 partial_T *partial, /* optional, can be NULL */
8677 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
8679 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680#define ERROR_UNKNOWN 0
8681#define ERROR_TOOMANY 1
8682#define ERROR_TOOFEW 2
8683#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008684#define ERROR_DICT 4
8685#define ERROR_NONE 5
8686#define ERROR_OTHER 6
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008687#define ERROR_BOTH 7
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 int error = ERROR_NONE;
8689 int i;
8690 int llen;
8691 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692#define FLEN_FIXED 40
8693 char_u fname_buf[FLEN_FIXED + 1];
8694 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008695 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008696 int argcount = argcount_in;
8697 typval_T *argvars = argvars_in;
8698 dict_T *selfdict = selfdict_in;
8699 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8700 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008701
8702 /* Make a copy of the name, if it comes from a funcref variable it could
8703 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008704 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008705 if (name == NULL)
8706 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707
8708 /*
8709 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8710 * Change <SNR>123_name() to K_SNR 123_name().
8711 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 llen = eval_fname_script(name);
8714 if (llen > 0)
8715 {
8716 fname_buf[0] = K_SPECIAL;
8717 fname_buf[1] = KS_EXTRA;
8718 fname_buf[2] = (int)KE_SNR;
8719 i = 3;
8720 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8721 {
8722 if (current_SID <= 0)
8723 error = ERROR_SCRIPT;
8724 else
8725 {
8726 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8727 i = (int)STRLEN(fname_buf);
8728 }
8729 }
8730 if (i + STRLEN(name + llen) < FLEN_FIXED)
8731 {
8732 STRCPY(fname_buf + i, name + llen);
8733 fname = fname_buf;
8734 }
8735 else
8736 {
8737 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8738 if (fname == NULL)
8739 error = ERROR_OTHER;
8740 else
8741 {
8742 mch_memmove(fname, fname_buf, (size_t)i);
8743 STRCPY(fname + i, name + llen);
8744 }
8745 }
8746 }
8747 else
8748 fname = name;
8749
8750 *doesrange = FALSE;
8751
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008752 if (partial != NULL)
8753 {
8754 if (partial->pt_dict != NULL)
8755 {
8756 if (selfdict_in != NULL)
8757 error = ERROR_BOTH;
8758 selfdict = partial->pt_dict;
8759 }
8760 if (error == ERROR_NONE && partial->pt_argc > 0)
8761 {
8762 int i;
8763
8764 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8765 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8766 for (i = 0; i < argcount_in; ++i)
8767 argv[i + argv_clear] = argvars_in[i];
8768 argvars = argv;
8769 argcount = partial->pt_argc + argcount_in;
8770 }
8771 }
8772
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773
8774 /* execute the function if no errors detected and executing */
8775 if (evaluate && error == ERROR_NONE)
8776 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008777 char_u *rfname = fname;
8778
8779 /* Ignore "g:" before a function name. */
8780 if (fname[0] == 'g' && fname[1] == ':')
8781 rfname = fname + 2;
8782
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008783 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8784 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 error = ERROR_UNKNOWN;
8786
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008787 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 {
8789 /*
8790 * User defined function.
8791 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008792 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008793
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008795 /* Trigger FuncUndefined event, may load the function. */
8796 if (fp == NULL
8797 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008798 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008799 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008801 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008802 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 }
8804#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008805 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008806 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008807 {
8808 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008809 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008810 }
8811
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 if (fp != NULL)
8813 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008814 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008816 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008818 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008820 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008821 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 else
8823 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008824 int did_save_redo = FALSE;
8825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 /*
8827 * Call the user function.
8828 * Save and restore search patterns, script variables and
8829 * redo buffer.
8830 */
8831 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008832#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008833 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008834#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008835 {
8836 saveRedobuff();
8837 did_save_redo = TRUE;
8838 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008839 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008841 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008842 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8843 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8844 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008845 /* Function was unreferenced while being used, free it
8846 * now. */
8847 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008848 if (did_save_redo)
8849 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 restore_search_patterns();
8851 error = ERROR_NONE;
8852 }
8853 }
8854 }
8855 else
8856 {
8857 /*
8858 * Find the function name in the table, call its implementation.
8859 */
8860 i = find_internal_func(fname);
8861 if (i >= 0)
8862 {
8863 if (argcount < functions[i].f_min_argc)
8864 error = ERROR_TOOFEW;
8865 else if (argcount > functions[i].f_max_argc)
8866 error = ERROR_TOOMANY;
8867 else
8868 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008869 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 error = ERROR_NONE;
8872 }
8873 }
8874 }
8875 /*
8876 * The function call (or "FuncUndefined" autocommand sequence) might
8877 * have been aborted by an error, an interrupt, or an explicitly thrown
8878 * exception that has not been caught so far. This situation can be
8879 * tested for by calling aborting(). For an error in an internal
8880 * function or for the "E132" error in call_user_func(), however, the
8881 * throw point at which the "force_abort" flag (temporarily reset by
8882 * emsg()) is normally updated has not been reached yet. We need to
8883 * update that flag first to make aborting() reliable.
8884 */
8885 update_force_abort();
8886 }
8887 if (error == ERROR_NONE)
8888 ret = OK;
8889
8890 /*
8891 * Report an error unless the argument evaluation or function call has been
8892 * cancelled due to an aborting error, an interrupt, or an exception.
8893 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008894 if (!aborting())
8895 {
8896 switch (error)
8897 {
8898 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008899 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008900 break;
8901 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008902 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008903 break;
8904 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008905 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008906 name);
8907 break;
8908 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008909 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008910 name);
8911 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008912 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008913 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008914 name);
8915 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008916 case ERROR_BOTH:
8917 emsg_funcname(N_("E924: can't have both a \"self\" dict and a partial: %s"),
8918 name);
8919 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008920 }
8921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008923 while (argv_clear > 0)
8924 clear_tv(&argv[--argv_clear]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 if (fname != name && fname != fname_buf)
8926 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008927 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928
8929 return ret;
8930}
8931
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008932/*
8933 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008934 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008935 */
8936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008937emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008938{
8939 char_u *p;
8940
8941 if (*name == K_SPECIAL)
8942 p = concat_str((char_u *)"<SNR>", name + 3);
8943 else
8944 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008945 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008946 if (p != name)
8947 vim_free(p);
8948}
8949
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008950/*
8951 * Return TRUE for a non-zero Number and a non-empty String.
8952 */
8953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008954non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008955{
8956 return ((argvars[0].v_type == VAR_NUMBER
8957 && argvars[0].vval.v_number != 0)
8958 || (argvars[0].v_type == VAR_STRING
8959 && argvars[0].vval.v_string != NULL
8960 && *argvars[0].vval.v_string != NUL));
8961}
8962
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963/*********************************************
8964 * Implementation of the built-in functions
8965 */
8966
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008967#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008968static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008969
8970/*
8971 * Get the float value of "argvars[0]" into "f".
8972 * Returns FAIL when the argument is not a Number or Float.
8973 */
8974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008975get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008976{
8977 if (argvars[0].v_type == VAR_FLOAT)
8978 {
8979 *f = argvars[0].vval.v_float;
8980 return OK;
8981 }
8982 if (argvars[0].v_type == VAR_NUMBER)
8983 {
8984 *f = (float_T)argvars[0].vval.v_number;
8985 return OK;
8986 }
8987 EMSG(_("E808: Number or Float required"));
8988 return FAIL;
8989}
8990
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008991/*
8992 * "abs(expr)" function
8993 */
8994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008995f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008996{
8997 if (argvars[0].v_type == VAR_FLOAT)
8998 {
8999 rettv->v_type = VAR_FLOAT;
9000 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9001 }
9002 else
9003 {
9004 varnumber_T n;
9005 int error = FALSE;
9006
9007 n = get_tv_number_chk(&argvars[0], &error);
9008 if (error)
9009 rettv->vval.v_number = -1;
9010 else if (n > 0)
9011 rettv->vval.v_number = n;
9012 else
9013 rettv->vval.v_number = -n;
9014 }
9015}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009016
9017/*
9018 * "acos()" function
9019 */
9020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009021f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009022{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009023 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009024
9025 rettv->v_type = VAR_FLOAT;
9026 if (get_float_arg(argvars, &f) == OK)
9027 rettv->vval.v_float = acos(f);
9028 else
9029 rettv->vval.v_float = 0.0;
9030}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009031#endif
9032
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009034 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 */
9036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009037f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009042 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009044 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009045 && !tv_check_lock(l->lv_lock,
9046 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009047 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009049 }
9050 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009051 EMSG(_(e_listreq));
9052}
9053
9054/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009055 * "alloc_fail(id, countdown, repeat)" function
9056 */
9057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009058f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009059{
9060 if (argvars[0].v_type != VAR_NUMBER
9061 || argvars[0].vval.v_number <= 0
9062 || argvars[1].v_type != VAR_NUMBER
9063 || argvars[1].vval.v_number < 0
9064 || argvars[2].v_type != VAR_NUMBER)
9065 EMSG(_(e_invarg));
9066 else
9067 {
9068 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009069 if (alloc_fail_id >= aid_last)
9070 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009071 alloc_fail_countdown = argvars[1].vval.v_number;
9072 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009073 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009074 }
9075}
9076
9077/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009078 * "and(expr, expr)" function
9079 */
9080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009081f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009082{
9083 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9084 & get_tv_number_chk(&argvars[1], NULL);
9085}
9086
9087/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009088 * "append(lnum, string/list)" function
9089 */
9090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009091f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009092{
9093 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009094 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009095 list_T *l = NULL;
9096 listitem_T *li = NULL;
9097 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009098 long added = 0;
9099
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009100 /* When coming here from Insert mode, sync undo, so that this can be
9101 * undone separately from what was previously inserted. */
9102 if (u_sync_once == 2)
9103 {
9104 u_sync_once = 1; /* notify that u_sync() was called */
9105 u_sync(TRUE);
9106 }
9107
Bram Moolenaar0d660222005-01-07 21:51:51 +00009108 lnum = get_tv_lnum(argvars);
9109 if (lnum >= 0
9110 && lnum <= curbuf->b_ml.ml_line_count
9111 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009112 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009113 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009114 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009115 l = argvars[1].vval.v_list;
9116 if (l == NULL)
9117 return;
9118 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009119 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009120 for (;;)
9121 {
9122 if (l == NULL)
9123 tv = &argvars[1]; /* append a string */
9124 else if (li == NULL)
9125 break; /* end of list */
9126 else
9127 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 line = get_tv_string_chk(tv);
9129 if (line == NULL) /* type error */
9130 {
9131 rettv->vval.v_number = 1; /* Failed */
9132 break;
9133 }
9134 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009135 ++added;
9136 if (l == NULL)
9137 break;
9138 li = li->li_next;
9139 }
9140
9141 appended_lines_mark(lnum, added);
9142 if (curwin->w_cursor.lnum > lnum)
9143 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009145 else
9146 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147}
9148
9149/*
9150 * "argc()" function
9151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009153f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156}
9157
9158/*
9159 * "argidx()" function
9160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009162f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165}
9166
9167/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009168 * "arglistid()" function
9169 */
9170 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009171f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009172{
9173 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009174
9175 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009176 wp = find_tabwin(&argvars[0], &argvars[1]);
9177 if (wp != NULL)
9178 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009179}
9180
9181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 * "argv(nr)" function
9183 */
9184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009185f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187 int idx;
9188
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009189 if (argvars[0].v_type != VAR_UNKNOWN)
9190 {
9191 idx = get_tv_number_chk(&argvars[0], NULL);
9192 if (idx >= 0 && idx < ARGCOUNT)
9193 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9194 else
9195 rettv->vval.v_string = NULL;
9196 rettv->v_type = VAR_STRING;
9197 }
9198 else if (rettv_list_alloc(rettv) == OK)
9199 for (idx = 0; idx < ARGCOUNT; ++idx)
9200 list_append_string(rettv->vval.v_list,
9201 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009204static void prepare_assert_error(garray_T*gap);
9205static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9206static void assert_error(garray_T *gap);
9207static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009208
9209/*
9210 * Prepare "gap" for an assert error and add the sourcing position.
9211 */
9212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009213prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009214{
9215 char buf[NUMBUFLEN];
9216
9217 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009218 if (sourcing_name != NULL)
9219 {
9220 ga_concat(gap, sourcing_name);
9221 if (sourcing_lnum > 0)
9222 ga_concat(gap, (char_u *)" ");
9223 }
9224 if (sourcing_lnum > 0)
9225 {
9226 sprintf(buf, "line %ld", (long)sourcing_lnum);
9227 ga_concat(gap, (char_u *)buf);
9228 }
9229 if (sourcing_name != NULL || sourcing_lnum > 0)
9230 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009231}
9232
9233/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009234 * Append "str" to "gap", escaping unprintable characters.
9235 * Changes NL to \n, CR to \r, etc.
9236 */
9237 static void
9238ga_concat_esc(garray_T *gap, char_u *str)
9239{
9240 char_u *p;
9241 char_u buf[NUMBUFLEN];
9242
9243 for (p = str; *p != NUL; ++p)
9244 switch (*p)
9245 {
9246 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9247 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9248 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9249 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9250 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9251 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9252 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9253 default:
9254 if (*p < ' ')
9255 {
9256 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9257 ga_concat(gap, buf);
9258 }
9259 else
9260 ga_append(gap, *p);
9261 break;
9262 }
9263}
9264
9265/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009266 * Fill "gap" with information about an assert error.
9267 */
9268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009269fill_assert_error(
9270 garray_T *gap,
9271 typval_T *opt_msg_tv,
9272 char_u *exp_str,
9273 typval_T *exp_tv,
9274 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009275{
9276 char_u numbuf[NUMBUFLEN];
9277 char_u *tofree;
9278
9279 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9280 {
9281 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9282 vim_free(tofree);
9283 }
9284 else
9285 {
9286 ga_concat(gap, (char_u *)"Expected ");
9287 if (exp_str == NULL)
9288 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009289 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009290 vim_free(tofree);
9291 }
9292 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009293 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009294 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009295 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009296 vim_free(tofree);
9297 }
9298}
Bram Moolenaar43345542015-11-29 17:35:35 +01009299
9300/*
9301 * Add an assert error to v:errors.
9302 */
9303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009304assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009305{
9306 struct vimvar *vp = &vimvars[VV_ERRORS];
9307
9308 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9309 /* Make sure v:errors is a list. */
9310 set_vim_var_list(VV_ERRORS, list_alloc());
9311 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9312}
9313
Bram Moolenaar43345542015-11-29 17:35:35 +01009314/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009315 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009316 */
9317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009318f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009319{
9320 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009321
9322 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9323 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009324 prepare_assert_error(&ga);
9325 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9326 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009327 ga_clear(&ga);
9328 }
9329}
9330
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009331/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009332 * "assert_exception(string[, msg])" function
9333 */
9334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009335f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009336{
9337 garray_T ga;
9338 char *error;
9339
9340 error = (char *)get_tv_string_chk(&argvars[0]);
9341 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9342 {
9343 prepare_assert_error(&ga);
9344 ga_concat(&ga, (char_u *)"v:exception is not set");
9345 assert_error(&ga);
9346 ga_clear(&ga);
9347 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009348 else if (error != NULL
9349 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009350 {
9351 prepare_assert_error(&ga);
9352 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9353 &vimvars[VV_EXCEPTION].vv_tv);
9354 assert_error(&ga);
9355 ga_clear(&ga);
9356 }
9357}
9358
9359/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009360 * "assert_fails(cmd [, error])" function
9361 */
9362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009363f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009364{
9365 char_u *cmd = get_tv_string_chk(&argvars[0]);
9366 garray_T ga;
9367
9368 called_emsg = FALSE;
9369 suppress_errthrow = TRUE;
9370 emsg_silent = TRUE;
9371 do_cmdline_cmd(cmd);
9372 if (!called_emsg)
9373 {
9374 prepare_assert_error(&ga);
9375 ga_concat(&ga, (char_u *)"command did not fail: ");
9376 ga_concat(&ga, cmd);
9377 assert_error(&ga);
9378 ga_clear(&ga);
9379 }
9380 else if (argvars[1].v_type != VAR_UNKNOWN)
9381 {
9382 char_u buf[NUMBUFLEN];
9383 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9384
9385 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9386 {
9387 prepare_assert_error(&ga);
9388 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9389 &vimvars[VV_ERRMSG].vv_tv);
9390 assert_error(&ga);
9391 ga_clear(&ga);
9392 }
9393 }
9394
9395 called_emsg = FALSE;
9396 suppress_errthrow = FALSE;
9397 emsg_silent = FALSE;
9398 emsg_on_display = FALSE;
9399 set_vim_var_string(VV_ERRMSG, NULL, 0);
9400}
9401
9402/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009403 * Common for assert_true() and assert_false().
9404 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009406assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009407{
9408 int error = FALSE;
9409 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009410
Bram Moolenaar37127922016-02-06 20:29:28 +01009411 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009412 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009413 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009414 if (argvars[0].v_type != VAR_NUMBER
9415 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9416 || error)
9417 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009418 prepare_assert_error(&ga);
9419 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009420 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009421 NULL, &argvars[0]);
9422 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009423 ga_clear(&ga);
9424 }
9425}
9426
9427/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009428 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009429 */
9430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009431f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009432{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009433 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009434}
9435
9436/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009437 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009438 */
9439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009440f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009441{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009442 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009443}
9444
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009445#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009446/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009447 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009448 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009450f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009451{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009452 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009453
9454 rettv->v_type = VAR_FLOAT;
9455 if (get_float_arg(argvars, &f) == OK)
9456 rettv->vval.v_float = asin(f);
9457 else
9458 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009459}
9460
9461/*
9462 * "atan()" function
9463 */
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009466{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009467 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009468
9469 rettv->v_type = VAR_FLOAT;
9470 if (get_float_arg(argvars, &f) == OK)
9471 rettv->vval.v_float = atan(f);
9472 else
9473 rettv->vval.v_float = 0.0;
9474}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009475
9476/*
9477 * "atan2()" function
9478 */
9479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009480f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009481{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009482 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009483
9484 rettv->v_type = VAR_FLOAT;
9485 if (get_float_arg(argvars, &fx) == OK
9486 && get_float_arg(&argvars[1], &fy) == OK)
9487 rettv->vval.v_float = atan2(fx, fy);
9488 else
9489 rettv->vval.v_float = 0.0;
9490}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009491#endif
9492
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493/*
9494 * "browse(save, title, initdir, default)" function
9495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009497f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499#ifdef FEAT_BROWSE
9500 int save;
9501 char_u *title;
9502 char_u *initdir;
9503 char_u *defname;
9504 char_u buf[NUMBUFLEN];
9505 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009508 save = get_tv_number_chk(&argvars[0], &error);
9509 title = get_tv_string_chk(&argvars[1]);
9510 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9511 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 if (error || title == NULL || initdir == NULL || defname == NULL)
9514 rettv->vval.v_string = NULL;
9515 else
9516 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009517 do_browse(save ? BROWSE_SAVE : 0,
9518 title, defname, NULL, initdir, NULL, curbuf);
9519#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009520 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009521#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009522 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009523}
9524
9525/*
9526 * "browsedir(title, initdir)" function
9527 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009529f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009530{
9531#ifdef FEAT_BROWSE
9532 char_u *title;
9533 char_u *initdir;
9534 char_u buf[NUMBUFLEN];
9535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009536 title = get_tv_string_chk(&argvars[0]);
9537 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009539 if (title == NULL || initdir == NULL)
9540 rettv->vval.v_string = NULL;
9541 else
9542 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009543 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548}
9549
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009550static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009551
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552/*
9553 * Find a buffer by number or exact name.
9554 */
9555 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009556find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557{
9558 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009560 if (avar->v_type == VAR_NUMBER)
9561 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009562 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009564 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009565 if (buf == NULL)
9566 {
9567 /* No full path name match, try a match with a URL or a "nofile"
9568 * buffer, these don't use the full path. */
9569 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9570 if (buf->b_fname != NULL
9571 && (path_with_url(buf->b_fname)
9572#ifdef FEAT_QUICKFIX
9573 || bt_nofile(buf)
9574#endif
9575 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009576 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009577 break;
9578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 }
9580 return buf;
9581}
9582
9583/*
9584 * "bufexists(expr)" function
9585 */
9586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009587f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590}
9591
9592/*
9593 * "buflisted(expr)" function
9594 */
9595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009596f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598 buf_T *buf;
9599
9600 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009601 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602}
9603
9604/*
9605 * "bufloaded(expr)" function
9606 */
9607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009608f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
9610 buf_T *buf;
9611
9612 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009613 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614}
9615
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009616 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009617buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 int save_magic;
9620 char_u *save_cpo;
9621 buf_T *buf;
9622
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9624 save_magic = p_magic;
9625 p_magic = TRUE;
9626 save_cpo = p_cpo;
9627 p_cpo = (char_u *)"";
9628
9629 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009630 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631
9632 p_magic = save_magic;
9633 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009634 return buf;
9635}
9636
9637/*
9638 * Get buffer by number or pattern.
9639 */
9640 static buf_T *
9641get_buf_tv(typval_T *tv, int curtab_only)
9642{
9643 char_u *name = tv->vval.v_string;
9644 buf_T *buf;
9645
9646 if (tv->v_type == VAR_NUMBER)
9647 return buflist_findnr((int)tv->vval.v_number);
9648 if (tv->v_type != VAR_STRING)
9649 return NULL;
9650 if (name == NULL || *name == NUL)
9651 return curbuf;
9652 if (name[0] == '$' && name[1] == NUL)
9653 return lastbuf;
9654
9655 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656
9657 /* If not found, try expanding the name, like done for bufexists(). */
9658 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660
9661 return buf;
9662}
9663
9664/*
9665 * "bufname(expr)" function
9666 */
9667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009668f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
9670 buf_T *buf;
9671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009672 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009674 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009675 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009679 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 --emsg_off;
9681}
9682
9683/*
9684 * "bufnr(expr)" function
9685 */
9686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009687f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688{
9689 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009690 int error = FALSE;
9691 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009695 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009696 --emsg_off;
9697
9698 /* If the buffer isn't found and the second argument is not zero create a
9699 * new buffer. */
9700 if (buf == NULL
9701 && argvars[1].v_type != VAR_UNKNOWN
9702 && get_tv_number_chk(&argvars[1], &error) != 0
9703 && !error
9704 && (name = get_tv_string_chk(&argvars[0])) != NULL
9705 && !error)
9706 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9707
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712}
9713
9714/*
9715 * "bufwinnr(nr)" function
9716 */
9717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009718f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719{
9720#ifdef FEAT_WINDOWS
9721 win_T *wp;
9722 int winnr = 0;
9723#endif
9724 buf_T *buf;
9725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009726 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009728 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729#ifdef FEAT_WINDOWS
9730 for (wp = firstwin; wp; wp = wp->w_next)
9731 {
9732 ++winnr;
9733 if (wp->w_buffer == buf)
9734 break;
9735 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739#endif
9740 --emsg_off;
9741}
9742
9743/*
9744 * "byte2line(byte)" function
9745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009747f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
9749#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751#else
9752 long boff = 0;
9753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009754 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 (linenr_T)0, &boff);
9760#endif
9761}
9762
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009764byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009765{
9766#ifdef FEAT_MBYTE
9767 char_u *t;
9768#endif
9769 char_u *str;
9770 long idx;
9771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 str = get_tv_string_chk(&argvars[0]);
9773 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009776 return;
9777
9778#ifdef FEAT_MBYTE
9779 t = str;
9780 for ( ; idx > 0; idx--)
9781 {
9782 if (*t == NUL) /* EOL reached */
9783 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009784 if (enc_utf8 && comp)
9785 t += utf_ptr2len(t);
9786 else
9787 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009788 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009789 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009790#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009791 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009793#endif
9794}
9795
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009796/*
9797 * "byteidx()" function
9798 */
9799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009800f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009801{
9802 byteidx(argvars, rettv, FALSE);
9803}
9804
9805/*
9806 * "byteidxcomp()" function
9807 */
9808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009809f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009810{
9811 byteidx(argvars, rettv, TRUE);
9812}
9813
Bram Moolenaardb913952012-06-29 12:54:53 +02009814 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009815func_call(
9816 char_u *name,
9817 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009818 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009819 dict_T *selfdict,
9820 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009821{
9822 listitem_T *item;
9823 typval_T argv[MAX_FUNC_ARGS + 1];
9824 int argc = 0;
9825 int dummy;
9826 int r = 0;
9827
9828 for (item = args->vval.v_list->lv_first; item != NULL;
9829 item = item->li_next)
9830 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009831 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009832 {
9833 EMSG(_("E699: Too many arguments"));
9834 break;
9835 }
9836 /* Make a copy of each argument. This is needed to be able to set
9837 * v_lock to VAR_FIXED in the copy without changing the original list.
9838 */
9839 copy_tv(&item->li_tv, &argv[argc++]);
9840 }
9841
9842 if (item == NULL)
9843 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9844 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009845 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009846
9847 /* Free the arguments. */
9848 while (argc > 0)
9849 clear_tv(&argv[--argc]);
9850
9851 return r;
9852}
9853
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009854/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009855 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009856 */
9857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009858f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009859{
9860 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009861 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009862 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009863
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009864 if (argvars[1].v_type != VAR_LIST)
9865 {
9866 EMSG(_(e_listreq));
9867 return;
9868 }
9869 if (argvars[1].vval.v_list == NULL)
9870 return;
9871
9872 if (argvars[0].v_type == VAR_FUNC)
9873 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009874 else if (argvars[0].v_type == VAR_PARTIAL)
9875 {
9876 partial = argvars[0].vval.v_partial;
9877 func = partial->pt_name;
9878 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009879 else
9880 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881 if (*func == NUL)
9882 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009883
Bram Moolenaare9a41262005-01-15 22:18:47 +00009884 if (argvars[2].v_type != VAR_UNKNOWN)
9885 {
9886 if (argvars[2].v_type != VAR_DICT)
9887 {
9888 EMSG(_(e_dictreq));
9889 return;
9890 }
9891 selfdict = argvars[2].vval.v_dict;
9892 }
9893
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009894 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009895}
9896
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009897#ifdef FEAT_FLOAT
9898/*
9899 * "ceil({float})" function
9900 */
9901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009902f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009903{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009904 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009905
9906 rettv->v_type = VAR_FLOAT;
9907 if (get_float_arg(argvars, &f) == OK)
9908 rettv->vval.v_float = ceil(f);
9909 else
9910 rettv->vval.v_float = 0.0;
9911}
9912#endif
9913
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009914#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009915/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009916 * "ch_close()" function
9917 */
9918 static void
9919f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9920{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009921 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009922
9923 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009924 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009925 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009926 channel_clear(channel);
9927 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009928}
9929
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009930/*
9931 * "ch_getbufnr()" function
9932 */
9933 static void
9934f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9935{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009936 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009937
9938 rettv->vval.v_number = -1;
9939 if (channel != NULL)
9940 {
9941 char_u *what = get_tv_string(&argvars[1]);
9942 int part;
9943
9944 if (STRCMP(what, "err") == 0)
9945 part = PART_ERR;
9946 else if (STRCMP(what, "out") == 0)
9947 part = PART_OUT;
9948 else if (STRCMP(what, "in") == 0)
9949 part = PART_IN;
9950 else
9951 part = PART_SOCK;
9952 if (channel->ch_part[part].ch_buffer != NULL)
9953 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
9954 }
9955}
9956
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009957/*
9958 * "ch_getjob()" function
9959 */
9960 static void
9961f_ch_getjob(typval_T *argvars, typval_T *rettv)
9962{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009963 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009964
9965 if (channel != NULL)
9966 {
9967 rettv->v_type = VAR_JOB;
9968 rettv->vval.v_job = channel->ch_job;
9969 if (channel->ch_job != NULL)
9970 ++channel->ch_job->jv_refcount;
9971 }
9972}
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009973
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009974/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009975 * "ch_log()" function
9976 */
9977 static void
9978f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
9979{
9980 char_u *msg = get_tv_string(&argvars[0]);
9981 channel_T *channel = NULL;
9982
9983 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009984 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009985
9986 ch_log(channel, (char *)msg);
9987}
9988
9989/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009990 * "ch_logfile()" function
9991 */
9992 static void
9993f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9994{
9995 char_u *fname;
9996 char_u *opt = (char_u *)"";
9997 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009998
9999 fname = get_tv_string(&argvars[0]);
10000 if (argvars[1].v_type == VAR_STRING)
10001 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010002 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010003}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010004
10005/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010006 * "ch_open()" function
10007 */
10008 static void
10009f_ch_open(typval_T *argvars, typval_T *rettv)
10010{
Bram Moolenaar77073442016-02-13 23:23:53 +010010011 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010012 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010013}
10014
10015/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010016 * "ch_read()" function
10017 */
10018 static void
10019f_ch_read(typval_T *argvars, typval_T *rettv)
10020{
10021 common_channel_read(argvars, rettv, FALSE);
10022}
10023
10024/*
10025 * "ch_readraw()" function
10026 */
10027 static void
10028f_ch_readraw(typval_T *argvars, typval_T *rettv)
10029{
10030 common_channel_read(argvars, rettv, TRUE);
10031}
10032
10033/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010034 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010035 */
10036 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010037f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10038{
10039 ch_expr_common(argvars, rettv, TRUE);
10040}
10041
10042/*
10043 * "ch_sendexpr()" function
10044 */
10045 static void
10046f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10047{
10048 ch_expr_common(argvars, rettv, FALSE);
10049}
10050
10051/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010052 * "ch_evalraw()" function
10053 */
10054 static void
10055f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10056{
10057 ch_raw_common(argvars, rettv, TRUE);
10058}
10059
10060/*
10061 * "ch_sendraw()" function
10062 */
10063 static void
10064f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10065{
10066 ch_raw_common(argvars, rettv, FALSE);
10067}
10068
10069/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010070 * "ch_setoptions()" function
10071 */
10072 static void
10073f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10074{
10075 channel_T *channel;
10076 jobopt_T opt;
10077
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010078 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010079 if (channel == NULL)
10080 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010081 clear_job_options(&opt);
10082 if (get_job_options(&argvars[1], &opt,
10083 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010084 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010085 channel_set_options(channel, &opt);
10086}
10087
10088/*
10089 * "ch_status()" function
10090 */
10091 static void
10092f_ch_status(typval_T *argvars, typval_T *rettv)
10093{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010094 channel_T *channel;
10095
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010096 /* return an empty string by default */
10097 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010098 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010099
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010100 channel = get_channel_arg(&argvars[0], FALSE);
10101 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010102}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010103#endif
10104
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010105/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010106 * "changenr()" function
10107 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010109f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010110{
10111 rettv->vval.v_number = curbuf->b_u_seq_cur;
10112}
10113
10114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 * "char2nr(string)" function
10116 */
10117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010118f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
10120#ifdef FEAT_MBYTE
10121 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010122 {
10123 int utf8 = 0;
10124
10125 if (argvars[1].v_type != VAR_UNKNOWN)
10126 utf8 = get_tv_number_chk(&argvars[1], NULL);
10127
10128 if (utf8)
10129 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10130 else
10131 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 else
10134#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136}
10137
10138/*
10139 * "cindent(lnum)" function
10140 */
10141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010142f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143{
10144#ifdef FEAT_CINDENT
10145 pos_T pos;
10146 linenr_T lnum;
10147
10148 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10151 {
10152 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 curwin->w_cursor = pos;
10155 }
10156 else
10157#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159}
10160
10161/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010162 * "clearmatches()" function
10163 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010165f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010166{
10167#ifdef FEAT_SEARCH_EXTRA
10168 clear_matches(curwin);
10169#endif
10170}
10171
10172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 * "col(string)" function
10174 */
10175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010176f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177{
10178 colnr_T col = 0;
10179 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010180 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010182 fp = var2fpos(&argvars[0], FALSE, &fnum);
10183 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 {
10185 if (fp->col == MAXCOL)
10186 {
10187 /* '> can be MAXCOL, get the length of the line then */
10188 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010189 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 else
10191 col = MAXCOL;
10192 }
10193 else
10194 {
10195 col = fp->col + 1;
10196#ifdef FEAT_VIRTUALEDIT
10197 /* col(".") when the cursor is on the NUL at the end of the line
10198 * because of "coladd" can be seen as an extra column. */
10199 if (virtual_active() && fp == &curwin->w_cursor)
10200 {
10201 char_u *p = ml_get_cursor();
10202
10203 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10204 curwin->w_virtcol - curwin->w_cursor.coladd))
10205 {
10206# ifdef FEAT_MBYTE
10207 int l;
10208
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010209 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 col += l;
10211# else
10212 if (*p != NUL && p[1] == NUL)
10213 ++col;
10214# endif
10215 }
10216 }
10217#endif
10218 }
10219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221}
10222
Bram Moolenaar572cb562005-08-05 21:35:02 +000010223#if defined(FEAT_INS_EXPAND)
10224/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010225 * "complete()" function
10226 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010228f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010229{
10230 int startcol;
10231
10232 if ((State & INSERT) == 0)
10233 {
10234 EMSG(_("E785: complete() can only be used in Insert mode"));
10235 return;
10236 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010237
10238 /* Check for undo allowed here, because if something was already inserted
10239 * the line was already saved for undo and this check isn't done. */
10240 if (!undo_allowed())
10241 return;
10242
Bram Moolenaarade00832006-03-10 21:46:58 +000010243 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10244 {
10245 EMSG(_(e_invarg));
10246 return;
10247 }
10248
10249 startcol = get_tv_number_chk(&argvars[0], NULL);
10250 if (startcol <= 0)
10251 return;
10252
10253 set_completion(startcol - 1, argvars[1].vval.v_list);
10254}
10255
10256/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010257 * "complete_add()" function
10258 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010260f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010261{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010262 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010263}
10264
10265/*
10266 * "complete_check()" function
10267 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010269f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010270{
10271 int saved = RedrawingDisabled;
10272
10273 RedrawingDisabled = 0;
10274 ins_compl_check_keys(0);
10275 rettv->vval.v_number = compl_interrupted;
10276 RedrawingDisabled = saved;
10277}
10278#endif
10279
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280/*
10281 * "confirm(message, buttons[, default [, type]])" function
10282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010284f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285{
10286#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10287 char_u *message;
10288 char_u *buttons = NULL;
10289 char_u buf[NUMBUFLEN];
10290 char_u buf2[NUMBUFLEN];
10291 int def = 1;
10292 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 char_u *typestr;
10294 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010296 message = get_tv_string_chk(&argvars[0]);
10297 if (message == NULL)
10298 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010299 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010301 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10302 if (buttons == NULL)
10303 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010304 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010307 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010309 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10310 if (typestr == NULL)
10311 error = TRUE;
10312 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 switch (TOUPPER_ASC(*typestr))
10315 {
10316 case 'E': type = VIM_ERROR; break;
10317 case 'Q': type = VIM_QUESTION; break;
10318 case 'I': type = VIM_INFO; break;
10319 case 'W': type = VIM_WARNING; break;
10320 case 'G': type = VIM_GENERIC; break;
10321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 }
10323 }
10324 }
10325 }
10326
10327 if (buttons == NULL || *buttons == NUL)
10328 buttons = (char_u *)_("&Ok");
10329
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010330 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010331 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010332 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333#endif
10334}
10335
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010336/*
10337 * "copy()" function
10338 */
10339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010340f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010341{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010342 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010343}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010345#ifdef FEAT_FLOAT
10346/*
10347 * "cos()" function
10348 */
10349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010350f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010351{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010352 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010353
10354 rettv->v_type = VAR_FLOAT;
10355 if (get_float_arg(argvars, &f) == OK)
10356 rettv->vval.v_float = cos(f);
10357 else
10358 rettv->vval.v_float = 0.0;
10359}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010360
10361/*
10362 * "cosh()" function
10363 */
10364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010365f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010366{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010367 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010368
10369 rettv->v_type = VAR_FLOAT;
10370 if (get_float_arg(argvars, &f) == OK)
10371 rettv->vval.v_float = cosh(f);
10372 else
10373 rettv->vval.v_float = 0.0;
10374}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010375#endif
10376
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010378 * "count()" function
10379 */
10380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010381f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010382{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010383 long n = 0;
10384 int ic = FALSE;
10385
Bram Moolenaare9a41262005-01-15 22:18:47 +000010386 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010387 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010388 listitem_T *li;
10389 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010390 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010391
Bram Moolenaare9a41262005-01-15 22:18:47 +000010392 if ((l = argvars[0].vval.v_list) != NULL)
10393 {
10394 li = l->lv_first;
10395 if (argvars[2].v_type != VAR_UNKNOWN)
10396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010397 int error = FALSE;
10398
10399 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010400 if (argvars[3].v_type != VAR_UNKNOWN)
10401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010402 idx = get_tv_number_chk(&argvars[3], &error);
10403 if (!error)
10404 {
10405 li = list_find(l, idx);
10406 if (li == NULL)
10407 EMSGN(_(e_listidx), idx);
10408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010410 if (error)
10411 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010412 }
10413
10414 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010415 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010416 ++n;
10417 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010418 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010419 else if (argvars[0].v_type == VAR_DICT)
10420 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010421 int todo;
10422 dict_T *d;
10423 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010424
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010425 if ((d = argvars[0].vval.v_dict) != NULL)
10426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 int error = FALSE;
10428
Bram Moolenaare9a41262005-01-15 22:18:47 +000010429 if (argvars[2].v_type != VAR_UNKNOWN)
10430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010431 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010432 if (argvars[3].v_type != VAR_UNKNOWN)
10433 EMSG(_(e_invarg));
10434 }
10435
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010436 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010437 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010438 {
10439 if (!HASHITEM_EMPTY(hi))
10440 {
10441 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010442 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010443 ++n;
10444 }
10445 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010446 }
10447 }
10448 else
10449 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010450 rettv->vval.v_number = n;
10451}
10452
10453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10455 *
10456 * Checks the existence of a cscope connection.
10457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010459f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460{
10461#ifdef FEAT_CSCOPE
10462 int num = 0;
10463 char_u *dbpath = NULL;
10464 char_u *prepend = NULL;
10465 char_u buf[NUMBUFLEN];
10466
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010467 if (argvars[0].v_type != VAR_UNKNOWN
10468 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470 num = (int)get_tv_number(&argvars[0]);
10471 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010472 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010473 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 }
10475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477#endif
10478}
10479
10480/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010481 * "cursor(lnum, col)" function, or
10482 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010484 * Moves the cursor to the specified line and column.
10485 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010488f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489{
10490 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010491#ifdef FEAT_VIRTUALEDIT
10492 long coladd = 0;
10493#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010494 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010496 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010497 if (argvars[1].v_type == VAR_UNKNOWN)
10498 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010499 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010500 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010501
Bram Moolenaar493c1782014-05-28 14:34:46 +020010502 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010503 {
10504 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010505 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010506 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010507 line = pos.lnum;
10508 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010509#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010510 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010511#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010512 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010513 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010514 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010515 set_curswant = FALSE;
10516 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010517 }
10518 else
10519 {
10520 line = get_tv_lnum(argvars);
10521 col = get_tv_number_chk(&argvars[1], NULL);
10522#ifdef FEAT_VIRTUALEDIT
10523 if (argvars[2].v_type != VAR_UNKNOWN)
10524 coladd = get_tv_number_chk(&argvars[2], NULL);
10525#endif
10526 }
10527 if (line < 0 || col < 0
10528#ifdef FEAT_VIRTUALEDIT
10529 || coladd < 0
10530#endif
10531 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010532 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 if (line > 0)
10534 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 if (col > 0)
10536 curwin->w_cursor.col = col - 1;
10537#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010538 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539#endif
10540
10541 /* Make sure the cursor is in a valid position. */
10542 check_cursor();
10543#ifdef FEAT_MBYTE
10544 /* Correct cursor for multi-byte character. */
10545 if (has_mbyte)
10546 mb_adjust_cursor();
10547#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010548
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010549 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010550 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551}
10552
10553/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010554 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 */
10556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010557f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010559 int noref = 0;
10560
10561 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010562 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010563 if (noref < 0 || noref > 1)
10564 EMSG(_(e_invarg));
10565 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010566 {
10567 current_copyID += COPYID_INC;
10568 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570}
10571
10572/*
10573 * "delete()" function
10574 */
10575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010576f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010578 char_u nbuf[NUMBUFLEN];
10579 char_u *name;
10580 char_u *flags;
10581
10582 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010584 return;
10585
10586 name = get_tv_string(&argvars[0]);
10587 if (name == NULL || *name == NUL)
10588 {
10589 EMSG(_(e_invarg));
10590 return;
10591 }
10592
10593 if (argvars[1].v_type != VAR_UNKNOWN)
10594 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010596 flags = (char_u *)"";
10597
10598 if (*flags == NUL)
10599 /* delete a file */
10600 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10601 else if (STRCMP(flags, "d") == 0)
10602 /* delete an empty directory */
10603 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10604 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010605 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010606 rettv->vval.v_number = delete_recursive(name);
10607 else
10608 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609}
10610
10611/*
10612 * "did_filetype()" function
10613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010615f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616{
10617#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619#endif
10620}
10621
10622/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010623 * "diff_filler()" function
10624 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010626f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010627{
10628#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010630#endif
10631}
10632
10633/*
10634 * "diff_hlID()" function
10635 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010637f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010638{
10639#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010640 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010641 static linenr_T prev_lnum = 0;
10642 static int changedtick = 0;
10643 static int fnum = 0;
10644 static int change_start = 0;
10645 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010646 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010647 int filler_lines;
10648 int col;
10649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010650 if (lnum < 0) /* ignore type error in {lnum} arg */
10651 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010652 if (lnum != prev_lnum
10653 || changedtick != curbuf->b_changedtick
10654 || fnum != curbuf->b_fnum)
10655 {
10656 /* New line, buffer, change: need to get the values. */
10657 filler_lines = diff_check(curwin, lnum);
10658 if (filler_lines < 0)
10659 {
10660 if (filler_lines == -1)
10661 {
10662 change_start = MAXCOL;
10663 change_end = -1;
10664 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10665 hlID = HLF_ADD; /* added line */
10666 else
10667 hlID = HLF_CHD; /* changed line */
10668 }
10669 else
10670 hlID = HLF_ADD; /* added line */
10671 }
10672 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010673 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010674 prev_lnum = lnum;
10675 changedtick = curbuf->b_changedtick;
10676 fnum = curbuf->b_fnum;
10677 }
10678
10679 if (hlID == HLF_CHD || hlID == HLF_TXD)
10680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010681 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010682 if (col >= change_start && col <= change_end)
10683 hlID = HLF_TXD; /* changed text */
10684 else
10685 hlID = HLF_CHD; /* changed line */
10686 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010687 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010688#endif
10689}
10690
10691/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010692 * "disable_char_avail_for_testing({expr})" function
10693 */
10694 static void
10695f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10696{
10697 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10698}
10699
10700/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010701 * "empty({expr})" function
10702 */
10703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010704f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010705{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010706 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010707
10708 switch (argvars[0].v_type)
10709 {
10710 case VAR_STRING:
10711 case VAR_FUNC:
10712 n = argvars[0].vval.v_string == NULL
10713 || *argvars[0].vval.v_string == NUL;
10714 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010715 case VAR_PARTIAL:
10716 n = FALSE;
10717 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010718 case VAR_NUMBER:
10719 n = argvars[0].vval.v_number == 0;
10720 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010721 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010722#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010723 n = argvars[0].vval.v_float == 0.0;
10724 break;
10725#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010726 case VAR_LIST:
10727 n = argvars[0].vval.v_list == NULL
10728 || argvars[0].vval.v_list->lv_first == NULL;
10729 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010730 case VAR_DICT:
10731 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010732 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010733 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010734 case VAR_SPECIAL:
10735 n = argvars[0].vval.v_number != VVAL_TRUE;
10736 break;
10737
Bram Moolenaar835dc632016-02-07 14:27:38 +010010738 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010739#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010740 n = argvars[0].vval.v_job == NULL
10741 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10742 break;
10743#endif
10744 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010745#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010746 n = argvars[0].vval.v_channel == NULL
10747 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010748 break;
10749#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010750 case VAR_UNKNOWN:
10751 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10752 n = TRUE;
10753 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010754 }
10755
10756 rettv->vval.v_number = n;
10757}
10758
10759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 * "escape({string}, {chars})" function
10761 */
10762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010763f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764{
10765 char_u buf[NUMBUFLEN];
10766
Bram Moolenaar758711c2005-02-02 23:11:38 +000010767 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10768 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770}
10771
10772/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010773 * "eval()" function
10774 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010776f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010777{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010778 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010780 s = get_tv_string_chk(&argvars[0]);
10781 if (s != NULL)
10782 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010783
Bram Moolenaar615b9972015-01-14 17:15:05 +010010784 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010785 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10786 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010787 if (p != NULL && !aborting())
10788 EMSG2(_(e_invexpr2), p);
10789 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010790 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010791 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010792 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010793 else if (*s != NUL)
10794 EMSG(_(e_trailing));
10795}
10796
10797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 * "eventhandler()" function
10799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010801f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010803 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804}
10805
10806/*
10807 * "executable()" function
10808 */
10809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010810f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010812 char_u *name = get_tv_string(&argvars[0]);
10813
10814 /* Check in $PATH and also check directly if there is a directory name. */
10815 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10816 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010817}
10818
10819/*
10820 * "exepath()" function
10821 */
10822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010823f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010824{
10825 char_u *p = NULL;
10826
Bram Moolenaarb5971142015-03-21 17:32:19 +010010827 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010828 rettv->v_type = VAR_STRING;
10829 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830}
10831
10832/*
10833 * "exists()" function
10834 */
10835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010836f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837{
10838 char_u *p;
10839 char_u *name;
10840 int n = FALSE;
10841 int len = 0;
10842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 if (*p == '$') /* environment variable */
10845 {
10846 /* first try "normal" environment variables (fast) */
10847 if (mch_getenv(p + 1) != NULL)
10848 n = TRUE;
10849 else
10850 {
10851 /* try expanding things like $VIM and ${HOME} */
10852 p = expand_env_save(p);
10853 if (p != NULL && *p != '$')
10854 n = TRUE;
10855 vim_free(p);
10856 }
10857 }
10858 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010860 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010861 if (*skipwhite(p) != NUL)
10862 n = FALSE; /* trailing garbage */
10863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 else if (*p == '*') /* internal or user defined function */
10865 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010866 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 }
10868 else if (*p == ':')
10869 {
10870 n = cmd_exists(p + 1);
10871 }
10872 else if (*p == '#')
10873 {
10874#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010875 if (p[1] == '#')
10876 n = autocmd_supported(p + 2);
10877 else
10878 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879#endif
10880 }
10881 else /* internal variable */
10882 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010883 char_u *tofree;
10884 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010886 /* get_name_len() takes care of expanding curly braces */
10887 name = p;
10888 len = get_name_len(&p, &tofree, TRUE, FALSE);
10889 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010891 if (tofree != NULL)
10892 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010893 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010894 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010896 /* handle d.key, l[idx], f(expr) */
10897 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10898 if (n)
10899 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 }
10901 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010902 if (*p != NUL)
10903 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010905 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 }
10907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909}
10910
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010911#ifdef FEAT_FLOAT
10912/*
10913 * "exp()" function
10914 */
10915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010916f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010917{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010918 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010919
10920 rettv->v_type = VAR_FLOAT;
10921 if (get_float_arg(argvars, &f) == OK)
10922 rettv->vval.v_float = exp(f);
10923 else
10924 rettv->vval.v_float = 0.0;
10925}
10926#endif
10927
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928/*
10929 * "expand()" function
10930 */
10931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010932f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933{
10934 char_u *s;
10935 int len;
10936 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010937 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010940 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010943 if (argvars[1].v_type != VAR_UNKNOWN
10944 && argvars[2].v_type != VAR_UNKNOWN
10945 && get_tv_number_chk(&argvars[2], &error)
10946 && !error)
10947 {
10948 rettv->v_type = VAR_LIST;
10949 rettv->vval.v_list = NULL;
10950 }
10951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010952 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 if (*s == '%' || *s == '#' || *s == '<')
10954 {
10955 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010956 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010958 if (rettv->v_type == VAR_LIST)
10959 {
10960 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10961 list_append_string(rettv->vval.v_list, result, -1);
10962 else
10963 vim_free(result);
10964 }
10965 else
10966 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 }
10968 else
10969 {
10970 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010971 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 if (argvars[1].v_type != VAR_UNKNOWN
10973 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010974 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010975 if (!error)
10976 {
10977 ExpandInit(&xpc);
10978 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010979 if (p_wic)
10980 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010981 if (rettv->v_type == VAR_STRING)
10982 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10983 options, WILD_ALL);
10984 else if (rettv_list_alloc(rettv) != FAIL)
10985 {
10986 int i;
10987
10988 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10989 for (i = 0; i < xpc.xp_numfiles; i++)
10990 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10991 ExpandCleanup(&xpc);
10992 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010993 }
10994 else
10995 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 }
10997}
10998
10999/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011000 * Go over all entries in "d2" and add them to "d1".
11001 * When "action" is "error" then a duplicate key is an error.
11002 * When "action" is "force" then a duplicate key is overwritten.
11003 * Otherwise duplicate keys are ignored ("action" is "keep").
11004 */
11005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011006dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011007{
11008 dictitem_T *di1;
11009 hashitem_T *hi2;
11010 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011011 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011012
11013 todo = (int)d2->dv_hashtab.ht_used;
11014 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11015 {
11016 if (!HASHITEM_EMPTY(hi2))
11017 {
11018 --todo;
11019 di1 = dict_find(d1, hi2->hi_key, -1);
11020 if (d1->dv_scope != 0)
11021 {
11022 /* Disallow replacing a builtin function in l: and g:.
11023 * Check the key to be valid when adding to any
11024 * scope. */
11025 if (d1->dv_scope == VAR_DEF_SCOPE
11026 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11027 && var_check_func_name(hi2->hi_key,
11028 di1 == NULL))
11029 break;
11030 if (!valid_varname(hi2->hi_key))
11031 break;
11032 }
11033 if (di1 == NULL)
11034 {
11035 di1 = dictitem_copy(HI2DI(hi2));
11036 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11037 dictitem_free(di1);
11038 }
11039 else if (*action == 'e')
11040 {
11041 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11042 break;
11043 }
11044 else if (*action == 'f' && HI2DI(hi2) != di1)
11045 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011046 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11047 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011048 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011049 clear_tv(&di1->di_tv);
11050 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11051 }
11052 }
11053 }
11054}
11055
11056/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011057 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011058 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011059 */
11060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011061f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011062{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011063 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011064
Bram Moolenaare9a41262005-01-15 22:18:47 +000011065 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011066 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011067 list_T *l1, *l2;
11068 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011069 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011071
Bram Moolenaare9a41262005-01-15 22:18:47 +000011072 l1 = argvars[0].vval.v_list;
11073 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011074 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011075 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011076 {
11077 if (argvars[2].v_type != VAR_UNKNOWN)
11078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 before = get_tv_number_chk(&argvars[2], &error);
11080 if (error)
11081 return; /* type error; errmsg already given */
11082
Bram Moolenaar758711c2005-02-02 23:11:38 +000011083 if (before == l1->lv_len)
11084 item = NULL;
11085 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011086 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011087 item = list_find(l1, before);
11088 if (item == NULL)
11089 {
11090 EMSGN(_(e_listidx), before);
11091 return;
11092 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 }
11094 }
11095 else
11096 item = NULL;
11097 list_extend(l1, l2, item);
11098
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 copy_tv(&argvars[0], rettv);
11100 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011101 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011102 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11103 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011104 dict_T *d1, *d2;
11105 char_u *action;
11106 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107
11108 d1 = argvars[0].vval.v_dict;
11109 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011110 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011111 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 {
11113 /* Check the third argument. */
11114 if (argvars[2].v_type != VAR_UNKNOWN)
11115 {
11116 static char *(av[]) = {"keep", "force", "error"};
11117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011118 action = get_tv_string_chk(&argvars[2]);
11119 if (action == NULL)
11120 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 for (i = 0; i < 3; ++i)
11122 if (STRCMP(action, av[i]) == 0)
11123 break;
11124 if (i == 3)
11125 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011126 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011127 return;
11128 }
11129 }
11130 else
11131 action = (char_u *)"force";
11132
Bram Moolenaara9922d62013-05-30 13:01:18 +020011133 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011134
Bram Moolenaare9a41262005-01-15 22:18:47 +000011135 copy_tv(&argvars[0], rettv);
11136 }
11137 }
11138 else
11139 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011140}
11141
11142/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011143 * "feedkeys()" function
11144 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011146f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011147{
11148 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011149 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011150 char_u *keys, *flags;
11151 char_u nbuf[NUMBUFLEN];
11152 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011153 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011154 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011155
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011156 /* This is not allowed in the sandbox. If the commands would still be
11157 * executed in the sandbox it would be OK, but it probably happens later,
11158 * when "sandbox" is no longer set. */
11159 if (check_secure())
11160 return;
11161
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011162 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011163
11164 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011165 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011166 flags = get_tv_string_buf(&argvars[1], nbuf);
11167 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011168 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011169 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011170 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011171 case 'n': remap = FALSE; break;
11172 case 'm': remap = TRUE; break;
11173 case 't': typed = TRUE; break;
11174 case 'i': insert = TRUE; break;
11175 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011176 }
11177 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011178 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011179
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011180 if (*keys != NUL || execute)
11181 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011182 /* Need to escape K_SPECIAL and CSI before putting the string in the
11183 * typeahead buffer. */
11184 keys_esc = vim_strsave_escape_csi(keys);
11185 if (keys_esc != NULL)
11186 {
11187 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011188 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011189 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011190 if (vgetc_busy)
11191 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011192 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011193 {
11194 int save_msg_scroll = msg_scroll;
11195
11196 /* Avoid a 1 second delay when the keys start Insert mode. */
11197 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011198 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011199 msg_scroll |= save_msg_scroll;
11200 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011201 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011202 }
11203}
11204
11205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 * "filereadable()" function
11207 */
11208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011209f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011211 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 char_u *p;
11213 int n;
11214
Bram Moolenaarc236c162008-07-13 17:41:49 +000011215#ifndef O_NONBLOCK
11216# define O_NONBLOCK 0
11217#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011219 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11220 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 {
11222 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011223 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 }
11225 else
11226 n = FALSE;
11227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011228 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229}
11230
11231/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011232 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 * rights to write into.
11234 */
11235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011236f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011238 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239}
11240
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011241 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011242findfilendir(
11243 typval_T *argvars UNUSED,
11244 typval_T *rettv,
11245 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011246{
11247#ifdef FEAT_SEARCHPATH
11248 char_u *fname;
11249 char_u *fresult = NULL;
11250 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11251 char_u *p;
11252 char_u pathbuf[NUMBUFLEN];
11253 int count = 1;
11254 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011255 int error = FALSE;
11256#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011257
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011258 rettv->vval.v_string = NULL;
11259 rettv->v_type = VAR_STRING;
11260
11261#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011263
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011264 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011266 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11267 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011268 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 else
11270 {
11271 if (*p != NUL)
11272 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011274 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011275 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011276 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011277 }
11278
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011279 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11280 error = TRUE;
11281
11282 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284 do
11285 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011286 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011287 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011288 fresult = find_file_in_path_option(first ? fname : NULL,
11289 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011290 0, first, path,
11291 find_what,
11292 curbuf->b_ffname,
11293 find_what == FINDFILE_DIR
11294 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011295 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011296
11297 if (fresult != NULL && rettv->v_type == VAR_LIST)
11298 list_append_string(rettv->vval.v_list, fresult, -1);
11299
11300 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011302
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011303 if (rettv->v_type == VAR_STRING)
11304 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011305#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011306}
11307
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011308static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11309static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011310
11311/*
11312 * Implementation of map() and filter().
11313 */
11314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011315filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011316{
11317 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011318 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011319 listitem_T *li, *nli;
11320 list_T *l = NULL;
11321 dictitem_T *di;
11322 hashtab_T *ht;
11323 hashitem_T *hi;
11324 dict_T *d = NULL;
11325 typval_T save_val;
11326 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011327 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011328 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011329 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011330 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011331 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011332 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011333 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011334
Bram Moolenaare9a41262005-01-15 22:18:47 +000011335 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011336 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011337 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011338 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011339 return;
11340 }
11341 else if (argvars[0].v_type == VAR_DICT)
11342 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011343 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011344 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011345 return;
11346 }
11347 else
11348 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011349 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011350 return;
11351 }
11352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011353 expr = get_tv_string_buf_chk(&argvars[1], buf);
11354 /* On type errors, the preceding call has already displayed an error
11355 * message. Avoid a misleading error message for an empty string that
11356 * was not passed as argument. */
11357 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 prepare_vimvar(VV_VAL, &save_val);
11360 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011361
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011362 /* We reset "did_emsg" to be able to detect whether an error
11363 * occurred during evaluation of the expression. */
11364 save_did_emsg = did_emsg;
11365 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011366
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011367 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011370 vimvars[VV_KEY].vv_type = VAR_STRING;
11371
11372 ht = &d->dv_hashtab;
11373 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011374 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377 if (!HASHITEM_EMPTY(hi))
11378 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011379 int r;
11380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 --todo;
11382 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011383 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011384 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11385 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 break;
11387 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011388 r = filter_map_one(&di->di_tv, expr, map, &rem);
11389 clear_tv(&vimvars[VV_KEY].vv_tv);
11390 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011391 break;
11392 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011393 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011394 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11395 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011396 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011398 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011399 }
11400 }
11401 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 }
11403 else
11404 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011405 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 for (li = l->lv_first; li != NULL; li = nli)
11408 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011409 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011410 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011412 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011413 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011414 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011415 break;
11416 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011418 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011419 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011420 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011421
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011422 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011423 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011424
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011425 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011427
11428 copy_tv(&argvars[0], rettv);
11429}
11430
11431 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011432filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011433{
Bram Moolenaar33570922005-01-25 22:26:29 +000011434 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011435 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011436 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011437
Bram Moolenaar33570922005-01-25 22:26:29 +000011438 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011439 s = expr;
11440 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011441 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011442 if (*s != NUL) /* check for trailing chars after expr */
11443 {
11444 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011445 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011446 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011447 }
11448 if (map)
11449 {
11450 /* map(): replace the list item value */
11451 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011452 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011453 *tv = rettv;
11454 }
11455 else
11456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 int error = FALSE;
11458
Bram Moolenaare9a41262005-01-15 22:18:47 +000011459 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011461 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011462 /* On type error, nothing has been removed; return FAIL to stop the
11463 * loop. The error message was given by get_tv_number_chk(). */
11464 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011465 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011466 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011467 retval = OK;
11468theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011469 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011470 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011471}
11472
11473/*
11474 * "filter()" function
11475 */
11476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011477f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011478{
11479 filter_map(argvars, rettv, FALSE);
11480}
11481
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011482/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011483 * "finddir({fname}[, {path}[, {count}]])" function
11484 */
11485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011486f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011487{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011488 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011489}
11490
11491/*
11492 * "findfile({fname}[, {path}[, {count}]])" function
11493 */
11494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011495f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011496{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011497 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011498}
11499
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011500#ifdef FEAT_FLOAT
11501/*
11502 * "float2nr({float})" function
11503 */
11504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011505f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011506{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011507 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011508
11509 if (get_float_arg(argvars, &f) == OK)
11510 {
11511 if (f < -0x7fffffff)
11512 rettv->vval.v_number = -0x7fffffff;
11513 else if (f > 0x7fffffff)
11514 rettv->vval.v_number = 0x7fffffff;
11515 else
11516 rettv->vval.v_number = (varnumber_T)f;
11517 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011518}
11519
11520/*
11521 * "floor({float})" function
11522 */
11523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011524f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011525{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011526 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011527
11528 rettv->v_type = VAR_FLOAT;
11529 if (get_float_arg(argvars, &f) == OK)
11530 rettv->vval.v_float = floor(f);
11531 else
11532 rettv->vval.v_float = 0.0;
11533}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011534
11535/*
11536 * "fmod()" function
11537 */
11538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011539f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011540{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011541 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011542
11543 rettv->v_type = VAR_FLOAT;
11544 if (get_float_arg(argvars, &fx) == OK
11545 && get_float_arg(&argvars[1], &fy) == OK)
11546 rettv->vval.v_float = fmod(fx, fy);
11547 else
11548 rettv->vval.v_float = 0.0;
11549}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011550#endif
11551
Bram Moolenaar0d660222005-01-07 21:51:51 +000011552/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011553 * "fnameescape({string})" function
11554 */
11555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011556f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011557{
11558 rettv->vval.v_string = vim_strsave_fnameescape(
11559 get_tv_string(&argvars[0]), FALSE);
11560 rettv->v_type = VAR_STRING;
11561}
11562
11563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 * "fnamemodify({fname}, {mods})" function
11565 */
11566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011567f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568{
11569 char_u *fname;
11570 char_u *mods;
11571 int usedlen = 0;
11572 int len;
11573 char_u *fbuf = NULL;
11574 char_u buf[NUMBUFLEN];
11575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011576 fname = get_tv_string_chk(&argvars[0]);
11577 mods = get_tv_string_buf_chk(&argvars[1], buf);
11578 if (fname == NULL || mods == NULL)
11579 fname = NULL;
11580 else
11581 {
11582 len = (int)STRLEN(fname);
11583 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011590 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 vim_free(fbuf);
11592}
11593
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011594static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595
11596/*
11597 * "foldclosed()" function
11598 */
11599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011600foldclosed_both(
11601 typval_T *argvars UNUSED,
11602 typval_T *rettv,
11603 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604{
11605#ifdef FEAT_FOLDING
11606 linenr_T lnum;
11607 linenr_T first, last;
11608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011609 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011610 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11611 {
11612 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11613 {
11614 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 return;
11619 }
11620 }
11621#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623}
11624
11625/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011626 * "foldclosed()" function
11627 */
11628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011629f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011630{
11631 foldclosed_both(argvars, rettv, FALSE);
11632}
11633
11634/*
11635 * "foldclosedend()" function
11636 */
11637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011638f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011639{
11640 foldclosed_both(argvars, rettv, TRUE);
11641}
11642
11643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 * "foldlevel()" function
11645 */
11646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011647f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648{
11649#ifdef FEAT_FOLDING
11650 linenr_T lnum;
11651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656}
11657
11658/*
11659 * "foldtext()" function
11660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011662f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663{
11664#ifdef FEAT_FOLDING
11665 linenr_T lnum;
11666 char_u *s;
11667 char_u *r;
11668 int len;
11669 char *txt;
11670#endif
11671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011672 rettv->v_type = VAR_STRING;
11673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011675 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11676 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11677 <= curbuf->b_ml.ml_line_count
11678 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 {
11680 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011681 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11682 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 {
11684 if (!linewhite(lnum))
11685 break;
11686 ++lnum;
11687 }
11688
11689 /* Find interesting text in this line. */
11690 s = skipwhite(ml_get(lnum));
11691 /* skip C comment-start */
11692 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011693 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011695 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011696 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011697 {
11698 s = skipwhite(ml_get(lnum + 1));
11699 if (*s == '*')
11700 s = skipwhite(s + 1);
11701 }
11702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 txt = _("+-%s%3ld lines: ");
11704 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 + 20 /* for %3ld */
11707 + STRLEN(s))); /* concatenated */
11708 if (r != NULL)
11709 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011710 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11711 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11712 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 len = (int)STRLEN(r);
11714 STRCAT(r, s);
11715 /* remove 'foldmarker' and 'commentstring' */
11716 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011717 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 }
11719 }
11720#endif
11721}
11722
11723/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011724 * "foldtextresult(lnum)" function
11725 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011727f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011728{
11729#ifdef FEAT_FOLDING
11730 linenr_T lnum;
11731 char_u *text;
11732 char_u buf[51];
11733 foldinfo_T foldinfo;
11734 int fold_count;
11735#endif
11736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 rettv->v_type = VAR_STRING;
11738 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011739#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011740 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011741 /* treat illegal types and illegal string values for {lnum} the same */
11742 if (lnum < 0)
11743 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011744 fold_count = foldedCount(curwin, lnum, &foldinfo);
11745 if (fold_count > 0)
11746 {
11747 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11748 &foldinfo, buf);
11749 if (text == buf)
11750 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011752 }
11753#endif
11754}
11755
11756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 * "foreground()" function
11758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011760f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762#ifdef FEAT_GUI
11763 if (gui.in_use)
11764 gui_mch_set_foreground();
11765#else
11766# ifdef WIN32
11767 win32_set_foreground();
11768# endif
11769#endif
11770}
11771
11772/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011773 * "function()" function
11774 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011776f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011777{
11778 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011779 char_u *name;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011782 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011783 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011784 /* Don't check an autoload name for existence here. */
11785 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011786 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011787 else
11788 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011789 int dict_idx = 0;
11790 int arg_idx = 0;
11791 list_T *list = NULL;
11792
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011793 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011794 {
11795 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011796 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011797
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011798 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11799 * also be called from another script. Using trans_function_name()
11800 * would also work, but some plugins depend on the name being
11801 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011802 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011803 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11804 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011805 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011806 STRCPY(name, sid_buf);
11807 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011808 }
11809 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011810 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011811 name = vim_strsave(s);
11812
11813 if (argvars[1].v_type != VAR_UNKNOWN)
11814 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011815 if (argvars[2].v_type != VAR_UNKNOWN)
11816 {
11817 /* function(name, [args], dict) */
11818 arg_idx = 1;
11819 dict_idx = 2;
11820 }
11821 else if (argvars[1].v_type == VAR_DICT)
11822 /* function(name, dict) */
11823 dict_idx = 1;
11824 else
11825 /* function(name, [args]) */
11826 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011827 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011828 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011829 if (argvars[dict_idx].v_type != VAR_DICT)
11830 {
11831 EMSG(_("E922: expected a dict"));
11832 vim_free(name);
11833 return;
11834 }
11835 if (argvars[dict_idx].vval.v_dict == NULL)
11836 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011837 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011838 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011839 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011840 if (argvars[arg_idx].v_type != VAR_LIST)
11841 {
11842 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11843 vim_free(name);
11844 return;
11845 }
11846 list = argvars[arg_idx].vval.v_list;
11847 if (list == NULL || list->lv_len == 0)
11848 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011849 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011850 }
11851 if (dict_idx > 0 || arg_idx > 0)
11852 {
11853 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011854
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011855 if (pt != NULL)
11856 {
11857 if (arg_idx > 0)
11858 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011859 listitem_T *li;
11860 int i = 0;
11861
11862 pt->pt_argv = (typval_T *)alloc(
11863 sizeof(typval_T) * list->lv_len);
11864 if (pt->pt_argv == NULL)
11865 {
11866 vim_free(pt);
11867 vim_free(name);
11868 return;
11869 }
11870 else
11871 {
11872 pt->pt_argc = list->lv_len;
11873 for (li = list->lv_first; li != NULL; li = li->li_next)
11874 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
11875 }
11876 }
11877
11878 if (dict_idx > 0)
11879 {
11880 pt->pt_dict = argvars[dict_idx].vval.v_dict;
11881 ++pt->pt_dict->dv_refcount;
11882 }
11883
11884 pt->pt_refcount = 1;
11885 pt->pt_name = name;
11886 func_ref(pt->pt_name);
11887 }
11888 rettv->v_type = VAR_PARTIAL;
11889 rettv->vval.v_partial = pt;
11890 }
11891 else
11892 {
11893 rettv->v_type = VAR_FUNC;
11894 rettv->vval.v_string = name;
11895 func_ref(name);
11896 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011897 }
11898}
11899
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011900 static void
11901partial_free(partial_T *pt)
11902{
11903 int i;
11904
11905 for (i = 0; i < pt->pt_argc; ++i)
11906 clear_tv(&pt->pt_argv[i]);
11907 vim_free(pt->pt_argv);
11908 func_unref(pt->pt_name);
11909 vim_free(pt->pt_name);
11910 vim_free(pt);
11911}
11912
11913/*
11914 * Unreference a closure: decrement the reference count and free it when it
11915 * becomes zero.
11916 */
11917 void
11918partial_unref(partial_T *pt)
11919{
11920 if (pt != NULL && --pt->pt_refcount <= 0)
11921 partial_free(pt);
11922}
11923
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011924/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011925 * "garbagecollect()" function
11926 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011928f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011929{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011930 /* This is postponed until we are back at the toplevel, because we may be
11931 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11932 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011933
11934 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11935 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011936}
11937
11938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011939 * "get()" function
11940 */
11941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011942f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011943{
Bram Moolenaar33570922005-01-25 22:26:29 +000011944 listitem_T *li;
11945 list_T *l;
11946 dictitem_T *di;
11947 dict_T *d;
11948 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011949
Bram Moolenaare9a41262005-01-15 22:18:47 +000011950 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011951 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011952 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011954 int error = FALSE;
11955
11956 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11957 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011958 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011959 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011960 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011961 else if (argvars[0].v_type == VAR_DICT)
11962 {
11963 if ((d = argvars[0].vval.v_dict) != NULL)
11964 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011965 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011966 if (di != NULL)
11967 tv = &di->di_tv;
11968 }
11969 }
11970 else
11971 EMSG2(_(e_listdictarg), "get()");
11972
11973 if (tv == NULL)
11974 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011975 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011976 copy_tv(&argvars[2], rettv);
11977 }
11978 else
11979 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011980}
11981
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011982static 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 +000011983
11984/*
11985 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011986 * Return a range (from start to end) of lines in rettv from the specified
11987 * buffer.
11988 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011989 */
11990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011991get_buffer_lines(
11992 buf_T *buf,
11993 linenr_T start,
11994 linenr_T end,
11995 int retlist,
11996 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011997{
11998 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011999
Bram Moolenaar959a1432013-12-14 12:17:38 +010012000 rettv->v_type = VAR_STRING;
12001 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012002 if (retlist && rettv_list_alloc(rettv) == FAIL)
12003 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012004
12005 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12006 return;
12007
12008 if (!retlist)
12009 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012010 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12011 p = ml_get_buf(buf, start, FALSE);
12012 else
12013 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012014 rettv->vval.v_string = vim_strsave(p);
12015 }
12016 else
12017 {
12018 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012019 return;
12020
12021 if (start < 1)
12022 start = 1;
12023 if (end > buf->b_ml.ml_line_count)
12024 end = buf->b_ml.ml_line_count;
12025 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012026 if (list_append_string(rettv->vval.v_list,
12027 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012028 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012029 }
12030}
12031
12032/*
12033 * "getbufline()" function
12034 */
12035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012036f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012037{
12038 linenr_T lnum;
12039 linenr_T end;
12040 buf_T *buf;
12041
12042 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12043 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012044 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012045 --emsg_off;
12046
Bram Moolenaar661b1822005-07-28 22:36:45 +000012047 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012048 if (argvars[2].v_type == VAR_UNKNOWN)
12049 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012050 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012051 end = get_tv_lnum_buf(&argvars[2], buf);
12052
Bram Moolenaar342337a2005-07-21 21:11:17 +000012053 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012054}
12055
Bram Moolenaar0d660222005-01-07 21:51:51 +000012056/*
12057 * "getbufvar()" function
12058 */
12059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012060f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012061{
12062 buf_T *buf;
12063 buf_T *save_curbuf;
12064 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012065 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012066 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012068 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12069 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012070 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012071 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012072
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012073 rettv->v_type = VAR_STRING;
12074 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012075
12076 if (buf != NULL && varname != NULL)
12077 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012078 /* set curbuf to be our buf, temporarily */
12079 save_curbuf = curbuf;
12080 curbuf = buf;
12081
Bram Moolenaar0d660222005-01-07 21:51:51 +000012082 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012083 {
12084 if (get_option_tv(&varname, rettv, TRUE) == OK)
12085 done = TRUE;
12086 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012087 else if (STRCMP(varname, "changedtick") == 0)
12088 {
12089 rettv->v_type = VAR_NUMBER;
12090 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012091 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012092 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012093 else
12094 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012095 /* Look up the variable. */
12096 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12097 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12098 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012099 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012100 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012101 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012102 done = TRUE;
12103 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012104 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012105
12106 /* restore previous notion of curbuf */
12107 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108 }
12109
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012110 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12111 /* use the default value */
12112 copy_tv(&argvars[2], rettv);
12113
Bram Moolenaar0d660222005-01-07 21:51:51 +000012114 --emsg_off;
12115}
12116
12117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 * "getchar()" function
12119 */
12120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012121f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122{
12123 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012124 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012126 /* Position the cursor. Needed after a message that ends in a space. */
12127 windgoto(msg_row, msg_col);
12128
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129 ++no_mapping;
12130 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012131 for (;;)
12132 {
12133 if (argvars[0].v_type == VAR_UNKNOWN)
12134 /* getchar(): blocking wait. */
12135 n = safe_vgetc();
12136 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12137 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012138 n = vpeekc_any();
12139 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012140 /* illegal argument or getchar(0) and no char avail: return zero */
12141 n = 0;
12142 else
12143 /* getchar(0) and char avail: return char */
12144 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012145
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012146 if (n == K_IGNORE)
12147 continue;
12148 break;
12149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 --no_mapping;
12151 --allow_keys;
12152
Bram Moolenaar219b8702006-11-01 14:32:36 +000012153 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12154 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12155 vimvars[VV_MOUSE_COL].vv_nr = 0;
12156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012157 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 if (IS_SPECIAL(n) || mod_mask != 0)
12159 {
12160 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12161 int i = 0;
12162
12163 /* Turn a special key into three bytes, plus modifier. */
12164 if (mod_mask != 0)
12165 {
12166 temp[i++] = K_SPECIAL;
12167 temp[i++] = KS_MODIFIER;
12168 temp[i++] = mod_mask;
12169 }
12170 if (IS_SPECIAL(n))
12171 {
12172 temp[i++] = K_SPECIAL;
12173 temp[i++] = K_SECOND(n);
12174 temp[i++] = K_THIRD(n);
12175 }
12176#ifdef FEAT_MBYTE
12177 else if (has_mbyte)
12178 i += (*mb_char2bytes)(n, temp + i);
12179#endif
12180 else
12181 temp[i++] = n;
12182 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 rettv->v_type = VAR_STRING;
12184 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012185
12186#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012187 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012188 {
12189 int row = mouse_row;
12190 int col = mouse_col;
12191 win_T *win;
12192 linenr_T lnum;
12193# ifdef FEAT_WINDOWS
12194 win_T *wp;
12195# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012196 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012197
12198 if (row >= 0 && col >= 0)
12199 {
12200 /* Find the window at the mouse coordinates and compute the
12201 * text position. */
12202 win = mouse_find_win(&row, &col);
12203 (void)mouse_comp_pos(win, &row, &col, &lnum);
12204# ifdef FEAT_WINDOWS
12205 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012206 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012207# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012208 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012209 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12210 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12211 }
12212 }
12213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214 }
12215}
12216
12217/*
12218 * "getcharmod()" function
12219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012221f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012223 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224}
12225
12226/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012227 * "getcharsearch()" function
12228 */
12229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012230f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012231{
12232 if (rettv_dict_alloc(rettv) != FAIL)
12233 {
12234 dict_T *dict = rettv->vval.v_dict;
12235
12236 dict_add_nr_str(dict, "char", 0L, last_csearch());
12237 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12238 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12239 }
12240}
12241
12242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243 * "getcmdline()" function
12244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012246f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012248 rettv->v_type = VAR_STRING;
12249 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250}
12251
12252/*
12253 * "getcmdpos()" function
12254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012256f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259}
12260
12261/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012262 * "getcmdtype()" function
12263 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012265f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012266{
12267 rettv->v_type = VAR_STRING;
12268 rettv->vval.v_string = alloc(2);
12269 if (rettv->vval.v_string != NULL)
12270 {
12271 rettv->vval.v_string[0] = get_cmdline_type();
12272 rettv->vval.v_string[1] = NUL;
12273 }
12274}
12275
12276/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012277 * "getcmdwintype()" function
12278 */
12279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012280f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012281{
12282 rettv->v_type = VAR_STRING;
12283 rettv->vval.v_string = NULL;
12284#ifdef FEAT_CMDWIN
12285 rettv->vval.v_string = alloc(2);
12286 if (rettv->vval.v_string != NULL)
12287 {
12288 rettv->vval.v_string[0] = cmdwin_type;
12289 rettv->vval.v_string[1] = NUL;
12290 }
12291#endif
12292}
12293
12294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 * "getcwd()" function
12296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012298f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012300 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012301 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012303 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012304 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012305
12306 wp = find_tabwin(&argvars[0], &argvars[1]);
12307 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012309 if (wp->w_localdir != NULL)
12310 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12311 else if(globaldir != NULL)
12312 rettv->vval.v_string = vim_strsave(globaldir);
12313 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012314 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012315 cwd = alloc(MAXPATHL);
12316 if (cwd != NULL)
12317 {
12318 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12319 rettv->vval.v_string = vim_strsave(cwd);
12320 vim_free(cwd);
12321 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012322 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012323#ifdef BACKSLASH_IN_FILENAME
12324 if (rettv->vval.v_string != NULL)
12325 slash_adjust(rettv->vval.v_string);
12326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 }
12328}
12329
12330/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012331 * "getfontname()" function
12332 */
12333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012334f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012335{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012336 rettv->v_type = VAR_STRING;
12337 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012338#ifdef FEAT_GUI
12339 if (gui.in_use)
12340 {
12341 GuiFont font;
12342 char_u *name = NULL;
12343
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012344 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012345 {
12346 /* Get the "Normal" font. Either the name saved by
12347 * hl_set_font_name() or from the font ID. */
12348 font = gui.norm_font;
12349 name = hl_get_font_name();
12350 }
12351 else
12352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012353 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012354 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12355 return;
12356 font = gui_mch_get_font(name, FALSE);
12357 if (font == NOFONT)
12358 return; /* Invalid font name, return empty string. */
12359 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012361 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012362 gui_mch_free_font(font);
12363 }
12364#endif
12365}
12366
12367/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012368 * "getfperm({fname})" function
12369 */
12370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012371f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012372{
12373 char_u *fname;
12374 struct stat st;
12375 char_u *perm = NULL;
12376 char_u flags[] = "rwx";
12377 int i;
12378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012381 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012382 if (mch_stat((char *)fname, &st) >= 0)
12383 {
12384 perm = vim_strsave((char_u *)"---------");
12385 if (perm != NULL)
12386 {
12387 for (i = 0; i < 9; i++)
12388 {
12389 if (st.st_mode & (1 << (8 - i)))
12390 perm[i] = flags[i % 3];
12391 }
12392 }
12393 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012395}
12396
12397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 * "getfsize({fname})" function
12399 */
12400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012401f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402{
12403 char_u *fname;
12404 struct stat st;
12405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012408 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409
12410 if (mch_stat((char *)fname, &st) >= 0)
12411 {
12412 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012413 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012416 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012417
12418 /* non-perfect check for overflow */
12419 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12420 rettv->vval.v_number = -2;
12421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422 }
12423 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012424 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425}
12426
12427/*
12428 * "getftime({fname})" function
12429 */
12430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012431f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432{
12433 char_u *fname;
12434 struct stat st;
12435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012436 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437
12438 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442}
12443
12444/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012445 * "getftype({fname})" function
12446 */
12447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012448f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012449{
12450 char_u *fname;
12451 struct stat st;
12452 char_u *type = NULL;
12453 char *t;
12454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012455 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012458 if (mch_lstat((char *)fname, &st) >= 0)
12459 {
12460#ifdef S_ISREG
12461 if (S_ISREG(st.st_mode))
12462 t = "file";
12463 else if (S_ISDIR(st.st_mode))
12464 t = "dir";
12465# ifdef S_ISLNK
12466 else if (S_ISLNK(st.st_mode))
12467 t = "link";
12468# endif
12469# ifdef S_ISBLK
12470 else if (S_ISBLK(st.st_mode))
12471 t = "bdev";
12472# endif
12473# ifdef S_ISCHR
12474 else if (S_ISCHR(st.st_mode))
12475 t = "cdev";
12476# endif
12477# ifdef S_ISFIFO
12478 else if (S_ISFIFO(st.st_mode))
12479 t = "fifo";
12480# endif
12481# ifdef S_ISSOCK
12482 else if (S_ISSOCK(st.st_mode))
12483 t = "fifo";
12484# endif
12485 else
12486 t = "other";
12487#else
12488# ifdef S_IFMT
12489 switch (st.st_mode & S_IFMT)
12490 {
12491 case S_IFREG: t = "file"; break;
12492 case S_IFDIR: t = "dir"; break;
12493# ifdef S_IFLNK
12494 case S_IFLNK: t = "link"; break;
12495# endif
12496# ifdef S_IFBLK
12497 case S_IFBLK: t = "bdev"; break;
12498# endif
12499# ifdef S_IFCHR
12500 case S_IFCHR: t = "cdev"; break;
12501# endif
12502# ifdef S_IFIFO
12503 case S_IFIFO: t = "fifo"; break;
12504# endif
12505# ifdef S_IFSOCK
12506 case S_IFSOCK: t = "socket"; break;
12507# endif
12508 default: t = "other";
12509 }
12510# else
12511 if (mch_isdir(fname))
12512 t = "dir";
12513 else
12514 t = "file";
12515# endif
12516#endif
12517 type = vim_strsave((char_u *)t);
12518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012519 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012520}
12521
12522/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012523 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012524 */
12525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012526f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012527{
12528 linenr_T lnum;
12529 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012530 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012531
12532 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012533 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012534 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012535 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012536 retlist = FALSE;
12537 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012539 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012541 retlist = TRUE;
12542 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012543
Bram Moolenaar342337a2005-07-21 21:11:17 +000012544 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012545}
12546
12547/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012548 * "getmatches()" function
12549 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012551f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012552{
12553#ifdef FEAT_SEARCH_EXTRA
12554 dict_T *dict;
12555 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012556 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012557
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012558 if (rettv_list_alloc(rettv) == OK)
12559 {
12560 while (cur != NULL)
12561 {
12562 dict = dict_alloc();
12563 if (dict == NULL)
12564 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012565 if (cur->match.regprog == NULL)
12566 {
12567 /* match added with matchaddpos() */
12568 for (i = 0; i < MAXPOSMATCH; ++i)
12569 {
12570 llpos_T *llpos;
12571 char buf[6];
12572 list_T *l;
12573
12574 llpos = &cur->pos.pos[i];
12575 if (llpos->lnum == 0)
12576 break;
12577 l = list_alloc();
12578 if (l == NULL)
12579 break;
12580 list_append_number(l, (varnumber_T)llpos->lnum);
12581 if (llpos->col > 0)
12582 {
12583 list_append_number(l, (varnumber_T)llpos->col);
12584 list_append_number(l, (varnumber_T)llpos->len);
12585 }
12586 sprintf(buf, "pos%d", i + 1);
12587 dict_add_list(dict, buf, l);
12588 }
12589 }
12590 else
12591 {
12592 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12593 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012594 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012595 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12596 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012597# ifdef FEAT_CONCEAL
12598 if (cur->conceal_char)
12599 {
12600 char_u buf[MB_MAXBYTES + 1];
12601
12602 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12603 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12604 }
12605# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012606 list_append_dict(rettv->vval.v_list, dict);
12607 cur = cur->next;
12608 }
12609 }
12610#endif
12611}
12612
12613/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012614 * "getpid()" function
12615 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012617f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012618{
12619 rettv->vval.v_number = mch_get_pid();
12620}
12621
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012622static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012623
12624/*
12625 * "getcurpos()" function
12626 */
12627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012628f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012629{
12630 getpos_both(argvars, rettv, TRUE);
12631}
12632
Bram Moolenaar18081e32008-02-20 19:11:07 +000012633/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012634 * "getpos(string)" function
12635 */
12636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012637f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012638{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012639 getpos_both(argvars, rettv, FALSE);
12640}
12641
12642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012643getpos_both(
12644 typval_T *argvars,
12645 typval_T *rettv,
12646 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012647{
Bram Moolenaara5525202006-03-02 22:52:09 +000012648 pos_T *fp;
12649 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012650 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012651
12652 if (rettv_list_alloc(rettv) == OK)
12653 {
12654 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012655 if (getcurpos)
12656 fp = &curwin->w_cursor;
12657 else
12658 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012659 if (fnum != -1)
12660 list_append_number(l, (varnumber_T)fnum);
12661 else
12662 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012663 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12664 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012665 list_append_number(l, (fp != NULL)
12666 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012667 : (varnumber_T)0);
12668 list_append_number(l,
12669#ifdef FEAT_VIRTUALEDIT
12670 (fp != NULL) ? (varnumber_T)fp->coladd :
12671#endif
12672 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012673 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012674 {
12675 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012676 list_append_number(l, curwin->w_curswant == MAXCOL ?
12677 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012678 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012679 }
12680 else
12681 rettv->vval.v_number = FALSE;
12682}
12683
12684/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012685 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012686 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012688f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012689{
12690#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012691 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012692#endif
12693
Bram Moolenaar2641f772005-03-25 21:58:17 +000012694#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012695 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012696 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012697 wp = NULL;
12698 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12699 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012700 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012701 if (wp == NULL)
12702 return;
12703 }
12704
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012705 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012706 }
12707#endif
12708}
12709
12710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 * "getreg()" function
12712 */
12713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012714f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715{
12716 char_u *strregname;
12717 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012718 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012719 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012720 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012722 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012724 strregname = get_tv_string_chk(&argvars[0]);
12725 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012726 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012727 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012728 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012729 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12730 return_list = get_tv_number_chk(&argvars[2], &error);
12731 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012734 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012735
12736 if (error)
12737 return;
12738
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 regname = (strregname == NULL ? '"' : *strregname);
12740 if (regname == 0)
12741 regname = '"';
12742
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012743 if (return_list)
12744 {
12745 rettv->v_type = VAR_LIST;
12746 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12747 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012748 if (rettv->vval.v_list != NULL)
12749 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012750 }
12751 else
12752 {
12753 rettv->v_type = VAR_STRING;
12754 rettv->vval.v_string = get_reg_contents(regname,
12755 arg2 ? GREG_EXPR_SRC : 0);
12756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757}
12758
12759/*
12760 * "getregtype()" function
12761 */
12762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012763f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764{
12765 char_u *strregname;
12766 int regname;
12767 char_u buf[NUMBUFLEN + 2];
12768 long reglen = 0;
12769
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012770 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012771 {
12772 strregname = get_tv_string_chk(&argvars[0]);
12773 if (strregname == NULL) /* type error; errmsg already given */
12774 {
12775 rettv->v_type = VAR_STRING;
12776 rettv->vval.v_string = NULL;
12777 return;
12778 }
12779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 else
12781 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012782 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783
12784 regname = (strregname == NULL ? '"' : *strregname);
12785 if (regname == 0)
12786 regname = '"';
12787
12788 buf[0] = NUL;
12789 buf[1] = NUL;
12790 switch (get_reg_type(regname, &reglen))
12791 {
12792 case MLINE: buf[0] = 'V'; break;
12793 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 case MBLOCK:
12795 buf[0] = Ctrl_V;
12796 sprintf((char *)buf + 1, "%ld", reglen + 1);
12797 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 rettv->v_type = VAR_STRING;
12800 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801}
12802
12803/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012804 * "gettabvar()" function
12805 */
12806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012807f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012808{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012809 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012810 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012811 dictitem_T *v;
12812 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012813 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012814
12815 rettv->v_type = VAR_STRING;
12816 rettv->vval.v_string = NULL;
12817
12818 varname = get_tv_string_chk(&argvars[1]);
12819 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12820 if (tp != NULL && varname != NULL)
12821 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012822 /* Set tp to be our tabpage, temporarily. Also set the window to the
12823 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012824 if (switch_win(&oldcurwin, &oldtabpage,
12825 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012826 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012827 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012828 /* look up the variable */
12829 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12830 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12831 if (v != NULL)
12832 {
12833 copy_tv(&v->di_tv, rettv);
12834 done = TRUE;
12835 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012836 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012837
12838 /* restore previous notion of curwin */
12839 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012840 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012841
12842 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012843 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012844}
12845
12846/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012847 * "gettabwinvar()" function
12848 */
12849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012850f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012851{
12852 getwinvar(argvars, rettv, 1);
12853}
12854
12855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 * "getwinposx()" function
12857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012859f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862#ifdef FEAT_GUI
12863 if (gui.in_use)
12864 {
12865 int x, y;
12866
12867 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 }
12870#endif
12871}
12872
12873/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012874 * "win_findbuf()" function
12875 */
12876 static void
12877f_win_findbuf(typval_T *argvars, typval_T *rettv)
12878{
12879 if (rettv_list_alloc(rettv) != FAIL)
12880 win_findbuf(argvars, rettv->vval.v_list);
12881}
12882
12883/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012884 * "win_getid()" function
12885 */
12886 static void
12887f_win_getid(typval_T *argvars, typval_T *rettv)
12888{
12889 rettv->vval.v_number = win_getid(argvars);
12890}
12891
12892/*
12893 * "win_gotoid()" function
12894 */
12895 static void
12896f_win_gotoid(typval_T *argvars, typval_T *rettv)
12897{
12898 rettv->vval.v_number = win_gotoid(argvars);
12899}
12900
12901/*
12902 * "win_id2tabwin()" function
12903 */
12904 static void
12905f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
12906{
12907 if (rettv_list_alloc(rettv) != FAIL)
12908 win_id2tabwin(argvars, rettv->vval.v_list);
12909}
12910
12911/*
12912 * "win_id2win()" function
12913 */
12914 static void
12915f_win_id2win(typval_T *argvars, typval_T *rettv)
12916{
12917 rettv->vval.v_number = win_id2win(argvars);
12918}
12919
12920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 * "getwinposy()" function
12922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012924f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012926 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927#ifdef FEAT_GUI
12928 if (gui.in_use)
12929 {
12930 int x, y;
12931
12932 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934 }
12935#endif
12936}
12937
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012938/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012939 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012940 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012941 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012942find_win_by_nr(
12943 typval_T *vp,
12944 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012945{
12946#ifdef FEAT_WINDOWS
12947 win_T *wp;
12948#endif
12949 int nr;
12950
12951 nr = get_tv_number_chk(vp, NULL);
12952
12953#ifdef FEAT_WINDOWS
12954 if (nr < 0)
12955 return NULL;
12956 if (nr == 0)
12957 return curwin;
12958
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012959 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12960 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012961 if (--nr <= 0)
12962 break;
12963 return wp;
12964#else
12965 if (nr == 0 || nr == 1)
12966 return curwin;
12967 return NULL;
12968#endif
12969}
12970
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012972 * Find window specified by "wvp" in tabpage "tvp".
12973 */
12974 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012975find_tabwin(
12976 typval_T *wvp, /* VAR_UNKNOWN for current window */
12977 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012978{
12979 win_T *wp = NULL;
12980 tabpage_T *tp = NULL;
12981 long n;
12982
12983 if (wvp->v_type != VAR_UNKNOWN)
12984 {
12985 if (tvp->v_type != VAR_UNKNOWN)
12986 {
12987 n = get_tv_number(tvp);
12988 if (n >= 0)
12989 tp = find_tabpage(n);
12990 }
12991 else
12992 tp = curtab;
12993
12994 if (tp != NULL)
12995 wp = find_win_by_nr(wvp, tp);
12996 }
12997 else
12998 wp = curwin;
12999
13000 return wp;
13001}
13002
13003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004 * "getwinvar()" function
13005 */
13006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013007f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013009 getwinvar(argvars, rettv, 0);
13010}
13011
13012/*
13013 * getwinvar() and gettabwinvar()
13014 */
13015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013016getwinvar(
13017 typval_T *argvars,
13018 typval_T *rettv,
13019 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013020{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013021 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013023 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013024 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013025 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013026#ifdef FEAT_WINDOWS
13027 win_T *oldcurwin;
13028 tabpage_T *oldtabpage;
13029 int need_switch_win;
13030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013032#ifdef FEAT_WINDOWS
13033 if (off == 1)
13034 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13035 else
13036 tp = curtab;
13037#endif
13038 win = find_win_by_nr(&argvars[off], tp);
13039 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013040 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013042 rettv->v_type = VAR_STRING;
13043 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044
13045 if (win != NULL && varname != NULL)
13046 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013047#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013048 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013049 * otherwise the window is not valid. Only do this when needed,
13050 * autocommands get blocked. */
13051 need_switch_win = !(tp == curtab && win == curwin);
13052 if (!need_switch_win
13053 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13054#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013055 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013056 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013057 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013058 if (get_option_tv(&varname, rettv, 1) == OK)
13059 done = TRUE;
13060 }
13061 else
13062 {
13063 /* Look up the variable. */
13064 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13065 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13066 varname, FALSE);
13067 if (v != NULL)
13068 {
13069 copy_tv(&v->di_tv, rettv);
13070 done = TRUE;
13071 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013074
Bram Moolenaarba117c22015-09-29 16:53:22 +020013075#ifdef FEAT_WINDOWS
13076 if (need_switch_win)
13077 /* restore previous notion of curwin */
13078 restore_win(oldcurwin, oldtabpage, TRUE);
13079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080 }
13081
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013082 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13083 /* use the default return value */
13084 copy_tv(&argvars[off + 2], rettv);
13085
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086 --emsg_off;
13087}
13088
13089/*
13090 * "glob()" function
13091 */
13092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013093f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013095 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013097 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013099 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013100 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013102 if (argvars[1].v_type != VAR_UNKNOWN)
13103 {
13104 if (get_tv_number_chk(&argvars[1], &error))
13105 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013106 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013107 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013108 if (get_tv_number_chk(&argvars[2], &error))
13109 {
13110 rettv->v_type = VAR_LIST;
13111 rettv->vval.v_list = NULL;
13112 }
13113 if (argvars[3].v_type != VAR_UNKNOWN
13114 && get_tv_number_chk(&argvars[3], &error))
13115 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013116 }
13117 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013118 if (!error)
13119 {
13120 ExpandInit(&xpc);
13121 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013122 if (p_wic)
13123 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013124 if (rettv->v_type == VAR_STRING)
13125 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013126 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013127 else if (rettv_list_alloc(rettv) != FAIL)
13128 {
13129 int i;
13130
13131 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13132 NULL, options, WILD_ALL_KEEP);
13133 for (i = 0; i < xpc.xp_numfiles; i++)
13134 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13135
13136 ExpandCleanup(&xpc);
13137 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013138 }
13139 else
13140 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141}
13142
13143/*
13144 * "globpath()" function
13145 */
13146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013147f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013149 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013151 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013152 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013153 garray_T ga;
13154 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013156 /* When the optional second argument is non-zero, don't remove matches
13157 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013159 if (argvars[2].v_type != VAR_UNKNOWN)
13160 {
13161 if (get_tv_number_chk(&argvars[2], &error))
13162 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013163 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013164 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013165 if (get_tv_number_chk(&argvars[3], &error))
13166 {
13167 rettv->v_type = VAR_LIST;
13168 rettv->vval.v_list = NULL;
13169 }
13170 if (argvars[4].v_type != VAR_UNKNOWN
13171 && get_tv_number_chk(&argvars[4], &error))
13172 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013173 }
13174 }
13175 if (file != NULL && !error)
13176 {
13177 ga_init2(&ga, (int)sizeof(char_u *), 10);
13178 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13179 if (rettv->v_type == VAR_STRING)
13180 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13181 else if (rettv_list_alloc(rettv) != FAIL)
13182 for (i = 0; i < ga.ga_len; ++i)
13183 list_append_string(rettv->vval.v_list,
13184 ((char_u **)(ga.ga_data))[i], -1);
13185 ga_clear_strings(&ga);
13186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013187 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013188 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189}
13190
13191/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013192 * "glob2regpat()" function
13193 */
13194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013195f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013196{
13197 char_u *pat = get_tv_string_chk(&argvars[0]);
13198
13199 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013200 rettv->vval.v_string = (pat == NULL)
13201 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013202}
13203
13204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 * "has()" function
13206 */
13207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013208f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209{
13210 int i;
13211 char_u *name;
13212 int n = FALSE;
13213 static char *(has_list[]) =
13214 {
13215#ifdef AMIGA
13216 "amiga",
13217# ifdef FEAT_ARP
13218 "arp",
13219# endif
13220#endif
13221#ifdef __BEOS__
13222 "beos",
13223#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013224#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 "mac",
13226#endif
13227#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013228 "macunix", /* built with 'darwin' enabled */
13229#endif
13230#if defined(__APPLE__) && __APPLE__ == 1
13231 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233#ifdef __QNX__
13234 "qnx",
13235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236#ifdef UNIX
13237 "unix",
13238#endif
13239#ifdef VMS
13240 "vms",
13241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242#ifdef WIN32
13243 "win32",
13244#endif
13245#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13246 "win32unix",
13247#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013248#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249 "win64",
13250#endif
13251#ifdef EBCDIC
13252 "ebcdic",
13253#endif
13254#ifndef CASE_INSENSITIVE_FILENAME
13255 "fname_case",
13256#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013257#ifdef HAVE_ACL
13258 "acl",
13259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260#ifdef FEAT_ARABIC
13261 "arabic",
13262#endif
13263#ifdef FEAT_AUTOCMD
13264 "autocmd",
13265#endif
13266#ifdef FEAT_BEVAL
13267 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013268# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13269 "balloon_multiline",
13270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271#endif
13272#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13273 "builtin_terms",
13274# ifdef ALL_BUILTIN_TCAPS
13275 "all_builtin_terms",
13276# endif
13277#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013278#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13279 || defined(FEAT_GUI_W32) \
13280 || defined(FEAT_GUI_MOTIF))
13281 "browsefilter",
13282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283#ifdef FEAT_BYTEOFF
13284 "byte_offset",
13285#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013286#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013287 "channel",
13288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289#ifdef FEAT_CINDENT
13290 "cindent",
13291#endif
13292#ifdef FEAT_CLIENTSERVER
13293 "clientserver",
13294#endif
13295#ifdef FEAT_CLIPBOARD
13296 "clipboard",
13297#endif
13298#ifdef FEAT_CMDL_COMPL
13299 "cmdline_compl",
13300#endif
13301#ifdef FEAT_CMDHIST
13302 "cmdline_hist",
13303#endif
13304#ifdef FEAT_COMMENTS
13305 "comments",
13306#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013307#ifdef FEAT_CONCEAL
13308 "conceal",
13309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310#ifdef FEAT_CRYPT
13311 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013312 "crypt-blowfish",
13313 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314#endif
13315#ifdef FEAT_CSCOPE
13316 "cscope",
13317#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013318#ifdef FEAT_CURSORBIND
13319 "cursorbind",
13320#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013321#ifdef CURSOR_SHAPE
13322 "cursorshape",
13323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324#ifdef DEBUG
13325 "debug",
13326#endif
13327#ifdef FEAT_CON_DIALOG
13328 "dialog_con",
13329#endif
13330#ifdef FEAT_GUI_DIALOG
13331 "dialog_gui",
13332#endif
13333#ifdef FEAT_DIFF
13334 "diff",
13335#endif
13336#ifdef FEAT_DIGRAPHS
13337 "digraphs",
13338#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013339#ifdef FEAT_DIRECTX
13340 "directx",
13341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342#ifdef FEAT_DND
13343 "dnd",
13344#endif
13345#ifdef FEAT_EMACS_TAGS
13346 "emacs_tags",
13347#endif
13348 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013349 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350#ifdef FEAT_SEARCH_EXTRA
13351 "extra_search",
13352#endif
13353#ifdef FEAT_FKMAP
13354 "farsi",
13355#endif
13356#ifdef FEAT_SEARCHPATH
13357 "file_in_path",
13358#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013359#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013360 "filterpipe",
13361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362#ifdef FEAT_FIND_ID
13363 "find_in_path",
13364#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013365#ifdef FEAT_FLOAT
13366 "float",
13367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368#ifdef FEAT_FOLDING
13369 "folding",
13370#endif
13371#ifdef FEAT_FOOTER
13372 "footer",
13373#endif
13374#if !defined(USE_SYSTEM) && defined(UNIX)
13375 "fork",
13376#endif
13377#ifdef FEAT_GETTEXT
13378 "gettext",
13379#endif
13380#ifdef FEAT_GUI
13381 "gui",
13382#endif
13383#ifdef FEAT_GUI_ATHENA
13384# ifdef FEAT_GUI_NEXTAW
13385 "gui_neXtaw",
13386# else
13387 "gui_athena",
13388# endif
13389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390#ifdef FEAT_GUI_GTK
13391 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013392# ifdef USE_GTK3
13393 "gui_gtk3",
13394# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013398#ifdef FEAT_GUI_GNOME
13399 "gui_gnome",
13400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401#ifdef FEAT_GUI_MAC
13402 "gui_mac",
13403#endif
13404#ifdef FEAT_GUI_MOTIF
13405 "gui_motif",
13406#endif
13407#ifdef FEAT_GUI_PHOTON
13408 "gui_photon",
13409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410#ifdef FEAT_GUI_W32
13411 "gui_win32",
13412#endif
13413#ifdef FEAT_HANGULIN
13414 "hangul_input",
13415#endif
13416#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13417 "iconv",
13418#endif
13419#ifdef FEAT_INS_EXPAND
13420 "insert_expand",
13421#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013422#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013423 "job",
13424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425#ifdef FEAT_JUMPLIST
13426 "jumplist",
13427#endif
13428#ifdef FEAT_KEYMAP
13429 "keymap",
13430#endif
13431#ifdef FEAT_LANGMAP
13432 "langmap",
13433#endif
13434#ifdef FEAT_LIBCALL
13435 "libcall",
13436#endif
13437#ifdef FEAT_LINEBREAK
13438 "linebreak",
13439#endif
13440#ifdef FEAT_LISP
13441 "lispindent",
13442#endif
13443#ifdef FEAT_LISTCMDS
13444 "listcmds",
13445#endif
13446#ifdef FEAT_LOCALMAP
13447 "localmap",
13448#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013449#ifdef FEAT_LUA
13450# ifndef DYNAMIC_LUA
13451 "lua",
13452# endif
13453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454#ifdef FEAT_MENU
13455 "menu",
13456#endif
13457#ifdef FEAT_SESSION
13458 "mksession",
13459#endif
13460#ifdef FEAT_MODIFY_FNAME
13461 "modify_fname",
13462#endif
13463#ifdef FEAT_MOUSE
13464 "mouse",
13465#endif
13466#ifdef FEAT_MOUSESHAPE
13467 "mouseshape",
13468#endif
13469#if defined(UNIX) || defined(VMS)
13470# ifdef FEAT_MOUSE_DEC
13471 "mouse_dec",
13472# endif
13473# ifdef FEAT_MOUSE_GPM
13474 "mouse_gpm",
13475# endif
13476# ifdef FEAT_MOUSE_JSB
13477 "mouse_jsbterm",
13478# endif
13479# ifdef FEAT_MOUSE_NET
13480 "mouse_netterm",
13481# endif
13482# ifdef FEAT_MOUSE_PTERM
13483 "mouse_pterm",
13484# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013485# ifdef FEAT_MOUSE_SGR
13486 "mouse_sgr",
13487# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013488# ifdef FEAT_SYSMOUSE
13489 "mouse_sysmouse",
13490# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013491# ifdef FEAT_MOUSE_URXVT
13492 "mouse_urxvt",
13493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494# ifdef FEAT_MOUSE_XTERM
13495 "mouse_xterm",
13496# endif
13497#endif
13498#ifdef FEAT_MBYTE
13499 "multi_byte",
13500#endif
13501#ifdef FEAT_MBYTE_IME
13502 "multi_byte_ime",
13503#endif
13504#ifdef FEAT_MULTI_LANG
13505 "multi_lang",
13506#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013507#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013508#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013509 "mzscheme",
13510#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512#ifdef FEAT_OLE
13513 "ole",
13514#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013515 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516#ifdef FEAT_PATH_EXTRA
13517 "path_extra",
13518#endif
13519#ifdef FEAT_PERL
13520#ifndef DYNAMIC_PERL
13521 "perl",
13522#endif
13523#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013524#ifdef FEAT_PERSISTENT_UNDO
13525 "persistent_undo",
13526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527#ifdef FEAT_PYTHON
13528#ifndef DYNAMIC_PYTHON
13529 "python",
13530#endif
13531#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013532#ifdef FEAT_PYTHON3
13533#ifndef DYNAMIC_PYTHON3
13534 "python3",
13535#endif
13536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537#ifdef FEAT_POSTSCRIPT
13538 "postscript",
13539#endif
13540#ifdef FEAT_PRINTER
13541 "printer",
13542#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013543#ifdef FEAT_PROFILE
13544 "profile",
13545#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013546#ifdef FEAT_RELTIME
13547 "reltime",
13548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549#ifdef FEAT_QUICKFIX
13550 "quickfix",
13551#endif
13552#ifdef FEAT_RIGHTLEFT
13553 "rightleft",
13554#endif
13555#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13556 "ruby",
13557#endif
13558#ifdef FEAT_SCROLLBIND
13559 "scrollbind",
13560#endif
13561#ifdef FEAT_CMDL_INFO
13562 "showcmd",
13563 "cmdline_info",
13564#endif
13565#ifdef FEAT_SIGNS
13566 "signs",
13567#endif
13568#ifdef FEAT_SMARTINDENT
13569 "smartindent",
13570#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013571#ifdef STARTUPTIME
13572 "startuptime",
13573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574#ifdef FEAT_STL_OPT
13575 "statusline",
13576#endif
13577#ifdef FEAT_SUN_WORKSHOP
13578 "sun_workshop",
13579#endif
13580#ifdef FEAT_NETBEANS_INTG
13581 "netbeans_intg",
13582#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013583#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013584 "spell",
13585#endif
13586#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 "syntax",
13588#endif
13589#if defined(USE_SYSTEM) || !defined(UNIX)
13590 "system",
13591#endif
13592#ifdef FEAT_TAG_BINS
13593 "tag_binary",
13594#endif
13595#ifdef FEAT_TAG_OLDSTATIC
13596 "tag_old_static",
13597#endif
13598#ifdef FEAT_TAG_ANYWHITE
13599 "tag_any_white",
13600#endif
13601#ifdef FEAT_TCL
13602# ifndef DYNAMIC_TCL
13603 "tcl",
13604# endif
13605#endif
13606#ifdef TERMINFO
13607 "terminfo",
13608#endif
13609#ifdef FEAT_TERMRESPONSE
13610 "termresponse",
13611#endif
13612#ifdef FEAT_TEXTOBJ
13613 "textobjects",
13614#endif
13615#ifdef HAVE_TGETENT
13616 "tgetent",
13617#endif
13618#ifdef FEAT_TITLE
13619 "title",
13620#endif
13621#ifdef FEAT_TOOLBAR
13622 "toolbar",
13623#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013624#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13625 "unnamedplus",
13626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627#ifdef FEAT_USR_CMDS
13628 "user-commands", /* was accidentally included in 5.4 */
13629 "user_commands",
13630#endif
13631#ifdef FEAT_VIMINFO
13632 "viminfo",
13633#endif
13634#ifdef FEAT_VERTSPLIT
13635 "vertsplit",
13636#endif
13637#ifdef FEAT_VIRTUALEDIT
13638 "virtualedit",
13639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641#ifdef FEAT_VISUALEXTRA
13642 "visualextra",
13643#endif
13644#ifdef FEAT_VREPLACE
13645 "vreplace",
13646#endif
13647#ifdef FEAT_WILDIGN
13648 "wildignore",
13649#endif
13650#ifdef FEAT_WILDMENU
13651 "wildmenu",
13652#endif
13653#ifdef FEAT_WINDOWS
13654 "windows",
13655#endif
13656#ifdef FEAT_WAK
13657 "winaltkeys",
13658#endif
13659#ifdef FEAT_WRITEBACKUP
13660 "writebackup",
13661#endif
13662#ifdef FEAT_XIM
13663 "xim",
13664#endif
13665#ifdef FEAT_XFONTSET
13666 "xfontset",
13667#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013668#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013669 "xpm",
13670 "xpm_w32", /* for backward compatibility */
13671#else
13672# if defined(HAVE_XPM)
13673 "xpm",
13674# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676#ifdef USE_XSMP
13677 "xsmp",
13678#endif
13679#ifdef USE_XSMP_INTERACT
13680 "xsmp_interact",
13681#endif
13682#ifdef FEAT_XCLIPBOARD
13683 "xterm_clipboard",
13684#endif
13685#ifdef FEAT_XTERM_SAVE
13686 "xterm_save",
13687#endif
13688#if defined(UNIX) && defined(FEAT_X11)
13689 "X11",
13690#endif
13691 NULL
13692 };
13693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 for (i = 0; has_list[i] != NULL; ++i)
13696 if (STRICMP(name, has_list[i]) == 0)
13697 {
13698 n = TRUE;
13699 break;
13700 }
13701
13702 if (n == FALSE)
13703 {
13704 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013705 {
13706 if (name[5] == '-'
13707 && STRLEN(name) > 11
13708 && vim_isdigit(name[6])
13709 && vim_isdigit(name[8])
13710 && vim_isdigit(name[10]))
13711 {
13712 int major = atoi((char *)name + 6);
13713 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013714
13715 /* Expect "patch-9.9.01234". */
13716 n = (major < VIM_VERSION_MAJOR
13717 || (major == VIM_VERSION_MAJOR
13718 && (minor < VIM_VERSION_MINOR
13719 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013720 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013721 }
13722 else
13723 n = has_patch(atoi((char *)name + 5));
13724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725 else if (STRICMP(name, "vim_starting") == 0)
13726 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013727#ifdef FEAT_MBYTE
13728 else if (STRICMP(name, "multi_byte_encoding") == 0)
13729 n = has_mbyte;
13730#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013731#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13732 else if (STRICMP(name, "balloon_multiline") == 0)
13733 n = multiline_balloon_available();
13734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735#ifdef DYNAMIC_TCL
13736 else if (STRICMP(name, "tcl") == 0)
13737 n = tcl_enabled(FALSE);
13738#endif
13739#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13740 else if (STRICMP(name, "iconv") == 0)
13741 n = iconv_enabled(FALSE);
13742#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013743#ifdef DYNAMIC_LUA
13744 else if (STRICMP(name, "lua") == 0)
13745 n = lua_enabled(FALSE);
13746#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013747#ifdef DYNAMIC_MZSCHEME
13748 else if (STRICMP(name, "mzscheme") == 0)
13749 n = mzscheme_enabled(FALSE);
13750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751#ifdef DYNAMIC_RUBY
13752 else if (STRICMP(name, "ruby") == 0)
13753 n = ruby_enabled(FALSE);
13754#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013755#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756#ifdef DYNAMIC_PYTHON
13757 else if (STRICMP(name, "python") == 0)
13758 n = python_enabled(FALSE);
13759#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013760#endif
13761#ifdef FEAT_PYTHON3
13762#ifdef DYNAMIC_PYTHON3
13763 else if (STRICMP(name, "python3") == 0)
13764 n = python3_enabled(FALSE);
13765#endif
13766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767#ifdef DYNAMIC_PERL
13768 else if (STRICMP(name, "perl") == 0)
13769 n = perl_enabled(FALSE);
13770#endif
13771#ifdef FEAT_GUI
13772 else if (STRICMP(name, "gui_running") == 0)
13773 n = (gui.in_use || gui.starting);
13774# ifdef FEAT_GUI_W32
13775 else if (STRICMP(name, "gui_win32s") == 0)
13776 n = gui_is_win32s();
13777# endif
13778# ifdef FEAT_BROWSE
13779 else if (STRICMP(name, "browse") == 0)
13780 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13781# endif
13782#endif
13783#ifdef FEAT_SYN_HL
13784 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013785 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786#endif
13787#if defined(WIN3264)
13788 else if (STRICMP(name, "win95") == 0)
13789 n = mch_windows95();
13790#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013791#ifdef FEAT_NETBEANS_INTG
13792 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013793 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 }
13796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798}
13799
13800/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013801 * "has_key()" function
13802 */
13803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013804f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013805{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013806 if (argvars[0].v_type != VAR_DICT)
13807 {
13808 EMSG(_(e_dictreq));
13809 return;
13810 }
13811 if (argvars[0].vval.v_dict == NULL)
13812 return;
13813
13814 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013815 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013816}
13817
13818/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013819 * "haslocaldir()" function
13820 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013822f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013823{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013824 win_T *wp = NULL;
13825
13826 wp = find_tabwin(&argvars[0], &argvars[1]);
13827 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013828}
13829
13830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 * "hasmapto()" function
13832 */
13833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013834f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835{
13836 char_u *name;
13837 char_u *mode;
13838 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013839 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013842 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 mode = (char_u *)"nvo";
13844 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013846 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013847 if (argvars[2].v_type != VAR_UNKNOWN)
13848 abbr = get_tv_number(&argvars[2]);
13849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850
Bram Moolenaar2c932302006-03-18 21:42:09 +000013851 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013852 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855}
13856
13857/*
13858 * "histadd()" function
13859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013861f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862{
13863#ifdef FEAT_CMDHIST
13864 int histype;
13865 char_u *str;
13866 char_u buf[NUMBUFLEN];
13867#endif
13868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013869 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 if (check_restricted() || check_secure())
13871 return;
13872#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13874 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 if (histype >= 0)
13876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013877 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 if (*str != NUL)
13879 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013880 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013882 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 return;
13884 }
13885 }
13886#endif
13887}
13888
13889/*
13890 * "histdel()" function
13891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013893f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894{
13895#ifdef FEAT_CMDHIST
13896 int n;
13897 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013900 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13901 if (str == NULL)
13902 n = 0;
13903 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013905 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013906 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013908 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013909 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 else
13911 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013912 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913 get_tv_string_buf(&argvars[1], buf));
13914 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915#endif
13916}
13917
13918/*
13919 * "histget()" function
13920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013922f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923{
13924#ifdef FEAT_CMDHIST
13925 int type;
13926 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013927 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013929 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13930 if (str == NULL)
13931 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013933 {
13934 type = get_histtype(str);
13935 if (argvars[1].v_type == VAR_UNKNOWN)
13936 idx = get_history_idx(type);
13937 else
13938 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13939 /* -1 on type error */
13940 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013943 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013945 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946}
13947
13948/*
13949 * "histnr()" function
13950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013952f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953{
13954 int i;
13955
13956#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013957 char_u *history = get_tv_string_chk(&argvars[0]);
13958
13959 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 if (i >= HIST_CMD && i < HIST_COUNT)
13961 i = get_history_idx(i);
13962 else
13963#endif
13964 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013965 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966}
13967
13968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 * "highlightID(name)" function
13970 */
13971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013972f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975}
13976
13977/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013978 * "highlight_exists()" function
13979 */
13980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013981f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013982{
13983 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13984}
13985
13986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 * "hostname()" function
13988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013990f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991{
13992 char_u hostname[256];
13993
13994 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995 rettv->v_type = VAR_STRING;
13996 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997}
13998
13999/*
14000 * iconv() function
14001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014003f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004{
14005#ifdef FEAT_MBYTE
14006 char_u buf1[NUMBUFLEN];
14007 char_u buf2[NUMBUFLEN];
14008 char_u *from, *to, *str;
14009 vimconv_T vimconv;
14010#endif
14011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014012 rettv->v_type = VAR_STRING;
14013 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014
14015#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014016 str = get_tv_string(&argvars[0]);
14017 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14018 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019 vimconv.vc_type = CONV_NONE;
14020 convert_setup(&vimconv, from, to);
14021
14022 /* If the encodings are equal, no conversion needed. */
14023 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014024 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014026 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027
14028 convert_setup(&vimconv, NULL, NULL);
14029 vim_free(from);
14030 vim_free(to);
14031#endif
14032}
14033
14034/*
14035 * "indent()" function
14036 */
14037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014038f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039{
14040 linenr_T lnum;
14041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014042 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014044 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014046 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047}
14048
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014049/*
14050 * "index()" function
14051 */
14052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014053f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014054{
Bram Moolenaar33570922005-01-25 22:26:29 +000014055 list_T *l;
14056 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014057 long idx = 0;
14058 int ic = FALSE;
14059
14060 rettv->vval.v_number = -1;
14061 if (argvars[0].v_type != VAR_LIST)
14062 {
14063 EMSG(_(e_listreq));
14064 return;
14065 }
14066 l = argvars[0].vval.v_list;
14067 if (l != NULL)
14068 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014069 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014070 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014072 int error = FALSE;
14073
Bram Moolenaar758711c2005-02-02 23:11:38 +000014074 /* Start at specified item. Use the cached index that list_find()
14075 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014077 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014078 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014079 ic = get_tv_number_chk(&argvars[3], &error);
14080 if (error)
14081 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014082 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014083
Bram Moolenaar758711c2005-02-02 23:11:38 +000014084 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014085 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014086 {
14087 rettv->vval.v_number = idx;
14088 break;
14089 }
14090 }
14091}
14092
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093static int inputsecret_flag = 0;
14094
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014095static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014096
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014098 * This function is used by f_input() and f_inputdialog() functions. The third
14099 * argument to f_input() specifies the type of completion to use at the
14100 * prompt. The third argument to f_inputdialog() specifies the value to return
14101 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 */
14103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014104get_user_input(
14105 typval_T *argvars,
14106 typval_T *rettv,
14107 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110 char_u *p = NULL;
14111 int c;
14112 char_u buf[NUMBUFLEN];
14113 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014114 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014115 int xp_type = EXPAND_NOTHING;
14116 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014118 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014119 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120
14121#ifdef NO_CONSOLE_INPUT
14122 /* While starting up, there is no place to enter text. */
14123 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125#endif
14126
14127 cmd_silent = FALSE; /* Want to see the prompt. */
14128 if (prompt != NULL)
14129 {
14130 /* Only the part of the message after the last NL is considered as
14131 * prompt for the command line */
14132 p = vim_strrchr(prompt, '\n');
14133 if (p == NULL)
14134 p = prompt;
14135 else
14136 {
14137 ++p;
14138 c = *p;
14139 *p = NUL;
14140 msg_start();
14141 msg_clr_eos();
14142 msg_puts_attr(prompt, echo_attr);
14143 msg_didout = FALSE;
14144 msg_starthere();
14145 *p = c;
14146 }
14147 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 if (argvars[1].v_type != VAR_UNKNOWN)
14150 {
14151 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14152 if (defstr != NULL)
14153 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014155 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014156 {
14157 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014158 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014159 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014160
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014161 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014162 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014163
Bram Moolenaar4463f292005-09-25 22:20:24 +000014164 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14165 if (xp_name == NULL)
14166 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014167
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014168 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014169
Bram Moolenaar4463f292005-09-25 22:20:24 +000014170 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14171 &xp_arg) == FAIL)
14172 return;
14173 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014174 }
14175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014177 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014178 int save_ex_normal_busy = ex_normal_busy;
14179 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014181 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14182 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014183 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014184 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014185 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014186 && argvars[1].v_type != VAR_UNKNOWN
14187 && argvars[2].v_type != VAR_UNKNOWN)
14188 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14189 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014190
14191 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 /* since the user typed this, no need to wait for return */
14194 need_wait_return = FALSE;
14195 msg_didout = FALSE;
14196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 cmd_silent = cmd_silent_save;
14198}
14199
14200/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014201 * "input()" function
14202 * Also handles inputsecret() when inputsecret is set.
14203 */
14204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014205f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014206{
14207 get_user_input(argvars, rettv, FALSE);
14208}
14209
14210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 * "inputdialog()" function
14212 */
14213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014214f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215{
14216#if defined(FEAT_GUI_TEXTDIALOG)
14217 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14218 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14219 {
14220 char_u *message;
14221 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 message = get_tv_string_chk(&argvars[0]);
14225 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014226 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014227 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 else
14229 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 if (message != NULL && defstr != NULL
14231 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014232 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014233 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 else
14235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 if (message != NULL && defstr != NULL
14237 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014238 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239 rettv->vval.v_string = vim_strsave(
14240 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 }
14246 else
14247#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014248 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249}
14250
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014251/*
14252 * "inputlist()" function
14253 */
14254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014255f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014256{
14257 listitem_T *li;
14258 int selected;
14259 int mouse_used;
14260
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014261#ifdef NO_CONSOLE_INPUT
14262 /* While starting up, there is no place to enter text. */
14263 if (no_console_input())
14264 return;
14265#endif
14266 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14267 {
14268 EMSG2(_(e_listarg), "inputlist()");
14269 return;
14270 }
14271
14272 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014273 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014274 lines_left = Rows; /* avoid more prompt */
14275 msg_scroll = TRUE;
14276 msg_clr_eos();
14277
14278 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14279 {
14280 msg_puts(get_tv_string(&li->li_tv));
14281 msg_putchar('\n');
14282 }
14283
14284 /* Ask for choice. */
14285 selected = prompt_for_number(&mouse_used);
14286 if (mouse_used)
14287 selected -= lines_left;
14288
14289 rettv->vval.v_number = selected;
14290}
14291
14292
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14294
14295/*
14296 * "inputrestore()" function
14297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014299f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300{
14301 if (ga_userinput.ga_len > 0)
14302 {
14303 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14305 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014306 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 }
14308 else if (p_verbose > 1)
14309 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014310 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 }
14313}
14314
14315/*
14316 * "inputsave()" function
14317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014319f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014321 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 if (ga_grow(&ga_userinput, 1) == OK)
14323 {
14324 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14325 + ga_userinput.ga_len);
14326 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014327 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 }
14329 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014330 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331}
14332
14333/*
14334 * "inputsecret()" function
14335 */
14336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014337f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338{
14339 ++cmdline_star;
14340 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 --cmdline_star;
14343 --inputsecret_flag;
14344}
14345
14346/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014347 * "insert()" function
14348 */
14349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014350f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014351{
14352 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014353 listitem_T *item;
14354 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014355 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014356
14357 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014358 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014359 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014360 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014361 {
14362 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 before = get_tv_number_chk(&argvars[2], &error);
14364 if (error)
14365 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014366
Bram Moolenaar758711c2005-02-02 23:11:38 +000014367 if (before == l->lv_len)
14368 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014369 else
14370 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014371 item = list_find(l, before);
14372 if (item == NULL)
14373 {
14374 EMSGN(_(e_listidx), before);
14375 l = NULL;
14376 }
14377 }
14378 if (l != NULL)
14379 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014380 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014381 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014382 }
14383 }
14384}
14385
14386/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014387 * "invert(expr)" function
14388 */
14389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014390f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014391{
14392 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14393}
14394
14395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 * "isdirectory()" function
14397 */
14398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014399f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014401 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402}
14403
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014404/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014405 * "islocked()" function
14406 */
14407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014408f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014409{
14410 lval_T lv;
14411 char_u *end;
14412 dictitem_T *di;
14413
14414 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014415 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14416 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014417 if (end != NULL && lv.ll_name != NULL)
14418 {
14419 if (*end != NUL)
14420 EMSG(_(e_trailing));
14421 else
14422 {
14423 if (lv.ll_tv == NULL)
14424 {
14425 if (check_changedtick(lv.ll_name))
14426 rettv->vval.v_number = 1; /* always locked */
14427 else
14428 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014429 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014430 if (di != NULL)
14431 {
14432 /* Consider a variable locked when:
14433 * 1. the variable itself is locked
14434 * 2. the value of the variable is locked.
14435 * 3. the List or Dict value is locked.
14436 */
14437 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14438 || tv_islocked(&di->di_tv));
14439 }
14440 }
14441 }
14442 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014443 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014444 else if (lv.ll_newkey != NULL)
14445 EMSG2(_(e_dictkey), lv.ll_newkey);
14446 else if (lv.ll_list != NULL)
14447 /* List item. */
14448 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14449 else
14450 /* Dictionary item. */
14451 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14452 }
14453 }
14454
14455 clear_lval(&lv);
14456}
14457
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014458#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14459/*
14460 * "isnan()" function
14461 */
14462 static void
14463f_isnan(typval_T *argvars, typval_T *rettv)
14464{
14465 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14466 && isnan(argvars[0].vval.v_float);
14467}
14468#endif
14469
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014470static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014471
14472/*
14473 * Turn a dict into a list:
14474 * "what" == 0: list of keys
14475 * "what" == 1: list of values
14476 * "what" == 2: list of items
14477 */
14478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014479dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014480{
Bram Moolenaar33570922005-01-25 22:26:29 +000014481 list_T *l2;
14482 dictitem_T *di;
14483 hashitem_T *hi;
14484 listitem_T *li;
14485 listitem_T *li2;
14486 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014487 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014488
Bram Moolenaar8c711452005-01-14 21:53:12 +000014489 if (argvars[0].v_type != VAR_DICT)
14490 {
14491 EMSG(_(e_dictreq));
14492 return;
14493 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014494 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014495 return;
14496
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014497 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014498 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014499
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014500 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014501 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014502 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014503 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014505 --todo;
14506 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014507
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014508 li = listitem_alloc();
14509 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014510 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014511 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014512
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014513 if (what == 0)
14514 {
14515 /* keys() */
14516 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014517 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014518 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14519 }
14520 else if (what == 1)
14521 {
14522 /* values() */
14523 copy_tv(&di->di_tv, &li->li_tv);
14524 }
14525 else
14526 {
14527 /* items() */
14528 l2 = list_alloc();
14529 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014530 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014531 li->li_tv.vval.v_list = l2;
14532 if (l2 == NULL)
14533 break;
14534 ++l2->lv_refcount;
14535
14536 li2 = listitem_alloc();
14537 if (li2 == NULL)
14538 break;
14539 list_append(l2, li2);
14540 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014541 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014542 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14543
14544 li2 = listitem_alloc();
14545 if (li2 == NULL)
14546 break;
14547 list_append(l2, li2);
14548 copy_tv(&di->di_tv, &li2->li_tv);
14549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014550 }
14551 }
14552}
14553
14554/*
14555 * "items(dict)" function
14556 */
14557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014558f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014559{
14560 dict_list(argvars, rettv, 2);
14561}
14562
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014563#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014564/*
14565 * Get the job from the argument.
14566 * Returns NULL if the job is invalid.
14567 */
14568 static job_T *
14569get_job_arg(typval_T *tv)
14570{
14571 job_T *job;
14572
14573 if (tv->v_type != VAR_JOB)
14574 {
14575 EMSG2(_(e_invarg2), get_tv_string(tv));
14576 return NULL;
14577 }
14578 job = tv->vval.v_job;
14579
14580 if (job == NULL)
14581 EMSG(_("E916: not a valid job"));
14582 return job;
14583}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014584
Bram Moolenaar835dc632016-02-07 14:27:38 +010014585/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014586 * "job_getchannel()" function
14587 */
14588 static void
14589f_job_getchannel(typval_T *argvars, typval_T *rettv)
14590{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014591 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014592
Bram Moolenaar65edff82016-02-21 16:40:11 +010014593 if (job != NULL)
14594 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014595 rettv->v_type = VAR_CHANNEL;
14596 rettv->vval.v_channel = job->jv_channel;
14597 if (job->jv_channel != NULL)
14598 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014599 }
14600}
14601
14602/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014603 * "job_info()" function
14604 */
14605 static void
14606f_job_info(typval_T *argvars, typval_T *rettv)
14607{
14608 job_T *job = get_job_arg(&argvars[0]);
14609
14610 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14611 job_info(job, rettv->vval.v_dict);
14612}
14613
14614/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014615 * "job_setoptions()" function
14616 */
14617 static void
14618f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14619{
14620 job_T *job = get_job_arg(&argvars[0]);
14621 jobopt_T opt;
14622
14623 if (job == NULL)
14624 return;
14625 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014626 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014627 return;
14628 job_set_options(job, &opt);
14629}
14630
14631/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014632 * "job_start()" function
14633 */
14634 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014635f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014636{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014637 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014638 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014639}
14640
14641/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014642 * "job_status()" function
14643 */
14644 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014645f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014646{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014647 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014648
Bram Moolenaar65edff82016-02-21 16:40:11 +010014649 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014650 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014651 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014652 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014653 }
14654}
14655
14656/*
14657 * "job_stop()" function
14658 */
14659 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014660f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014661{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014662 job_T *job = get_job_arg(&argvars[0]);
14663
14664 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014665 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014666}
14667#endif
14668
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014670 * "join()" function
14671 */
14672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014673f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014674{
14675 garray_T ga;
14676 char_u *sep;
14677
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014678 if (argvars[0].v_type != VAR_LIST)
14679 {
14680 EMSG(_(e_listreq));
14681 return;
14682 }
14683 if (argvars[0].vval.v_list == NULL)
14684 return;
14685 if (argvars[1].v_type == VAR_UNKNOWN)
14686 sep = (char_u *)" ";
14687 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014688 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014689
14690 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014691
14692 if (sep != NULL)
14693 {
14694 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014695 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014696 ga_append(&ga, NUL);
14697 rettv->vval.v_string = (char_u *)ga.ga_data;
14698 }
14699 else
14700 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014701}
14702
14703/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014704 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014705 */
14706 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014707f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014708{
14709 js_read_T reader;
14710
14711 reader.js_buf = get_tv_string(&argvars[0]);
14712 reader.js_fill = NULL;
14713 reader.js_used = 0;
14714 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14715 EMSG(_(e_invarg));
14716}
14717
14718/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014719 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014720 */
14721 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014722f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014723{
14724 rettv->v_type = VAR_STRING;
14725 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14726}
14727
14728/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014729 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014730 */
14731 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014732f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014733{
14734 js_read_T reader;
14735
14736 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014737 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014738 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014739 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014740 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014741}
14742
14743/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014744 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014745 */
14746 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014747f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014748{
14749 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014750 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014751}
14752
14753/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014754 * "keys()" function
14755 */
14756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014757f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014758{
14759 dict_list(argvars, rettv, 0);
14760}
14761
14762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 * "last_buffer_nr()" function.
14764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014766f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767{
14768 int n = 0;
14769 buf_T *buf;
14770
14771 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14772 if (n < buf->b_fnum)
14773 n = buf->b_fnum;
14774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014775 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014776}
14777
14778/*
14779 * "len()" function
14780 */
14781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014782f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014783{
14784 switch (argvars[0].v_type)
14785 {
14786 case VAR_STRING:
14787 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788 rettv->vval.v_number = (varnumber_T)STRLEN(
14789 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014790 break;
14791 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014792 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014793 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014794 case VAR_DICT:
14795 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14796 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014797 case VAR_UNKNOWN:
14798 case VAR_SPECIAL:
14799 case VAR_FLOAT:
14800 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014801 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014802 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014803 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014804 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014805 break;
14806 }
14807}
14808
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014809static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014810
14811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014812libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014813{
14814#ifdef FEAT_LIBCALL
14815 char_u *string_in;
14816 char_u **string_result;
14817 int nr_result;
14818#endif
14819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014820 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014821 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014822 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014823
14824 if (check_restricted() || check_secure())
14825 return;
14826
14827#ifdef FEAT_LIBCALL
14828 /* The first two args must be strings, otherwise its meaningless */
14829 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14830 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014831 string_in = NULL;
14832 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014833 string_in = argvars[2].vval.v_string;
14834 if (type == VAR_NUMBER)
14835 string_result = NULL;
14836 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014837 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014838 if (mch_libcall(argvars[0].vval.v_string,
14839 argvars[1].vval.v_string,
14840 string_in,
14841 argvars[2].vval.v_number,
14842 string_result,
14843 &nr_result) == OK
14844 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014845 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014846 }
14847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848}
14849
14850/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014851 * "libcall()" function
14852 */
14853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014854f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014855{
14856 libcall_common(argvars, rettv, VAR_STRING);
14857}
14858
14859/*
14860 * "libcallnr()" function
14861 */
14862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014863f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014864{
14865 libcall_common(argvars, rettv, VAR_NUMBER);
14866}
14867
14868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014869 * "line(string)" function
14870 */
14871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014872f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873{
14874 linenr_T lnum = 0;
14875 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014876 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014878 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879 if (fp != NULL)
14880 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014881 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882}
14883
14884/*
14885 * "line2byte(lnum)" function
14886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014888f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889{
14890#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892#else
14893 linenr_T lnum;
14894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014895 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014899 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14900 if (rettv->vval.v_number >= 0)
14901 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902#endif
14903}
14904
14905/*
14906 * "lispindent(lnum)" function
14907 */
14908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014909f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910{
14911#ifdef FEAT_LISP
14912 pos_T pos;
14913 linenr_T lnum;
14914
14915 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014916 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14918 {
14919 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014920 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 curwin->w_cursor = pos;
14922 }
14923 else
14924#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014925 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926}
14927
14928/*
14929 * "localtime()" function
14930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014932f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014934 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935}
14936
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014937static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938
14939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014940get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941{
14942 char_u *keys;
14943 char_u *which;
14944 char_u buf[NUMBUFLEN];
14945 char_u *keys_buf = NULL;
14946 char_u *rhs;
14947 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014948 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014949 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014950 mapblock_T *mp;
14951 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952
14953 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014954 rettv->v_type = VAR_STRING;
14955 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014957 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 if (*keys == NUL)
14959 return;
14960
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014961 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014963 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014965 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014966 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014967 if (argvars[3].v_type != VAR_UNKNOWN)
14968 get_dict = get_tv_number(&argvars[3]);
14969 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 else
14972 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014973 if (which == NULL)
14974 return;
14975
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 mode = get_map_mode(&which, 0);
14977
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014978 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014979 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014981
14982 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014984 /* Return a string. */
14985 if (rhs != NULL)
14986 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987
Bram Moolenaarbd743252010-10-20 21:23:33 +020014988 }
14989 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14990 {
14991 /* Return a dictionary. */
14992 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14993 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14994 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995
Bram Moolenaarbd743252010-10-20 21:23:33 +020014996 dict_add_nr_str(dict, "lhs", 0L, lhs);
14997 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14998 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14999 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15000 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15001 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15002 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015003 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015004 dict_add_nr_str(dict, "mode", 0L, mapmode);
15005
15006 vim_free(lhs);
15007 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 }
15009}
15010
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015011#ifdef FEAT_FLOAT
15012/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015013 * "log()" function
15014 */
15015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015016f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015017{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015018 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015019
15020 rettv->v_type = VAR_FLOAT;
15021 if (get_float_arg(argvars, &f) == OK)
15022 rettv->vval.v_float = log(f);
15023 else
15024 rettv->vval.v_float = 0.0;
15025}
15026
15027/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015028 * "log10()" function
15029 */
15030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015031f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015032{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015033 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015034
15035 rettv->v_type = VAR_FLOAT;
15036 if (get_float_arg(argvars, &f) == OK)
15037 rettv->vval.v_float = log10(f);
15038 else
15039 rettv->vval.v_float = 0.0;
15040}
15041#endif
15042
Bram Moolenaar1dced572012-04-05 16:54:08 +020015043#ifdef FEAT_LUA
15044/*
15045 * "luaeval()" function
15046 */
15047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015048f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015049{
15050 char_u *str;
15051 char_u buf[NUMBUFLEN];
15052
15053 str = get_tv_string_buf(&argvars[0], buf);
15054 do_luaeval(str, argvars + 1, rettv);
15055}
15056#endif
15057
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015059 * "map()" function
15060 */
15061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015062f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015063{
15064 filter_map(argvars, rettv, TRUE);
15065}
15066
15067/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069 */
15070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015071f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015073 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074}
15075
15076/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078 */
15079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015080f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083}
15084
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015085static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086
15087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015088find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015090 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015091 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015092 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 char_u *pat;
15094 regmatch_T regmatch;
15095 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015096 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097 char_u *save_cpo;
15098 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015099 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015100 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015101 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015102 list_T *l = NULL;
15103 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015104 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015105 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106
15107 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15108 save_cpo = p_cpo;
15109 p_cpo = (char_u *)"";
15110
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015111 rettv->vval.v_number = -1;
15112 if (type == 3)
15113 {
15114 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015115 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015116 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015117 }
15118 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015120 rettv->v_type = VAR_STRING;
15121 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015124 if (argvars[0].v_type == VAR_LIST)
15125 {
15126 if ((l = argvars[0].vval.v_list) == NULL)
15127 goto theend;
15128 li = l->lv_first;
15129 }
15130 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015131 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015132 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015133 len = (long)STRLEN(str);
15134 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015136 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15137 if (pat == NULL)
15138 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015139
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015140 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015142 int error = FALSE;
15143
15144 start = get_tv_number_chk(&argvars[2], &error);
15145 if (error)
15146 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015147 if (l != NULL)
15148 {
15149 li = list_find(l, start);
15150 if (li == NULL)
15151 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015152 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015153 }
15154 else
15155 {
15156 if (start < 0)
15157 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015158 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015159 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015160 /* When "count" argument is there ignore matches before "start",
15161 * otherwise skip part of the string. Differs when pattern is "^"
15162 * or "\<". */
15163 if (argvars[3].v_type != VAR_UNKNOWN)
15164 startcol = start;
15165 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015166 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015167 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015168 len -= start;
15169 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015170 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015171
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015172 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015173 nth = get_tv_number_chk(&argvars[3], &error);
15174 if (error)
15175 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 }
15177
15178 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15179 if (regmatch.regprog != NULL)
15180 {
15181 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015182
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015183 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015184 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015185 if (l != NULL)
15186 {
15187 if (li == NULL)
15188 {
15189 match = FALSE;
15190 break;
15191 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015192 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015193 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015194 if (str == NULL)
15195 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015196 }
15197
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015198 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015199
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015200 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015201 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015202 if (l == NULL && !match)
15203 break;
15204
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015205 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015206 if (l != NULL)
15207 {
15208 li = li->li_next;
15209 ++idx;
15210 }
15211 else
15212 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015213#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015214 startcol = (colnr_T)(regmatch.startp[0]
15215 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015216#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015217 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015218#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015219 if (startcol > (colnr_T)len
15220 || str + startcol <= regmatch.startp[0])
15221 {
15222 match = FALSE;
15223 break;
15224 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015225 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015226 }
15227
15228 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015230 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015232 int i;
15233
15234 /* return list with matched string and submatches */
15235 for (i = 0; i < NSUBEXP; ++i)
15236 {
15237 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015238 {
15239 if (list_append_string(rettv->vval.v_list,
15240 (char_u *)"", 0) == FAIL)
15241 break;
15242 }
15243 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015244 regmatch.startp[i],
15245 (int)(regmatch.endp[i] - regmatch.startp[i]))
15246 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015247 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015248 }
15249 }
15250 else if (type == 2)
15251 {
15252 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015253 if (l != NULL)
15254 copy_tv(&li->li_tv, rettv);
15255 else
15256 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015258 }
15259 else if (l != NULL)
15260 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261 else
15262 {
15263 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015264 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265 (varnumber_T)(regmatch.startp[0] - str);
15266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015269 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270 }
15271 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015272 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 }
15274
15275theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015276 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 p_cpo = save_cpo;
15278}
15279
15280/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281 * "match()" function
15282 */
15283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015284f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015285{
15286 find_some_match(argvars, rettv, 1);
15287}
15288
15289/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015290 * "matchadd()" function
15291 */
15292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015293f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015294{
15295#ifdef FEAT_SEARCH_EXTRA
15296 char_u buf[NUMBUFLEN];
15297 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15298 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15299 int prio = 10; /* default priority */
15300 int id = -1;
15301 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015302 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015303
15304 rettv->vval.v_number = -1;
15305
15306 if (grp == NULL || pat == NULL)
15307 return;
15308 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015309 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015310 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015311 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015312 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015313 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015314 if (argvars[4].v_type != VAR_UNKNOWN)
15315 {
15316 if (argvars[4].v_type != VAR_DICT)
15317 {
15318 EMSG(_(e_dictreq));
15319 return;
15320 }
15321 if (dict_find(argvars[4].vval.v_dict,
15322 (char_u *)"conceal", -1) != NULL)
15323 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15324 (char_u *)"conceal", FALSE);
15325 }
15326 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015327 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015328 if (error == TRUE)
15329 return;
15330 if (id >= 1 && id <= 3)
15331 {
15332 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15333 return;
15334 }
15335
Bram Moolenaar6561d522015-07-21 15:48:27 +020015336 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15337 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015338#endif
15339}
15340
15341/*
15342 * "matchaddpos()" function
15343 */
15344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015345f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015346{
15347#ifdef FEAT_SEARCH_EXTRA
15348 char_u buf[NUMBUFLEN];
15349 char_u *group;
15350 int prio = 10;
15351 int id = -1;
15352 int error = FALSE;
15353 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015354 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015355
15356 rettv->vval.v_number = -1;
15357
15358 group = get_tv_string_buf_chk(&argvars[0], buf);
15359 if (group == NULL)
15360 return;
15361
15362 if (argvars[1].v_type != VAR_LIST)
15363 {
15364 EMSG2(_(e_listarg), "matchaddpos()");
15365 return;
15366 }
15367 l = argvars[1].vval.v_list;
15368 if (l == NULL)
15369 return;
15370
15371 if (argvars[2].v_type != VAR_UNKNOWN)
15372 {
15373 prio = get_tv_number_chk(&argvars[2], &error);
15374 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015375 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015376 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015377 if (argvars[4].v_type != VAR_UNKNOWN)
15378 {
15379 if (argvars[4].v_type != VAR_DICT)
15380 {
15381 EMSG(_(e_dictreq));
15382 return;
15383 }
15384 if (dict_find(argvars[4].vval.v_dict,
15385 (char_u *)"conceal", -1) != NULL)
15386 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15387 (char_u *)"conceal", FALSE);
15388 }
15389 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015390 }
15391 if (error == TRUE)
15392 return;
15393
15394 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15395 if (id == 1 || id == 2)
15396 {
15397 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15398 return;
15399 }
15400
Bram Moolenaar6561d522015-07-21 15:48:27 +020015401 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15402 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015403#endif
15404}
15405
15406/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015407 * "matcharg()" function
15408 */
15409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015410f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015411{
15412 if (rettv_list_alloc(rettv) == OK)
15413 {
15414#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015415 int id = get_tv_number(&argvars[0]);
15416 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015417
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015418 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015419 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015420 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15421 {
15422 list_append_string(rettv->vval.v_list,
15423 syn_id2name(m->hlg_id), -1);
15424 list_append_string(rettv->vval.v_list, m->pattern, -1);
15425 }
15426 else
15427 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015428 list_append_string(rettv->vval.v_list, NULL, -1);
15429 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015430 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015431 }
15432#endif
15433 }
15434}
15435
15436/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015437 * "matchdelete()" function
15438 */
15439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015440f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015441{
15442#ifdef FEAT_SEARCH_EXTRA
15443 rettv->vval.v_number = match_delete(curwin,
15444 (int)get_tv_number(&argvars[0]), TRUE);
15445#endif
15446}
15447
15448/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015449 * "matchend()" function
15450 */
15451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015452f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015453{
15454 find_some_match(argvars, rettv, 0);
15455}
15456
15457/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015458 * "matchlist()" function
15459 */
15460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015461f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015462{
15463 find_some_match(argvars, rettv, 3);
15464}
15465
15466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015467 * "matchstr()" function
15468 */
15469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015470f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015471{
15472 find_some_match(argvars, rettv, 2);
15473}
15474
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015475static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015476
15477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015478max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015479{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015480 long n = 0;
15481 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015482 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015483
15484 if (argvars[0].v_type == VAR_LIST)
15485 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015486 list_T *l;
15487 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015488
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015489 l = argvars[0].vval.v_list;
15490 if (l != NULL)
15491 {
15492 li = l->lv_first;
15493 if (li != NULL)
15494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015495 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015496 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015497 {
15498 li = li->li_next;
15499 if (li == NULL)
15500 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015501 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015502 if (domax ? i > n : i < n)
15503 n = i;
15504 }
15505 }
15506 }
15507 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015508 else if (argvars[0].v_type == VAR_DICT)
15509 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015510 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015511 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015512 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015513 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015514
15515 d = argvars[0].vval.v_dict;
15516 if (d != NULL)
15517 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015518 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015519 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015520 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015521 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015522 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015523 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015524 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015525 if (first)
15526 {
15527 n = i;
15528 first = FALSE;
15529 }
15530 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015531 n = i;
15532 }
15533 }
15534 }
15535 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015536 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015537 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015538 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015539}
15540
15541/*
15542 * "max()" function
15543 */
15544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015545f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015546{
15547 max_min(argvars, rettv, TRUE);
15548}
15549
15550/*
15551 * "min()" function
15552 */
15553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015554f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015555{
15556 max_min(argvars, rettv, FALSE);
15557}
15558
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015559static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015560
15561/*
15562 * Create the directory in which "dir" is located, and higher levels when
15563 * needed.
15564 */
15565 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015566mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015567{
15568 char_u *p;
15569 char_u *updir;
15570 int r = FAIL;
15571
15572 /* Get end of directory name in "dir".
15573 * We're done when it's "/" or "c:/". */
15574 p = gettail_sep(dir);
15575 if (p <= get_past_head(dir))
15576 return OK;
15577
15578 /* If the directory exists we're done. Otherwise: create it.*/
15579 updir = vim_strnsave(dir, (int)(p - dir));
15580 if (updir == NULL)
15581 return FAIL;
15582 if (mch_isdir(updir))
15583 r = OK;
15584 else if (mkdir_recurse(updir, prot) == OK)
15585 r = vim_mkdir_emsg(updir, prot);
15586 vim_free(updir);
15587 return r;
15588}
15589
15590#ifdef vim_mkdir
15591/*
15592 * "mkdir()" function
15593 */
15594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015595f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015596{
15597 char_u *dir;
15598 char_u buf[NUMBUFLEN];
15599 int prot = 0755;
15600
15601 rettv->vval.v_number = FAIL;
15602 if (check_restricted() || check_secure())
15603 return;
15604
15605 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015606 if (*dir == NUL)
15607 rettv->vval.v_number = FAIL;
15608 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015609 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015610 if (*gettail(dir) == NUL)
15611 /* remove trailing slashes */
15612 *gettail_sep(dir) = NUL;
15613
15614 if (argvars[1].v_type != VAR_UNKNOWN)
15615 {
15616 if (argvars[2].v_type != VAR_UNKNOWN)
15617 prot = get_tv_number_chk(&argvars[2], NULL);
15618 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15619 mkdir_recurse(dir, prot);
15620 }
15621 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015622 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015623}
15624#endif
15625
Bram Moolenaar0d660222005-01-07 21:51:51 +000015626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 * "mode()" function
15628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015630f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015632 char_u buf[3];
15633
15634 buf[1] = NUL;
15635 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 if (VIsual_active)
15638 {
15639 if (VIsual_select)
15640 buf[0] = VIsual_mode + 's' - 'v';
15641 else
15642 buf[0] = VIsual_mode;
15643 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015644 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015645 || State == CONFIRM)
15646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015648 if (State == ASKMORE)
15649 buf[1] = 'm';
15650 else if (State == CONFIRM)
15651 buf[1] = '?';
15652 }
15653 else if (State == EXTERNCMD)
15654 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655 else if (State & INSERT)
15656 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015657#ifdef FEAT_VREPLACE
15658 if (State & VREPLACE_FLAG)
15659 {
15660 buf[0] = 'R';
15661 buf[1] = 'v';
15662 }
15663 else
15664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 if (State & REPLACE_FLAG)
15666 buf[0] = 'R';
15667 else
15668 buf[0] = 'i';
15669 }
15670 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015673 if (exmode_active)
15674 buf[1] = 'v';
15675 }
15676 else if (exmode_active)
15677 {
15678 buf[0] = 'c';
15679 buf[1] = 'e';
15680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015684 if (finish_op)
15685 buf[1] = 'o';
15686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015688 /* Clear out the minor mode when the argument is not a non-zero number or
15689 * non-empty string. */
15690 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015691 buf[1] = NUL;
15692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015693 rettv->vval.v_string = vim_strsave(buf);
15694 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695}
15696
Bram Moolenaar429fa852013-04-15 12:27:36 +020015697#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015698/*
15699 * "mzeval()" function
15700 */
15701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015702f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015703{
15704 char_u *str;
15705 char_u buf[NUMBUFLEN];
15706
15707 str = get_tv_string_buf(&argvars[0], buf);
15708 do_mzeval(str, rettv);
15709}
Bram Moolenaar75676462013-01-30 14:55:42 +010015710
15711 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015712mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015713{
15714 typval_T argvars[3];
15715
15716 argvars[0].v_type = VAR_STRING;
15717 argvars[0].vval.v_string = name;
15718 copy_tv(args, &argvars[1]);
15719 argvars[2].v_type = VAR_UNKNOWN;
15720 f_call(argvars, rettv);
15721 clear_tv(&argvars[1]);
15722}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015723#endif
15724
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015726 * "nextnonblank()" function
15727 */
15728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015729f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015730{
15731 linenr_T lnum;
15732
15733 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015735 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015736 {
15737 lnum = 0;
15738 break;
15739 }
15740 if (*skipwhite(ml_get(lnum)) != NUL)
15741 break;
15742 }
15743 rettv->vval.v_number = lnum;
15744}
15745
15746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 * "nr2char()" function
15748 */
15749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015750f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751{
15752 char_u buf[NUMBUFLEN];
15753
15754#ifdef FEAT_MBYTE
15755 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015756 {
15757 int utf8 = 0;
15758
15759 if (argvars[1].v_type != VAR_UNKNOWN)
15760 utf8 = get_tv_number_chk(&argvars[1], NULL);
15761 if (utf8)
15762 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15763 else
15764 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766 else
15767#endif
15768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015769 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 buf[1] = NUL;
15771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772 rettv->v_type = VAR_STRING;
15773 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015774}
15775
15776/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015777 * "or(expr, expr)" function
15778 */
15779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015780f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015781{
15782 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15783 | get_tv_number_chk(&argvars[1], NULL);
15784}
15785
15786/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015787 * "pathshorten()" function
15788 */
15789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015790f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015791{
15792 char_u *p;
15793
15794 rettv->v_type = VAR_STRING;
15795 p = get_tv_string_chk(&argvars[0]);
15796 if (p == NULL)
15797 rettv->vval.v_string = NULL;
15798 else
15799 {
15800 p = vim_strsave(p);
15801 rettv->vval.v_string = p;
15802 if (p != NULL)
15803 shorten_dir(p);
15804 }
15805}
15806
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015807#ifdef FEAT_PERL
15808/*
15809 * "perleval()" function
15810 */
15811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015812f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015813{
15814 char_u *str;
15815 char_u buf[NUMBUFLEN];
15816
15817 str = get_tv_string_buf(&argvars[0], buf);
15818 do_perleval(str, rettv);
15819}
15820#endif
15821
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015822#ifdef FEAT_FLOAT
15823/*
15824 * "pow()" function
15825 */
15826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015827f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015828{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015829 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015830
15831 rettv->v_type = VAR_FLOAT;
15832 if (get_float_arg(argvars, &fx) == OK
15833 && get_float_arg(&argvars[1], &fy) == OK)
15834 rettv->vval.v_float = pow(fx, fy);
15835 else
15836 rettv->vval.v_float = 0.0;
15837}
15838#endif
15839
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015840/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015841 * "prevnonblank()" function
15842 */
15843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015844f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015845{
15846 linenr_T lnum;
15847
15848 lnum = get_tv_lnum(argvars);
15849 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15850 lnum = 0;
15851 else
15852 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15853 --lnum;
15854 rettv->vval.v_number = lnum;
15855}
15856
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015857/* This dummy va_list is here because:
15858 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15859 * - locally in the function results in a "used before set" warning
15860 * - using va_start() to initialize it gives "function with fixed args" error */
15861static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015862
Bram Moolenaar8c711452005-01-14 21:53:12 +000015863/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015864 * "printf()" function
15865 */
15866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015867f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015868{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015869 char_u buf[NUMBUFLEN];
15870 int len;
15871 char_u *s;
15872 int saved_did_emsg = did_emsg;
15873 char *fmt;
15874
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015875 rettv->v_type = VAR_STRING;
15876 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015877
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015878 /* Get the required length, allocate the buffer and do it for real. */
15879 did_emsg = FALSE;
15880 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15881 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15882 if (!did_emsg)
15883 {
15884 s = alloc(len + 1);
15885 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015886 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015887 rettv->vval.v_string = s;
15888 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015889 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015890 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015891 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015892}
15893
15894/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015895 * "pumvisible()" function
15896 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015898f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015899{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015900#ifdef FEAT_INS_EXPAND
15901 if (pum_visible())
15902 rettv->vval.v_number = 1;
15903#endif
15904}
15905
Bram Moolenaardb913952012-06-29 12:54:53 +020015906#ifdef FEAT_PYTHON3
15907/*
15908 * "py3eval()" function
15909 */
15910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015911f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015912{
15913 char_u *str;
15914 char_u buf[NUMBUFLEN];
15915
15916 str = get_tv_string_buf(&argvars[0], buf);
15917 do_py3eval(str, rettv);
15918}
15919#endif
15920
15921#ifdef FEAT_PYTHON
15922/*
15923 * "pyeval()" function
15924 */
15925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015926f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015927{
15928 char_u *str;
15929 char_u buf[NUMBUFLEN];
15930
15931 str = get_tv_string_buf(&argvars[0], buf);
15932 do_pyeval(str, rettv);
15933}
15934#endif
15935
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015936/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015937 * "range()" function
15938 */
15939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015940f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015941{
15942 long start;
15943 long end;
15944 long stride = 1;
15945 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015946 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015948 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015949 if (argvars[1].v_type == VAR_UNKNOWN)
15950 {
15951 end = start - 1;
15952 start = 0;
15953 }
15954 else
15955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015956 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015957 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015958 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015959 }
15960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015961 if (error)
15962 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015963 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015964 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015965 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015966 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015967 else
15968 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015969 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015970 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015971 if (list_append_number(rettv->vval.v_list,
15972 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015973 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015974 }
15975}
15976
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015977/*
15978 * "readfile()" function
15979 */
15980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015981f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015982{
15983 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015984 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015985 char_u *fname;
15986 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015987 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15988 int io_size = sizeof(buf);
15989 int readlen; /* size of last fread() */
15990 char_u *prev = NULL; /* previously read bytes, if any */
15991 long prevlen = 0; /* length of data in prev */
15992 long prevsize = 0; /* size of prev buffer */
15993 long maxline = MAXLNUM;
15994 long cnt = 0;
15995 char_u *p; /* position in buf */
15996 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015997
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015998 if (argvars[1].v_type != VAR_UNKNOWN)
15999 {
16000 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16001 binary = TRUE;
16002 if (argvars[2].v_type != VAR_UNKNOWN)
16003 maxline = get_tv_number(&argvars[2]);
16004 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016005
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016006 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016007 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016008
16009 /* Always open the file in binary mode, library functions have a mind of
16010 * their own about CR-LF conversion. */
16011 fname = get_tv_string(&argvars[0]);
16012 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16013 {
16014 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16015 return;
16016 }
16017
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016018 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016019 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016020 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016021
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016022 /* This for loop processes what was read, but is also entered at end
16023 * of file so that either:
16024 * - an incomplete line gets written
16025 * - a "binary" file gets an empty line at the end if it ends in a
16026 * newline. */
16027 for (p = buf, start = buf;
16028 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16029 ++p)
16030 {
16031 if (*p == '\n' || readlen <= 0)
16032 {
16033 listitem_T *li;
16034 char_u *s = NULL;
16035 long_u len = p - start;
16036
16037 /* Finished a line. Remove CRs before NL. */
16038 if (readlen > 0 && !binary)
16039 {
16040 while (len > 0 && start[len - 1] == '\r')
16041 --len;
16042 /* removal may cross back to the "prev" string */
16043 if (len == 0)
16044 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16045 --prevlen;
16046 }
16047 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016048 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016049 else
16050 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016051 /* Change "prev" buffer to be the right size. This way
16052 * the bytes are only copied once, and very long lines are
16053 * allocated only once. */
16054 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016055 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016056 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016057 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016058 prev = NULL; /* the list will own the string */
16059 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016060 }
16061 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016062 if (s == NULL)
16063 {
16064 do_outofmem_msg((long_u) prevlen + len + 1);
16065 failed = TRUE;
16066 break;
16067 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016068
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016069 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016070 {
16071 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016072 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016073 break;
16074 }
16075 li->li_tv.v_type = VAR_STRING;
16076 li->li_tv.v_lock = 0;
16077 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016078 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016079
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016080 start = p + 1; /* step over newline */
16081 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016082 break;
16083 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016084 else if (*p == NUL)
16085 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016086#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016087 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16088 * when finding the BF and check the previous two bytes. */
16089 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016090 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016091 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16092 * + 1, these may be in the "prev" string. */
16093 char_u back1 = p >= buf + 1 ? p[-1]
16094 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16095 char_u back2 = p >= buf + 2 ? p[-2]
16096 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16097 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016098
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016099 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016100 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016101 char_u *dest = p - 2;
16102
16103 /* Usually a BOM is at the beginning of a file, and so at
16104 * the beginning of a line; then we can just step over it.
16105 */
16106 if (start == dest)
16107 start = p + 1;
16108 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016109 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016110 /* have to shuffle buf to close gap */
16111 int adjust_prevlen = 0;
16112
16113 if (dest < buf)
16114 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016115 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016116 dest = buf;
16117 }
16118 if (readlen > p - buf + 1)
16119 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16120 readlen -= 3 - adjust_prevlen;
16121 prevlen -= adjust_prevlen;
16122 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016123 }
16124 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016125 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016126#endif
16127 } /* for */
16128
16129 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16130 break;
16131 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016132 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016133 /* There's part of a line in buf, store it in "prev". */
16134 if (p - start + prevlen >= prevsize)
16135 {
16136 /* need bigger "prev" buffer */
16137 char_u *newprev;
16138
16139 /* A common use case is ordinary text files and "prev" gets a
16140 * fragment of a line, so the first allocation is made
16141 * small, to avoid repeatedly 'allocing' large and
16142 * 'reallocing' small. */
16143 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016144 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016145 else
16146 {
16147 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016148 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016149 prevsize = grow50pc > growmin ? grow50pc : growmin;
16150 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016151 newprev = prev == NULL ? alloc(prevsize)
16152 : vim_realloc(prev, prevsize);
16153 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016154 {
16155 do_outofmem_msg((long_u)prevsize);
16156 failed = TRUE;
16157 break;
16158 }
16159 prev = newprev;
16160 }
16161 /* Add the line part to end of "prev". */
16162 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016163 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016164 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016165 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016166
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016167 /*
16168 * For a negative line count use only the lines at the end of the file,
16169 * free the rest.
16170 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016171 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016172 while (cnt > -maxline)
16173 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016174 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016175 --cnt;
16176 }
16177
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016178 if (failed)
16179 {
16180 list_free(rettv->vval.v_list, TRUE);
16181 /* readfile doc says an empty list is returned on error */
16182 rettv->vval.v_list = list_alloc();
16183 }
16184
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016185 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016186 fclose(fd);
16187}
16188
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016189#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016190static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016191
16192/*
16193 * Convert a List to proftime_T.
16194 * Return FAIL when there is something wrong.
16195 */
16196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016197list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016198{
16199 long n1, n2;
16200 int error = FALSE;
16201
16202 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16203 || arg->vval.v_list->lv_len != 2)
16204 return FAIL;
16205 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16206 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16207# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016208 tm->HighPart = n1;
16209 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016210# else
16211 tm->tv_sec = n1;
16212 tm->tv_usec = n2;
16213# endif
16214 return error ? FAIL : OK;
16215}
16216#endif /* FEAT_RELTIME */
16217
16218/*
16219 * "reltime()" function
16220 */
16221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016222f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016223{
16224#ifdef FEAT_RELTIME
16225 proftime_T res;
16226 proftime_T start;
16227
16228 if (argvars[0].v_type == VAR_UNKNOWN)
16229 {
16230 /* No arguments: get current time. */
16231 profile_start(&res);
16232 }
16233 else if (argvars[1].v_type == VAR_UNKNOWN)
16234 {
16235 if (list2proftime(&argvars[0], &res) == FAIL)
16236 return;
16237 profile_end(&res);
16238 }
16239 else
16240 {
16241 /* Two arguments: compute the difference. */
16242 if (list2proftime(&argvars[0], &start) == FAIL
16243 || list2proftime(&argvars[1], &res) == FAIL)
16244 return;
16245 profile_sub(&res, &start);
16246 }
16247
16248 if (rettv_list_alloc(rettv) == OK)
16249 {
16250 long n1, n2;
16251
16252# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016253 n1 = res.HighPart;
16254 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016255# else
16256 n1 = res.tv_sec;
16257 n2 = res.tv_usec;
16258# endif
16259 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16260 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16261 }
16262#endif
16263}
16264
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016265#ifdef FEAT_FLOAT
16266/*
16267 * "reltimefloat()" function
16268 */
16269 static void
16270f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16271{
16272# ifdef FEAT_RELTIME
16273 proftime_T tm;
16274# endif
16275
16276 rettv->v_type = VAR_FLOAT;
16277 rettv->vval.v_float = 0;
16278# ifdef FEAT_RELTIME
16279 if (list2proftime(&argvars[0], &tm) == OK)
16280 rettv->vval.v_float = profile_float(&tm);
16281# endif
16282}
16283#endif
16284
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016285/*
16286 * "reltimestr()" function
16287 */
16288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016289f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016290{
16291#ifdef FEAT_RELTIME
16292 proftime_T tm;
16293#endif
16294
16295 rettv->v_type = VAR_STRING;
16296 rettv->vval.v_string = NULL;
16297#ifdef FEAT_RELTIME
16298 if (list2proftime(&argvars[0], &tm) == OK)
16299 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16300#endif
16301}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016302
Bram Moolenaar0d660222005-01-07 21:51:51 +000016303#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016304static void make_connection(void);
16305static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016306
16307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016308make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016309{
16310 if (X_DISPLAY == NULL
16311# ifdef FEAT_GUI
16312 && !gui.in_use
16313# endif
16314 )
16315 {
16316 x_force_connect = TRUE;
16317 setup_term_clip();
16318 x_force_connect = FALSE;
16319 }
16320}
16321
16322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016323check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016324{
16325 make_connection();
16326 if (X_DISPLAY == NULL)
16327 {
16328 EMSG(_("E240: No connection to Vim server"));
16329 return FAIL;
16330 }
16331 return OK;
16332}
16333#endif
16334
16335#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016336static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337
16338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016339remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016340{
16341 char_u *server_name;
16342 char_u *keys;
16343 char_u *r = NULL;
16344 char_u buf[NUMBUFLEN];
16345# ifdef WIN32
16346 HWND w;
16347# else
16348 Window w;
16349# endif
16350
16351 if (check_restricted() || check_secure())
16352 return;
16353
16354# ifdef FEAT_X11
16355 if (check_connection() == FAIL)
16356 return;
16357# endif
16358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016359 server_name = get_tv_string_chk(&argvars[0]);
16360 if (server_name == NULL)
16361 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016362 keys = get_tv_string_buf(&argvars[1], buf);
16363# ifdef WIN32
16364 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16365# else
16366 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16367 < 0)
16368# endif
16369 {
16370 if (r != NULL)
16371 EMSG(r); /* sending worked but evaluation failed */
16372 else
16373 EMSG2(_("E241: Unable to send to %s"), server_name);
16374 return;
16375 }
16376
16377 rettv->vval.v_string = r;
16378
16379 if (argvars[2].v_type != VAR_UNKNOWN)
16380 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016381 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016382 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016383 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016384
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016385 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016386 v.di_tv.v_type = VAR_STRING;
16387 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016388 idvar = get_tv_string_chk(&argvars[2]);
16389 if (idvar != NULL)
16390 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016391 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392 }
16393}
16394#endif
16395
16396/*
16397 * "remote_expr()" function
16398 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016400f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016401{
16402 rettv->v_type = VAR_STRING;
16403 rettv->vval.v_string = NULL;
16404#ifdef FEAT_CLIENTSERVER
16405 remote_common(argvars, rettv, TRUE);
16406#endif
16407}
16408
16409/*
16410 * "remote_foreground()" function
16411 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016413f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016415#ifdef FEAT_CLIENTSERVER
16416# ifdef WIN32
16417 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016418 {
16419 char_u *server_name = get_tv_string_chk(&argvars[0]);
16420
16421 if (server_name != NULL)
16422 serverForeground(server_name);
16423 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424# else
16425 /* Send a foreground() expression to the server. */
16426 argvars[1].v_type = VAR_STRING;
16427 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16428 argvars[2].v_type = VAR_UNKNOWN;
16429 remote_common(argvars, rettv, TRUE);
16430 vim_free(argvars[1].vval.v_string);
16431# endif
16432#endif
16433}
16434
Bram Moolenaar0d660222005-01-07 21:51:51 +000016435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016436f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016437{
16438#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016439 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440 char_u *s = NULL;
16441# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016442 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016444 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445
16446 if (check_restricted() || check_secure())
16447 {
16448 rettv->vval.v_number = -1;
16449 return;
16450 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016451 serverid = get_tv_string_chk(&argvars[0]);
16452 if (serverid == NULL)
16453 {
16454 rettv->vval.v_number = -1;
16455 return; /* type error; errmsg already given */
16456 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016457# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016458 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459 if (n == 0)
16460 rettv->vval.v_number = -1;
16461 else
16462 {
16463 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16464 rettv->vval.v_number = (s != NULL);
16465 }
16466# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016467 if (check_connection() == FAIL)
16468 return;
16469
16470 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016471 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016472# endif
16473
16474 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016476 char_u *retvar;
16477
Bram Moolenaar33570922005-01-25 22:26:29 +000016478 v.di_tv.v_type = VAR_STRING;
16479 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016480 retvar = get_tv_string_chk(&argvars[1]);
16481 if (retvar != NULL)
16482 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016483 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016484 }
16485#else
16486 rettv->vval.v_number = -1;
16487#endif
16488}
16489
Bram Moolenaar0d660222005-01-07 21:51:51 +000016490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016491f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492{
16493 char_u *r = NULL;
16494
16495#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016496 char_u *serverid = get_tv_string_chk(&argvars[0]);
16497
16498 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016499 {
16500# ifdef WIN32
16501 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016502 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016504 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016505 if (n != 0)
16506 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16507 if (r == NULL)
16508# else
16509 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016510 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016511# endif
16512 EMSG(_("E277: Unable to read a server reply"));
16513 }
16514#endif
16515 rettv->v_type = VAR_STRING;
16516 rettv->vval.v_string = r;
16517}
16518
16519/*
16520 * "remote_send()" function
16521 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016523f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524{
16525 rettv->v_type = VAR_STRING;
16526 rettv->vval.v_string = NULL;
16527#ifdef FEAT_CLIENTSERVER
16528 remote_common(argvars, rettv, FALSE);
16529#endif
16530}
16531
16532/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016533 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016534 */
16535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016536f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016537{
Bram Moolenaar33570922005-01-25 22:26:29 +000016538 list_T *l;
16539 listitem_T *item, *item2;
16540 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016541 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016542 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016543 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016544 dict_T *d;
16545 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016546 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016547
Bram Moolenaar8c711452005-01-14 21:53:12 +000016548 if (argvars[0].v_type == VAR_DICT)
16549 {
16550 if (argvars[2].v_type != VAR_UNKNOWN)
16551 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016552 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016553 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016555 key = get_tv_string_chk(&argvars[1]);
16556 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016558 di = dict_find(d, key, -1);
16559 if (di == NULL)
16560 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016561 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16562 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016563 {
16564 *rettv = di->di_tv;
16565 init_tv(&di->di_tv);
16566 dictitem_remove(d, di);
16567 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016568 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016569 }
16570 }
16571 else if (argvars[0].v_type != VAR_LIST)
16572 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016573 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016574 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016576 int error = FALSE;
16577
16578 idx = get_tv_number_chk(&argvars[1], &error);
16579 if (error)
16580 ; /* type error: do nothing, errmsg already given */
16581 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016582 EMSGN(_(e_listidx), idx);
16583 else
16584 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016585 if (argvars[2].v_type == VAR_UNKNOWN)
16586 {
16587 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016588 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016589 *rettv = item->li_tv;
16590 vim_free(item);
16591 }
16592 else
16593 {
16594 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016595 end = get_tv_number_chk(&argvars[2], &error);
16596 if (error)
16597 ; /* type error: do nothing */
16598 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016599 EMSGN(_(e_listidx), end);
16600 else
16601 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016602 int cnt = 0;
16603
16604 for (li = item; li != NULL; li = li->li_next)
16605 {
16606 ++cnt;
16607 if (li == item2)
16608 break;
16609 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016610 if (li == NULL) /* didn't find "item2" after "item" */
16611 EMSG(_(e_invrange));
16612 else
16613 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016614 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016615 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016616 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016617 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016618 l->lv_first = item;
16619 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016620 item->li_prev = NULL;
16621 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016622 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016623 }
16624 }
16625 }
16626 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016627 }
16628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629}
16630
16631/*
16632 * "rename({from}, {to})" function
16633 */
16634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016635f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636{
16637 char_u buf[NUMBUFLEN];
16638
16639 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016640 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016642 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16643 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644}
16645
16646/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016647 * "repeat()" function
16648 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016650f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016651{
16652 char_u *p;
16653 int n;
16654 int slen;
16655 int len;
16656 char_u *r;
16657 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016658
16659 n = get_tv_number(&argvars[1]);
16660 if (argvars[0].v_type == VAR_LIST)
16661 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016662 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016663 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016664 if (list_extend(rettv->vval.v_list,
16665 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016666 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016667 }
16668 else
16669 {
16670 p = get_tv_string(&argvars[0]);
16671 rettv->v_type = VAR_STRING;
16672 rettv->vval.v_string = NULL;
16673
16674 slen = (int)STRLEN(p);
16675 len = slen * n;
16676 if (len <= 0)
16677 return;
16678
16679 r = alloc(len + 1);
16680 if (r != NULL)
16681 {
16682 for (i = 0; i < n; i++)
16683 mch_memmove(r + i * slen, p, (size_t)slen);
16684 r[len] = NUL;
16685 }
16686
16687 rettv->vval.v_string = r;
16688 }
16689}
16690
16691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 * "resolve()" function
16693 */
16694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016695f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696{
16697 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016698#ifdef HAVE_READLINK
16699 char_u *buf = NULL;
16700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016702 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703#ifdef FEAT_SHORTCUT
16704 {
16705 char_u *v = NULL;
16706
16707 v = mch_resolve_shortcut(p);
16708 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016709 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016711 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 }
16713#else
16714# ifdef HAVE_READLINK
16715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 char_u *cpy;
16717 int len;
16718 char_u *remain = NULL;
16719 char_u *q;
16720 int is_relative_to_current = FALSE;
16721 int has_trailing_pathsep = FALSE;
16722 int limit = 100;
16723
16724 p = vim_strsave(p);
16725
16726 if (p[0] == '.' && (vim_ispathsep(p[1])
16727 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16728 is_relative_to_current = TRUE;
16729
16730 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016731 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016732 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016734 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736
16737 q = getnextcomp(p);
16738 if (*q != NUL)
16739 {
16740 /* Separate the first path component in "p", and keep the
16741 * remainder (beginning with the path separator). */
16742 remain = vim_strsave(q - 1);
16743 q[-1] = NUL;
16744 }
16745
Bram Moolenaard9462e32011-04-11 21:35:11 +020016746 buf = alloc(MAXPATHL + 1);
16747 if (buf == NULL)
16748 goto fail;
16749
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 for (;;)
16751 {
16752 for (;;)
16753 {
16754 len = readlink((char *)p, (char *)buf, MAXPATHL);
16755 if (len <= 0)
16756 break;
16757 buf[len] = NUL;
16758
16759 if (limit-- == 0)
16760 {
16761 vim_free(p);
16762 vim_free(remain);
16763 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765 goto fail;
16766 }
16767
16768 /* Ensure that the result will have a trailing path separator
16769 * if the argument has one. */
16770 if (remain == NULL && has_trailing_pathsep)
16771 add_pathsep(buf);
16772
16773 /* Separate the first path component in the link value and
16774 * concatenate the remainders. */
16775 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16776 if (*q != NUL)
16777 {
16778 if (remain == NULL)
16779 remain = vim_strsave(q - 1);
16780 else
16781 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016782 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 if (cpy != NULL)
16784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 vim_free(remain);
16786 remain = cpy;
16787 }
16788 }
16789 q[-1] = NUL;
16790 }
16791
16792 q = gettail(p);
16793 if (q > p && *q == NUL)
16794 {
16795 /* Ignore trailing path separator. */
16796 q[-1] = NUL;
16797 q = gettail(p);
16798 }
16799 if (q > p && !mch_isFullName(buf))
16800 {
16801 /* symlink is relative to directory of argument */
16802 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16803 if (cpy != NULL)
16804 {
16805 STRCPY(cpy, p);
16806 STRCPY(gettail(cpy), buf);
16807 vim_free(p);
16808 p = cpy;
16809 }
16810 }
16811 else
16812 {
16813 vim_free(p);
16814 p = vim_strsave(buf);
16815 }
16816 }
16817
16818 if (remain == NULL)
16819 break;
16820
16821 /* Append the first path component of "remain" to "p". */
16822 q = getnextcomp(remain + 1);
16823 len = q - remain - (*q != NUL);
16824 cpy = vim_strnsave(p, STRLEN(p) + len);
16825 if (cpy != NULL)
16826 {
16827 STRNCAT(cpy, remain, len);
16828 vim_free(p);
16829 p = cpy;
16830 }
16831 /* Shorten "remain". */
16832 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016833 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 else
16835 {
16836 vim_free(remain);
16837 remain = NULL;
16838 }
16839 }
16840
16841 /* If the result is a relative path name, make it explicitly relative to
16842 * the current directory if and only if the argument had this form. */
16843 if (!vim_ispathsep(*p))
16844 {
16845 if (is_relative_to_current
16846 && *p != NUL
16847 && !(p[0] == '.'
16848 && (p[1] == NUL
16849 || vim_ispathsep(p[1])
16850 || (p[1] == '.'
16851 && (p[2] == NUL
16852 || vim_ispathsep(p[2]))))))
16853 {
16854 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016855 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856 if (cpy != NULL)
16857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858 vim_free(p);
16859 p = cpy;
16860 }
16861 }
16862 else if (!is_relative_to_current)
16863 {
16864 /* Strip leading "./". */
16865 q = p;
16866 while (q[0] == '.' && vim_ispathsep(q[1]))
16867 q += 2;
16868 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016869 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 }
16871 }
16872
16873 /* Ensure that the result will have no trailing path separator
16874 * if the argument had none. But keep "/" or "//". */
16875 if (!has_trailing_pathsep)
16876 {
16877 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016878 if (after_pathsep(p, q))
16879 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 }
16881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016882 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 }
16884# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016885 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886# endif
16887#endif
16888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016889 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890
16891#ifdef HAVE_READLINK
16892fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016893 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016895 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896}
16897
16898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016899 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 */
16901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016902f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903{
Bram Moolenaar33570922005-01-25 22:26:29 +000016904 list_T *l;
16905 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907 if (argvars[0].v_type != VAR_LIST)
16908 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016909 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016910 && !tv_check_lock(l->lv_lock,
16911 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016912 {
16913 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016914 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016915 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016916 while (li != NULL)
16917 {
16918 ni = li->li_prev;
16919 list_append(l, li);
16920 li = ni;
16921 }
16922 rettv->vval.v_list = l;
16923 rettv->v_type = VAR_LIST;
16924 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016925 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927}
16928
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016929#define SP_NOMOVE 0x01 /* don't move cursor */
16930#define SP_REPEAT 0x02 /* repeat to find outer pair */
16931#define SP_RETCOUNT 0x04 /* return matchcount */
16932#define SP_SETPCMARK 0x08 /* set previous context mark */
16933#define SP_START 0x10 /* accept match at start position */
16934#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16935#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016936#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016937
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016938static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939
16940/*
16941 * Get flags for a search function.
16942 * Possibly sets "p_ws".
16943 * Returns BACKWARD, FORWARD or zero (for an error).
16944 */
16945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016946get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947{
16948 int dir = FORWARD;
16949 char_u *flags;
16950 char_u nbuf[NUMBUFLEN];
16951 int mask;
16952
16953 if (varp->v_type != VAR_UNKNOWN)
16954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016955 flags = get_tv_string_buf_chk(varp, nbuf);
16956 if (flags == NULL)
16957 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958 while (*flags != NUL)
16959 {
16960 switch (*flags)
16961 {
16962 case 'b': dir = BACKWARD; break;
16963 case 'w': p_ws = TRUE; break;
16964 case 'W': p_ws = FALSE; break;
16965 default: mask = 0;
16966 if (flagsp != NULL)
16967 switch (*flags)
16968 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016969 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016970 case 'e': mask = SP_END; break;
16971 case 'm': mask = SP_RETCOUNT; break;
16972 case 'n': mask = SP_NOMOVE; break;
16973 case 'p': mask = SP_SUBPAT; break;
16974 case 'r': mask = SP_REPEAT; break;
16975 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016976 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977 }
16978 if (mask == 0)
16979 {
16980 EMSG2(_(e_invarg2), flags);
16981 dir = 0;
16982 }
16983 else
16984 *flagsp |= mask;
16985 }
16986 if (dir == 0)
16987 break;
16988 ++flags;
16989 }
16990 }
16991 return dir;
16992}
16993
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016995 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016997 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016998search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017000 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 char_u *pat;
17002 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017003 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 int save_p_ws = p_ws;
17005 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017006 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017007 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017008 proftime_T tm;
17009#ifdef FEAT_RELTIME
17010 long time_limit = 0;
17011#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017012 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017013 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017015 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017016 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017017 if (dir == 0)
17018 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017019 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017020 if (flags & SP_START)
17021 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017022 if (flags & SP_END)
17023 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017024 if (flags & SP_COLUMN)
17025 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017026
Bram Moolenaar76929292008-01-06 19:07:36 +000017027 /* Optional arguments: line number to stop searching and timeout. */
17028 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017029 {
17030 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17031 if (lnum_stop < 0)
17032 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017033#ifdef FEAT_RELTIME
17034 if (argvars[3].v_type != VAR_UNKNOWN)
17035 {
17036 time_limit = get_tv_number_chk(&argvars[3], NULL);
17037 if (time_limit < 0)
17038 goto theend;
17039 }
17040#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017041 }
17042
Bram Moolenaar76929292008-01-06 19:07:36 +000017043#ifdef FEAT_RELTIME
17044 /* Set the time limit, if there is one. */
17045 profile_setlimit(time_limit, &tm);
17046#endif
17047
Bram Moolenaar231334e2005-07-25 20:46:57 +000017048 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017049 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017050 * Check to make sure only those flags are set.
17051 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17052 * flags cannot be set. Check for that condition also.
17053 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017054 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017055 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017056 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017057 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017058 goto theend;
17059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017061 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017062 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017063 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017064 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017066 if (flags & SP_SUBPAT)
17067 retval = subpatnum;
17068 else
17069 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017070 if (flags & SP_SETPCMARK)
17071 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017073 if (match_pos != NULL)
17074 {
17075 /* Store the match cursor position */
17076 match_pos->lnum = pos.lnum;
17077 match_pos->col = pos.col + 1;
17078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 /* "/$" will put the cursor after the end of the line, may need to
17080 * correct that here */
17081 check_cursor();
17082 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017083
17084 /* If 'n' flag is used: restore cursor position. */
17085 if (flags & SP_NOMOVE)
17086 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017087 else
17088 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017089theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017091
17092 return retval;
17093}
17094
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017095#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017096
17097/*
17098 * round() is not in C90, use ceil() or floor() instead.
17099 */
17100 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017101vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017102{
17103 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17104}
17105
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017106/*
17107 * "round({float})" function
17108 */
17109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017110f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017111{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017112 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017113
17114 rettv->v_type = VAR_FLOAT;
17115 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017116 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017117 else
17118 rettv->vval.v_float = 0.0;
17119}
17120#endif
17121
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017122/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017123 * "screenattr()" function
17124 */
17125 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017126f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017127{
17128 int row;
17129 int col;
17130 int c;
17131
17132 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17133 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17134 if (row < 0 || row >= screen_Rows
17135 || col < 0 || col >= screen_Columns)
17136 c = -1;
17137 else
17138 c = ScreenAttrs[LineOffset[row] + col];
17139 rettv->vval.v_number = c;
17140}
17141
17142/*
17143 * "screenchar()" function
17144 */
17145 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017146f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017147{
17148 int row;
17149 int col;
17150 int off;
17151 int c;
17152
17153 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17154 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17155 if (row < 0 || row >= screen_Rows
17156 || col < 0 || col >= screen_Columns)
17157 c = -1;
17158 else
17159 {
17160 off = LineOffset[row] + col;
17161#ifdef FEAT_MBYTE
17162 if (enc_utf8 && ScreenLinesUC[off] != 0)
17163 c = ScreenLinesUC[off];
17164 else
17165#endif
17166 c = ScreenLines[off];
17167 }
17168 rettv->vval.v_number = c;
17169}
17170
17171/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017172 * "screencol()" function
17173 *
17174 * First column is 1 to be consistent with virtcol().
17175 */
17176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017177f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017178{
17179 rettv->vval.v_number = screen_screencol() + 1;
17180}
17181
17182/*
17183 * "screenrow()" function
17184 */
17185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017186f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017187{
17188 rettv->vval.v_number = screen_screenrow() + 1;
17189}
17190
17191/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017192 * "search()" function
17193 */
17194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017195f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017196{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017197 int flags = 0;
17198
17199 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200}
17201
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017203 * "searchdecl()" function
17204 */
17205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017206f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017207{
17208 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017209 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017210 int error = FALSE;
17211 char_u *name;
17212
17213 rettv->vval.v_number = 1; /* default: FAIL */
17214
17215 name = get_tv_string_chk(&argvars[0]);
17216 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017217 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017218 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017219 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17220 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17221 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017222 if (!error && name != NULL)
17223 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017224 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017225}
17226
17227/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017228 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017231searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232{
17233 char_u *spat, *mpat, *epat;
17234 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 int dir;
17237 int flags = 0;
17238 char_u nbuf1[NUMBUFLEN];
17239 char_u nbuf2[NUMBUFLEN];
17240 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017241 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017242 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017243 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017246 spat = get_tv_string_chk(&argvars[0]);
17247 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17248 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17249 if (spat == NULL || mpat == NULL || epat == NULL)
17250 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252 /* Handle the optional fourth argument: flags */
17253 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017254 if (dir == 0)
17255 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017256
17257 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017258 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17259 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017260 if ((flags & (SP_END | SP_SUBPAT)) != 0
17261 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017262 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017263 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017264 goto theend;
17265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017267 /* Using 'r' implies 'W', otherwise it doesn't work. */
17268 if (flags & SP_REPEAT)
17269 p_ws = FALSE;
17270
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017271 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017272 if (argvars[3].v_type == VAR_UNKNOWN
17273 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 skip = (char_u *)"";
17275 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017277 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017278 if (argvars[5].v_type != VAR_UNKNOWN)
17279 {
17280 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17281 if (lnum_stop < 0)
17282 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017283#ifdef FEAT_RELTIME
17284 if (argvars[6].v_type != VAR_UNKNOWN)
17285 {
17286 time_limit = get_tv_number_chk(&argvars[6], NULL);
17287 if (time_limit < 0)
17288 goto theend;
17289 }
17290#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017291 }
17292 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017293 if (skip == NULL)
17294 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017296 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017297 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017298
17299theend:
17300 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017301
17302 return retval;
17303}
17304
17305/*
17306 * "searchpair()" function
17307 */
17308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017309f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017310{
17311 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17312}
17313
17314/*
17315 * "searchpairpos()" function
17316 */
17317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017318f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017319{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017320 pos_T match_pos;
17321 int lnum = 0;
17322 int col = 0;
17323
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017324 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017325 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017326
17327 if (searchpair_cmn(argvars, &match_pos) > 0)
17328 {
17329 lnum = match_pos.lnum;
17330 col = match_pos.col;
17331 }
17332
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017333 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17334 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017335}
17336
17337/*
17338 * Search for a start/middle/end thing.
17339 * Used by searchpair(), see its documentation for the details.
17340 * Returns 0 or -1 for no match,
17341 */
17342 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017343do_searchpair(
17344 char_u *spat, /* start pattern */
17345 char_u *mpat, /* middle pattern */
17346 char_u *epat, /* end pattern */
17347 int dir, /* BACKWARD or FORWARD */
17348 char_u *skip, /* skip expression */
17349 int flags, /* SP_SETPCMARK and other SP_ values */
17350 pos_T *match_pos,
17351 linenr_T lnum_stop, /* stop at this line if not zero */
17352 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017353{
17354 char_u *save_cpo;
17355 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17356 long retval = 0;
17357 pos_T pos;
17358 pos_T firstpos;
17359 pos_T foundpos;
17360 pos_T save_cursor;
17361 pos_T save_pos;
17362 int n;
17363 int r;
17364 int nest = 1;
17365 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017366 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017367 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017368
17369 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17370 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017371 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017372
Bram Moolenaar76929292008-01-06 19:07:36 +000017373#ifdef FEAT_RELTIME
17374 /* Set the time limit, if there is one. */
17375 profile_setlimit(time_limit, &tm);
17376#endif
17377
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017378 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17379 * start/middle/end (pat3, for the top pair). */
17380 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17381 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17382 if (pat2 == NULL || pat3 == NULL)
17383 goto theend;
17384 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17385 if (*mpat == NUL)
17386 STRCPY(pat3, pat2);
17387 else
17388 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17389 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017390 if (flags & SP_START)
17391 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017392
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 save_cursor = curwin->w_cursor;
17394 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017395 clearpos(&firstpos);
17396 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397 pat = pat3;
17398 for (;;)
17399 {
17400 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017401 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17403 /* didn't find it or found the first match again: FAIL */
17404 break;
17405
17406 if (firstpos.lnum == 0)
17407 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017408 if (equalpos(pos, foundpos))
17409 {
17410 /* Found the same position again. Can happen with a pattern that
17411 * has "\zs" at the end and searching backwards. Advance one
17412 * character and try again. */
17413 if (dir == BACKWARD)
17414 decl(&pos);
17415 else
17416 incl(&pos);
17417 }
17418 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017420 /* clear the start flag to avoid getting stuck here */
17421 options &= ~SEARCH_START;
17422
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 /* If the skip pattern matches, ignore this match. */
17424 if (*skip != NUL)
17425 {
17426 save_pos = curwin->w_cursor;
17427 curwin->w_cursor = pos;
17428 r = eval_to_bool(skip, &err, NULL, FALSE);
17429 curwin->w_cursor = save_pos;
17430 if (err)
17431 {
17432 /* Evaluating {skip} caused an error, break here. */
17433 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017434 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 break;
17436 }
17437 if (r)
17438 continue;
17439 }
17440
17441 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17442 {
17443 /* Found end when searching backwards or start when searching
17444 * forward: nested pair. */
17445 ++nest;
17446 pat = pat2; /* nested, don't search for middle */
17447 }
17448 else
17449 {
17450 /* Found end when searching forward or start when searching
17451 * backward: end of (nested) pair; or found middle in outer pair. */
17452 if (--nest == 1)
17453 pat = pat3; /* outer level, search for middle */
17454 }
17455
17456 if (nest == 0)
17457 {
17458 /* Found the match: return matchcount or line number. */
17459 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017460 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017462 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017463 if (flags & SP_SETPCMARK)
17464 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465 curwin->w_cursor = pos;
17466 if (!(flags & SP_REPEAT))
17467 break;
17468 nest = 1; /* search for next unmatched */
17469 }
17470 }
17471
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017472 if (match_pos != NULL)
17473 {
17474 /* Store the match cursor position */
17475 match_pos->lnum = curwin->w_cursor.lnum;
17476 match_pos->col = curwin->w_cursor.col + 1;
17477 }
17478
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017480 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 curwin->w_cursor = save_cursor;
17482
17483theend:
17484 vim_free(pat2);
17485 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017486 if (p_cpo == empty_option)
17487 p_cpo = save_cpo;
17488 else
17489 /* Darn, evaluating the {skip} expression changed the value. */
17490 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017491
17492 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493}
17494
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017495/*
17496 * "searchpos()" function
17497 */
17498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017499f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017500{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017501 pos_T match_pos;
17502 int lnum = 0;
17503 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017504 int n;
17505 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017506
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017507 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017508 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017509
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017510 n = search_cmn(argvars, &match_pos, &flags);
17511 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017512 {
17513 lnum = match_pos.lnum;
17514 col = match_pos.col;
17515 }
17516
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017517 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17518 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017519 if (flags & SP_SUBPAT)
17520 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017521}
17522
Bram Moolenaar0d660222005-01-07 21:51:51 +000017523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017524f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526#ifdef FEAT_CLIENTSERVER
17527 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017528 char_u *server = get_tv_string_chk(&argvars[0]);
17529 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530
Bram Moolenaar0d660222005-01-07 21:51:51 +000017531 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017532 if (server == NULL || reply == NULL)
17533 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017534 if (check_restricted() || check_secure())
17535 return;
17536# ifdef FEAT_X11
17537 if (check_connection() == FAIL)
17538 return;
17539# endif
17540
17541 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017543 EMSG(_("E258: Unable to send to client"));
17544 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017546 rettv->vval.v_number = 0;
17547#else
17548 rettv->vval.v_number = -1;
17549#endif
17550}
17551
Bram Moolenaar0d660222005-01-07 21:51:51 +000017552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017553f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017554{
17555 char_u *r = NULL;
17556
17557#ifdef FEAT_CLIENTSERVER
17558# ifdef WIN32
17559 r = serverGetVimNames();
17560# else
17561 make_connection();
17562 if (X_DISPLAY != NULL)
17563 r = serverGetVimNames(X_DISPLAY);
17564# endif
17565#endif
17566 rettv->v_type = VAR_STRING;
17567 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568}
17569
17570/*
17571 * "setbufvar()" function
17572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017574f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575{
17576 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017579 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 char_u nbuf[NUMBUFLEN];
17581
17582 if (check_restricted() || check_secure())
17583 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017584 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17585 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017586 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 varp = &argvars[2];
17588
17589 if (buf != NULL && varname != NULL && varp != NULL)
17590 {
17591 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593
17594 if (*varname == '&')
17595 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017596 long numval;
17597 char_u *strval;
17598 int error = FALSE;
17599
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017601 numval = get_tv_number_chk(varp, &error);
17602 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017603 if (!error && strval != NULL)
17604 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 }
17606 else
17607 {
17608 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17609 if (bufvarname != NULL)
17610 {
17611 STRCPY(bufvarname, "b:");
17612 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017613 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 vim_free(bufvarname);
17615 }
17616 }
17617
17618 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621}
17622
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017624f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017625{
17626 dict_T *d;
17627 dictitem_T *di;
17628 char_u *csearch;
17629
17630 if (argvars[0].v_type != VAR_DICT)
17631 {
17632 EMSG(_(e_dictreq));
17633 return;
17634 }
17635
17636 if ((d = argvars[0].vval.v_dict) != NULL)
17637 {
17638 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17639 if (csearch != NULL)
17640 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017641#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017642 if (enc_utf8)
17643 {
17644 int pcc[MAX_MCO];
17645 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017646
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017647 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17648 }
17649 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017650#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017651 set_last_csearch(PTR2CHAR(csearch),
17652 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017653 }
17654
17655 di = dict_find(d, (char_u *)"forward", -1);
17656 if (di != NULL)
17657 set_csearch_direction(get_tv_number(&di->di_tv)
17658 ? FORWARD : BACKWARD);
17659
17660 di = dict_find(d, (char_u *)"until", -1);
17661 if (di != NULL)
17662 set_csearch_until(!!get_tv_number(&di->di_tv));
17663 }
17664}
17665
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666/*
17667 * "setcmdpos()" function
17668 */
17669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017670f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017672 int pos = (int)get_tv_number(&argvars[0]) - 1;
17673
17674 if (pos >= 0)
17675 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676}
17677
17678/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017679 * "setfperm({fname}, {mode})" function
17680 */
17681 static void
17682f_setfperm(typval_T *argvars, typval_T *rettv)
17683{
17684 char_u *fname;
17685 char_u modebuf[NUMBUFLEN];
17686 char_u *mode_str;
17687 int i;
17688 int mask;
17689 int mode = 0;
17690
17691 rettv->vval.v_number = 0;
17692 fname = get_tv_string_chk(&argvars[0]);
17693 if (fname == NULL)
17694 return;
17695 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17696 if (mode_str == NULL)
17697 return;
17698 if (STRLEN(mode_str) != 9)
17699 {
17700 EMSG2(_(e_invarg2), mode_str);
17701 return;
17702 }
17703
17704 mask = 1;
17705 for (i = 8; i >= 0; --i)
17706 {
17707 if (mode_str[i] != '-')
17708 mode |= mask;
17709 mask = mask << 1;
17710 }
17711 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17712}
17713
17714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 * "setline()" function
17716 */
17717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017718f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719{
17720 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017721 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017722 list_T *l = NULL;
17723 listitem_T *li = NULL;
17724 long added = 0;
17725 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017727 lnum = get_tv_lnum(&argvars[0]);
17728 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017730 l = argvars[1].vval.v_list;
17731 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017733 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017734 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017735
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017736 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017737 for (;;)
17738 {
17739 if (l != NULL)
17740 {
17741 /* list argument, get next string */
17742 if (li == NULL)
17743 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017744 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017745 li = li->li_next;
17746 }
17747
17748 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017749 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017750 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017751
17752 /* When coming here from Insert mode, sync undo, so that this can be
17753 * undone separately from what was previously inserted. */
17754 if (u_sync_once == 2)
17755 {
17756 u_sync_once = 1; /* notify that u_sync() was called */
17757 u_sync(TRUE);
17758 }
17759
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017760 if (lnum <= curbuf->b_ml.ml_line_count)
17761 {
17762 /* existing line, replace it */
17763 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17764 {
17765 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017766 if (lnum == curwin->w_cursor.lnum)
17767 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017768 rettv->vval.v_number = 0; /* OK */
17769 }
17770 }
17771 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17772 {
17773 /* lnum is one past the last line, append the line */
17774 ++added;
17775 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17776 rettv->vval.v_number = 0; /* OK */
17777 }
17778
17779 if (l == NULL) /* only one string argument */
17780 break;
17781 ++lnum;
17782 }
17783
17784 if (added > 0)
17785 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786}
17787
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017788static 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 +000017789
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017791 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017792 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017794set_qf_ll_list(
17795 win_T *wp UNUSED,
17796 typval_T *list_arg UNUSED,
17797 typval_T *action_arg UNUSED,
17798 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017799{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017800#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017801 char_u *act;
17802 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017803#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017804
Bram Moolenaar2641f772005-03-25 21:58:17 +000017805 rettv->vval.v_number = -1;
17806
17807#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017808 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017809 EMSG(_(e_listreq));
17810 else
17811 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017812 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017813
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017814 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017815 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017816 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017817 if (act == NULL)
17818 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017819 if (*act == 'a' || *act == 'r')
17820 action = *act;
17821 }
17822
Bram Moolenaar81484f42012-12-05 15:16:47 +010017823 if (l != NULL && set_errorlist(wp, l, action,
17824 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017825 rettv->vval.v_number = 0;
17826 }
17827#endif
17828}
17829
17830/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017831 * "setloclist()" function
17832 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017834f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017835{
17836 win_T *win;
17837
17838 rettv->vval.v_number = -1;
17839
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017840 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017841 if (win != NULL)
17842 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17843}
17844
17845/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017846 * "setmatches()" function
17847 */
17848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017849f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017850{
17851#ifdef FEAT_SEARCH_EXTRA
17852 list_T *l;
17853 listitem_T *li;
17854 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017855 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017856
17857 rettv->vval.v_number = -1;
17858 if (argvars[0].v_type != VAR_LIST)
17859 {
17860 EMSG(_(e_listreq));
17861 return;
17862 }
17863 if ((l = argvars[0].vval.v_list) != NULL)
17864 {
17865
17866 /* To some extent make sure that we are dealing with a list from
17867 * "getmatches()". */
17868 li = l->lv_first;
17869 while (li != NULL)
17870 {
17871 if (li->li_tv.v_type != VAR_DICT
17872 || (d = li->li_tv.vval.v_dict) == NULL)
17873 {
17874 EMSG(_(e_invarg));
17875 return;
17876 }
17877 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017878 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17879 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017880 && dict_find(d, (char_u *)"priority", -1) != NULL
17881 && dict_find(d, (char_u *)"id", -1) != NULL))
17882 {
17883 EMSG(_(e_invarg));
17884 return;
17885 }
17886 li = li->li_next;
17887 }
17888
17889 clear_matches(curwin);
17890 li = l->lv_first;
17891 while (li != NULL)
17892 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017893 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017894 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017895 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017896 char_u *group;
17897 int priority;
17898 int id;
17899 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017900
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017901 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017902 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17903 {
17904 if (s == NULL)
17905 {
17906 s = list_alloc();
17907 if (s == NULL)
17908 return;
17909 }
17910
17911 /* match from matchaddpos() */
17912 for (i = 1; i < 9; i++)
17913 {
17914 sprintf((char *)buf, (char *)"pos%d", i);
17915 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17916 {
17917 if (di->di_tv.v_type != VAR_LIST)
17918 return;
17919
17920 list_append_tv(s, &di->di_tv);
17921 s->lv_refcount++;
17922 }
17923 else
17924 break;
17925 }
17926 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017927
17928 group = get_dict_string(d, (char_u *)"group", FALSE);
17929 priority = (int)get_dict_number(d, (char_u *)"priority");
17930 id = (int)get_dict_number(d, (char_u *)"id");
17931 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17932 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17933 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017934 if (i == 0)
17935 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017936 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017937 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017938 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017939 }
17940 else
17941 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017942 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017943 list_unref(s);
17944 s = NULL;
17945 }
17946
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017947 li = li->li_next;
17948 }
17949 rettv->vval.v_number = 0;
17950 }
17951#endif
17952}
17953
17954/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017955 * "setpos()" function
17956 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017958f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017959{
17960 pos_T pos;
17961 int fnum;
17962 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017963 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017964
Bram Moolenaar08250432008-02-13 11:42:46 +000017965 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017966 name = get_tv_string_chk(argvars);
17967 if (name != NULL)
17968 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017969 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017970 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017971 if (--pos.col < 0)
17972 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017973 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017974 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017975 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017976 if (fnum == curbuf->b_fnum)
17977 {
17978 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017979 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017980 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017981 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017982 curwin->w_set_curswant = FALSE;
17983 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017984 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017985 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017986 }
17987 else
17988 EMSG(_(e_invarg));
17989 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017990 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17991 {
17992 /* set mark */
17993 if (setmark_pos(name[1], &pos, fnum) == OK)
17994 rettv->vval.v_number = 0;
17995 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017996 else
17997 EMSG(_(e_invarg));
17998 }
17999 }
18000}
18001
18002/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018003 * "setqflist()" function
18004 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018006f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018007{
18008 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18009}
18010
18011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012 * "setreg()" function
18013 */
18014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018015f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016{
18017 int regname;
18018 char_u *strregname;
18019 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018020 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 int append;
18022 char_u yank_type;
18023 long block_len;
18024
18025 block_len = -1;
18026 yank_type = MAUTO;
18027 append = FALSE;
18028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018029 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018030 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018032 if (strregname == NULL)
18033 return; /* type error; errmsg already given */
18034 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 if (regname == 0 || regname == '@')
18036 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018038 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018040 stropt = get_tv_string_chk(&argvars[2]);
18041 if (stropt == NULL)
18042 return; /* type error */
18043 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 switch (*stropt)
18045 {
18046 case 'a': case 'A': /* append */
18047 append = TRUE;
18048 break;
18049 case 'v': case 'c': /* character-wise selection */
18050 yank_type = MCHAR;
18051 break;
18052 case 'V': case 'l': /* line-wise selection */
18053 yank_type = MLINE;
18054 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 case 'b': case Ctrl_V: /* block-wise selection */
18056 yank_type = MBLOCK;
18057 if (VIM_ISDIGIT(stropt[1]))
18058 {
18059 ++stropt;
18060 block_len = getdigits(&stropt) - 1;
18061 --stropt;
18062 }
18063 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 }
18065 }
18066
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018067 if (argvars[1].v_type == VAR_LIST)
18068 {
18069 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018070 char_u **allocval;
18071 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018072 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018073 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018074 int len = argvars[1].vval.v_list->lv_len;
18075 listitem_T *li;
18076
Bram Moolenaar7d647822014-04-05 21:28:56 +020018077 /* First half: use for pointers to result lines; second half: use for
18078 * pointers to allocated copies. */
18079 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018080 if (lstval == NULL)
18081 return;
18082 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018083 allocval = lstval + len + 2;
18084 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018085
18086 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18087 li = li->li_next)
18088 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018089 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018090 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018091 goto free_lstval;
18092 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018093 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018094 /* Need to make a copy, next get_tv_string_buf_chk() will
18095 * overwrite the string. */
18096 strval = vim_strsave(buf);
18097 if (strval == NULL)
18098 goto free_lstval;
18099 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018100 }
18101 *curval++ = strval;
18102 }
18103 *curval++ = NULL;
18104
18105 write_reg_contents_lst(regname, lstval, -1,
18106 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018107free_lstval:
18108 while (curallocval > allocval)
18109 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018110 vim_free(lstval);
18111 }
18112 else
18113 {
18114 strval = get_tv_string_chk(&argvars[1]);
18115 if (strval == NULL)
18116 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018117 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018119 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018120 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121}
18122
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018123/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018124 * "settabvar()" function
18125 */
18126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018127f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018128{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018129#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018130 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018131 tabpage_T *tp;
18132#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018133 char_u *varname, *tabvarname;
18134 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018135
18136 rettv->vval.v_number = 0;
18137
18138 if (check_restricted() || check_secure())
18139 return;
18140
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018141#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018142 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018143#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018144 varname = get_tv_string_chk(&argvars[1]);
18145 varp = &argvars[2];
18146
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018147 if (varname != NULL && varp != NULL
18148#ifdef FEAT_WINDOWS
18149 && tp != NULL
18150#endif
18151 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018152 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018153#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018154 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018155 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018156#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018157
18158 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18159 if (tabvarname != NULL)
18160 {
18161 STRCPY(tabvarname, "t:");
18162 STRCPY(tabvarname + 2, varname);
18163 set_var(tabvarname, varp, TRUE);
18164 vim_free(tabvarname);
18165 }
18166
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018167#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018168 /* Restore current tabpage */
18169 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018170 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018171#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018172 }
18173}
18174
18175/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018176 * "settabwinvar()" function
18177 */
18178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018179f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018180{
18181 setwinvar(argvars, rettv, 1);
18182}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183
18184/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018185 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018188f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018190 setwinvar(argvars, rettv, 0);
18191}
18192
18193/*
18194 * "setwinvar()" and "settabwinvar()" functions
18195 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018196
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018198setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018199{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 win_T *win;
18201#ifdef FEAT_WINDOWS
18202 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018203 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018204 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205#endif
18206 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018207 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018209 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210
18211 if (check_restricted() || check_secure())
18212 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018213
18214#ifdef FEAT_WINDOWS
18215 if (off == 1)
18216 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18217 else
18218 tp = curtab;
18219#endif
18220 win = find_win_by_nr(&argvars[off], tp);
18221 varname = get_tv_string_chk(&argvars[off + 1]);
18222 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223
18224 if (win != NULL && varname != NULL && varp != NULL)
18225 {
18226#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018227 need_switch_win = !(tp == curtab && win == curwin);
18228 if (!need_switch_win
18229 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018232 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018234 long numval;
18235 char_u *strval;
18236 int error = FALSE;
18237
18238 ++varname;
18239 numval = get_tv_number_chk(varp, &error);
18240 strval = get_tv_string_buf_chk(varp, nbuf);
18241 if (!error && strval != NULL)
18242 set_option_value(varname, numval, strval, OPT_LOCAL);
18243 }
18244 else
18245 {
18246 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18247 if (winvarname != NULL)
18248 {
18249 STRCPY(winvarname, "w:");
18250 STRCPY(winvarname + 2, varname);
18251 set_var(winvarname, varp, TRUE);
18252 vim_free(winvarname);
18253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 }
18255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018257 if (need_switch_win)
18258 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259#endif
18260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261}
18262
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018263#ifdef FEAT_CRYPT
18264/*
18265 * "sha256({string})" function
18266 */
18267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018268f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018269{
18270 char_u *p;
18271
18272 p = get_tv_string(&argvars[0]);
18273 rettv->vval.v_string = vim_strsave(
18274 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18275 rettv->v_type = VAR_STRING;
18276}
18277#endif /* FEAT_CRYPT */
18278
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018280 * "shellescape({string})" function
18281 */
18282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018283f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018284{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018285 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018286 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018287 rettv->v_type = VAR_STRING;
18288}
18289
18290/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018291 * shiftwidth() function
18292 */
18293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018294f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018295{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018296 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018297}
18298
18299/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018300 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 */
18302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018303f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018305 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306
Bram Moolenaar0d660222005-01-07 21:51:51 +000018307 p = get_tv_string(&argvars[0]);
18308 rettv->vval.v_string = vim_strsave(p);
18309 simplify_filename(rettv->vval.v_string); /* simplify in place */
18310 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311}
18312
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018313#ifdef FEAT_FLOAT
18314/*
18315 * "sin()" function
18316 */
18317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018318f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018319{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018320 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018321
18322 rettv->v_type = VAR_FLOAT;
18323 if (get_float_arg(argvars, &f) == OK)
18324 rettv->vval.v_float = sin(f);
18325 else
18326 rettv->vval.v_float = 0.0;
18327}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018328
18329/*
18330 * "sinh()" function
18331 */
18332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018333f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018334{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018335 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018336
18337 rettv->v_type = VAR_FLOAT;
18338 if (get_float_arg(argvars, &f) == OK)
18339 rettv->vval.v_float = sinh(f);
18340 else
18341 rettv->vval.v_float = 0.0;
18342}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018343#endif
18344
Bram Moolenaar0d660222005-01-07 21:51:51 +000018345static int
18346#ifdef __BORLANDC__
18347 _RTLENTRYF
18348#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018349 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018350static int
18351#ifdef __BORLANDC__
18352 _RTLENTRYF
18353#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018354 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018355
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018356/* struct used in the array that's given to qsort() */
18357typedef struct
18358{
18359 listitem_T *item;
18360 int idx;
18361} sortItem_T;
18362
Bram Moolenaar0b962472016-02-22 22:51:33 +010018363/* struct storing information about current sort */
18364typedef struct
18365{
18366 int item_compare_ic;
18367 int item_compare_numeric;
18368 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018369#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018370 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018371#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018372 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018373 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018374 dict_T *item_compare_selfdict;
18375 int item_compare_func_err;
18376 int item_compare_keep_zero;
18377} sortinfo_T;
18378static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018379static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018380#define ITEM_COMPARE_FAIL 999
18381
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018383 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018385 static int
18386#ifdef __BORLANDC__
18387_RTLENTRYF
18388#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018389item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018391 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018392 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018393 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018394 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018395 int res;
18396 char_u numbuf1[NUMBUFLEN];
18397 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018399 si1 = (sortItem_T *)s1;
18400 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018401 tv1 = &si1->item->li_tv;
18402 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018403
Bram Moolenaar0b962472016-02-22 22:51:33 +010018404 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018405 {
18406 long v1 = get_tv_number(tv1);
18407 long v2 = get_tv_number(tv2);
18408
18409 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18410 }
18411
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018412#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018413 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018414 {
18415 float_T v1 = get_tv_float(tv1);
18416 float_T v2 = get_tv_float(tv2);
18417
18418 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18419 }
18420#endif
18421
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018422 /* tv2string() puts quotes around a string and allocates memory. Don't do
18423 * that for string variables. Use a single quote when comparing with a
18424 * non-string to do what the docs promise. */
18425 if (tv1->v_type == VAR_STRING)
18426 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018427 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018428 p1 = (char_u *)"'";
18429 else
18430 p1 = tv1->vval.v_string;
18431 }
18432 else
18433 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18434 if (tv2->v_type == VAR_STRING)
18435 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018436 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018437 p2 = (char_u *)"'";
18438 else
18439 p2 = tv2->vval.v_string;
18440 }
18441 else
18442 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018443 if (p1 == NULL)
18444 p1 = (char_u *)"";
18445 if (p2 == NULL)
18446 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018447 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018448 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018449 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018450 res = STRICMP(p1, p2);
18451 else
18452 res = STRCMP(p1, p2);
18453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018455 {
18456 double n1, n2;
18457 n1 = strtod((char *)p1, (char **)&p1);
18458 n2 = strtod((char *)p2, (char **)&p2);
18459 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18460 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018461
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018462 /* When the result would be zero, compare the item indexes. Makes the
18463 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018464 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018465 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018466
Bram Moolenaar0d660222005-01-07 21:51:51 +000018467 vim_free(tofree1);
18468 vim_free(tofree2);
18469 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470}
18471
18472 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018473#ifdef __BORLANDC__
18474_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018476item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018477{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018478 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018479 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018480 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018481 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018482 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018483 char_u *func_name;
18484 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018486 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018487 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018488 return 0;
18489
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018490 si1 = (sortItem_T *)s1;
18491 si2 = (sortItem_T *)s2;
18492
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018493 if (partial == NULL)
18494 func_name = sortinfo->item_compare_func;
18495 else
18496 func_name = partial->pt_name;
18497
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018498 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018499 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018500 copy_tv(&si1->item->li_tv, &argv[0]);
18501 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018502
18503 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018504 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018505 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018506 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018507 clear_tv(&argv[0]);
18508 clear_tv(&argv[1]);
18509
18510 if (res == FAIL)
18511 res = ITEM_COMPARE_FAIL;
18512 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018513 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18514 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018515 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018516 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018517
18518 /* When the result would be zero, compare the pointers themselves. Makes
18519 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018520 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018521 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018522
Bram Moolenaar0d660222005-01-07 21:51:51 +000018523 return res;
18524}
18525
18526/*
18527 * "sort({list})" function
18528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018530do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531{
Bram Moolenaar33570922005-01-25 22:26:29 +000018532 list_T *l;
18533 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018534 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018535 sortinfo_T *old_sortinfo;
18536 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018537 long len;
18538 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539
Bram Moolenaar0b962472016-02-22 22:51:33 +010018540 /* Pointer to current info struct used in compare function. Save and
18541 * restore the current one for nested calls. */
18542 old_sortinfo = sortinfo;
18543 sortinfo = &info;
18544
Bram Moolenaar0d660222005-01-07 21:51:51 +000018545 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018546 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 else
18548 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018549 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018550 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018551 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18552 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018553 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018554 rettv->vval.v_list = l;
18555 rettv->v_type = VAR_LIST;
18556 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557
Bram Moolenaar0d660222005-01-07 21:51:51 +000018558 len = list_len(l);
18559 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018560 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561
Bram Moolenaar0b962472016-02-22 22:51:33 +010018562 info.item_compare_ic = FALSE;
18563 info.item_compare_numeric = FALSE;
18564 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018565#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018566 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018567#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018568 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018569 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018570 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018571 if (argvars[1].v_type != VAR_UNKNOWN)
18572 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018573 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018574 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018575 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018576 else if (argvars[1].v_type == VAR_PARTIAL)
18577 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018578 else
18579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018580 int error = FALSE;
18581
18582 i = get_tv_number_chk(&argvars[1], &error);
18583 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018584 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018585 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018586 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018587 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018588 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018589 else if (i != 0)
18590 {
18591 EMSG(_(e_invarg));
18592 goto theend;
18593 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018594 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018595 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018596 if (*info.item_compare_func == NUL)
18597 {
18598 /* empty string means default sort */
18599 info.item_compare_func = NULL;
18600 }
18601 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018602 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018603 info.item_compare_func = NULL;
18604 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018605 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018606 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018607 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018608 info.item_compare_func = NULL;
18609 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018610 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018611#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018612 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018613 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018614 info.item_compare_func = NULL;
18615 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018616 }
18617#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018618 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018619 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018620 info.item_compare_func = NULL;
18621 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018622 }
18623 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018624 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018625
18626 if (argvars[2].v_type != VAR_UNKNOWN)
18627 {
18628 /* optional third argument: {dict} */
18629 if (argvars[2].v_type != VAR_DICT)
18630 {
18631 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018632 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018633 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018634 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018635 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637
Bram Moolenaar0d660222005-01-07 21:51:51 +000018638 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018639 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018640 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018641 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642
Bram Moolenaar327aa022014-03-25 18:24:23 +010018643 i = 0;
18644 if (sort)
18645 {
18646 /* sort(): ptrs will be the list to sort */
18647 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018648 {
18649 ptrs[i].item = li;
18650 ptrs[i].idx = i;
18651 ++i;
18652 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018653
Bram Moolenaar0b962472016-02-22 22:51:33 +010018654 info.item_compare_func_err = FALSE;
18655 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018656 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018657 if ((info.item_compare_func != NULL
18658 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018659 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018660 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018661 EMSG(_("E702: Sort compare function failed"));
18662 else
18663 {
18664 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018665 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018666 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018667 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018668 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018669
Bram Moolenaar0b962472016-02-22 22:51:33 +010018670 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018671 {
18672 /* Clear the List and append the items in sorted order. */
18673 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18674 l->lv_len = 0;
18675 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018676 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018677 }
18678 }
18679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018681 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018682 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018683
18684 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018685 info.item_compare_func_err = FALSE;
18686 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018687 item_compare_func_ptr = info.item_compare_func != NULL
18688 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018689 ? item_compare2 : item_compare;
18690
18691 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18692 li = li->li_next)
18693 {
18694 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18695 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018696 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018697 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018698 {
18699 EMSG(_("E882: Uniq compare function failed"));
18700 break;
18701 }
18702 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018703
Bram Moolenaar0b962472016-02-22 22:51:33 +010018704 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018705 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018706 while (--i >= 0)
18707 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018708 li = ptrs[i].item->li_next;
18709 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018710 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018711 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018712 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018713 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018714 list_fix_watch(l, li);
18715 listitem_free(li);
18716 l->lv_len--;
18717 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018718 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018719 }
18720
18721 vim_free(ptrs);
18722 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018723theend:
18724 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018725}
18726
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018727/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018728 * "sort({list})" function
18729 */
18730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018731f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018732{
18733 do_sort_uniq(argvars, rettv, TRUE);
18734}
18735
18736/*
18737 * "uniq({list})" function
18738 */
18739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018740f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018741{
18742 do_sort_uniq(argvars, rettv, FALSE);
18743}
18744
18745/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018746 * "soundfold({word})" function
18747 */
18748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018749f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018750{
18751 char_u *s;
18752
18753 rettv->v_type = VAR_STRING;
18754 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018755#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018756 rettv->vval.v_string = eval_soundfold(s);
18757#else
18758 rettv->vval.v_string = vim_strsave(s);
18759#endif
18760}
18761
18762/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018763 * "spellbadword()" function
18764 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018766f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018767{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018768 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018769 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018770 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018771
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018772 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018773 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018774
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018775#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018776 if (argvars[0].v_type == VAR_UNKNOWN)
18777 {
18778 /* Find the start and length of the badly spelled word. */
18779 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18780 if (len != 0)
18781 word = ml_get_cursor();
18782 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018783 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018784 {
18785 char_u *str = get_tv_string_chk(&argvars[0]);
18786 int capcol = -1;
18787
18788 if (str != NULL)
18789 {
18790 /* Check the argument for spelling. */
18791 while (*str != NUL)
18792 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018793 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018794 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018795 {
18796 word = str;
18797 break;
18798 }
18799 str += len;
18800 }
18801 }
18802 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018803#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018804
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018805 list_append_string(rettv->vval.v_list, word, len);
18806 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018807 attr == HLF_SPB ? "bad" :
18808 attr == HLF_SPR ? "rare" :
18809 attr == HLF_SPL ? "local" :
18810 attr == HLF_SPC ? "caps" :
18811 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018812}
18813
18814/*
18815 * "spellsuggest()" function
18816 */
18817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018818f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018819{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018820#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018821 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018822 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018823 int maxcount;
18824 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018825 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018826 listitem_T *li;
18827 int need_capital = FALSE;
18828#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018829
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018830 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018831 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018832
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018833#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018834 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018835 {
18836 str = get_tv_string(&argvars[0]);
18837 if (argvars[1].v_type != VAR_UNKNOWN)
18838 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018839 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018840 if (maxcount <= 0)
18841 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018842 if (argvars[2].v_type != VAR_UNKNOWN)
18843 {
18844 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18845 if (typeerr)
18846 return;
18847 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018848 }
18849 else
18850 maxcount = 25;
18851
Bram Moolenaar4770d092006-01-12 23:22:24 +000018852 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018853
18854 for (i = 0; i < ga.ga_len; ++i)
18855 {
18856 str = ((char_u **)ga.ga_data)[i];
18857
18858 li = listitem_alloc();
18859 if (li == NULL)
18860 vim_free(str);
18861 else
18862 {
18863 li->li_tv.v_type = VAR_STRING;
18864 li->li_tv.v_lock = 0;
18865 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018866 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018867 }
18868 }
18869 ga_clear(&ga);
18870 }
18871#endif
18872}
18873
Bram Moolenaar0d660222005-01-07 21:51:51 +000018874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018875f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018876{
18877 char_u *str;
18878 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018879 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018880 regmatch_T regmatch;
18881 char_u patbuf[NUMBUFLEN];
18882 char_u *save_cpo;
18883 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018884 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018885 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018886 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018887
18888 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18889 save_cpo = p_cpo;
18890 p_cpo = (char_u *)"";
18891
18892 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018893 if (argvars[1].v_type != VAR_UNKNOWN)
18894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018895 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18896 if (pat == NULL)
18897 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018898 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018899 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018900 }
18901 if (pat == NULL || *pat == NUL)
18902 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018903
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018904 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018906 if (typeerr)
18907 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908
Bram Moolenaar0d660222005-01-07 21:51:51 +000018909 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18910 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018912 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018913 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018914 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018915 if (*str == NUL)
18916 match = FALSE; /* empty item at the end */
18917 else
18918 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018919 if (match)
18920 end = regmatch.startp[0];
18921 else
18922 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018923 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18924 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018925 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018926 if (list_append_string(rettv->vval.v_list, str,
18927 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018928 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929 }
18930 if (!match)
18931 break;
18932 /* Advance to just after the match. */
18933 if (regmatch.endp[0] > str)
18934 col = 0;
18935 else
18936 {
18937 /* Don't get stuck at the same match. */
18938#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018939 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018940#else
18941 col = 1;
18942#endif
18943 }
18944 str = regmatch.endp[0];
18945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946
Bram Moolenaar473de612013-06-08 18:19:48 +020018947 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949
Bram Moolenaar0d660222005-01-07 21:51:51 +000018950 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951}
18952
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018953#ifdef FEAT_FLOAT
18954/*
18955 * "sqrt()" function
18956 */
18957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018958f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018959{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018960 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018961
18962 rettv->v_type = VAR_FLOAT;
18963 if (get_float_arg(argvars, &f) == OK)
18964 rettv->vval.v_float = sqrt(f);
18965 else
18966 rettv->vval.v_float = 0.0;
18967}
18968
18969/*
18970 * "str2float()" function
18971 */
18972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018973f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018974{
18975 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18976
18977 if (*p == '+')
18978 p = skipwhite(p + 1);
18979 (void)string2float(p, &rettv->vval.v_float);
18980 rettv->v_type = VAR_FLOAT;
18981}
18982#endif
18983
Bram Moolenaar2c932302006-03-18 21:42:09 +000018984/*
18985 * "str2nr()" function
18986 */
18987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018988f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018989{
18990 int base = 10;
18991 char_u *p;
18992 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018993 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018994
18995 if (argvars[1].v_type != VAR_UNKNOWN)
18996 {
18997 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018998 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018999 {
19000 EMSG(_(e_invarg));
19001 return;
19002 }
19003 }
19004
19005 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019006 if (*p == '+')
19007 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019008 switch (base)
19009 {
19010 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19011 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19012 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19013 default: what = 0;
19014 }
19015 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019016 rettv->vval.v_number = n;
19017}
19018
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019#ifdef HAVE_STRFTIME
19020/*
19021 * "strftime({format}[, {time}])" function
19022 */
19023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019024f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025{
19026 char_u result_buf[256];
19027 struct tm *curtime;
19028 time_t seconds;
19029 char_u *p;
19030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019031 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019033 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019034 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 seconds = time(NULL);
19036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019037 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 curtime = localtime(&seconds);
19039 /* MSVC returns NULL for an invalid value of seconds. */
19040 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019041 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 else
19043 {
19044# ifdef FEAT_MBYTE
19045 vimconv_T conv;
19046 char_u *enc;
19047
19048 conv.vc_type = CONV_NONE;
19049 enc = enc_locale();
19050 convert_setup(&conv, p_enc, enc);
19051 if (conv.vc_type != CONV_NONE)
19052 p = string_convert(&conv, p, NULL);
19053# endif
19054 if (p != NULL)
19055 (void)strftime((char *)result_buf, sizeof(result_buf),
19056 (char *)p, curtime);
19057 else
19058 result_buf[0] = NUL;
19059
19060# ifdef FEAT_MBYTE
19061 if (conv.vc_type != CONV_NONE)
19062 vim_free(p);
19063 convert_setup(&conv, enc, p_enc);
19064 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019065 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066 else
19067# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019068 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069
19070# ifdef FEAT_MBYTE
19071 /* Release conversion descriptors */
19072 convert_setup(&conv, NULL, NULL);
19073 vim_free(enc);
19074# endif
19075 }
19076}
19077#endif
19078
19079/*
19080 * "stridx()" function
19081 */
19082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019083f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084{
19085 char_u buf[NUMBUFLEN];
19086 char_u *needle;
19087 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019088 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019090 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019092 needle = get_tv_string_chk(&argvars[1]);
19093 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019094 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019095 if (needle == NULL || haystack == NULL)
19096 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097
Bram Moolenaar33570922005-01-25 22:26:29 +000019098 if (argvars[2].v_type != VAR_UNKNOWN)
19099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019100 int error = FALSE;
19101
19102 start_idx = get_tv_number_chk(&argvars[2], &error);
19103 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019104 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019105 if (start_idx >= 0)
19106 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019107 }
19108
19109 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19110 if (pos != NULL)
19111 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112}
19113
19114/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019115 * "string()" function
19116 */
19117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019118f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019119{
19120 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019121 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019123 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019124 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019125 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019126 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019127 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128}
19129
19130/*
19131 * "strlen()" function
19132 */
19133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019134f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136 rettv->vval.v_number = (varnumber_T)(STRLEN(
19137 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138}
19139
19140/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019141 * "strchars()" function
19142 */
19143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019144f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019145{
19146 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019147 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019148#ifdef FEAT_MBYTE
19149 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019150 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019151#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019152
19153 if (argvars[1].v_type != VAR_UNKNOWN)
19154 skipcc = get_tv_number_chk(&argvars[1], NULL);
19155 if (skipcc < 0 || skipcc > 1)
19156 EMSG(_(e_invarg));
19157 else
19158 {
19159#ifdef FEAT_MBYTE
19160 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19161 while (*s != NUL)
19162 {
19163 func_mb_ptr2char_adv(&s);
19164 ++len;
19165 }
19166 rettv->vval.v_number = len;
19167#else
19168 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19169#endif
19170 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019171}
19172
19173/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019174 * "strdisplaywidth()" function
19175 */
19176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019177f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019178{
19179 char_u *s = get_tv_string(&argvars[0]);
19180 int col = 0;
19181
19182 if (argvars[1].v_type != VAR_UNKNOWN)
19183 col = get_tv_number(&argvars[1]);
19184
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019185 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019186}
19187
19188/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019189 * "strwidth()" function
19190 */
19191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019192f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019193{
19194 char_u *s = get_tv_string(&argvars[0]);
19195
19196 rettv->vval.v_number = (varnumber_T)(
19197#ifdef FEAT_MBYTE
19198 mb_string2cells(s, -1)
19199#else
19200 STRLEN(s)
19201#endif
19202 );
19203}
19204
19205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 * "strpart()" function
19207 */
19208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019209f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210{
19211 char_u *p;
19212 int n;
19213 int len;
19214 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019215 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019217 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 slen = (int)STRLEN(p);
19219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019220 n = get_tv_number_chk(&argvars[1], &error);
19221 if (error)
19222 len = 0;
19223 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019224 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 else
19226 len = slen - n; /* default len: all bytes that are available. */
19227
19228 /*
19229 * Only return the overlap between the specified part and the actual
19230 * string.
19231 */
19232 if (n < 0)
19233 {
19234 len += n;
19235 n = 0;
19236 }
19237 else if (n > slen)
19238 n = slen;
19239 if (len < 0)
19240 len = 0;
19241 else if (n + len > slen)
19242 len = slen - n;
19243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019244 rettv->v_type = VAR_STRING;
19245 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246}
19247
19248/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019249 * "strridx()" function
19250 */
19251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019252f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019253{
19254 char_u buf[NUMBUFLEN];
19255 char_u *needle;
19256 char_u *haystack;
19257 char_u *rest;
19258 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019259 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019261 needle = get_tv_string_chk(&argvars[1]);
19262 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019263
19264 rettv->vval.v_number = -1;
19265 if (needle == NULL || haystack == NULL)
19266 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019267
19268 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019269 if (argvars[2].v_type != VAR_UNKNOWN)
19270 {
19271 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019272 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019273 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019274 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019275 }
19276 else
19277 end_idx = haystack_len;
19278
Bram Moolenaar0d660222005-01-07 21:51:51 +000019279 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019280 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019281 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019282 lastmatch = haystack + end_idx;
19283 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019284 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019285 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019286 for (rest = haystack; *rest != '\0'; ++rest)
19287 {
19288 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019289 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019290 break;
19291 lastmatch = rest;
19292 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019293 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019294
19295 if (lastmatch == NULL)
19296 rettv->vval.v_number = -1;
19297 else
19298 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19299}
19300
19301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 * "strtrans()" function
19303 */
19304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019305f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019307 rettv->v_type = VAR_STRING;
19308 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309}
19310
19311/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019312 * "submatch()" function
19313 */
19314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019315f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019316{
Bram Moolenaar41571762014-04-02 19:00:58 +020019317 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019318 int no;
19319 int retList = 0;
19320
19321 no = (int)get_tv_number_chk(&argvars[0], &error);
19322 if (error)
19323 return;
19324 error = FALSE;
19325 if (argvars[1].v_type != VAR_UNKNOWN)
19326 retList = get_tv_number_chk(&argvars[1], &error);
19327 if (error)
19328 return;
19329
19330 if (retList == 0)
19331 {
19332 rettv->v_type = VAR_STRING;
19333 rettv->vval.v_string = reg_submatch(no);
19334 }
19335 else
19336 {
19337 rettv->v_type = VAR_LIST;
19338 rettv->vval.v_list = reg_submatch_list(no);
19339 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019340}
19341
19342/*
19343 * "substitute()" function
19344 */
19345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019346f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019347{
19348 char_u patbuf[NUMBUFLEN];
19349 char_u subbuf[NUMBUFLEN];
19350 char_u flagsbuf[NUMBUFLEN];
19351
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019352 char_u *str = get_tv_string_chk(&argvars[0]);
19353 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19354 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19355 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19356
Bram Moolenaar0d660222005-01-07 21:51:51 +000019357 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019358 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19359 rettv->vval.v_string = NULL;
19360 else
19361 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019362}
19363
19364/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019365 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019368f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369{
19370 int id = 0;
19371#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019372 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 long col;
19374 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019375 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019377 lnum = get_tv_lnum(argvars); /* -1 on type error */
19378 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19379 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019381 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019382 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019383 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384#endif
19385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019386 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387}
19388
19389/*
19390 * "synIDattr(id, what [, mode])" function
19391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019393f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394{
19395 char_u *p = NULL;
19396#ifdef FEAT_SYN_HL
19397 int id;
19398 char_u *what;
19399 char_u *mode;
19400 char_u modebuf[NUMBUFLEN];
19401 int modec;
19402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019403 id = get_tv_number(&argvars[0]);
19404 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019405 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019407 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019409 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 modec = 0; /* replace invalid with current */
19411 }
19412 else
19413 {
19414#ifdef FEAT_GUI
19415 if (gui.in_use)
19416 modec = 'g';
19417 else
19418#endif
19419 if (t_colors > 1)
19420 modec = 'c';
19421 else
19422 modec = 't';
19423 }
19424
19425
19426 switch (TOLOWER_ASC(what[0]))
19427 {
19428 case 'b':
19429 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19430 p = highlight_color(id, what, modec);
19431 else /* bold */
19432 p = highlight_has_attr(id, HL_BOLD, modec);
19433 break;
19434
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019435 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 p = highlight_color(id, what, modec);
19437 break;
19438
19439 case 'i':
19440 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19441 p = highlight_has_attr(id, HL_INVERSE, modec);
19442 else /* italic */
19443 p = highlight_has_attr(id, HL_ITALIC, modec);
19444 break;
19445
19446 case 'n': /* name */
19447 p = get_highlight_name(NULL, id - 1);
19448 break;
19449
19450 case 'r': /* reverse */
19451 p = highlight_has_attr(id, HL_INVERSE, modec);
19452 break;
19453
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019454 case 's':
19455 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19456 p = highlight_color(id, what, modec);
19457 else /* standout */
19458 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 break;
19460
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019461 case 'u':
19462 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19463 /* underline */
19464 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19465 else
19466 /* undercurl */
19467 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 break;
19469 }
19470
19471 if (p != NULL)
19472 p = vim_strsave(p);
19473#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019474 rettv->v_type = VAR_STRING;
19475 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476}
19477
19478/*
19479 * "synIDtrans(id)" function
19480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019482f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483{
19484 int id;
19485
19486#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019487 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488
19489 if (id > 0)
19490 id = syn_get_final_id(id);
19491 else
19492#endif
19493 id = 0;
19494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019495 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496}
19497
19498/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019499 * "synconcealed(lnum, col)" function
19500 */
19501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019502f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019503{
19504#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19505 long lnum;
19506 long col;
19507 int syntax_flags = 0;
19508 int cchar;
19509 int matchid = 0;
19510 char_u str[NUMBUFLEN];
19511#endif
19512
19513 rettv->v_type = VAR_LIST;
19514 rettv->vval.v_list = NULL;
19515
19516#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19517 lnum = get_tv_lnum(argvars); /* -1 on type error */
19518 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19519
19520 vim_memset(str, NUL, sizeof(str));
19521
19522 if (rettv_list_alloc(rettv) != FAIL)
19523 {
19524 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19525 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19526 && curwin->w_p_cole > 0)
19527 {
19528 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19529 syntax_flags = get_syntax_info(&matchid);
19530
19531 /* get the conceal character */
19532 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19533 {
19534 cchar = syn_get_sub_char();
19535 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19536 cchar = lcs_conceal;
19537 if (cchar != NUL)
19538 {
19539# ifdef FEAT_MBYTE
19540 if (has_mbyte)
19541 (*mb_char2bytes)(cchar, str);
19542 else
19543# endif
19544 str[0] = cchar;
19545 }
19546 }
19547 }
19548
19549 list_append_number(rettv->vval.v_list,
19550 (syntax_flags & HL_CONCEAL) != 0);
19551 /* -1 to auto-determine strlen */
19552 list_append_string(rettv->vval.v_list, str, -1);
19553 list_append_number(rettv->vval.v_list, matchid);
19554 }
19555#endif
19556}
19557
19558/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019559 * "synstack(lnum, col)" function
19560 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019562f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019563{
19564#ifdef FEAT_SYN_HL
19565 long lnum;
19566 long col;
19567 int i;
19568 int id;
19569#endif
19570
19571 rettv->v_type = VAR_LIST;
19572 rettv->vval.v_list = NULL;
19573
19574#ifdef FEAT_SYN_HL
19575 lnum = get_tv_lnum(argvars); /* -1 on type error */
19576 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19577
19578 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019579 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019580 && rettv_list_alloc(rettv) != FAIL)
19581 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019582 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019583 for (i = 0; ; ++i)
19584 {
19585 id = syn_get_stack_item(i);
19586 if (id < 0)
19587 break;
19588 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19589 break;
19590 }
19591 }
19592#endif
19593}
19594
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019596get_cmd_output_as_rettv(
19597 typval_T *argvars,
19598 typval_T *rettv,
19599 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019601 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019603 char_u *infile = NULL;
19604 char_u buf[NUMBUFLEN];
19605 int err = FALSE;
19606 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019607 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019608 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019610 rettv->v_type = VAR_STRING;
19611 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019612 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019613 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019614
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019615 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019616 {
19617 /*
19618 * Write the string to a temp file, to be used for input of the shell
19619 * command.
19620 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019621 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019622 {
19623 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019624 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019625 }
19626
19627 fd = mch_fopen((char *)infile, WRITEBIN);
19628 if (fd == NULL)
19629 {
19630 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019631 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019632 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019633 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019634 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019635 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19636 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019637 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019638 else
19639 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019640 size_t len;
19641
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019642 p = get_tv_string_buf_chk(&argvars[1], buf);
19643 if (p == NULL)
19644 {
19645 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019646 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019647 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019648 len = STRLEN(p);
19649 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019650 err = TRUE;
19651 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019652 if (fclose(fd) != 0)
19653 err = TRUE;
19654 if (err)
19655 {
19656 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019657 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019658 }
19659 }
19660
Bram Moolenaar52a72462014-08-29 15:53:52 +020019661 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19662 * echoes typeahead, that messes up the display. */
19663 if (!msg_silent)
19664 flags += SHELL_COOKED;
19665
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019666 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019668 int len;
19669 listitem_T *li;
19670 char_u *s = NULL;
19671 char_u *start;
19672 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019673 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674
Bram Moolenaar52a72462014-08-29 15:53:52 +020019675 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019676 if (res == NULL)
19677 goto errret;
19678
19679 list = list_alloc();
19680 if (list == NULL)
19681 goto errret;
19682
19683 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019685 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019686 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019687 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019688 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019689
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019690 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019691 if (s == NULL)
19692 goto errret;
19693
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019694 for (p = s; start < end; ++p, ++start)
19695 *p = *start == NUL ? NL : *start;
19696 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019697
19698 li = listitem_alloc();
19699 if (li == NULL)
19700 {
19701 vim_free(s);
19702 goto errret;
19703 }
19704 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019705 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019706 li->li_tv.vval.v_string = s;
19707 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019709
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019710 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019711 rettv->v_type = VAR_LIST;
19712 rettv->vval.v_list = list;
19713 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019715 else
19716 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019717 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019718#ifdef USE_CR
19719 /* translate <CR> into <NL> */
19720 if (res != NULL)
19721 {
19722 char_u *s;
19723
19724 for (s = res; *s; ++s)
19725 {
19726 if (*s == CAR)
19727 *s = NL;
19728 }
19729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730#else
19731# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019732 /* translate <CR><NL> into <NL> */
19733 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019735 char_u *s, *d;
19736
19737 d = res;
19738 for (s = res; *s; ++s)
19739 {
19740 if (s[0] == CAR && s[1] == NL)
19741 ++s;
19742 *d++ = *s;
19743 }
19744 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746# endif
19747#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019748 rettv->vval.v_string = res;
19749 res = NULL;
19750 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019751
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019752errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019753 if (infile != NULL)
19754 {
19755 mch_remove(infile);
19756 vim_free(infile);
19757 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019758 if (res != NULL)
19759 vim_free(res);
19760 if (list != NULL)
19761 list_free(list, TRUE);
19762}
19763
19764/*
19765 * "system()" function
19766 */
19767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019768f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019769{
19770 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19771}
19772
19773/*
19774 * "systemlist()" function
19775 */
19776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019777f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019778{
19779 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780}
19781
19782/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019783 * "tabpagebuflist()" function
19784 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019786f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019787{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019788#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019789 tabpage_T *tp;
19790 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019791
19792 if (argvars[0].v_type == VAR_UNKNOWN)
19793 wp = firstwin;
19794 else
19795 {
19796 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19797 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019798 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019799 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019800 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019801 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019802 for (; wp != NULL; wp = wp->w_next)
19803 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019804 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019805 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019806 }
19807#endif
19808}
19809
19810
19811/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019812 * "tabpagenr()" function
19813 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019815f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019816{
19817 int nr = 1;
19818#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019819 char_u *arg;
19820
19821 if (argvars[0].v_type != VAR_UNKNOWN)
19822 {
19823 arg = get_tv_string_chk(&argvars[0]);
19824 nr = 0;
19825 if (arg != NULL)
19826 {
19827 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019828 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019829 else
19830 EMSG2(_(e_invexpr2), arg);
19831 }
19832 }
19833 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019834 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019835#endif
19836 rettv->vval.v_number = nr;
19837}
19838
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019839
19840#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019841static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019842
19843/*
19844 * Common code for tabpagewinnr() and winnr().
19845 */
19846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019847get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019848{
19849 win_T *twin;
19850 int nr = 1;
19851 win_T *wp;
19852 char_u *arg;
19853
19854 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19855 if (argvar->v_type != VAR_UNKNOWN)
19856 {
19857 arg = get_tv_string_chk(argvar);
19858 if (arg == NULL)
19859 nr = 0; /* type error; errmsg already given */
19860 else if (STRCMP(arg, "$") == 0)
19861 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19862 else if (STRCMP(arg, "#") == 0)
19863 {
19864 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19865 if (twin == NULL)
19866 nr = 0;
19867 }
19868 else
19869 {
19870 EMSG2(_(e_invexpr2), arg);
19871 nr = 0;
19872 }
19873 }
19874
19875 if (nr > 0)
19876 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19877 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019878 {
19879 if (wp == NULL)
19880 {
19881 /* didn't find it in this tabpage */
19882 nr = 0;
19883 break;
19884 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019885 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019886 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019887 return nr;
19888}
19889#endif
19890
19891/*
19892 * "tabpagewinnr()" function
19893 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019895f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019896{
19897 int nr = 1;
19898#ifdef FEAT_WINDOWS
19899 tabpage_T *tp;
19900
19901 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19902 if (tp == NULL)
19903 nr = 0;
19904 else
19905 nr = get_winnr(tp, &argvars[1]);
19906#endif
19907 rettv->vval.v_number = nr;
19908}
19909
19910
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019911/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019912 * "tagfiles()" function
19913 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019915f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019916{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019917 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019918 tagname_T tn;
19919 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019920
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019921 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019922 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019923 fname = alloc(MAXPATHL);
19924 if (fname == NULL)
19925 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019926
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019927 for (first = TRUE; ; first = FALSE)
19928 if (get_tagfname(&tn, first, fname) == FAIL
19929 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019930 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019931 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019932 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019933}
19934
19935/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019936 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019937 */
19938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019939f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019940{
19941 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019942
19943 tag_pattern = get_tv_string(&argvars[0]);
19944
19945 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019946 if (*tag_pattern == NUL)
19947 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019948
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019949 if (rettv_list_alloc(rettv) == OK)
19950 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019951}
19952
19953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 * "tempname()" function
19955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019957f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958{
19959 static int x = 'A';
19960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019961 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019962 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963
19964 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19965 * names. Skip 'I' and 'O', they are used for shell redirection. */
19966 do
19967 {
19968 if (x == 'Z')
19969 x = '0';
19970 else if (x == '9')
19971 x = 'A';
19972 else
19973 {
19974#ifdef EBCDIC
19975 if (x == 'I')
19976 x = 'J';
19977 else if (x == 'R')
19978 x = 'S';
19979 else
19980#endif
19981 ++x;
19982 }
19983 } while (x == 'I' || x == 'O');
19984}
19985
19986/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019987 * "test(list)" function: Just checking the walls...
19988 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019990f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019991{
19992 /* Used for unit testing. Change the code below to your liking. */
19993#if 0
19994 listitem_T *li;
19995 list_T *l;
19996 char_u *bad, *good;
19997
19998 if (argvars[0].v_type != VAR_LIST)
19999 return;
20000 l = argvars[0].vval.v_list;
20001 if (l == NULL)
20002 return;
20003 li = l->lv_first;
20004 if (li == NULL)
20005 return;
20006 bad = get_tv_string(&li->li_tv);
20007 li = li->li_next;
20008 if (li == NULL)
20009 return;
20010 good = get_tv_string(&li->li_tv);
20011 rettv->vval.v_number = test_edit_score(bad, good);
20012#endif
20013}
20014
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020015#ifdef FEAT_FLOAT
20016/*
20017 * "tan()" function
20018 */
20019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020020f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020021{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020022 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020023
20024 rettv->v_type = VAR_FLOAT;
20025 if (get_float_arg(argvars, &f) == OK)
20026 rettv->vval.v_float = tan(f);
20027 else
20028 rettv->vval.v_float = 0.0;
20029}
20030
20031/*
20032 * "tanh()" function
20033 */
20034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020035f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020036{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020037 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020038
20039 rettv->v_type = VAR_FLOAT;
20040 if (get_float_arg(argvars, &f) == OK)
20041 rettv->vval.v_float = tanh(f);
20042 else
20043 rettv->vval.v_float = 0.0;
20044}
20045#endif
20046
Bram Moolenaard52d9742005-08-21 22:20:28 +000020047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 * "tolower(string)" function
20049 */
20050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020051f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052{
20053 char_u *p;
20054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020055 p = vim_strsave(get_tv_string(&argvars[0]));
20056 rettv->v_type = VAR_STRING;
20057 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058
20059 if (p != NULL)
20060 while (*p != NUL)
20061 {
20062#ifdef FEAT_MBYTE
20063 int l;
20064
20065 if (enc_utf8)
20066 {
20067 int c, lc;
20068
20069 c = utf_ptr2char(p);
20070 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020071 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 /* TODO: reallocate string when byte count changes. */
20073 if (utf_char2len(lc) == l)
20074 utf_char2bytes(lc, p);
20075 p += l;
20076 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020077 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 p += l; /* skip multi-byte character */
20079 else
20080#endif
20081 {
20082 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20083 ++p;
20084 }
20085 }
20086}
20087
20088/*
20089 * "toupper(string)" function
20090 */
20091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020092f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020094 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020095 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096}
20097
20098/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020099 * "tr(string, fromstr, tostr)" function
20100 */
20101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020102f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020103{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020104 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020105 char_u *fromstr;
20106 char_u *tostr;
20107 char_u *p;
20108#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020109 int inlen;
20110 int fromlen;
20111 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020112 int idx;
20113 char_u *cpstr;
20114 int cplen;
20115 int first = TRUE;
20116#endif
20117 char_u buf[NUMBUFLEN];
20118 char_u buf2[NUMBUFLEN];
20119 garray_T ga;
20120
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020121 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020122 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20123 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020124
20125 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020126 rettv->v_type = VAR_STRING;
20127 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020128 if (fromstr == NULL || tostr == NULL)
20129 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020130 ga_init2(&ga, (int)sizeof(char), 80);
20131
20132#ifdef FEAT_MBYTE
20133 if (!has_mbyte)
20134#endif
20135 /* not multi-byte: fromstr and tostr must be the same length */
20136 if (STRLEN(fromstr) != STRLEN(tostr))
20137 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020138#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020139error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020140#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020141 EMSG2(_(e_invarg2), fromstr);
20142 ga_clear(&ga);
20143 return;
20144 }
20145
20146 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020147 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020148 {
20149#ifdef FEAT_MBYTE
20150 if (has_mbyte)
20151 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020152 inlen = (*mb_ptr2len)(in_str);
20153 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020154 cplen = inlen;
20155 idx = 0;
20156 for (p = fromstr; *p != NUL; p += fromlen)
20157 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020158 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020159 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020160 {
20161 for (p = tostr; *p != NUL; p += tolen)
20162 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020163 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020164 if (idx-- == 0)
20165 {
20166 cplen = tolen;
20167 cpstr = p;
20168 break;
20169 }
20170 }
20171 if (*p == NUL) /* tostr is shorter than fromstr */
20172 goto error;
20173 break;
20174 }
20175 ++idx;
20176 }
20177
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020178 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020179 {
20180 /* Check that fromstr and tostr have the same number of
20181 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020182 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020183 first = FALSE;
20184 for (p = tostr; *p != NUL; p += tolen)
20185 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020186 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020187 --idx;
20188 }
20189 if (idx != 0)
20190 goto error;
20191 }
20192
Bram Moolenaarcde88542015-08-11 19:14:00 +020020193 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020194 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020195 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020196
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020197 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020198 }
20199 else
20200#endif
20201 {
20202 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020203 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020204 if (p != NULL)
20205 ga_append(&ga, tostr[p - fromstr]);
20206 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020207 ga_append(&ga, *in_str);
20208 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020209 }
20210 }
20211
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020212 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020213 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020214 ga_append(&ga, NUL);
20215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020216 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020217}
20218
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020219#ifdef FEAT_FLOAT
20220/*
20221 * "trunc({float})" function
20222 */
20223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020224f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020225{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020226 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020227
20228 rettv->v_type = VAR_FLOAT;
20229 if (get_float_arg(argvars, &f) == OK)
20230 /* trunc() is not in C90, use floor() or ceil() instead. */
20231 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20232 else
20233 rettv->vval.v_float = 0.0;
20234}
20235#endif
20236
Bram Moolenaar8299df92004-07-10 09:47:34 +000020237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 * "type(expr)" function
20239 */
20240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020241f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020243 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020244
20245 switch (argvars[0].v_type)
20246 {
20247 case VAR_NUMBER: n = 0; break;
20248 case VAR_STRING: n = 1; break;
20249 case VAR_FUNC: n = 2; break;
20250 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020251 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020252 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020253 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020254 if (argvars[0].vval.v_number == VVAL_FALSE
20255 || argvars[0].vval.v_number == VVAL_TRUE)
20256 n = 6;
20257 else
20258 n = 7;
20259 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020260 case VAR_JOB: n = 8; break;
20261 case VAR_CHANNEL: n = 9; break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010020262 case VAR_PARTIAL: n = 10; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020263 case VAR_UNKNOWN:
20264 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20265 n = -1;
20266 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020267 }
20268 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269}
20270
20271/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020272 * "undofile(name)" function
20273 */
20274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020275f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020276{
20277 rettv->v_type = VAR_STRING;
20278#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020279 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020280 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020281
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020282 if (*fname == NUL)
20283 {
20284 /* If there is no file name there will be no undo file. */
20285 rettv->vval.v_string = NULL;
20286 }
20287 else
20288 {
20289 char_u *ffname = FullName_save(fname, FALSE);
20290
20291 if (ffname != NULL)
20292 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20293 vim_free(ffname);
20294 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020295 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020296#else
20297 rettv->vval.v_string = NULL;
20298#endif
20299}
20300
20301/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020302 * "undotree()" function
20303 */
20304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020305f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020306{
20307 if (rettv_dict_alloc(rettv) == OK)
20308 {
20309 dict_T *dict = rettv->vval.v_dict;
20310 list_T *list;
20311
Bram Moolenaar730cde92010-06-27 05:18:54 +020020312 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020313 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020314 dict_add_nr_str(dict, "save_last",
20315 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020316 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20317 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020318 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020319
20320 list = list_alloc();
20321 if (list != NULL)
20322 {
20323 u_eval_tree(curbuf->b_u_oldhead, list);
20324 dict_add_list(dict, "entries", list);
20325 }
20326 }
20327}
20328
20329/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020330 * "values(dict)" function
20331 */
20332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020333f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020334{
20335 dict_list(argvars, rettv, 1);
20336}
20337
20338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 * "virtcol(string)" function
20340 */
20341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020342f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343{
20344 colnr_T vcol = 0;
20345 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020346 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020348 fp = var2fpos(&argvars[0], FALSE, &fnum);
20349 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20350 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 {
20352 getvvcol(curwin, fp, NULL, NULL, &vcol);
20353 ++vcol;
20354 }
20355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020356 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357}
20358
20359/*
20360 * "visualmode()" function
20361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020363f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 char_u str[2];
20366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 str[0] = curbuf->b_visual_mode_eval;
20369 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020370 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371
20372 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020373 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375}
20376
20377/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020378 * "wildmenumode()" function
20379 */
20380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020381f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020382{
20383#ifdef FEAT_WILDMENU
20384 if (wild_menu_showing)
20385 rettv->vval.v_number = 1;
20386#endif
20387}
20388
20389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 * "winbufnr(nr)" function
20391 */
20392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020393f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394{
20395 win_T *wp;
20396
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020397 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020399 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020401 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402}
20403
20404/*
20405 * "wincol()" function
20406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020408f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409{
20410 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020411 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412}
20413
20414/*
20415 * "winheight(nr)" function
20416 */
20417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020418f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419{
20420 win_T *wp;
20421
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020422 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020424 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020426 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427}
20428
20429/*
20430 * "winline()" function
20431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020433f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434{
20435 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020436 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437}
20438
20439/*
20440 * "winnr()" function
20441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020443f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444{
20445 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020446
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020448 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020450 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451}
20452
20453/*
20454 * "winrestcmd()" function
20455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020457f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458{
20459#ifdef FEAT_WINDOWS
20460 win_T *wp;
20461 int winnr = 1;
20462 garray_T ga;
20463 char_u buf[50];
20464
20465 ga_init2(&ga, (int)sizeof(char), 70);
20466 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20467 {
20468 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20469 ga_concat(&ga, buf);
20470# ifdef FEAT_VERTSPLIT
20471 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20472 ga_concat(&ga, buf);
20473# endif
20474 ++winnr;
20475 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020476 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020478 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020480 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020482 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483}
20484
20485/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020486 * "winrestview()" function
20487 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020489f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020490{
20491 dict_T *dict;
20492
20493 if (argvars[0].v_type != VAR_DICT
20494 || (dict = argvars[0].vval.v_dict) == NULL)
20495 EMSG(_(e_invarg));
20496 else
20497 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020498 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20499 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20500 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20501 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020502#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020503 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20504 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020505#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020506 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20507 {
20508 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20509 curwin->w_set_curswant = FALSE;
20510 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020511
Bram Moolenaar82c25852014-05-28 16:47:16 +020020512 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20513 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020514#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020515 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20516 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020517#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020518 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20519 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20520 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20521 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020522
20523 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020524 win_new_height(curwin, curwin->w_height);
20525# ifdef FEAT_VERTSPLIT
20526 win_new_width(curwin, W_WIDTH(curwin));
20527# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020528 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020529
Bram Moolenaarb851a962014-10-31 15:45:52 +010020530 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020531 curwin->w_topline = 1;
20532 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20533 curwin->w_topline = curbuf->b_ml.ml_line_count;
20534#ifdef FEAT_DIFF
20535 check_topfill(curwin, TRUE);
20536#endif
20537 }
20538}
20539
20540/*
20541 * "winsaveview()" function
20542 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020544f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020545{
20546 dict_T *dict;
20547
Bram Moolenaara800b422010-06-27 01:15:55 +020020548 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020549 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020550 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020551
20552 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20553 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20554#ifdef FEAT_VIRTUALEDIT
20555 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20556#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020557 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020558 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20559
20560 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20561#ifdef FEAT_DIFF
20562 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20563#endif
20564 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20565 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20566}
20567
20568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 * "winwidth(nr)" function
20570 */
20571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020572f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573{
20574 win_T *wp;
20575
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020576 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 else
20580#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020581 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020583 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584#endif
20585}
20586
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020588 * "wordcount()" function
20589 */
20590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020591f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020592{
20593 if (rettv_dict_alloc(rettv) == FAIL)
20594 return;
20595 cursor_pos_info(rettv->vval.v_dict);
20596}
20597
20598/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020599 * Write list of strings to file
20600 */
20601 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020602write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020603{
20604 listitem_T *li;
20605 int c;
20606 int ret = OK;
20607 char_u *s;
20608
20609 for (li = list->lv_first; li != NULL; li = li->li_next)
20610 {
20611 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20612 {
20613 if (*s == '\n')
20614 c = putc(NUL, fd);
20615 else
20616 c = putc(*s, fd);
20617 if (c == EOF)
20618 {
20619 ret = FAIL;
20620 break;
20621 }
20622 }
20623 if (!binary || li->li_next != NULL)
20624 if (putc('\n', fd) == EOF)
20625 {
20626 ret = FAIL;
20627 break;
20628 }
20629 if (ret == FAIL)
20630 {
20631 EMSG(_(e_write));
20632 break;
20633 }
20634 }
20635 return ret;
20636}
20637
20638/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020639 * "writefile()" function
20640 */
20641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020642f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020643{
20644 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020645 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020646 char_u *fname;
20647 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020648 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020649
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020650 if (check_restricted() || check_secure())
20651 return;
20652
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020653 if (argvars[0].v_type != VAR_LIST)
20654 {
20655 EMSG2(_(e_listarg), "writefile()");
20656 return;
20657 }
20658 if (argvars[0].vval.v_list == NULL)
20659 return;
20660
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020661 if (argvars[2].v_type != VAR_UNKNOWN)
20662 {
20663 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20664 binary = TRUE;
20665 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20666 append = TRUE;
20667 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020668
20669 /* Always open the file in binary mode, library functions have a mind of
20670 * their own about CR-LF conversion. */
20671 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020672 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20673 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020674 {
20675 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20676 ret = -1;
20677 }
20678 else
20679 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020680 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20681 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020682 fclose(fd);
20683 }
20684
20685 rettv->vval.v_number = ret;
20686}
20687
20688/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020689 * "xor(expr, expr)" function
20690 */
20691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020692f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020693{
20694 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20695 ^ get_tv_number_chk(&argvars[1], NULL);
20696}
20697
20698
20699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020701 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 */
20703 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020704var2fpos(
20705 typval_T *varp,
20706 int dollar_lnum, /* TRUE when $ is last line */
20707 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020709 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020711 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712
Bram Moolenaara5525202006-03-02 22:52:09 +000020713 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020714 if (varp->v_type == VAR_LIST)
20715 {
20716 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020717 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020718 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020719 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020720
20721 l = varp->vval.v_list;
20722 if (l == NULL)
20723 return NULL;
20724
20725 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020726 pos.lnum = list_find_nr(l, 0L, &error);
20727 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020728 return NULL; /* invalid line number */
20729
20730 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020731 pos.col = list_find_nr(l, 1L, &error);
20732 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020733 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020734 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020735
20736 /* We accept "$" for the column number: last column. */
20737 li = list_find(l, 1L);
20738 if (li != NULL && li->li_tv.v_type == VAR_STRING
20739 && li->li_tv.vval.v_string != NULL
20740 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20741 pos.col = len + 1;
20742
Bram Moolenaara5525202006-03-02 22:52:09 +000020743 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020744 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020745 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020746 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020747
Bram Moolenaara5525202006-03-02 22:52:09 +000020748#ifdef FEAT_VIRTUALEDIT
20749 /* Get the virtual offset. Defaults to zero. */
20750 pos.coladd = list_find_nr(l, 2L, &error);
20751 if (error)
20752 pos.coladd = 0;
20753#endif
20754
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020755 return &pos;
20756 }
20757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020758 name = get_tv_string_chk(varp);
20759 if (name == NULL)
20760 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020761 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020763 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20764 {
20765 if (VIsual_active)
20766 return &VIsual;
20767 return &curwin->w_cursor;
20768 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020769 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020771 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20773 return NULL;
20774 return pp;
20775 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020776
20777#ifdef FEAT_VIRTUALEDIT
20778 pos.coladd = 0;
20779#endif
20780
Bram Moolenaar477933c2007-07-17 14:32:23 +000020781 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020782 {
20783 pos.col = 0;
20784 if (name[1] == '0') /* "w0": first visible line */
20785 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020786 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020787 pos.lnum = curwin->w_topline;
20788 return &pos;
20789 }
20790 else if (name[1] == '$') /* "w$": last visible line */
20791 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020792 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020793 pos.lnum = curwin->w_botline - 1;
20794 return &pos;
20795 }
20796 }
20797 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020799 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 {
20801 pos.lnum = curbuf->b_ml.ml_line_count;
20802 pos.col = 0;
20803 }
20804 else
20805 {
20806 pos.lnum = curwin->w_cursor.lnum;
20807 pos.col = (colnr_T)STRLEN(ml_get_curline());
20808 }
20809 return &pos;
20810 }
20811 return NULL;
20812}
20813
20814/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020815 * Convert list in "arg" into a position and optional file number.
20816 * When "fnump" is NULL there is no file number, only 3 items.
20817 * Note that the column is passed on as-is, the caller may want to decrement
20818 * it to use 1 for the first column.
20819 * Return FAIL when conversion is not possible, doesn't check the position for
20820 * validity.
20821 */
20822 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020823list2fpos(
20824 typval_T *arg,
20825 pos_T *posp,
20826 int *fnump,
20827 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020828{
20829 list_T *l = arg->vval.v_list;
20830 long i = 0;
20831 long n;
20832
Bram Moolenaar493c1782014-05-28 14:34:46 +020020833 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20834 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020835 if (arg->v_type != VAR_LIST
20836 || l == NULL
20837 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020838 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020839 return FAIL;
20840
20841 if (fnump != NULL)
20842 {
20843 n = list_find_nr(l, i++, NULL); /* fnum */
20844 if (n < 0)
20845 return FAIL;
20846 if (n == 0)
20847 n = curbuf->b_fnum; /* current buffer */
20848 *fnump = n;
20849 }
20850
20851 n = list_find_nr(l, i++, NULL); /* lnum */
20852 if (n < 0)
20853 return FAIL;
20854 posp->lnum = n;
20855
20856 n = list_find_nr(l, i++, NULL); /* col */
20857 if (n < 0)
20858 return FAIL;
20859 posp->col = n;
20860
20861#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020862 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020863 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020864 posp->coladd = 0;
20865 else
20866 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020867#endif
20868
Bram Moolenaar493c1782014-05-28 14:34:46 +020020869 if (curswantp != NULL)
20870 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20871
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020872 return OK;
20873}
20874
20875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 * Get the length of an environment variable name.
20877 * Advance "arg" to the first character after the name.
20878 * Return 0 for error.
20879 */
20880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020881get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882{
20883 char_u *p;
20884 int len;
20885
20886 for (p = *arg; vim_isIDc(*p); ++p)
20887 ;
20888 if (p == *arg) /* no name found */
20889 return 0;
20890
20891 len = (int)(p - *arg);
20892 *arg = p;
20893 return len;
20894}
20895
20896/*
20897 * Get the length of the name of a function or internal variable.
20898 * "arg" is advanced to the first non-white character after the name.
20899 * Return 0 if something is wrong.
20900 */
20901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020902get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903{
20904 char_u *p;
20905 int len;
20906
20907 /* Find the end of the name. */
20908 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020909 {
20910 if (*p == ':')
20911 {
20912 /* "s:" is start of "s:var", but "n:" is not and can be used in
20913 * slice "[n:]". Also "xx:" is not a namespace. */
20914 len = (int)(p - *arg);
20915 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20916 || len > 1)
20917 break;
20918 }
20919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 if (p == *arg) /* no name found */
20921 return 0;
20922
20923 len = (int)(p - *arg);
20924 *arg = skipwhite(p);
20925
20926 return len;
20927}
20928
20929/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020930 * Get the length of the name of a variable or function.
20931 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020933 * Return -1 if curly braces expansion failed.
20934 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 * If the name contains 'magic' {}'s, expand them and return the
20936 * expanded name in an allocated string via 'alias' - caller must free.
20937 */
20938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020939get_name_len(
20940 char_u **arg,
20941 char_u **alias,
20942 int evaluate,
20943 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944{
20945 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 char_u *p;
20947 char_u *expr_start;
20948 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949
20950 *alias = NULL; /* default to no alias */
20951
20952 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20953 && (*arg)[2] == (int)KE_SNR)
20954 {
20955 /* hard coded <SNR>, already translated */
20956 *arg += 3;
20957 return get_id_len(arg) + 3;
20958 }
20959 len = eval_fname_script(*arg);
20960 if (len > 0)
20961 {
20962 /* literal "<SID>", "s:" or "<SNR>" */
20963 *arg += len;
20964 }
20965
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020967 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020969 p = find_name_end(*arg, &expr_start, &expr_end,
20970 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 if (expr_start != NULL)
20972 {
20973 char_u *temp_string;
20974
20975 if (!evaluate)
20976 {
20977 len += (int)(p - *arg);
20978 *arg = skipwhite(p);
20979 return len;
20980 }
20981
20982 /*
20983 * Include any <SID> etc in the expanded string:
20984 * Thus the -len here.
20985 */
20986 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20987 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020988 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 *alias = temp_string;
20990 *arg = skipwhite(p);
20991 return (int)STRLEN(temp_string);
20992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993
20994 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020995 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 EMSG2(_(e_invexpr2), *arg);
20997
20998 return len;
20999}
21000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021001/*
21002 * Find the end of a variable or function name, taking care of magic braces.
21003 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21004 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021005 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021006 * Return a pointer to just after the name. Equal to "arg" if there is no
21007 * valid name.
21008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021010find_name_end(
21011 char_u *arg,
21012 char_u **expr_start,
21013 char_u **expr_end,
21014 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021016 int mb_nest = 0;
21017 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021019 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021021 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021023 *expr_start = NULL;
21024 *expr_end = NULL;
21025 }
21026
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021027 /* Quick check for valid starting character. */
21028 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21029 return arg;
21030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021031 for (p = arg; *p != NUL
21032 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021033 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021034 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021035 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021036 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021037 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021038 if (*p == '\'')
21039 {
21040 /* skip over 'string' to avoid counting [ and ] inside it. */
21041 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21042 ;
21043 if (*p == NUL)
21044 break;
21045 }
21046 else if (*p == '"')
21047 {
21048 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21049 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21050 if (*p == '\\' && p[1] != NUL)
21051 ++p;
21052 if (*p == NUL)
21053 break;
21054 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021055 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21056 {
21057 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021058 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021059 len = (int)(p - arg);
21060 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021061 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021062 break;
21063 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021065 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021067 if (*p == '[')
21068 ++br_nest;
21069 else if (*p == ']')
21070 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021073 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021075 if (*p == '{')
21076 {
21077 mb_nest++;
21078 if (expr_start != NULL && *expr_start == NULL)
21079 *expr_start = p;
21080 }
21081 else if (*p == '}')
21082 {
21083 mb_nest--;
21084 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21085 *expr_end = p;
21086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 }
21089
21090 return p;
21091}
21092
21093/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021094 * Expands out the 'magic' {}'s in a variable/function name.
21095 * Note that this can call itself recursively, to deal with
21096 * constructs like foo{bar}{baz}{bam}
21097 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21098 * "in_start" ^
21099 * "expr_start" ^
21100 * "expr_end" ^
21101 * "in_end" ^
21102 *
21103 * Returns a new allocated string, which the caller must free.
21104 * Returns NULL for failure.
21105 */
21106 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021107make_expanded_name(
21108 char_u *in_start,
21109 char_u *expr_start,
21110 char_u *expr_end,
21111 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021112{
21113 char_u c1;
21114 char_u *retval = NULL;
21115 char_u *temp_result;
21116 char_u *nextcmd = NULL;
21117
21118 if (expr_end == NULL || in_end == NULL)
21119 return NULL;
21120 *expr_start = NUL;
21121 *expr_end = NUL;
21122 c1 = *in_end;
21123 *in_end = NUL;
21124
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021125 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021126 if (temp_result != NULL && nextcmd == NULL)
21127 {
21128 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21129 + (in_end - expr_end) + 1));
21130 if (retval != NULL)
21131 {
21132 STRCPY(retval, in_start);
21133 STRCAT(retval, temp_result);
21134 STRCAT(retval, expr_end + 1);
21135 }
21136 }
21137 vim_free(temp_result);
21138
21139 *in_end = c1; /* put char back for error messages */
21140 *expr_start = '{';
21141 *expr_end = '}';
21142
21143 if (retval != NULL)
21144 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021145 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021146 if (expr_start != NULL)
21147 {
21148 /* Further expansion! */
21149 temp_result = make_expanded_name(retval, expr_start,
21150 expr_end, temp_result);
21151 vim_free(retval);
21152 retval = temp_result;
21153 }
21154 }
21155
21156 return retval;
21157}
21158
21159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021161 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 */
21163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021164eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021166 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21167}
21168
21169/*
21170 * Return TRUE if character "c" can be used as the first character in a
21171 * variable or function name (excluding '{' and '}').
21172 */
21173 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021174eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021175{
21176 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177}
21178
21179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 * Set number v: variable to "val".
21181 */
21182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021183set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021185 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186}
21187
21188/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021189 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 */
21191 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021192get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021194 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195}
21196
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021197/*
21198 * Get string v: variable value. Uses a static buffer, can only be used once.
21199 */
21200 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021201get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021202{
21203 return get_tv_string(&vimvars[idx].vv_tv);
21204}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021205
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021207 * Get List v: variable value. Caller must take care of reference count when
21208 * needed.
21209 */
21210 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021211get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021212{
21213 return vimvars[idx].vv_list;
21214}
21215
21216/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021217 * Set v:char to character "c".
21218 */
21219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021220set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021221{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021222 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021223
21224#ifdef FEAT_MBYTE
21225 if (has_mbyte)
21226 buf[(*mb_char2bytes)(c, buf)] = NUL;
21227 else
21228#endif
21229 {
21230 buf[0] = c;
21231 buf[1] = NUL;
21232 }
21233 set_vim_var_string(VV_CHAR, buf, -1);
21234}
21235
21236/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021237 * Set v:count to "count" and v:count1 to "count1".
21238 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 */
21240 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021241set_vcount(
21242 long count,
21243 long count1,
21244 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021246 if (set_prevcount)
21247 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021248 vimvars[VV_COUNT].vv_nr = count;
21249 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250}
21251
21252/*
21253 * Set string v: variable to a copy of "val".
21254 */
21255 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021256set_vim_var_string(
21257 int idx,
21258 char_u *val,
21259 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260{
Bram Moolenaara542c682016-01-31 16:28:04 +010021261 clear_tv(&vimvars[idx].vv_di.di_tv);
21262 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021264 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021266 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021268 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269}
21270
21271/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021272 * Set List v: variable to "val".
21273 */
21274 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021275set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021276{
Bram Moolenaara542c682016-01-31 16:28:04 +010021277 clear_tv(&vimvars[idx].vv_di.di_tv);
21278 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021279 vimvars[idx].vv_list = val;
21280 if (val != NULL)
21281 ++val->lv_refcount;
21282}
21283
21284/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021285 * Set Dictionary v: variable to "val".
21286 */
21287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021288set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021289{
21290 int todo;
21291 hashitem_T *hi;
21292
Bram Moolenaara542c682016-01-31 16:28:04 +010021293 clear_tv(&vimvars[idx].vv_di.di_tv);
21294 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021295 vimvars[idx].vv_dict = val;
21296 if (val != NULL)
21297 {
21298 ++val->dv_refcount;
21299
21300 /* Set readonly */
21301 todo = (int)val->dv_hashtab.ht_used;
21302 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21303 {
21304 if (HASHITEM_EMPTY(hi))
21305 continue;
21306 --todo;
21307 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21308 }
21309 }
21310}
21311
21312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 * Set v:register if needed.
21314 */
21315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021316set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317{
21318 char_u regname;
21319
21320 if (c == 0 || c == ' ')
21321 regname = '"';
21322 else
21323 regname = c;
21324 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021325 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 set_vim_var_string(VV_REG, &regname, 1);
21327}
21328
21329/*
21330 * Get or set v:exception. If "oldval" == NULL, return the current value.
21331 * Otherwise, restore the value to "oldval" and return NULL.
21332 * Must always be called in pairs to save and restore v:exception! Does not
21333 * take care of memory allocations.
21334 */
21335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021336v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337{
21338 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021339 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340
Bram Moolenaare9a41262005-01-15 22:18:47 +000021341 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 return NULL;
21343}
21344
21345/*
21346 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21347 * Otherwise, restore the value to "oldval" and return NULL.
21348 * Must always be called in pairs to save and restore v:throwpoint! Does not
21349 * take care of memory allocations.
21350 */
21351 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021352v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353{
21354 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021355 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356
Bram Moolenaare9a41262005-01-15 22:18:47 +000021357 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 return NULL;
21359}
21360
21361#if defined(FEAT_AUTOCMD) || defined(PROTO)
21362/*
21363 * Set v:cmdarg.
21364 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21365 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21366 * Must always be called in pairs!
21367 */
21368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021369set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370{
21371 char_u *oldval;
21372 char_u *newval;
21373 unsigned len;
21374
Bram Moolenaare9a41262005-01-15 22:18:47 +000021375 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021376 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021378 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021379 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021380 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 }
21382
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021383 if (eap->force_bin == FORCE_BIN)
21384 len = 6;
21385 else if (eap->force_bin == FORCE_NOBIN)
21386 len = 8;
21387 else
21388 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021389
21390 if (eap->read_edit)
21391 len += 7;
21392
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021393 if (eap->force_ff != 0)
21394 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21395# ifdef FEAT_MBYTE
21396 if (eap->force_enc != 0)
21397 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021398 if (eap->bad_char != 0)
21399 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021400# endif
21401
21402 newval = alloc(len + 1);
21403 if (newval == NULL)
21404 return NULL;
21405
21406 if (eap->force_bin == FORCE_BIN)
21407 sprintf((char *)newval, " ++bin");
21408 else if (eap->force_bin == FORCE_NOBIN)
21409 sprintf((char *)newval, " ++nobin");
21410 else
21411 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021412
21413 if (eap->read_edit)
21414 STRCAT(newval, " ++edit");
21415
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021416 if (eap->force_ff != 0)
21417 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21418 eap->cmd + eap->force_ff);
21419# ifdef FEAT_MBYTE
21420 if (eap->force_enc != 0)
21421 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21422 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021423 if (eap->bad_char == BAD_KEEP)
21424 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21425 else if (eap->bad_char == BAD_DROP)
21426 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21427 else if (eap->bad_char != 0)
21428 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021429# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021430 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021431 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432}
21433#endif
21434
21435/*
21436 * Get the value of internal variable "name".
21437 * Return OK or FAIL.
21438 */
21439 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021440get_var_tv(
21441 char_u *name,
21442 int len, /* length of "name" */
21443 typval_T *rettv, /* NULL when only checking existence */
21444 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21445 int verbose, /* may give error message */
21446 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447{
21448 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021449 typval_T *tv = NULL;
21450 typval_T atv;
21451 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453
21454 /* truncate the name, so that we can use strcmp() */
21455 cc = name[len];
21456 name[len] = NUL;
21457
21458 /*
21459 * Check for "b:changedtick".
21460 */
21461 if (STRCMP(name, "b:changedtick") == 0)
21462 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021463 atv.v_type = VAR_NUMBER;
21464 atv.vval.v_number = curbuf->b_changedtick;
21465 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466 }
21467
21468 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 * Check for user-defined variables.
21470 */
21471 else
21472 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021473 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021475 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021476 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021477 if (dip != NULL)
21478 *dip = v;
21479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 }
21481
Bram Moolenaare9a41262005-01-15 22:18:47 +000021482 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021484 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021485 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 ret = FAIL;
21487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021488 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021489 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490
21491 name[len] = cc;
21492
21493 return ret;
21494}
21495
21496/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021497 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21498 * Also handle function call with Funcref variable: func(expr)
21499 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21500 */
21501 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021502handle_subscript(
21503 char_u **arg,
21504 typval_T *rettv,
21505 int evaluate, /* do more than finding the end */
21506 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021507{
21508 int ret = OK;
21509 dict_T *selfdict = NULL;
21510 char_u *s;
21511 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021512 typval_T functv;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021513 partial_T *pt = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021514
21515 while (ret == OK
21516 && (**arg == '['
21517 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021518 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21519 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021520 && !vim_iswhite(*(*arg - 1)))
21521 {
21522 if (**arg == '(')
21523 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021524 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021525 if (evaluate)
21526 {
21527 functv = *rettv;
21528 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021529
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021530 /* Invoke the function. Recursive! */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021531 if (rettv->v_type == VAR_PARTIAL)
21532 {
21533 pt = functv.vval.v_partial;
21534 s = pt->pt_name;
21535 }
21536 else
21537 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021538 }
21539 else
21540 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021541 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021542 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021543 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021544
21545 /* Clear the funcref afterwards, so that deleting it while
21546 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021547 if (evaluate)
21548 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021549
21550 /* Stop the expression evaluation when immediately aborting on
21551 * error, or when an interrupt occurred or an exception was thrown
21552 * but not caught. */
21553 if (aborting())
21554 {
21555 if (ret == OK)
21556 clear_tv(rettv);
21557 ret = FAIL;
21558 }
21559 dict_unref(selfdict);
21560 selfdict = NULL;
21561 }
21562 else /* **arg == '[' || **arg == '.' */
21563 {
21564 dict_unref(selfdict);
21565 if (rettv->v_type == VAR_DICT)
21566 {
21567 selfdict = rettv->vval.v_dict;
21568 if (selfdict != NULL)
21569 ++selfdict->dv_refcount;
21570 }
21571 else
21572 selfdict = NULL;
21573 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21574 {
21575 clear_tv(rettv);
21576 ret = FAIL;
21577 }
21578 }
21579 }
21580 dict_unref(selfdict);
21581 return ret;
21582}
21583
21584/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021585 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021586 * value).
21587 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021588 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021589alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021590{
Bram Moolenaar33570922005-01-25 22:26:29 +000021591 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021592}
21593
21594/*
21595 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 * The string "s" must have been allocated, it is consumed.
21597 * Return NULL for out of memory, the variable otherwise.
21598 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021599 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021600alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601{
Bram Moolenaar33570922005-01-25 22:26:29 +000021602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021604 rettv = alloc_tv();
21605 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021607 rettv->v_type = VAR_STRING;
21608 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 }
21610 else
21611 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021612 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613}
21614
21615/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021616 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021618 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021619free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620{
21621 if (varp != NULL)
21622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021623 switch (varp->v_type)
21624 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021625 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021626 func_unref(varp->vval.v_string);
21627 /*FALLTHROUGH*/
21628 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021629 vim_free(varp->vval.v_string);
21630 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021631 case VAR_PARTIAL:
21632 partial_unref(varp->vval.v_partial);
21633 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021634 case VAR_LIST:
21635 list_unref(varp->vval.v_list);
21636 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021637 case VAR_DICT:
21638 dict_unref(varp->vval.v_dict);
21639 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021640 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021641#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021642 job_unref(varp->vval.v_job);
21643 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021644#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021645 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021646#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021647 channel_unref(varp->vval.v_channel);
21648 break;
21649#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021650 case VAR_NUMBER:
21651 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021652 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021653 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021654 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 vim_free(varp);
21657 }
21658}
21659
21660/*
21661 * Free the memory for a variable value and set the value to NULL or 0.
21662 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021664clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665{
21666 if (varp != NULL)
21667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021668 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021670 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021671 func_unref(varp->vval.v_string);
21672 /*FALLTHROUGH*/
21673 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021674 vim_free(varp->vval.v_string);
21675 varp->vval.v_string = NULL;
21676 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021677 case VAR_PARTIAL:
21678 partial_unref(varp->vval.v_partial);
21679 varp->vval.v_partial = NULL;
21680 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021681 case VAR_LIST:
21682 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021683 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021684 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021685 case VAR_DICT:
21686 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021687 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021688 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021689 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021690 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021691 varp->vval.v_number = 0;
21692 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021693 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021694#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021695 varp->vval.v_float = 0.0;
21696 break;
21697#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021698 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021699#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021700 job_unref(varp->vval.v_job);
21701 varp->vval.v_job = NULL;
21702#endif
21703 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021704 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021705#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021706 channel_unref(varp->vval.v_channel);
21707 varp->vval.v_channel = NULL;
21708#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021709 case VAR_UNKNOWN:
21710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021712 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 }
21714}
21715
21716/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021717 * Set the value of a variable to NULL without freeing items.
21718 */
21719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021720init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021721{
21722 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021723 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021724}
21725
21726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 * Get the number value of a variable.
21728 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021729 * For incompatible types, return 0.
21730 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21731 * caller of incompatible types: it sets *denote to TRUE if "denote"
21732 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021734 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021735get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021737 int error = FALSE;
21738
21739 return get_tv_number_chk(varp, &error); /* return 0L on error */
21740}
21741
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021742 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021743get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021744{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021745 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021747 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021749 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021750 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021751 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021752#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021753 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021754 break;
21755#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021756 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021757 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021758 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021759 break;
21760 case VAR_STRING:
21761 if (varp->vval.v_string != NULL)
21762 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021763 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021764 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021765 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021766 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021767 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021768 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021769 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021770 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021771 case VAR_SPECIAL:
21772 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21773 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021774 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021775#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021776 EMSG(_("E910: Using a Job as a Number"));
21777 break;
21778#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021779 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021780#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021781 EMSG(_("E913: Using a Channel as a Number"));
21782 break;
21783#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021784 case VAR_UNKNOWN:
21785 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021786 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021788 if (denote == NULL) /* useful for values that must be unsigned */
21789 n = -1;
21790 else
21791 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021792 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793}
21794
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021795#ifdef FEAT_FLOAT
21796 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021797get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021798{
21799 switch (varp->v_type)
21800 {
21801 case VAR_NUMBER:
21802 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021803 case VAR_FLOAT:
21804 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021805 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021806 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021807 EMSG(_("E891: Using a Funcref as a Float"));
21808 break;
21809 case VAR_STRING:
21810 EMSG(_("E892: Using a String as a Float"));
21811 break;
21812 case VAR_LIST:
21813 EMSG(_("E893: Using a List as a Float"));
21814 break;
21815 case VAR_DICT:
21816 EMSG(_("E894: Using a Dictionary as a Float"));
21817 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021818 case VAR_SPECIAL:
21819 EMSG(_("E907: Using a special value as a Float"));
21820 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021821 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021822# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021823 EMSG(_("E911: Using a Job as a Float"));
21824 break;
21825# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021826 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021827# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021828 EMSG(_("E914: Using a Channel as a Float"));
21829 break;
21830# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021831 case VAR_UNKNOWN:
21832 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021833 break;
21834 }
21835 return 0;
21836}
21837#endif
21838
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021840 * Get the lnum from the first argument.
21841 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021842 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 */
21844 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021845get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846{
Bram Moolenaar33570922005-01-25 22:26:29 +000021847 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 linenr_T lnum;
21849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021850 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 if (lnum == 0) /* no valid number, try using line() */
21852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021853 rettv.v_type = VAR_NUMBER;
21854 f_line(argvars, &rettv);
21855 lnum = rettv.vval.v_number;
21856 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 }
21858 return lnum;
21859}
21860
21861/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021862 * Get the lnum from the first argument.
21863 * Also accepts "$", then "buf" is used.
21864 * Returns 0 on error.
21865 */
21866 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021867get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021868{
21869 if (argvars[0].v_type == VAR_STRING
21870 && argvars[0].vval.v_string != NULL
21871 && argvars[0].vval.v_string[0] == '$'
21872 && buf != NULL)
21873 return buf->b_ml.ml_line_count;
21874 return get_tv_number_chk(&argvars[0], NULL);
21875}
21876
21877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 * Get the string value of a variable.
21879 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021880 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21881 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 * If the String variable has never been set, return an empty string.
21883 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021884 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21885 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021887 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021888get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889{
21890 static char_u mybuf[NUMBUFLEN];
21891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021892 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893}
21894
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021895 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021896get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021898 char_u *res = get_tv_string_buf_chk(varp, buf);
21899
21900 return res != NULL ? res : (char_u *)"";
21901}
21902
Bram Moolenaar7d647822014-04-05 21:28:56 +020021903/*
21904 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21905 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021906 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021907get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021908{
21909 static char_u mybuf[NUMBUFLEN];
21910
21911 return get_tv_string_buf_chk(varp, mybuf);
21912}
21913
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021914 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021915get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021916{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021917 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021919 case VAR_NUMBER:
21920 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21921 return buf;
21922 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021923 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021924 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021925 break;
21926 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021927 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021928 break;
21929 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021930 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021931 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021932 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021933#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021934 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021935 break;
21936#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021937 case VAR_STRING:
21938 if (varp->vval.v_string != NULL)
21939 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021940 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021941 case VAR_SPECIAL:
21942 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21943 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021944 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021945#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021946 {
21947 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010021948 char *status;
21949
21950 if (job == NULL)
21951 return (char_u *)"no process";
21952 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010021953 : job->jv_status == JOB_ENDED ? "dead"
21954 : "run";
21955# ifdef UNIX
21956 vim_snprintf((char *)buf, NUMBUFLEN,
21957 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021958# elif defined(WIN32)
21959 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021960 "process %ld %s",
21961 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021962 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021963# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021964 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021965 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21966# endif
21967 return buf;
21968 }
21969#endif
21970 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021971 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021972#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021973 {
21974 channel_T *channel = varp->vval.v_channel;
21975 char *status = channel_status(channel);
21976
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021977 if (channel == NULL)
21978 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21979 else
21980 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021981 "channel %d %s", channel->ch_id, status);
21982 return buf;
21983 }
21984#endif
21985 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021986 case VAR_UNKNOWN:
21987 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021990 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991}
21992
21993/*
21994 * Find variable "name" in the list of variables.
21995 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021996 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021997 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021998 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022000 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022001find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022004 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005
Bram Moolenaara7043832005-01-21 11:56:39 +000022006 ht = find_var_ht(name, &varname);
22007 if (htp != NULL)
22008 *htp = ht;
22009 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022011 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012}
22013
22014/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022015 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022016 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022018 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022019find_var_in_ht(
22020 hashtab_T *ht,
22021 int htname,
22022 char_u *varname,
22023 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022024{
Bram Moolenaar33570922005-01-25 22:26:29 +000022025 hashitem_T *hi;
22026
22027 if (*varname == NUL)
22028 {
22029 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022030 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022031 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022032 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022033 case 'g': return &globvars_var;
22034 case 'v': return &vimvars_var;
22035 case 'b': return &curbuf->b_bufvar;
22036 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022037#ifdef FEAT_WINDOWS
22038 case 't': return &curtab->tp_winvar;
22039#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022040 case 'l': return current_funccal == NULL
22041 ? NULL : &current_funccal->l_vars_var;
22042 case 'a': return current_funccal == NULL
22043 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022044 }
22045 return NULL;
22046 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022047
22048 hi = hash_find(ht, varname);
22049 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022050 {
22051 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022052 * worked find the variable again. Don't auto-load a script if it was
22053 * loaded already, otherwise it would be loaded every time when
22054 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022055 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022056 {
22057 /* Note: script_autoload() may make "hi" invalid. It must either
22058 * be obtained again or not used. */
22059 if (!script_autoload(varname, FALSE) || aborting())
22060 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022061 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022062 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022063 if (HASHITEM_EMPTY(hi))
22064 return NULL;
22065 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022066 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022067}
22068
22069/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022070 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022071 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022072 * Set "varname" to the start of name without ':'.
22073 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022074 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022075find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022077 hashitem_T *hi;
22078
Bram Moolenaar73627d02015-08-11 15:46:09 +020022079 if (name[0] == NUL)
22080 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 if (name[1] != ':')
22082 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022083 /* The name must not start with a colon or #. */
22084 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 return NULL;
22086 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022087
22088 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022089 hi = hash_find(&compat_hashtab, name);
22090 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022091 return &compat_hashtab;
22092
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022094 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022095 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 }
22097 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022098 if (*name == 'g') /* global variable */
22099 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022100 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22101 */
22102 if (vim_strchr(name + 2, ':') != NULL
22103 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022104 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022106 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022108 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022109#ifdef FEAT_WINDOWS
22110 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022111 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022112#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022113 if (*name == 'v') /* v: variable */
22114 return &vimvarht;
22115 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022116 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022117 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022118 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 if (*name == 's' /* script variable */
22120 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22121 return &SCRIPT_VARS(current_SID);
22122 return NULL;
22123}
22124
22125/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022126 * Get function call environment based on bactrace debug level
22127 */
22128 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022129get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022130{
22131 int i;
22132 funccall_T *funccal;
22133 funccall_T *temp_funccal;
22134
22135 funccal = current_funccal;
22136 if (debug_backtrace_level > 0)
22137 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022138 for (i = 0; i < debug_backtrace_level; i++)
22139 {
22140 temp_funccal = funccal->caller;
22141 if (temp_funccal)
22142 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022143 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022144 /* backtrace level overflow. reset to max */
22145 debug_backtrace_level = i;
22146 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022147 }
22148 return funccal;
22149}
22150
22151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022153 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 * Returns NULL when it doesn't exist.
22155 */
22156 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022157get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158{
Bram Moolenaar33570922005-01-25 22:26:29 +000022159 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022161 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162 if (v == NULL)
22163 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022164 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165}
22166
22167/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022168 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 * sourcing this script and when executing functions defined in the script.
22170 */
22171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022172new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173{
Bram Moolenaara7043832005-01-21 11:56:39 +000022174 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022175 hashtab_T *ht;
22176 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022177
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22179 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022180 /* Re-allocating ga_data means that an ht_array pointing to
22181 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022182 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022183 for (i = 1; i <= ga_scripts.ga_len; ++i)
22184 {
22185 ht = &SCRIPT_VARS(i);
22186 if (ht->ht_mask == HT_INIT_SIZE - 1)
22187 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022188 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022189 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022190 }
22191
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192 while (ga_scripts.ga_len < id)
22193 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022194 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022195 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022196 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 }
22199 }
22200}
22201
22202/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022203 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22204 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 */
22206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022207init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208{
Bram Moolenaar33570922005-01-25 22:26:29 +000022209 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022210 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022211 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022212 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022213 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022214 dict_var->di_tv.vval.v_dict = dict;
22215 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022216 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022217 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22218 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219}
22220
22221/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022222 * Unreference a dictionary initialized by init_var_dict().
22223 */
22224 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022225unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022226{
22227 /* Now the dict needs to be freed if no one else is using it, go back to
22228 * normal reference counting. */
22229 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22230 dict_unref(dict);
22231}
22232
22233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022235 * Frees all allocated variables and the value they contain.
22236 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 */
22238 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022239vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022240{
22241 vars_clear_ext(ht, TRUE);
22242}
22243
22244/*
22245 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22246 */
22247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022248vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249{
Bram Moolenaara7043832005-01-21 11:56:39 +000022250 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022251 hashitem_T *hi;
22252 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253
Bram Moolenaar33570922005-01-25 22:26:29 +000022254 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022255 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022256 for (hi = ht->ht_array; todo > 0; ++hi)
22257 {
22258 if (!HASHITEM_EMPTY(hi))
22259 {
22260 --todo;
22261
Bram Moolenaar33570922005-01-25 22:26:29 +000022262 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022263 * ht_array might change then. hash_clear() takes care of it
22264 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022265 v = HI2DI(hi);
22266 if (free_val)
22267 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022268 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022269 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022270 }
22271 }
22272 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022273 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274}
22275
Bram Moolenaara7043832005-01-21 11:56:39 +000022276/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022277 * Delete a variable from hashtab "ht" at item "hi".
22278 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022281delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282{
Bram Moolenaar33570922005-01-25 22:26:29 +000022283 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022284
22285 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022286 clear_tv(&di->di_tv);
22287 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288}
22289
22290/*
22291 * List the value of one internal variable.
22292 */
22293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022294list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022296 char_u *tofree;
22297 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022298 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022299
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022300 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022301 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022302 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022303 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304}
22305
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022307list_one_var_a(
22308 char_u *prefix,
22309 char_u *name,
22310 int type,
22311 char_u *string,
22312 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313{
Bram Moolenaar31859182007-08-14 20:41:13 +000022314 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22315 msg_start();
22316 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 if (name != NULL) /* "a:" vars don't have a name stored */
22318 msg_puts(name);
22319 msg_putchar(' ');
22320 msg_advance(22);
22321 if (type == VAR_NUMBER)
22322 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022323 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022324 msg_putchar('*');
22325 else if (type == VAR_LIST)
22326 {
22327 msg_putchar('[');
22328 if (*string == '[')
22329 ++string;
22330 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022331 else if (type == VAR_DICT)
22332 {
22333 msg_putchar('{');
22334 if (*string == '{')
22335 ++string;
22336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 else
22338 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022339
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022341
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022342 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022343 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022344 if (*first)
22345 {
22346 msg_clr_eos();
22347 *first = FALSE;
22348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349}
22350
22351/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022352 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 * If the variable already exists, the value is updated.
22354 * Otherwise the variable is created.
22355 */
22356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022357set_var(
22358 char_u *name,
22359 typval_T *tv,
22360 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361{
Bram Moolenaar33570922005-01-25 22:26:29 +000022362 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022364 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022366 ht = find_var_ht(name, &varname);
22367 if (ht == NULL || *varname == NUL)
22368 {
22369 EMSG2(_(e_illvar), name);
22370 return;
22371 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022372 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022373
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022374 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22375 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022376 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022377
Bram Moolenaar33570922005-01-25 22:26:29 +000022378 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022380 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022381 if (var_check_ro(v->di_flags, name, FALSE)
22382 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022383 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022384
22385 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022386 * Handle setting internal v: variables separately where needed to
22387 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022388 */
22389 if (ht == &vimvarht)
22390 {
22391 if (v->di_tv.v_type == VAR_STRING)
22392 {
22393 vim_free(v->di_tv.vval.v_string);
22394 if (copy || tv->v_type != VAR_STRING)
22395 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22396 else
22397 {
22398 /* Take over the string to avoid an extra alloc/free. */
22399 v->di_tv.vval.v_string = tv->vval.v_string;
22400 tv->vval.v_string = NULL;
22401 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022402 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022403 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022404 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022405 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022406 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022407 if (STRCMP(varname, "searchforward") == 0)
22408 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022409#ifdef FEAT_SEARCH_EXTRA
22410 else if (STRCMP(varname, "hlsearch") == 0)
22411 {
22412 no_hlsearch = !v->di_tv.vval.v_number;
22413 redraw_all_later(SOME_VALID);
22414 }
22415#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022416 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022417 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022418 else if (v->di_tv.v_type != tv->v_type)
22419 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022420 }
22421
22422 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 }
22424 else /* add a new variable */
22425 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022426 /* Can't add "v:" variable. */
22427 if (ht == &vimvarht)
22428 {
22429 EMSG2(_(e_illvar), name);
22430 return;
22431 }
22432
Bram Moolenaar92124a32005-06-17 22:03:40 +000022433 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022434 if (!valid_varname(varname))
22435 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022436
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022437 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22438 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022439 if (v == NULL)
22440 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022441 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022442 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022444 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 return;
22446 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022447 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022449
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022450 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022451 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022452 else
22453 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022454 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022455 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022456 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458}
22459
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022460/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022461 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022462 * Also give an error message.
22463 */
22464 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022465var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022466{
22467 if (flags & DI_FLAGS_RO)
22468 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022469 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022470 return TRUE;
22471 }
22472 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22473 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022474 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022475 return TRUE;
22476 }
22477 return FALSE;
22478}
22479
22480/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022481 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22482 * Also give an error message.
22483 */
22484 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022485var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022486{
22487 if (flags & DI_FLAGS_FIX)
22488 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022489 EMSG2(_("E795: Cannot delete variable %s"),
22490 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022491 return TRUE;
22492 }
22493 return FALSE;
22494}
22495
22496/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022497 * Check if a funcref is assigned to a valid variable name.
22498 * Return TRUE and give an error if not.
22499 */
22500 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022501var_check_func_name(
22502 char_u *name, /* points to start of variable name */
22503 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022504{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022505 /* Allow for w: b: s: and t:. */
22506 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022507 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22508 ? name[2] : name[0]))
22509 {
22510 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22511 name);
22512 return TRUE;
22513 }
22514 /* Don't allow hiding a function. When "v" is not NULL we might be
22515 * assigning another function to the same var, the type is checked
22516 * below. */
22517 if (new_var && function_exists(name))
22518 {
22519 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22520 name);
22521 return TRUE;
22522 }
22523 return FALSE;
22524}
22525
22526/*
22527 * Check if a variable name is valid.
22528 * Return FALSE and give an error if not.
22529 */
22530 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022531valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022532{
22533 char_u *p;
22534
22535 for (p = varname; *p != NUL; ++p)
22536 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22537 && *p != AUTOLOAD_CHAR)
22538 {
22539 EMSG2(_(e_illvar), varname);
22540 return FALSE;
22541 }
22542 return TRUE;
22543}
22544
22545/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022546 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022547 * Also give an error message, using "name" or _("name") when use_gettext is
22548 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022549 */
22550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022551tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022552{
22553 if (lock & VAR_LOCKED)
22554 {
22555 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022556 name == NULL ? (char_u *)_("Unknown")
22557 : use_gettext ? (char_u *)_(name)
22558 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022559 return TRUE;
22560 }
22561 if (lock & VAR_FIXED)
22562 {
22563 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022564 name == NULL ? (char_u *)_("Unknown")
22565 : use_gettext ? (char_u *)_(name)
22566 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022567 return TRUE;
22568 }
22569 return FALSE;
22570}
22571
22572/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022573 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022574 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022575 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022576 * It is OK for "from" and "to" to point to the same item. This is used to
22577 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022578 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022580copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022582 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022583 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022584 switch (from->v_type)
22585 {
22586 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022587 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022588 to->vval.v_number = from->vval.v_number;
22589 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022590 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022591#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022592 to->vval.v_float = from->vval.v_float;
22593 break;
22594#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022595 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022596#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022597 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022598 if (to->vval.v_job != NULL)
22599 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022600 break;
22601#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022602 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022603#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022604 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022605 if (to->vval.v_channel != NULL)
22606 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022607 break;
22608#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022609 case VAR_STRING:
22610 case VAR_FUNC:
22611 if (from->vval.v_string == NULL)
22612 to->vval.v_string = NULL;
22613 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022614 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022615 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022616 if (from->v_type == VAR_FUNC)
22617 func_ref(to->vval.v_string);
22618 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022619 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022620 case VAR_PARTIAL:
22621 if (from->vval.v_partial == NULL)
22622 to->vval.v_partial = NULL;
22623 else
22624 {
22625 to->vval.v_partial = from->vval.v_partial;
22626 ++to->vval.v_partial->pt_refcount;
22627 }
22628 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022629 case VAR_LIST:
22630 if (from->vval.v_list == NULL)
22631 to->vval.v_list = NULL;
22632 else
22633 {
22634 to->vval.v_list = from->vval.v_list;
22635 ++to->vval.v_list->lv_refcount;
22636 }
22637 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022638 case VAR_DICT:
22639 if (from->vval.v_dict == NULL)
22640 to->vval.v_dict = NULL;
22641 else
22642 {
22643 to->vval.v_dict = from->vval.v_dict;
22644 ++to->vval.v_dict->dv_refcount;
22645 }
22646 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022647 case VAR_UNKNOWN:
22648 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022649 break;
22650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651}
22652
22653/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022654 * Make a copy of an item.
22655 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022656 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22657 * reference to an already copied list/dict can be used.
22658 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022659 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022661item_copy(
22662 typval_T *from,
22663 typval_T *to,
22664 int deep,
22665 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022666{
22667 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022668 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022669
Bram Moolenaar33570922005-01-25 22:26:29 +000022670 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022671 {
22672 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022673 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022674 }
22675 ++recurse;
22676
22677 switch (from->v_type)
22678 {
22679 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022680 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022681 case VAR_STRING:
22682 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022683 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022684 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022685 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022686 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022687 copy_tv(from, to);
22688 break;
22689 case VAR_LIST:
22690 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022691 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022692 if (from->vval.v_list == NULL)
22693 to->vval.v_list = NULL;
22694 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22695 {
22696 /* use the copy made earlier */
22697 to->vval.v_list = from->vval.v_list->lv_copylist;
22698 ++to->vval.v_list->lv_refcount;
22699 }
22700 else
22701 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22702 if (to->vval.v_list == NULL)
22703 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022704 break;
22705 case VAR_DICT:
22706 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022707 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022708 if (from->vval.v_dict == NULL)
22709 to->vval.v_dict = NULL;
22710 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22711 {
22712 /* use the copy made earlier */
22713 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22714 ++to->vval.v_dict->dv_refcount;
22715 }
22716 else
22717 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22718 if (to->vval.v_dict == NULL)
22719 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022720 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022721 case VAR_UNKNOWN:
22722 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022723 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022724 }
22725 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022726 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022727}
22728
22729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 * ":echo expr1 ..." print each argument separated with a space, add a
22731 * newline at the end.
22732 * ":echon expr1 ..." print each argument plain.
22733 */
22734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022735ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736{
22737 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022739 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 char_u *p;
22741 int needclr = TRUE;
22742 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022743 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744
22745 if (eap->skip)
22746 ++emsg_skip;
22747 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22748 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022749 /* If eval1() causes an error message the text from the command may
22750 * still need to be cleared. E.g., "echo 22,44". */
22751 need_clr_eos = needclr;
22752
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022754 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 {
22756 /*
22757 * Report the invalid expression unless the expression evaluation
22758 * has been cancelled due to an aborting error, an interrupt, or an
22759 * exception.
22760 */
22761 if (!aborting())
22762 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022763 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764 break;
22765 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022766 need_clr_eos = FALSE;
22767
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 if (!eap->skip)
22769 {
22770 if (atstart)
22771 {
22772 atstart = FALSE;
22773 /* Call msg_start() after eval1(), evaluating the expression
22774 * may cause a message to appear. */
22775 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022776 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022777 /* Mark the saved text as finishing the line, so that what
22778 * follows is displayed on a new line when scrolling back
22779 * at the more prompt. */
22780 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783 }
22784 else if (eap->cmdidx == CMD_echo)
22785 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022786 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022787 if (p != NULL)
22788 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022790 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022792 if (*p != TAB && needclr)
22793 {
22794 /* remove any text still there from the command */
22795 msg_clr_eos();
22796 needclr = FALSE;
22797 }
22798 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799 }
22800 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022801 {
22802#ifdef FEAT_MBYTE
22803 if (has_mbyte)
22804 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022805 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022806
22807 (void)msg_outtrans_len_attr(p, i, echo_attr);
22808 p += i - 1;
22809 }
22810 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022812 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022815 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022817 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 arg = skipwhite(arg);
22819 }
22820 eap->nextcmd = check_nextcmd(arg);
22821
22822 if (eap->skip)
22823 --emsg_skip;
22824 else
22825 {
22826 /* remove text that may still be there from the command */
22827 if (needclr)
22828 msg_clr_eos();
22829 if (eap->cmdidx == CMD_echo)
22830 msg_end();
22831 }
22832}
22833
22834/*
22835 * ":echohl {name}".
22836 */
22837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022838ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839{
22840 int id;
22841
22842 id = syn_name2id(eap->arg);
22843 if (id == 0)
22844 echo_attr = 0;
22845 else
22846 echo_attr = syn_id2attr(id);
22847}
22848
22849/*
22850 * ":execute expr1 ..." execute the result of an expression.
22851 * ":echomsg expr1 ..." Print a message
22852 * ":echoerr expr1 ..." Print an error
22853 * Each gets spaces around each argument and a newline at the end for
22854 * echo commands
22855 */
22856 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022857ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858{
22859 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022860 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861 int ret = OK;
22862 char_u *p;
22863 garray_T ga;
22864 int len;
22865 int save_did_emsg;
22866
22867 ga_init2(&ga, 1, 80);
22868
22869 if (eap->skip)
22870 ++emsg_skip;
22871 while (*arg != NUL && *arg != '|' && *arg != '\n')
22872 {
22873 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022874 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 {
22876 /*
22877 * Report the invalid expression unless the expression evaluation
22878 * has been cancelled due to an aborting error, an interrupt, or an
22879 * exception.
22880 */
22881 if (!aborting())
22882 EMSG2(_(e_invexpr2), p);
22883 ret = FAIL;
22884 break;
22885 }
22886
22887 if (!eap->skip)
22888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022889 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 len = (int)STRLEN(p);
22891 if (ga_grow(&ga, len + 2) == FAIL)
22892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894 ret = FAIL;
22895 break;
22896 }
22897 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 ga.ga_len += len;
22901 }
22902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022903 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 arg = skipwhite(arg);
22905 }
22906
22907 if (ret != FAIL && ga.ga_data != NULL)
22908 {
22909 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022912 out_flush();
22913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 else if (eap->cmdidx == CMD_echoerr)
22915 {
22916 /* We don't want to abort following commands, restore did_emsg. */
22917 save_did_emsg = did_emsg;
22918 EMSG((char_u *)ga.ga_data);
22919 if (!force_abort)
22920 did_emsg = save_did_emsg;
22921 }
22922 else if (eap->cmdidx == CMD_execute)
22923 do_cmdline((char_u *)ga.ga_data,
22924 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22925 }
22926
22927 ga_clear(&ga);
22928
22929 if (eap->skip)
22930 --emsg_skip;
22931
22932 eap->nextcmd = check_nextcmd(arg);
22933}
22934
22935/*
22936 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22937 * "arg" points to the "&" or '+' when called, to "option" when returning.
22938 * Returns NULL when no option name found. Otherwise pointer to the char
22939 * after the option name.
22940 */
22941 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022942find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943{
22944 char_u *p = *arg;
22945
22946 ++p;
22947 if (*p == 'g' && p[1] == ':')
22948 {
22949 *opt_flags = OPT_GLOBAL;
22950 p += 2;
22951 }
22952 else if (*p == 'l' && p[1] == ':')
22953 {
22954 *opt_flags = OPT_LOCAL;
22955 p += 2;
22956 }
22957 else
22958 *opt_flags = 0;
22959
22960 if (!ASCII_ISALPHA(*p))
22961 return NULL;
22962 *arg = p;
22963
22964 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22965 p += 4; /* termcap option */
22966 else
22967 while (ASCII_ISALPHA(*p))
22968 ++p;
22969 return p;
22970}
22971
22972/*
22973 * ":function"
22974 */
22975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022976ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977{
22978 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022979 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 int j;
22981 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022983 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 char_u *name = NULL;
22985 char_u *p;
22986 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022987 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 garray_T newargs;
22989 garray_T newlines;
22990 int varargs = FALSE;
22991 int mustend = FALSE;
22992 int flags = 0;
22993 ufunc_T *fp;
22994 int indent;
22995 int nesting;
22996 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022997 dictitem_T *v;
22998 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022999 static int func_nr = 0; /* number for nameless function */
23000 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023001 hashtab_T *ht;
23002 int todo;
23003 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023004 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005
23006 /*
23007 * ":function" without argument: list functions.
23008 */
23009 if (ends_excmd(*eap->arg))
23010 {
23011 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023012 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023013 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023014 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023015 {
23016 if (!HASHITEM_EMPTY(hi))
23017 {
23018 --todo;
23019 fp = HI2UF(hi);
23020 if (!isdigit(*fp->uf_name))
23021 list_func_head(fp, FALSE);
23022 }
23023 }
23024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 eap->nextcmd = check_nextcmd(eap->arg);
23026 return;
23027 }
23028
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023029 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023030 * ":function /pat": list functions matching pattern.
23031 */
23032 if (*eap->arg == '/')
23033 {
23034 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23035 if (!eap->skip)
23036 {
23037 regmatch_T regmatch;
23038
23039 c = *p;
23040 *p = NUL;
23041 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23042 *p = c;
23043 if (regmatch.regprog != NULL)
23044 {
23045 regmatch.rm_ic = p_ic;
23046
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023047 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023048 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23049 {
23050 if (!HASHITEM_EMPTY(hi))
23051 {
23052 --todo;
23053 fp = HI2UF(hi);
23054 if (!isdigit(*fp->uf_name)
23055 && vim_regexec(&regmatch, fp->uf_name, 0))
23056 list_func_head(fp, FALSE);
23057 }
23058 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023059 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023060 }
23061 }
23062 if (*p == '/')
23063 ++p;
23064 eap->nextcmd = check_nextcmd(p);
23065 return;
23066 }
23067
23068 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023069 * Get the function name. There are these situations:
23070 * func normal function name
23071 * "name" == func, "fudi.fd_dict" == NULL
23072 * dict.func new dictionary entry
23073 * "name" == NULL, "fudi.fd_dict" set,
23074 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23075 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023076 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023077 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23078 * dict.func existing dict entry that's not a Funcref
23079 * "name" == NULL, "fudi.fd_dict" set,
23080 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023081 * s:func script-local function name
23082 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023085 name = trans_function_name(&p, eap->skip, 0, &fudi);
23086 paren = (vim_strchr(p, '(') != NULL);
23087 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088 {
23089 /*
23090 * Return on an invalid expression in braces, unless the expression
23091 * evaluation has been cancelled due to an aborting error, an
23092 * interrupt, or an exception.
23093 */
23094 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023095 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023096 if (!eap->skip && fudi.fd_newkey != NULL)
23097 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023098 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 else
23102 eap->skip = TRUE;
23103 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023104
Bram Moolenaar071d4272004-06-13 20:20:40 +000023105 /* An error in a function call during evaluation of an expression in magic
23106 * braces should not cause the function not to be defined. */
23107 saved_did_emsg = did_emsg;
23108 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109
23110 /*
23111 * ":function func" with only function name: list function.
23112 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023113 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 {
23115 if (!ends_excmd(*skipwhite(p)))
23116 {
23117 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023118 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119 }
23120 eap->nextcmd = check_nextcmd(p);
23121 if (eap->nextcmd != NULL)
23122 *p = NUL;
23123 if (!eap->skip && !got_int)
23124 {
23125 fp = find_func(name);
23126 if (fp != NULL)
23127 {
23128 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023129 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023131 if (FUNCLINE(fp, j) == NULL)
23132 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133 msg_putchar('\n');
23134 msg_outnum((long)(j + 1));
23135 if (j < 9)
23136 msg_putchar(' ');
23137 if (j < 99)
23138 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023139 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 out_flush(); /* show a line at a time */
23141 ui_breakcheck();
23142 }
23143 if (!got_int)
23144 {
23145 msg_putchar('\n');
23146 msg_puts((char_u *)" endfunction");
23147 }
23148 }
23149 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023150 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023152 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 }
23154
23155 /*
23156 * ":function name(arg1, arg2)" Define function.
23157 */
23158 p = skipwhite(p);
23159 if (*p != '(')
23160 {
23161 if (!eap->skip)
23162 {
23163 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023164 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 }
23166 /* attempt to continue by skipping some text */
23167 if (vim_strchr(p, '(') != NULL)
23168 p = vim_strchr(p, '(');
23169 }
23170 p = skipwhite(p + 1);
23171
23172 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23173 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23174
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023175 if (!eap->skip)
23176 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023177 /* Check the name of the function. Unless it's a dictionary function
23178 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023179 if (name != NULL)
23180 arg = name;
23181 else
23182 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023183 if (arg != NULL && (fudi.fd_di == NULL
23184 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023185 {
23186 if (*arg == K_SPECIAL)
23187 j = 3;
23188 else
23189 j = 0;
23190 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23191 : eval_isnamec(arg[j])))
23192 ++j;
23193 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023194 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023195 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023196 /* Disallow using the g: dict. */
23197 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23198 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023199 }
23200
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 /*
23202 * Isolate the arguments: "arg1, arg2, ...)"
23203 */
23204 while (*p != ')')
23205 {
23206 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23207 {
23208 varargs = TRUE;
23209 p += 3;
23210 mustend = TRUE;
23211 }
23212 else
23213 {
23214 arg = p;
23215 while (ASCII_ISALNUM(*p) || *p == '_')
23216 ++p;
23217 if (arg == p || isdigit(*arg)
23218 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23219 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23220 {
23221 if (!eap->skip)
23222 EMSG2(_("E125: Illegal argument: %s"), arg);
23223 break;
23224 }
23225 if (ga_grow(&newargs, 1) == FAIL)
23226 goto erret;
23227 c = *p;
23228 *p = NUL;
23229 arg = vim_strsave(arg);
23230 if (arg == NULL)
23231 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023232
23233 /* Check for duplicate argument name. */
23234 for (i = 0; i < newargs.ga_len; ++i)
23235 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23236 {
23237 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023238 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023239 goto erret;
23240 }
23241
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23243 *p = c;
23244 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 if (*p == ',')
23246 ++p;
23247 else
23248 mustend = TRUE;
23249 }
23250 p = skipwhite(p);
23251 if (mustend && *p != ')')
23252 {
23253 if (!eap->skip)
23254 EMSG2(_(e_invarg2), eap->arg);
23255 break;
23256 }
23257 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023258 if (*p != ')')
23259 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 ++p; /* skip the ')' */
23261
Bram Moolenaare9a41262005-01-15 22:18:47 +000023262 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263 for (;;)
23264 {
23265 p = skipwhite(p);
23266 if (STRNCMP(p, "range", 5) == 0)
23267 {
23268 flags |= FC_RANGE;
23269 p += 5;
23270 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023271 else if (STRNCMP(p, "dict", 4) == 0)
23272 {
23273 flags |= FC_DICT;
23274 p += 4;
23275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276 else if (STRNCMP(p, "abort", 5) == 0)
23277 {
23278 flags |= FC_ABORT;
23279 p += 5;
23280 }
23281 else
23282 break;
23283 }
23284
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023285 /* When there is a line break use what follows for the function body.
23286 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23287 if (*p == '\n')
23288 line_arg = p + 1;
23289 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 EMSG(_(e_trailing));
23291
23292 /*
23293 * Read the body of the function, until ":endfunction" is found.
23294 */
23295 if (KeyTyped)
23296 {
23297 /* Check if the function already exists, don't let the user type the
23298 * whole function before telling him it doesn't work! For a script we
23299 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023300 if (!eap->skip && !eap->forceit)
23301 {
23302 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23303 EMSG(_(e_funcdict));
23304 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023305 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023308 if (!eap->skip && did_emsg)
23309 goto erret;
23310
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 msg_putchar('\n'); /* don't overwrite the function name */
23312 cmdline_row = msg_row;
23313 }
23314
23315 indent = 2;
23316 nesting = 0;
23317 for (;;)
23318 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023319 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023320 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023321 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023322 saved_wait_return = FALSE;
23323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023325 sourcing_lnum_off = sourcing_lnum;
23326
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023327 if (line_arg != NULL)
23328 {
23329 /* Use eap->arg, split up in parts by line breaks. */
23330 theline = line_arg;
23331 p = vim_strchr(theline, '\n');
23332 if (p == NULL)
23333 line_arg += STRLEN(line_arg);
23334 else
23335 {
23336 *p = NUL;
23337 line_arg = p + 1;
23338 }
23339 }
23340 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 theline = getcmdline(':', 0L, indent);
23342 else
23343 theline = eap->getline(':', eap->cookie, indent);
23344 if (KeyTyped)
23345 lines_left = Rows - 1;
23346 if (theline == NULL)
23347 {
23348 EMSG(_("E126: Missing :endfunction"));
23349 goto erret;
23350 }
23351
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023352 /* Detect line continuation: sourcing_lnum increased more than one. */
23353 if (sourcing_lnum > sourcing_lnum_off + 1)
23354 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23355 else
23356 sourcing_lnum_off = 0;
23357
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 if (skip_until != NULL)
23359 {
23360 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23361 * don't check for ":endfunc". */
23362 if (STRCMP(theline, skip_until) == 0)
23363 {
23364 vim_free(skip_until);
23365 skip_until = NULL;
23366 }
23367 }
23368 else
23369 {
23370 /* skip ':' and blanks*/
23371 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23372 ;
23373
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023374 /* Check for "endfunction". */
23375 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023377 if (line_arg == NULL)
23378 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 break;
23380 }
23381
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023382 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 * at "end". */
23384 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23385 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023386 else if (STRNCMP(p, "if", 2) == 0
23387 || STRNCMP(p, "wh", 2) == 0
23388 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389 || STRNCMP(p, "try", 3) == 0)
23390 indent += 2;
23391
23392 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023393 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023395 if (*p == '!')
23396 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023398 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23399 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023401 ++nesting;
23402 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023403 }
23404 }
23405
23406 /* Check for ":append" or ":insert". */
23407 p = skip_range(p, NULL);
23408 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23409 || (p[0] == 'i'
23410 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23411 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23412 skip_until = vim_strsave((char_u *)".");
23413
23414 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23415 arg = skipwhite(skiptowhite(p));
23416 if (arg[0] == '<' && arg[1] =='<'
23417 && ((p[0] == 'p' && p[1] == 'y'
23418 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23419 || (p[0] == 'p' && p[1] == 'e'
23420 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23421 || (p[0] == 't' && p[1] == 'c'
23422 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023423 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23424 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23426 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023427 || (p[0] == 'm' && p[1] == 'z'
23428 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429 ))
23430 {
23431 /* ":python <<" continues until a dot, like ":append" */
23432 p = skipwhite(arg + 2);
23433 if (*p == NUL)
23434 skip_until = vim_strsave((char_u *)".");
23435 else
23436 skip_until = vim_strsave(p);
23437 }
23438 }
23439
23440 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023441 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023442 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023443 if (line_arg == NULL)
23444 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023446 }
23447
23448 /* Copy the line to newly allocated memory. get_one_sourceline()
23449 * allocates 250 bytes per line, this saves 80% on average. The cost
23450 * is an extra alloc/free. */
23451 p = vim_strsave(theline);
23452 if (p != NULL)
23453 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023454 if (line_arg == NULL)
23455 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023456 theline = p;
23457 }
23458
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023459 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23460
23461 /* Add NULL lines for continuation lines, so that the line count is
23462 * equal to the index in the growarray. */
23463 while (sourcing_lnum_off-- > 0)
23464 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023465
23466 /* Check for end of eap->arg. */
23467 if (line_arg != NULL && *line_arg == NUL)
23468 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 }
23470
23471 /* Don't define the function when skipping commands or when an error was
23472 * detected. */
23473 if (eap->skip || did_emsg)
23474 goto erret;
23475
23476 /*
23477 * If there are no errors, add the function
23478 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023479 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023480 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023481 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023482 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023483 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023484 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023485 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023486 goto erret;
23487 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023488
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023489 fp = find_func(name);
23490 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023492 if (!eap->forceit)
23493 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023494 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023495 goto erret;
23496 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023497 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023498 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023499 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023500 name);
23501 goto erret;
23502 }
23503 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023504 ga_clear_strings(&(fp->uf_args));
23505 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023506 vim_free(name);
23507 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 }
23510 else
23511 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023512 char numbuf[20];
23513
23514 fp = NULL;
23515 if (fudi.fd_newkey == NULL && !eap->forceit)
23516 {
23517 EMSG(_(e_funcdict));
23518 goto erret;
23519 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023520 if (fudi.fd_di == NULL)
23521 {
23522 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023523 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023524 goto erret;
23525 }
23526 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023527 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023528 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023529
23530 /* Give the function a sequential number. Can only be used with a
23531 * Funcref! */
23532 vim_free(name);
23533 sprintf(numbuf, "%d", ++func_nr);
23534 name = vim_strsave((char_u *)numbuf);
23535 if (name == NULL)
23536 goto erret;
23537 }
23538
23539 if (fp == NULL)
23540 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023541 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023542 {
23543 int slen, plen;
23544 char_u *scriptname;
23545
23546 /* Check that the autoload name matches the script name. */
23547 j = FAIL;
23548 if (sourcing_name != NULL)
23549 {
23550 scriptname = autoload_name(name);
23551 if (scriptname != NULL)
23552 {
23553 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023554 plen = (int)STRLEN(p);
23555 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023556 if (slen > plen && fnamecmp(p,
23557 sourcing_name + slen - plen) == 0)
23558 j = OK;
23559 vim_free(scriptname);
23560 }
23561 }
23562 if (j == FAIL)
23563 {
23564 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23565 goto erret;
23566 }
23567 }
23568
23569 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570 if (fp == NULL)
23571 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023572
23573 if (fudi.fd_dict != NULL)
23574 {
23575 if (fudi.fd_di == NULL)
23576 {
23577 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023578 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023579 if (fudi.fd_di == NULL)
23580 {
23581 vim_free(fp);
23582 goto erret;
23583 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023584 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23585 {
23586 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023587 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023588 goto erret;
23589 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023590 }
23591 else
23592 /* overwrite existing dict entry */
23593 clear_tv(&fudi.fd_di->di_tv);
23594 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023595 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023596 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023597 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023598
23599 /* behave like "dict" was used */
23600 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023601 }
23602
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023604 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023605 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23606 {
23607 vim_free(fp);
23608 goto erret;
23609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023611 fp->uf_args = newargs;
23612 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023613#ifdef FEAT_PROFILE
23614 fp->uf_tml_count = NULL;
23615 fp->uf_tml_total = NULL;
23616 fp->uf_tml_self = NULL;
23617 fp->uf_profiling = FALSE;
23618 if (prof_def_func())
23619 func_do_profile(fp);
23620#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023621 fp->uf_varargs = varargs;
23622 fp->uf_flags = flags;
23623 fp->uf_calls = 0;
23624 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023625 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626
23627erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 ga_clear_strings(&newargs);
23629 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023630ret_free:
23631 vim_free(skip_until);
23632 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023635 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636}
23637
23638/*
23639 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023640 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023642 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023643 * TFN_INT: internal function name OK
23644 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023645 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 * Advances "pp" to just after the function name (if no error).
23647 */
23648 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023649trans_function_name(
23650 char_u **pp,
23651 int skip, /* only find the end, don't evaluate */
23652 int flags,
23653 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023655 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 char_u *start;
23657 char_u *end;
23658 int lead;
23659 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023661 lval_T lv;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023662 partial_T *partial;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023663
23664 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023665 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023666 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023667
23668 /* Check for hard coded <SNR>: already translated function ID (from a user
23669 * command). */
23670 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23671 && (*pp)[2] == (int)KE_SNR)
23672 {
23673 *pp += 3;
23674 len = get_id_len(pp) + 3;
23675 return vim_strnsave(start, len);
23676 }
23677
23678 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23679 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023681 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023683
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023684 /* Note that TFN_ flags use the same values as GLV_ flags. */
23685 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023686 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023687 if (end == start)
23688 {
23689 if (!skip)
23690 EMSG(_("E129: Function name required"));
23691 goto theend;
23692 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023693 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023694 {
23695 /*
23696 * Report an invalid expression in braces, unless the expression
23697 * evaluation has been cancelled due to an aborting error, an
23698 * interrupt, or an exception.
23699 */
23700 if (!aborting())
23701 {
23702 if (end != NULL)
23703 EMSG2(_(e_invarg2), start);
23704 }
23705 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023706 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023707 goto theend;
23708 }
23709
23710 if (lv.ll_tv != NULL)
23711 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023712 if (fdp != NULL)
23713 {
23714 fdp->fd_dict = lv.ll_dict;
23715 fdp->fd_newkey = lv.ll_newkey;
23716 lv.ll_newkey = NULL;
23717 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023719 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23720 {
23721 name = vim_strsave(lv.ll_tv->vval.v_string);
23722 *pp = end;
23723 }
23724 else
23725 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023726 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23727 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023728 EMSG(_(e_funcref));
23729 else
23730 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023731 name = NULL;
23732 }
23733 goto theend;
23734 }
23735
23736 if (lv.ll_name == NULL)
23737 {
23738 /* Error found, but continue after the function name. */
23739 *pp = end;
23740 goto theend;
23741 }
23742
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023743 /* Check if the name is a Funcref. If so, use the value. */
23744 if (lv.ll_exp_name != NULL)
23745 {
23746 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023747 name = deref_func_name(lv.ll_exp_name, &len, &partial,
23748 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023749 if (name == lv.ll_exp_name)
23750 name = NULL;
23751 }
23752 else
23753 {
23754 len = (int)(end - *pp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023755 name = deref_func_name(*pp, &len, &partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023756 if (name == *pp)
23757 name = NULL;
23758 }
23759 if (name != NULL)
23760 {
23761 name = vim_strsave(name);
23762 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023763 if (STRNCMP(name, "<SNR>", 5) == 0)
23764 {
23765 /* Change "<SNR>" to the byte sequence. */
23766 name[0] = K_SPECIAL;
23767 name[1] = KS_EXTRA;
23768 name[2] = (int)KE_SNR;
23769 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23770 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023771 goto theend;
23772 }
23773
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023774 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023775 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023776 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023777 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23778 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23779 {
23780 /* When there was "s:" already or the name expanded to get a
23781 * leading "s:" then remove it. */
23782 lv.ll_name += 2;
23783 len -= 2;
23784 lead = 2;
23785 }
23786 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023787 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023788 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023789 /* skip over "s:" and "g:" */
23790 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023791 lv.ll_name += 2;
23792 len = (int)(end - lv.ll_name);
23793 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023794
23795 /*
23796 * Copy the function name to allocated memory.
23797 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23798 * Accept <SNR>123_name() outside a script.
23799 */
23800 if (skip)
23801 lead = 0; /* do nothing */
23802 else if (lead > 0)
23803 {
23804 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023805 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23806 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023807 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023808 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023809 if (current_SID <= 0)
23810 {
23811 EMSG(_(e_usingsid));
23812 goto theend;
23813 }
23814 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23815 lead += (int)STRLEN(sid_buf);
23816 }
23817 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023818 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023819 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023820 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023821 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023822 goto theend;
23823 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023824 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023825 {
23826 char_u *cp = vim_strchr(lv.ll_name, ':');
23827
23828 if (cp != NULL && cp < end)
23829 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023830 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023831 goto theend;
23832 }
23833 }
23834
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023835 name = alloc((unsigned)(len + lead + 1));
23836 if (name != NULL)
23837 {
23838 if (lead > 0)
23839 {
23840 name[0] = K_SPECIAL;
23841 name[1] = KS_EXTRA;
23842 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023843 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023844 STRCPY(name + 3, sid_buf);
23845 }
23846 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023847 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023848 }
23849 *pp = end;
23850
23851theend:
23852 clear_lval(&lv);
23853 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854}
23855
23856/*
23857 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23858 * Return 2 if "p" starts with "s:".
23859 * Return 0 otherwise.
23860 */
23861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023862eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023864 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23865 * the standard library function. */
23866 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23867 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 return 5;
23869 if (p[0] == 's' && p[1] == ':')
23870 return 2;
23871 return 0;
23872}
23873
23874/*
23875 * Return TRUE if "p" starts with "<SID>" or "s:".
23876 * Only works if eval_fname_script() returned non-zero for "p"!
23877 */
23878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023879eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880{
23881 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23882}
23883
23884/*
23885 * List the head of the function: "name(arg1, arg2)".
23886 */
23887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023888list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889{
23890 int j;
23891
23892 msg_start();
23893 if (indent)
23894 MSG_PUTS(" ");
23895 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023896 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 {
23898 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023899 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 }
23901 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023902 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023904 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 {
23906 if (j)
23907 MSG_PUTS(", ");
23908 msg_puts(FUNCARG(fp, j));
23909 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023910 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023911 {
23912 if (j)
23913 MSG_PUTS(", ");
23914 MSG_PUTS("...");
23915 }
23916 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023917 if (fp->uf_flags & FC_ABORT)
23918 MSG_PUTS(" abort");
23919 if (fp->uf_flags & FC_RANGE)
23920 MSG_PUTS(" range");
23921 if (fp->uf_flags & FC_DICT)
23922 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023923 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023924 if (p_verbose > 0)
23925 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926}
23927
23928/*
23929 * Find a function by name, return pointer to it in ufuncs.
23930 * Return NULL for unknown function.
23931 */
23932 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023933find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023934{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023935 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023937 hi = hash_find(&func_hashtab, name);
23938 if (!HASHITEM_EMPTY(hi))
23939 return HI2UF(hi);
23940 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941}
23942
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023943#if defined(EXITFREE) || defined(PROTO)
23944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023945free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023946{
23947 hashitem_T *hi;
23948
23949 /* Need to start all over every time, because func_free() may change the
23950 * hash table. */
23951 while (func_hashtab.ht_used > 0)
23952 for (hi = func_hashtab.ht_array; ; ++hi)
23953 if (!HASHITEM_EMPTY(hi))
23954 {
23955 func_free(HI2UF(hi));
23956 break;
23957 }
23958}
23959#endif
23960
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023962translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023963{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023964 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023965 return find_internal_func(name) >= 0;
23966 return find_func(name) != NULL;
23967}
23968
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023969/*
23970 * Return TRUE if a function "name" exists.
23971 */
23972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023973function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023974{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023975 char_u *nm = name;
23976 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023977 int n = FALSE;
23978
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023979 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23980 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023981 nm = skipwhite(nm);
23982
23983 /* Only accept "funcname", "funcname ", "funcname (..." and
23984 * "funcname(...", not "funcname!...". */
23985 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023986 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023987 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023988 return n;
23989}
23990
Bram Moolenaara1544c02013-05-30 12:35:52 +020023991 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023992get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023993{
23994 char_u *nm = name;
23995 char_u *p;
23996
23997 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23998
23999 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024000 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024001 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024002
Bram Moolenaara1544c02013-05-30 12:35:52 +020024003 vim_free(p);
24004 return NULL;
24005}
24006
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024007/*
24008 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024009 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24010 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024011 */
24012 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024013builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024014{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024015 char_u *p;
24016
24017 if (!ASCII_ISLOWER(name[0]))
24018 return FALSE;
24019 p = vim_strchr(name, AUTOLOAD_CHAR);
24020 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024021}
24022
Bram Moolenaar05159a02005-02-26 23:04:13 +000024023#if defined(FEAT_PROFILE) || defined(PROTO)
24024/*
24025 * Start profiling function "fp".
24026 */
24027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024028func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024029{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024030 int len = fp->uf_lines.ga_len;
24031
24032 if (len == 0)
24033 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024034 fp->uf_tm_count = 0;
24035 profile_zero(&fp->uf_tm_self);
24036 profile_zero(&fp->uf_tm_total);
24037 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024038 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024039 if (fp->uf_tml_total == NULL)
24040 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024041 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024042 if (fp->uf_tml_self == NULL)
24043 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024044 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024045 fp->uf_tml_idx = -1;
24046 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24047 || fp->uf_tml_self == NULL)
24048 return; /* out of memory */
24049
24050 fp->uf_profiling = TRUE;
24051}
24052
24053/*
24054 * Dump the profiling results for all functions in file "fd".
24055 */
24056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024057func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024058{
24059 hashitem_T *hi;
24060 int todo;
24061 ufunc_T *fp;
24062 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024063 ufunc_T **sorttab;
24064 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024065
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024066 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024067 if (todo == 0)
24068 return; /* nothing to dump */
24069
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024070 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024071
Bram Moolenaar05159a02005-02-26 23:04:13 +000024072 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24073 {
24074 if (!HASHITEM_EMPTY(hi))
24075 {
24076 --todo;
24077 fp = HI2UF(hi);
24078 if (fp->uf_profiling)
24079 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024080 if (sorttab != NULL)
24081 sorttab[st_len++] = fp;
24082
Bram Moolenaar05159a02005-02-26 23:04:13 +000024083 if (fp->uf_name[0] == K_SPECIAL)
24084 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24085 else
24086 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24087 if (fp->uf_tm_count == 1)
24088 fprintf(fd, "Called 1 time\n");
24089 else
24090 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24091 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24092 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24093 fprintf(fd, "\n");
24094 fprintf(fd, "count total (s) self (s)\n");
24095
24096 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24097 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024098 if (FUNCLINE(fp, i) == NULL)
24099 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024100 prof_func_line(fd, fp->uf_tml_count[i],
24101 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024102 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24103 }
24104 fprintf(fd, "\n");
24105 }
24106 }
24107 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024108
24109 if (sorttab != NULL && st_len > 0)
24110 {
24111 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24112 prof_total_cmp);
24113 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24114 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24115 prof_self_cmp);
24116 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24117 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024118
24119 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024120}
Bram Moolenaar73830342005-02-28 22:48:19 +000024121
24122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024123prof_sort_list(
24124 FILE *fd,
24125 ufunc_T **sorttab,
24126 int st_len,
24127 char *title,
24128 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024129{
24130 int i;
24131 ufunc_T *fp;
24132
24133 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24134 fprintf(fd, "count total (s) self (s) function\n");
24135 for (i = 0; i < 20 && i < st_len; ++i)
24136 {
24137 fp = sorttab[i];
24138 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24139 prefer_self);
24140 if (fp->uf_name[0] == K_SPECIAL)
24141 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24142 else
24143 fprintf(fd, " %s()\n", fp->uf_name);
24144 }
24145 fprintf(fd, "\n");
24146}
24147
24148/*
24149 * Print the count and times for one function or function line.
24150 */
24151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024152prof_func_line(
24153 FILE *fd,
24154 int count,
24155 proftime_T *total,
24156 proftime_T *self,
24157 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024158{
24159 if (count > 0)
24160 {
24161 fprintf(fd, "%5d ", count);
24162 if (prefer_self && profile_equal(total, self))
24163 fprintf(fd, " ");
24164 else
24165 fprintf(fd, "%s ", profile_msg(total));
24166 if (!prefer_self && profile_equal(total, self))
24167 fprintf(fd, " ");
24168 else
24169 fprintf(fd, "%s ", profile_msg(self));
24170 }
24171 else
24172 fprintf(fd, " ");
24173}
24174
24175/*
24176 * Compare function for total time sorting.
24177 */
24178 static int
24179#ifdef __BORLANDC__
24180_RTLENTRYF
24181#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024182prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024183{
24184 ufunc_T *p1, *p2;
24185
24186 p1 = *(ufunc_T **)s1;
24187 p2 = *(ufunc_T **)s2;
24188 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24189}
24190
24191/*
24192 * Compare function for self time sorting.
24193 */
24194 static int
24195#ifdef __BORLANDC__
24196_RTLENTRYF
24197#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024198prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024199{
24200 ufunc_T *p1, *p2;
24201
24202 p1 = *(ufunc_T **)s1;
24203 p2 = *(ufunc_T **)s2;
24204 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24205}
24206
Bram Moolenaar05159a02005-02-26 23:04:13 +000024207#endif
24208
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024209/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024210 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024211 * Return TRUE if a package was loaded.
24212 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024213 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024214script_autoload(
24215 char_u *name,
24216 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024217{
24218 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024219 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024220 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024221 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024222
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024223 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024224 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024225 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024226 return FALSE;
24227
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024228 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024229
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024230 /* Find the name in the list of previously loaded package names. Skip
24231 * "autoload/", it's always the same. */
24232 for (i = 0; i < ga_loaded.ga_len; ++i)
24233 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24234 break;
24235 if (!reload && i < ga_loaded.ga_len)
24236 ret = FALSE; /* was loaded already */
24237 else
24238 {
24239 /* Remember the name if it wasn't loaded already. */
24240 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24241 {
24242 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24243 tofree = NULL;
24244 }
24245
24246 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024247 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024248 ret = TRUE;
24249 }
24250
24251 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024252 return ret;
24253}
24254
24255/*
24256 * Return the autoload script name for a function or variable name.
24257 * Returns NULL when out of memory.
24258 */
24259 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024260autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024261{
24262 char_u *p;
24263 char_u *scriptname;
24264
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024265 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024266 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24267 if (scriptname == NULL)
24268 return FALSE;
24269 STRCPY(scriptname, "autoload/");
24270 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024271 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024272 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024273 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024274 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024275 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024276}
24277
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24279
24280/*
24281 * Function given to ExpandGeneric() to obtain the list of user defined
24282 * function names.
24283 */
24284 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024285get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024287 static long_u done;
24288 static hashitem_T *hi;
24289 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024290
24291 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024292 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024293 done = 0;
24294 hi = func_hashtab.ht_array;
24295 }
24296 if (done < func_hashtab.ht_used)
24297 {
24298 if (done++ > 0)
24299 ++hi;
24300 while (HASHITEM_EMPTY(hi))
24301 ++hi;
24302 fp = HI2UF(hi);
24303
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024304 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024305 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024306
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024307 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24308 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309
24310 cat_func_name(IObuff, fp);
24311 if (xp->xp_context != EXPAND_USER_FUNC)
24312 {
24313 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024314 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 STRCAT(IObuff, ")");
24316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 return IObuff;
24318 }
24319 return NULL;
24320}
24321
24322#endif /* FEAT_CMDL_COMPL */
24323
24324/*
24325 * Copy the function name of "fp" to buffer "buf".
24326 * "buf" must be able to hold the function name plus three bytes.
24327 * Takes care of script-local function names.
24328 */
24329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024330cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024332 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333 {
24334 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024335 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336 }
24337 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024338 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339}
24340
24341/*
24342 * ":delfunction {name}"
24343 */
24344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024345ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024347 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348 char_u *p;
24349 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024350 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024351
24352 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024353 name = trans_function_name(&p, eap->skip, 0, &fudi);
24354 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024356 {
24357 if (fudi.fd_dict != NULL && !eap->skip)
24358 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361 if (!ends_excmd(*skipwhite(p)))
24362 {
24363 vim_free(name);
24364 EMSG(_(e_trailing));
24365 return;
24366 }
24367 eap->nextcmd = check_nextcmd(p);
24368 if (eap->nextcmd != NULL)
24369 *p = NUL;
24370
24371 if (!eap->skip)
24372 fp = find_func(name);
24373 vim_free(name);
24374
24375 if (!eap->skip)
24376 {
24377 if (fp == NULL)
24378 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024379 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 return;
24381 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024382 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 {
24384 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24385 return;
24386 }
24387
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024388 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024390 /* Delete the dict item that refers to the function, it will
24391 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024392 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024393 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024394 else
24395 func_free(fp);
24396 }
24397}
24398
24399/*
24400 * Free a function and remove it from the list of functions.
24401 */
24402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024403func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024404{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024405 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024406
24407 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024408 ga_clear_strings(&(fp->uf_args));
24409 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024410#ifdef FEAT_PROFILE
24411 vim_free(fp->uf_tml_count);
24412 vim_free(fp->uf_tml_total);
24413 vim_free(fp->uf_tml_self);
24414#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024415
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024416 /* remove the function from the function hashtable */
24417 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24418 if (HASHITEM_EMPTY(hi))
24419 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024420 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024421 hash_remove(&func_hashtab, hi);
24422
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024423 vim_free(fp);
24424}
24425
24426/*
24427 * Unreference a Function: decrement the reference count and free it when it
24428 * becomes zero. Only for numbered functions.
24429 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024430 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024431func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024432{
24433 ufunc_T *fp;
24434
24435 if (name != NULL && isdigit(*name))
24436 {
24437 fp = find_func(name);
24438 if (fp == NULL)
24439 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024440 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024441 {
24442 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024443 * when "uf_calls" becomes zero. */
24444 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024445 func_free(fp);
24446 }
24447 }
24448}
24449
24450/*
24451 * Count a reference to a Function.
24452 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024454func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024455{
24456 ufunc_T *fp;
24457
24458 if (name != NULL && isdigit(*name))
24459 {
24460 fp = find_func(name);
24461 if (fp == NULL)
24462 EMSG2(_(e_intern2), "func_ref()");
24463 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024464 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024465 }
24466}
24467
24468/*
24469 * Call a user function.
24470 */
24471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024472call_user_func(
24473 ufunc_T *fp, /* pointer to function */
24474 int argcount, /* nr of args */
24475 typval_T *argvars, /* arguments */
24476 typval_T *rettv, /* return value */
24477 linenr_T firstline, /* first line of range */
24478 linenr_T lastline, /* last line of range */
24479 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480{
Bram Moolenaar33570922005-01-25 22:26:29 +000024481 char_u *save_sourcing_name;
24482 linenr_T save_sourcing_lnum;
24483 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024484 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024485 int save_did_emsg;
24486 static int depth = 0;
24487 dictitem_T *v;
24488 int fixvar_idx = 0; /* index in fixvar[] */
24489 int i;
24490 int ai;
24491 char_u numbuf[NUMBUFLEN];
24492 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024493 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024494#ifdef FEAT_PROFILE
24495 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024496 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498
24499 /* If depth of calling is getting too high, don't execute the function */
24500 if (depth >= p_mfd)
24501 {
24502 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024503 rettv->v_type = VAR_NUMBER;
24504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505 return;
24506 }
24507 ++depth;
24508
24509 line_breakcheck(); /* check for CTRL-C hit */
24510
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024511 fc = (funccall_T *)alloc(sizeof(funccall_T));
24512 fc->caller = current_funccal;
24513 current_funccal = fc;
24514 fc->func = fp;
24515 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024516 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024517 fc->linenr = 0;
24518 fc->returned = FALSE;
24519 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024520 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024521 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24522 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523
Bram Moolenaar33570922005-01-25 22:26:29 +000024524 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024525 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024526 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24527 * each argument variable and saves a lot of time.
24528 */
24529 /*
24530 * Init l: variables.
24531 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024532 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024533 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024534 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024535 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24536 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024537 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024538 name = v->di_key;
24539 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024540 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024541 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024542 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024543 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024544 v->di_tv.vval.v_dict = selfdict;
24545 ++selfdict->dv_refcount;
24546 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024547
Bram Moolenaar33570922005-01-25 22:26:29 +000024548 /*
24549 * Init a: variables.
24550 * Set a:0 to "argcount".
24551 * Set a:000 to a list with room for the "..." arguments.
24552 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024553 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024554 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024555 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024556 /* Use "name" to avoid a warning from some compiler that checks the
24557 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024558 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024559 name = v->di_key;
24560 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024561 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024562 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024563 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024564 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024565 v->di_tv.vval.v_list = &fc->l_varlist;
24566 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24567 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24568 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024569
24570 /*
24571 * Set a:firstline to "firstline" and a:lastline to "lastline".
24572 * Set a:name to named arguments.
24573 * Set a:N to the "..." arguments.
24574 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024575 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024576 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024577 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024578 (varnumber_T)lastline);
24579 for (i = 0; i < argcount; ++i)
24580 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024581 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024582 if (ai < 0)
24583 /* named argument a:name */
24584 name = FUNCARG(fp, i);
24585 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024586 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024587 /* "..." argument a:1, a:2, etc. */
24588 sprintf((char *)numbuf, "%d", ai + 1);
24589 name = numbuf;
24590 }
24591 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24592 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024593 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024594 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24595 }
24596 else
24597 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024598 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24599 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024600 if (v == NULL)
24601 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024602 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024603 }
24604 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024605 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024606
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024607 /* Note: the values are copied directly to avoid alloc/free.
24608 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024609 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024610 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024611
24612 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24613 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024614 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24615 fc->l_listitems[ai].li_tv = argvars[i];
24616 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024617 }
24618 }
24619
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 /* Don't redraw while executing the function. */
24621 ++RedrawingDisabled;
24622 save_sourcing_name = sourcing_name;
24623 save_sourcing_lnum = sourcing_lnum;
24624 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024625 /* need space for function name + ("function " + 3) or "[number]" */
24626 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24627 + STRLEN(fp->uf_name) + 20;
24628 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 if (sourcing_name != NULL)
24630 {
24631 if (save_sourcing_name != NULL
24632 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024633 sprintf((char *)sourcing_name, "%s[%d]..",
24634 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635 else
24636 STRCPY(sourcing_name, "function ");
24637 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24638
24639 if (p_verbose >= 12)
24640 {
24641 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024642 verbose_enter_scroll();
24643
Bram Moolenaar555b2802005-05-19 21:08:39 +000024644 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 if (p_verbose >= 14)
24646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024648 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024649 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024650 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651
24652 msg_puts((char_u *)"(");
24653 for (i = 0; i < argcount; ++i)
24654 {
24655 if (i > 0)
24656 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024657 if (argvars[i].v_type == VAR_NUMBER)
24658 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 else
24660 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024661 /* Do not want errors such as E724 here. */
24662 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024663 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024664 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024665 if (s != NULL)
24666 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024667 if (vim_strsize(s) > MSG_BUF_CLEN)
24668 {
24669 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24670 s = buf;
24671 }
24672 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024673 vim_free(tofree);
24674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675 }
24676 }
24677 msg_puts((char_u *)")");
24678 }
24679 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024680
24681 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682 --no_wait_return;
24683 }
24684 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024685#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024686 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024687 {
24688 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24689 func_do_profile(fp);
24690 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024691 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024692 {
24693 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024694 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024695 profile_zero(&fp->uf_tm_children);
24696 }
24697 script_prof_save(&wait_start);
24698 }
24699#endif
24700
Bram Moolenaar071d4272004-06-13 20:20:40 +000024701 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024702 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024703 save_did_emsg = did_emsg;
24704 did_emsg = FALSE;
24705
24706 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024707 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24709
24710 --RedrawingDisabled;
24711
24712 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024713 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024715 clear_tv(rettv);
24716 rettv->v_type = VAR_NUMBER;
24717 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024718 }
24719
Bram Moolenaar05159a02005-02-26 23:04:13 +000024720#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024721 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024722 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024723 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024724 profile_end(&call_start);
24725 profile_sub_wait(&wait_start, &call_start);
24726 profile_add(&fp->uf_tm_total, &call_start);
24727 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024728 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024729 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024730 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24731 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024732 }
24733 }
24734#endif
24735
Bram Moolenaar071d4272004-06-13 20:20:40 +000024736 /* when being verbose, mention the return value */
24737 if (p_verbose >= 12)
24738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024740 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024743 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024744 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024745 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024746 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024747 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024748 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024749 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024750 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024751 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024752 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024753
Bram Moolenaar555b2802005-05-19 21:08:39 +000024754 /* The value may be very long. Skip the middle part, so that we
24755 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024756 * truncate it at the end. Don't want errors such as E724 here. */
24757 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024758 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024759 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024760 if (s != NULL)
24761 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024762 if (vim_strsize(s) > MSG_BUF_CLEN)
24763 {
24764 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24765 s = buf;
24766 }
24767 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024768 vim_free(tofree);
24769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770 }
24771 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024772
24773 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774 --no_wait_return;
24775 }
24776
24777 vim_free(sourcing_name);
24778 sourcing_name = save_sourcing_name;
24779 sourcing_lnum = save_sourcing_lnum;
24780 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024781#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024782 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024783 script_prof_restore(&wait_start);
24784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785
24786 if (p_verbose >= 12 && sourcing_name != NULL)
24787 {
24788 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024789 verbose_enter_scroll();
24790
Bram Moolenaar555b2802005-05-19 21:08:39 +000024791 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024793
24794 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024795 --no_wait_return;
24796 }
24797
24798 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024799 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024801
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024802 /* If the a:000 list and the l: and a: dicts are not referenced we can
24803 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024804 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24805 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24806 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24807 {
24808 free_funccal(fc, FALSE);
24809 }
24810 else
24811 {
24812 hashitem_T *hi;
24813 listitem_T *li;
24814 int todo;
24815
24816 /* "fc" is still in use. This can happen when returning "a:000" or
24817 * assigning "l:" to a global variable.
24818 * Link "fc" in the list for garbage collection later. */
24819 fc->caller = previous_funccal;
24820 previous_funccal = fc;
24821
24822 /* Make a copy of the a: variables, since we didn't do that above. */
24823 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24824 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24825 {
24826 if (!HASHITEM_EMPTY(hi))
24827 {
24828 --todo;
24829 v = HI2DI(hi);
24830 copy_tv(&v->di_tv, &v->di_tv);
24831 }
24832 }
24833
24834 /* Make a copy of the a:000 items, since we didn't do that above. */
24835 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24836 copy_tv(&li->li_tv, &li->li_tv);
24837 }
24838}
24839
24840/*
24841 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024842 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024843 */
24844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024845can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024846{
24847 return (fc->l_varlist.lv_copyID != copyID
24848 && fc->l_vars.dv_copyID != copyID
24849 && fc->l_avars.dv_copyID != copyID);
24850}
24851
24852/*
24853 * Free "fc" and what it contains.
24854 */
24855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024856free_funccal(
24857 funccall_T *fc,
24858 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024859{
24860 listitem_T *li;
24861
24862 /* The a: variables typevals may not have been allocated, only free the
24863 * allocated variables. */
24864 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24865
24866 /* free all l: variables */
24867 vars_clear(&fc->l_vars.dv_hashtab);
24868
24869 /* Free the a:000 variables if they were allocated. */
24870 if (free_val)
24871 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24872 clear_tv(&li->li_tv);
24873
24874 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875}
24876
24877/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024878 * Add a number variable "name" to dict "dp" with value "nr".
24879 */
24880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024881add_nr_var(
24882 dict_T *dp,
24883 dictitem_T *v,
24884 char *name,
24885 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024886{
24887 STRCPY(v->di_key, name);
24888 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24889 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24890 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024891 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024892 v->di_tv.vval.v_number = nr;
24893}
24894
24895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896 * ":return [expr]"
24897 */
24898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024899ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900{
24901 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024902 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 int returning = FALSE;
24904
24905 if (current_funccal == NULL)
24906 {
24907 EMSG(_("E133: :return not inside a function"));
24908 return;
24909 }
24910
24911 if (eap->skip)
24912 ++emsg_skip;
24913
24914 eap->nextcmd = NULL;
24915 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024916 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917 {
24918 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024919 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024921 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 }
24923 /* It's safer to return also on error. */
24924 else if (!eap->skip)
24925 {
24926 /*
24927 * Return unless the expression evaluation has been cancelled due to an
24928 * aborting error, an interrupt, or an exception.
24929 */
24930 if (!aborting())
24931 returning = do_return(eap, FALSE, TRUE, NULL);
24932 }
24933
24934 /* When skipping or the return gets pending, advance to the next command
24935 * in this line (!returning). Otherwise, ignore the rest of the line.
24936 * Following lines will be ignored by get_func_line(). */
24937 if (returning)
24938 eap->nextcmd = NULL;
24939 else if (eap->nextcmd == NULL) /* no argument */
24940 eap->nextcmd = check_nextcmd(arg);
24941
24942 if (eap->skip)
24943 --emsg_skip;
24944}
24945
24946/*
24947 * Return from a function. Possibly makes the return pending. Also called
24948 * for a pending return at the ":endtry" or after returning from an extra
24949 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024950 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024951 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952 * FALSE when the return gets pending.
24953 */
24954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024955do_return(
24956 exarg_T *eap,
24957 int reanimate,
24958 int is_cmd,
24959 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024960{
24961 int idx;
24962 struct condstack *cstack = eap->cstack;
24963
24964 if (reanimate)
24965 /* Undo the return. */
24966 current_funccal->returned = FALSE;
24967
24968 /*
24969 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24970 * not in its finally clause (which then is to be executed next) is found.
24971 * In this case, make the ":return" pending for execution at the ":endtry".
24972 * Otherwise, return normally.
24973 */
24974 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24975 if (idx >= 0)
24976 {
24977 cstack->cs_pending[idx] = CSTP_RETURN;
24978
24979 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024980 /* A pending return again gets pending. "rettv" points to an
24981 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024982 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024983 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024984 else
24985 {
24986 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024987 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024989 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024991 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992 {
24993 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024994 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024995 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996 else
24997 EMSG(_(e_outofmem));
24998 }
24999 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025000 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001
25002 if (reanimate)
25003 {
25004 /* The pending return value could be overwritten by a ":return"
25005 * without argument in a finally clause; reset the default
25006 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025007 current_funccal->rettv->v_type = VAR_NUMBER;
25008 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025009 }
25010 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025011 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 }
25013 else
25014 {
25015 current_funccal->returned = TRUE;
25016
25017 /* If the return is carried out now, store the return value. For
25018 * a return immediately after reanimation, the value is already
25019 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025020 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025022 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025023 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025025 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026 }
25027 }
25028
25029 return idx < 0;
25030}
25031
25032/*
25033 * Free the variable with a pending return value.
25034 */
25035 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025036discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025037{
Bram Moolenaar33570922005-01-25 22:26:29 +000025038 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039}
25040
25041/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025042 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 * is an allocated string. Used by report_pending() for verbose messages.
25044 */
25045 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025046get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025048 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025049 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025050 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025051
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025052 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025053 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025054 if (s == NULL)
25055 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025056
25057 STRCPY(IObuff, ":return ");
25058 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25059 if (STRLEN(s) + 8 >= IOSIZE)
25060 STRCPY(IObuff + IOSIZE - 4, "...");
25061 vim_free(tofree);
25062 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025063}
25064
25065/*
25066 * Get next function line.
25067 * Called by do_cmdline() to get the next line.
25068 * Returns allocated string, or NULL for end of function.
25069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025070 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025071get_func_line(
25072 int c UNUSED,
25073 void *cookie,
25074 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025075{
Bram Moolenaar33570922005-01-25 22:26:29 +000025076 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025077 ufunc_T *fp = fcp->func;
25078 char_u *retval;
25079 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080
25081 /* If breakpoints have been added/deleted need to check for it. */
25082 if (fcp->dbg_tick != debug_tick)
25083 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025084 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085 sourcing_lnum);
25086 fcp->dbg_tick = debug_tick;
25087 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025088#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025089 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025090 func_line_end(cookie);
25091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092
Bram Moolenaar05159a02005-02-26 23:04:13 +000025093 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025094 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25095 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025096 retval = NULL;
25097 else
25098 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025099 /* Skip NULL lines (continuation lines). */
25100 while (fcp->linenr < gap->ga_len
25101 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25102 ++fcp->linenr;
25103 if (fcp->linenr >= gap->ga_len)
25104 retval = NULL;
25105 else
25106 {
25107 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25108 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025109#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025110 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025111 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025112#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025114 }
25115
25116 /* Did we encounter a breakpoint? */
25117 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25118 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025119 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025121 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122 sourcing_lnum);
25123 fcp->dbg_tick = debug_tick;
25124 }
25125
25126 return retval;
25127}
25128
Bram Moolenaar05159a02005-02-26 23:04:13 +000025129#if defined(FEAT_PROFILE) || defined(PROTO)
25130/*
25131 * Called when starting to read a function line.
25132 * "sourcing_lnum" must be correct!
25133 * When skipping lines it may not actually be executed, but we won't find out
25134 * until later and we need to store the time now.
25135 */
25136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025137func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025138{
25139 funccall_T *fcp = (funccall_T *)cookie;
25140 ufunc_T *fp = fcp->func;
25141
25142 if (fp->uf_profiling && sourcing_lnum >= 1
25143 && sourcing_lnum <= fp->uf_lines.ga_len)
25144 {
25145 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025146 /* Skip continuation lines. */
25147 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25148 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025149 fp->uf_tml_execed = FALSE;
25150 profile_start(&fp->uf_tml_start);
25151 profile_zero(&fp->uf_tml_children);
25152 profile_get_wait(&fp->uf_tml_wait);
25153 }
25154}
25155
25156/*
25157 * Called when actually executing a function line.
25158 */
25159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025160func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025161{
25162 funccall_T *fcp = (funccall_T *)cookie;
25163 ufunc_T *fp = fcp->func;
25164
25165 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25166 fp->uf_tml_execed = TRUE;
25167}
25168
25169/*
25170 * Called when done with a function line.
25171 */
25172 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025173func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025174{
25175 funccall_T *fcp = (funccall_T *)cookie;
25176 ufunc_T *fp = fcp->func;
25177
25178 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25179 {
25180 if (fp->uf_tml_execed)
25181 {
25182 ++fp->uf_tml_count[fp->uf_tml_idx];
25183 profile_end(&fp->uf_tml_start);
25184 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025185 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025186 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25187 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025188 }
25189 fp->uf_tml_idx = -1;
25190 }
25191}
25192#endif
25193
Bram Moolenaar071d4272004-06-13 20:20:40 +000025194/*
25195 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025196 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 */
25198 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025199func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200{
Bram Moolenaar33570922005-01-25 22:26:29 +000025201 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202
25203 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25204 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025205 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 || fcp->returned);
25207}
25208
25209/*
25210 * return TRUE if cookie indicates a function which "abort"s on errors.
25211 */
25212 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025213func_has_abort(
25214 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025216 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217}
25218
25219#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25220typedef enum
25221{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025222 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25223 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25224 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225} var_flavour_T;
25226
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025227static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228
25229 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025230var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231{
25232 char_u *p = varname;
25233
25234 if (ASCII_ISUPPER(*p))
25235 {
25236 while (*(++p))
25237 if (ASCII_ISLOWER(*p))
25238 return VAR_FLAVOUR_SESSION;
25239 return VAR_FLAVOUR_VIMINFO;
25240 }
25241 else
25242 return VAR_FLAVOUR_DEFAULT;
25243}
25244#endif
25245
25246#if defined(FEAT_VIMINFO) || defined(PROTO)
25247/*
25248 * Restore global vars that start with a capital from the viminfo file
25249 */
25250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025251read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252{
25253 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025254 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025255 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025256 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257
25258 if (!writing && (find_viminfo_parameter('!') != NULL))
25259 {
25260 tab = vim_strchr(virp->vir_line + 1, '\t');
25261 if (tab != NULL)
25262 {
25263 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025264 switch (*tab)
25265 {
25266 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025267#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025268 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025269#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025270 case 'D': type = VAR_DICT; break;
25271 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025272 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274
25275 tab = vim_strchr(tab, '\t');
25276 if (tab != NULL)
25277 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025278 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025279 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025280 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025282#ifdef FEAT_FLOAT
25283 else if (type == VAR_FLOAT)
25284 (void)string2float(tab + 1, &tv.vval.v_float);
25285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025287 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025288 if (type == VAR_DICT || type == VAR_LIST)
25289 {
25290 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25291
25292 if (etv == NULL)
25293 /* Failed to parse back the dict or list, use it as a
25294 * string. */
25295 tv.v_type = VAR_STRING;
25296 else
25297 {
25298 vim_free(tv.vval.v_string);
25299 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025300 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025301 }
25302 }
25303
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025304 /* when in a function use global variables */
25305 save_funccal = current_funccal;
25306 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025307 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025308 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025309
25310 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025311 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025312 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25313 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314 }
25315 }
25316 }
25317
25318 return viminfo_readline(virp);
25319}
25320
25321/*
25322 * Write global vars that start with a capital to the viminfo file
25323 */
25324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025325write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326{
Bram Moolenaar33570922005-01-25 22:26:29 +000025327 hashitem_T *hi;
25328 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025329 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025330 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025331 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025332 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025333 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334
25335 if (find_viminfo_parameter('!') == NULL)
25336 return;
25337
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025338 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025339
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025340 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025341 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025343 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025345 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025346 this_var = HI2DI(hi);
25347 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025348 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025349 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025350 {
25351 case VAR_STRING: s = "STR"; break;
25352 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025353 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025354 case VAR_DICT: s = "DIC"; break;
25355 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025356 case VAR_SPECIAL: s = "XPL"; break;
25357
25358 case VAR_UNKNOWN:
25359 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025360 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025361 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025362 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025363 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025364 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025365 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025366 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025367 if (p != NULL)
25368 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025369 vim_free(tofree);
25370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371 }
25372 }
25373}
25374#endif
25375
25376#if defined(FEAT_SESSION) || defined(PROTO)
25377 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025378store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379{
Bram Moolenaar33570922005-01-25 22:26:29 +000025380 hashitem_T *hi;
25381 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025382 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383 char_u *p, *t;
25384
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025385 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025386 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025388 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025390 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025391 this_var = HI2DI(hi);
25392 if ((this_var->di_tv.v_type == VAR_NUMBER
25393 || this_var->di_tv.v_type == VAR_STRING)
25394 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025395 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025396 /* Escape special characters with a backslash. Turn a LF and
25397 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025398 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025399 (char_u *)"\\\"\n\r");
25400 if (p == NULL) /* out of memory */
25401 break;
25402 for (t = p; *t != NUL; ++t)
25403 if (*t == '\n')
25404 *t = 'n';
25405 else if (*t == '\r')
25406 *t = 'r';
25407 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025408 this_var->di_key,
25409 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25410 : ' ',
25411 p,
25412 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25413 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025414 || put_eol(fd) == FAIL)
25415 {
25416 vim_free(p);
25417 return FAIL;
25418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 vim_free(p);
25420 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025421#ifdef FEAT_FLOAT
25422 else if (this_var->di_tv.v_type == VAR_FLOAT
25423 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25424 {
25425 float_T f = this_var->di_tv.vval.v_float;
25426 int sign = ' ';
25427
25428 if (f < 0)
25429 {
25430 f = -f;
25431 sign = '-';
25432 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025433 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025434 this_var->di_key, sign, f) < 0)
25435 || put_eol(fd) == FAIL)
25436 return FAIL;
25437 }
25438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025439 }
25440 }
25441 return OK;
25442}
25443#endif
25444
Bram Moolenaar661b1822005-07-28 22:36:45 +000025445/*
25446 * Display script name where an item was last set.
25447 * Should only be invoked when 'verbose' is non-zero.
25448 */
25449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025450last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025451{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025452 char_u *p;
25453
Bram Moolenaar661b1822005-07-28 22:36:45 +000025454 if (scriptID != 0)
25455 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025456 p = home_replace_save(NULL, get_scriptname(scriptID));
25457 if (p != NULL)
25458 {
25459 verbose_enter();
25460 MSG_PUTS(_("\n\tLast set from "));
25461 MSG_PUTS(p);
25462 vim_free(p);
25463 verbose_leave();
25464 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025465 }
25466}
25467
Bram Moolenaard812df62008-11-09 12:46:09 +000025468/*
25469 * List v:oldfiles in a nice way.
25470 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025471 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025472ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025473{
25474 list_T *l = vimvars[VV_OLDFILES].vv_list;
25475 listitem_T *li;
25476 int nr = 0;
25477
25478 if (l == NULL)
25479 msg((char_u *)_("No old files"));
25480 else
25481 {
25482 msg_start();
25483 msg_scroll = TRUE;
25484 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25485 {
25486 msg_outnum((long)++nr);
25487 MSG_PUTS(": ");
25488 msg_outtrans(get_tv_string(&li->li_tv));
25489 msg_putchar('\n');
25490 out_flush(); /* output one line at a time */
25491 ui_breakcheck();
25492 }
25493 /* Assume "got_int" was set to truncate the listing. */
25494 got_int = FALSE;
25495
25496#ifdef FEAT_BROWSE_CMD
25497 if (cmdmod.browse)
25498 {
25499 quit_more = FALSE;
25500 nr = prompt_for_number(FALSE);
25501 msg_starthere();
25502 if (nr > 0)
25503 {
25504 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25505 (long)nr);
25506
25507 if (p != NULL)
25508 {
25509 p = expand_env_save(p);
25510 eap->arg = p;
25511 eap->cmdidx = CMD_edit;
25512 cmdmod.browse = FALSE;
25513 do_exedit(eap, NULL);
25514 vim_free(p);
25515 }
25516 }
25517 }
25518#endif
25519 }
25520}
25521
Bram Moolenaar53744302015-07-17 17:38:22 +020025522/* reset v:option_new, v:option_old and v:option_type */
25523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025524reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025525{
25526 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25527 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25528 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25529}
25530
25531
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532#endif /* FEAT_EVAL */
25533
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025535#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536
25537#ifdef WIN3264
25538/*
25539 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25540 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025541static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25542static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25543static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544
25545/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025546 * Get the short path (8.3) for the filename in "fnamep".
25547 * Only works for a valid file name.
25548 * When the path gets longer "fnamep" is changed and the allocated buffer
25549 * is put in "bufp".
25550 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25551 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552 */
25553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025554get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025555{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025556 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 char_u *newbuf;
25558
25559 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025560 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561 if (l > len - 1)
25562 {
25563 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025564 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 newbuf = vim_strnsave(*fnamep, l);
25566 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025567 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568
25569 vim_free(*bufp);
25570 *fnamep = *bufp = newbuf;
25571
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025572 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025573 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 }
25575
25576 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025577 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025578}
25579
25580/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025581 * Get the short path (8.3) for the filename in "fname". The converted
25582 * path is returned in "bufp".
25583 *
25584 * Some of the directories specified in "fname" may not exist. This function
25585 * will shorten the existing directories at the beginning of the path and then
25586 * append the remaining non-existing path.
25587 *
25588 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025589 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025590 * bufp - Pointer to an allocated buffer for the filename.
25591 * fnamelen - Length of the filename pointed to by fname
25592 *
25593 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594 */
25595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025596shortpath_for_invalid_fname(
25597 char_u **fname,
25598 char_u **bufp,
25599 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025600{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025601 char_u *short_fname, *save_fname, *pbuf_unused;
25602 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025603 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025604 int old_len, len;
25605 int new_len, sfx_len;
25606 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607
25608 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025609 old_len = *fnamelen;
25610 save_fname = vim_strnsave(*fname, old_len);
25611 pbuf_unused = NULL;
25612 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025613
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025614 endp = save_fname + old_len - 1; /* Find the end of the copy */
25615 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025617 /*
25618 * Try shortening the supplied path till it succeeds by removing one
25619 * directory at a time from the tail of the path.
25620 */
25621 len = 0;
25622 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025624 /* go back one path-separator */
25625 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25626 --endp;
25627 if (endp <= save_fname)
25628 break; /* processed the complete path */
25629
25630 /*
25631 * Replace the path separator with a NUL and try to shorten the
25632 * resulting path.
25633 */
25634 ch = *endp;
25635 *endp = 0;
25636 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025637 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025638 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25639 {
25640 retval = FAIL;
25641 goto theend;
25642 }
25643 *endp = ch; /* preserve the string */
25644
25645 if (len > 0)
25646 break; /* successfully shortened the path */
25647
25648 /* failed to shorten the path. Skip the path separator */
25649 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650 }
25651
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025652 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025654 /*
25655 * Succeeded in shortening the path. Now concatenate the shortened
25656 * path with the remaining path at the tail.
25657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025659 /* Compute the length of the new path. */
25660 sfx_len = (int)(save_endp - endp) + 1;
25661 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025662
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025663 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025665 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025667 /* There is not enough space in the currently allocated string,
25668 * copy it to a buffer big enough. */
25669 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025671 {
25672 retval = FAIL;
25673 goto theend;
25674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025675 }
25676 else
25677 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025678 /* Transfer short_fname to the main buffer (it's big enough),
25679 * unless get_short_pathname() did its work in-place. */
25680 *fname = *bufp = save_fname;
25681 if (short_fname != save_fname)
25682 vim_strncpy(save_fname, short_fname, len);
25683 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025684 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025685
25686 /* concat the not-shortened part of the path */
25687 vim_strncpy(*fname + len, endp, sfx_len);
25688 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025690
25691theend:
25692 vim_free(pbuf_unused);
25693 vim_free(save_fname);
25694
25695 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025696}
25697
25698/*
25699 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025700 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701 */
25702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025703shortpath_for_partial(
25704 char_u **fnamep,
25705 char_u **bufp,
25706 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025707{
25708 int sepcount, len, tflen;
25709 char_u *p;
25710 char_u *pbuf, *tfname;
25711 int hasTilde;
25712
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025713 /* Count up the path separators from the RHS.. so we know which part
25714 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025715 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025716 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025717 if (vim_ispathsep(*p))
25718 ++sepcount;
25719
25720 /* Need full path first (use expand_env() to remove a "~/") */
25721 hasTilde = (**fnamep == '~');
25722 if (hasTilde)
25723 pbuf = tfname = expand_env_save(*fnamep);
25724 else
25725 pbuf = tfname = FullName_save(*fnamep, FALSE);
25726
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025727 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025729 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25730 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731
25732 if (len == 0)
25733 {
25734 /* Don't have a valid filename, so shorten the rest of the
25735 * path if we can. This CAN give us invalid 8.3 filenames, but
25736 * there's not a lot of point in guessing what it might be.
25737 */
25738 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025739 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25740 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741 }
25742
25743 /* Count the paths backward to find the beginning of the desired string. */
25744 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025745 {
25746#ifdef FEAT_MBYTE
25747 if (has_mbyte)
25748 p -= mb_head_off(tfname, p);
25749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025750 if (vim_ispathsep(*p))
25751 {
25752 if (sepcount == 0 || (hasTilde && sepcount == 1))
25753 break;
25754 else
25755 sepcount --;
25756 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758 if (hasTilde)
25759 {
25760 --p;
25761 if (p >= tfname)
25762 *p = '~';
25763 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025764 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025765 }
25766 else
25767 ++p;
25768
25769 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25770 vim_free(*bufp);
25771 *fnamelen = (int)STRLEN(p);
25772 *bufp = pbuf;
25773 *fnamep = p;
25774
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025775 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776}
25777#endif /* WIN3264 */
25778
25779/*
25780 * Adjust a filename, according to a string of modifiers.
25781 * *fnamep must be NUL terminated when called. When returning, the length is
25782 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025783 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784 * When there is an error, *fnamep is set to NULL.
25785 */
25786 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025787modify_fname(
25788 char_u *src, /* string with modifiers */
25789 int *usedlen, /* characters after src that are used */
25790 char_u **fnamep, /* file name so far */
25791 char_u **bufp, /* buffer for allocated file name or NULL */
25792 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793{
25794 int valid = 0;
25795 char_u *tail;
25796 char_u *s, *p, *pbuf;
25797 char_u dirname[MAXPATHL];
25798 int c;
25799 int has_fullname = 0;
25800#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025801 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802 int has_shortname = 0;
25803#endif
25804
25805repeat:
25806 /* ":p" - full path/file_name */
25807 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25808 {
25809 has_fullname = 1;
25810
25811 valid |= VALID_PATH;
25812 *usedlen += 2;
25813
25814 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25815 if ((*fnamep)[0] == '~'
25816#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25817 && ((*fnamep)[1] == '/'
25818# ifdef BACKSLASH_IN_FILENAME
25819 || (*fnamep)[1] == '\\'
25820# endif
25821 || (*fnamep)[1] == NUL)
25822
25823#endif
25824 )
25825 {
25826 *fnamep = expand_env_save(*fnamep);
25827 vim_free(*bufp); /* free any allocated file name */
25828 *bufp = *fnamep;
25829 if (*fnamep == NULL)
25830 return -1;
25831 }
25832
25833 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025834 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025835 {
25836 if (vim_ispathsep(*p)
25837 && p[1] == '.'
25838 && (p[2] == NUL
25839 || vim_ispathsep(p[2])
25840 || (p[2] == '.'
25841 && (p[3] == NUL || vim_ispathsep(p[3])))))
25842 break;
25843 }
25844
25845 /* FullName_save() is slow, don't use it when not needed. */
25846 if (*p != NUL || !vim_isAbsName(*fnamep))
25847 {
25848 *fnamep = FullName_save(*fnamep, *p != NUL);
25849 vim_free(*bufp); /* free any allocated file name */
25850 *bufp = *fnamep;
25851 if (*fnamep == NULL)
25852 return -1;
25853 }
25854
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025855#ifdef WIN3264
25856# if _WIN32_WINNT >= 0x0500
25857 if (vim_strchr(*fnamep, '~') != NULL)
25858 {
25859 /* Expand 8.3 filename to full path. Needed to make sure the same
25860 * file does not have two different names.
25861 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25862 p = alloc(_MAX_PATH + 1);
25863 if (p != NULL)
25864 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025865 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025866 {
25867 vim_free(*bufp);
25868 *bufp = *fnamep = p;
25869 }
25870 else
25871 vim_free(p);
25872 }
25873 }
25874# endif
25875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025876 /* Append a path separator to a directory. */
25877 if (mch_isdir(*fnamep))
25878 {
25879 /* Make room for one or two extra characters. */
25880 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25881 vim_free(*bufp); /* free any allocated file name */
25882 *bufp = *fnamep;
25883 if (*fnamep == NULL)
25884 return -1;
25885 add_pathsep(*fnamep);
25886 }
25887 }
25888
25889 /* ":." - path relative to the current directory */
25890 /* ":~" - path relative to the home directory */
25891 /* ":8" - shortname path - postponed till after */
25892 while (src[*usedlen] == ':'
25893 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25894 {
25895 *usedlen += 2;
25896 if (c == '8')
25897 {
25898#ifdef WIN3264
25899 has_shortname = 1; /* Postpone this. */
25900#endif
25901 continue;
25902 }
25903 pbuf = NULL;
25904 /* Need full path first (use expand_env() to remove a "~/") */
25905 if (!has_fullname)
25906 {
25907 if (c == '.' && **fnamep == '~')
25908 p = pbuf = expand_env_save(*fnamep);
25909 else
25910 p = pbuf = FullName_save(*fnamep, FALSE);
25911 }
25912 else
25913 p = *fnamep;
25914
25915 has_fullname = 0;
25916
25917 if (p != NULL)
25918 {
25919 if (c == '.')
25920 {
25921 mch_dirname(dirname, MAXPATHL);
25922 s = shorten_fname(p, dirname);
25923 if (s != NULL)
25924 {
25925 *fnamep = s;
25926 if (pbuf != NULL)
25927 {
25928 vim_free(*bufp); /* free any allocated file name */
25929 *bufp = pbuf;
25930 pbuf = NULL;
25931 }
25932 }
25933 }
25934 else
25935 {
25936 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25937 /* Only replace it when it starts with '~' */
25938 if (*dirname == '~')
25939 {
25940 s = vim_strsave(dirname);
25941 if (s != NULL)
25942 {
25943 *fnamep = s;
25944 vim_free(*bufp);
25945 *bufp = s;
25946 }
25947 }
25948 }
25949 vim_free(pbuf);
25950 }
25951 }
25952
25953 tail = gettail(*fnamep);
25954 *fnamelen = (int)STRLEN(*fnamep);
25955
25956 /* ":h" - head, remove "/file_name", can be repeated */
25957 /* Don't remove the first "/" or "c:\" */
25958 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25959 {
25960 valid |= VALID_HEAD;
25961 *usedlen += 2;
25962 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025963 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025964 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025965 *fnamelen = (int)(tail - *fnamep);
25966#ifdef VMS
25967 if (*fnamelen > 0)
25968 *fnamelen += 1; /* the path separator is part of the path */
25969#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025970 if (*fnamelen == 0)
25971 {
25972 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25973 p = vim_strsave((char_u *)".");
25974 if (p == NULL)
25975 return -1;
25976 vim_free(*bufp);
25977 *bufp = *fnamep = tail = p;
25978 *fnamelen = 1;
25979 }
25980 else
25981 {
25982 while (tail > s && !after_pathsep(s, tail))
25983 mb_ptr_back(*fnamep, tail);
25984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025985 }
25986
25987 /* ":8" - shortname */
25988 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25989 {
25990 *usedlen += 2;
25991#ifdef WIN3264
25992 has_shortname = 1;
25993#endif
25994 }
25995
25996#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025997 /*
25998 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 */
26000 if (has_shortname)
26001 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026002 /* Copy the string if it is shortened by :h and when it wasn't copied
26003 * yet, because we are going to change it in place. Avoids changing
26004 * the buffer name for "%:8". */
26005 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026006 {
26007 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026008 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026009 return -1;
26010 vim_free(*bufp);
26011 *bufp = *fnamep = p;
26012 }
26013
26014 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026015 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026016 if (!has_fullname && !vim_isAbsName(*fnamep))
26017 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026018 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019 return -1;
26020 }
26021 else
26022 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026023 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026024
Bram Moolenaardc935552011-08-17 15:23:23 +020026025 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026027 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026028 return -1;
26029
26030 if (l == 0)
26031 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026032 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026033 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026034 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035 return -1;
26036 }
26037 *fnamelen = l;
26038 }
26039 }
26040#endif /* WIN3264 */
26041
26042 /* ":t" - tail, just the basename */
26043 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26044 {
26045 *usedlen += 2;
26046 *fnamelen -= (int)(tail - *fnamep);
26047 *fnamep = tail;
26048 }
26049
26050 /* ":e" - extension, can be repeated */
26051 /* ":r" - root, without extension, can be repeated */
26052 while (src[*usedlen] == ':'
26053 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26054 {
26055 /* find a '.' in the tail:
26056 * - for second :e: before the current fname
26057 * - otherwise: The last '.'
26058 */
26059 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26060 s = *fnamep - 2;
26061 else
26062 s = *fnamep + *fnamelen - 1;
26063 for ( ; s > tail; --s)
26064 if (s[0] == '.')
26065 break;
26066 if (src[*usedlen + 1] == 'e') /* :e */
26067 {
26068 if (s > tail)
26069 {
26070 *fnamelen += (int)(*fnamep - (s + 1));
26071 *fnamep = s + 1;
26072#ifdef VMS
26073 /* cut version from the extension */
26074 s = *fnamep + *fnamelen - 1;
26075 for ( ; s > *fnamep; --s)
26076 if (s[0] == ';')
26077 break;
26078 if (s > *fnamep)
26079 *fnamelen = s - *fnamep;
26080#endif
26081 }
26082 else if (*fnamep <= tail)
26083 *fnamelen = 0;
26084 }
26085 else /* :r */
26086 {
26087 if (s > tail) /* remove one extension */
26088 *fnamelen = (int)(s - *fnamep);
26089 }
26090 *usedlen += 2;
26091 }
26092
26093 /* ":s?pat?foo?" - substitute */
26094 /* ":gs?pat?foo?" - global substitute */
26095 if (src[*usedlen] == ':'
26096 && (src[*usedlen + 1] == 's'
26097 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26098 {
26099 char_u *str;
26100 char_u *pat;
26101 char_u *sub;
26102 int sep;
26103 char_u *flags;
26104 int didit = FALSE;
26105
26106 flags = (char_u *)"";
26107 s = src + *usedlen + 2;
26108 if (src[*usedlen + 1] == 'g')
26109 {
26110 flags = (char_u *)"g";
26111 ++s;
26112 }
26113
26114 sep = *s++;
26115 if (sep)
26116 {
26117 /* find end of pattern */
26118 p = vim_strchr(s, sep);
26119 if (p != NULL)
26120 {
26121 pat = vim_strnsave(s, (int)(p - s));
26122 if (pat != NULL)
26123 {
26124 s = p + 1;
26125 /* find end of substitution */
26126 p = vim_strchr(s, sep);
26127 if (p != NULL)
26128 {
26129 sub = vim_strnsave(s, (int)(p - s));
26130 str = vim_strnsave(*fnamep, *fnamelen);
26131 if (sub != NULL && str != NULL)
26132 {
26133 *usedlen = (int)(p + 1 - src);
26134 s = do_string_sub(str, pat, sub, flags);
26135 if (s != NULL)
26136 {
26137 *fnamep = s;
26138 *fnamelen = (int)STRLEN(s);
26139 vim_free(*bufp);
26140 *bufp = s;
26141 didit = TRUE;
26142 }
26143 }
26144 vim_free(sub);
26145 vim_free(str);
26146 }
26147 vim_free(pat);
26148 }
26149 }
26150 /* after using ":s", repeat all the modifiers */
26151 if (didit)
26152 goto repeat;
26153 }
26154 }
26155
Bram Moolenaar26df0922014-02-23 23:39:13 +010026156 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26157 {
26158 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26159 if (p == NULL)
26160 return -1;
26161 vim_free(*bufp);
26162 *bufp = *fnamep = p;
26163 *fnamelen = (int)STRLEN(p);
26164 *usedlen += 2;
26165 }
26166
Bram Moolenaar071d4272004-06-13 20:20:40 +000026167 return valid;
26168}
26169
26170/*
26171 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26172 * "flags" can be "g" to do a global substitute.
26173 * Returns an allocated string, NULL for error.
26174 */
26175 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026176do_string_sub(
26177 char_u *str,
26178 char_u *pat,
26179 char_u *sub,
26180 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026181{
26182 int sublen;
26183 regmatch_T regmatch;
26184 int i;
26185 int do_all;
26186 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026187 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026188 garray_T ga;
26189 char_u *ret;
26190 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026191 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192
26193 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26194 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026195 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026196
26197 ga_init2(&ga, 1, 200);
26198
26199 do_all = (flags[0] == 'g');
26200
26201 regmatch.rm_ic = p_ic;
26202 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26203 if (regmatch.regprog != NULL)
26204 {
26205 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026206 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026207 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26208 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026209 /* Skip empty match except for first match. */
26210 if (regmatch.startp[0] == regmatch.endp[0])
26211 {
26212 if (zero_width == regmatch.startp[0])
26213 {
26214 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026215 i = MB_PTR2LEN(tail);
26216 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26217 (size_t)i);
26218 ga.ga_len += i;
26219 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026220 continue;
26221 }
26222 zero_width = regmatch.startp[0];
26223 }
26224
Bram Moolenaar071d4272004-06-13 20:20:40 +000026225 /*
26226 * Get some space for a temporary buffer to do the substitution
26227 * into. It will contain:
26228 * - The text up to where the match is.
26229 * - The substituted text.
26230 * - The text after the match.
26231 */
26232 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026233 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026234 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26235 {
26236 ga_clear(&ga);
26237 break;
26238 }
26239
26240 /* copy the text up to where the match is */
26241 i = (int)(regmatch.startp[0] - tail);
26242 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26243 /* add the substituted text */
26244 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26245 + ga.ga_len + i, TRUE, TRUE, FALSE);
26246 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026247 tail = regmatch.endp[0];
26248 if (*tail == NUL)
26249 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026250 if (!do_all)
26251 break;
26252 }
26253
26254 if (ga.ga_data != NULL)
26255 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26256
Bram Moolenaar473de612013-06-08 18:19:48 +020026257 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026258 }
26259
26260 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26261 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026262 if (p_cpo == empty_option)
26263 p_cpo = save_cpo;
26264 else
26265 /* Darn, evaluating {sub} expression changed the value. */
26266 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026267
26268 return ret;
26269}
26270
26271#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */