blob: a5c015d6a9dd76789f3505f6dd8f34b5dae41c92 [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 Moolenaar975b5272016-03-15 23:10:59 +0100796#ifdef FEAT_TIMERS
797static void f_timer_start(typval_T *argvars, typval_T *rettv);
798static void f_timer_stop(typval_T *argvars, typval_T *rettv);
799#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_tolower(typval_T *argvars, typval_T *rettv);
801static void f_toupper(typval_T *argvars, typval_T *rettv);
802static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000803#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100804static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000805#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100806static void f_type(typval_T *argvars, typval_T *rettv);
807static void f_undofile(typval_T *argvars, typval_T *rettv);
808static void f_undotree(typval_T *argvars, typval_T *rettv);
809static void f_uniq(typval_T *argvars, typval_T *rettv);
810static void f_values(typval_T *argvars, typval_T *rettv);
811static void f_virtcol(typval_T *argvars, typval_T *rettv);
812static void f_visualmode(typval_T *argvars, typval_T *rettv);
813static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100814static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100815static void f_win_getid(typval_T *argvars, typval_T *rettv);
816static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
817static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
818static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static void f_winbufnr(typval_T *argvars, typval_T *rettv);
820static void f_wincol(typval_T *argvars, typval_T *rettv);
821static void f_winheight(typval_T *argvars, typval_T *rettv);
822static void f_winline(typval_T *argvars, typval_T *rettv);
823static void f_winnr(typval_T *argvars, typval_T *rettv);
824static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
825static void f_winrestview(typval_T *argvars, typval_T *rettv);
826static void f_winsaveview(typval_T *argvars, typval_T *rettv);
827static void f_winwidth(typval_T *argvars, typval_T *rettv);
828static void f_writefile(typval_T *argvars, typval_T *rettv);
829static void f_wordcount(typval_T *argvars, typval_T *rettv);
830static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000831
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
833static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
834static int get_env_len(char_u **arg);
835static int get_id_len(char_u **arg);
836static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
837static 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 +0000838#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
839#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
840 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
842static int eval_isnamec(int c);
843static int eval_isnamec1(int c);
844static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
845static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static typval_T *alloc_string_tv(char_u *string);
847static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100848#ifdef FEAT_FLOAT
849static float_T get_tv_float(typval_T *varp);
850#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100851static linenr_T get_tv_lnum(typval_T *argvars);
852static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100853static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
854static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
855static hashtab_T *find_var_ht(char_u *name, char_u **varname);
856static funccall_T *get_funccal(void);
857static void vars_clear_ext(hashtab_T *ht, int free_val);
858static void delete_var(hashtab_T *ht, hashitem_T *hi);
859static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
860static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
861static void set_var(char_u *name, typval_T *varp, int copy);
862static int var_check_ro(int flags, char_u *name, int use_gettext);
863static int var_check_fixed(int flags, char_u *name, int use_gettext);
864static int var_check_func_name(char_u *name, int new_var);
865static int valid_varname(char_u *varname);
866static int tv_check_lock(int lock, char_u *name, int use_gettext);
867static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
868static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100869static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100870static int eval_fname_script(char_u *p);
871static int eval_fname_sid(char_u *p);
872static void list_func_head(ufunc_T *fp, int indent);
873static ufunc_T *find_func(char_u *name);
874static int function_exists(char_u *name);
875static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000876#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100877static void func_do_profile(ufunc_T *fp);
878static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
879static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000880static int
881# ifdef __BORLANDC__
882 _RTLENTRYF
883# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000885static int
886# ifdef __BORLANDC__
887 _RTLENTRYF
888# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100889 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100891static int script_autoload(char_u *name, int reload);
892static char_u *autoload_name(char_u *name);
893static void cat_func_name(char_u *buf, ufunc_T *fp);
894static void func_free(ufunc_T *fp);
895static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
896static int can_free_funccal(funccall_T *fc, int copyID) ;
897static void free_funccal(funccall_T *fc, int free_val);
898static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
899static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
900static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
901static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
902static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
903static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
904static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
905static int write_list(FILE *fd, list_T *list, int binary);
906static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000907
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200908
909#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100910static int compare_func_name(const void *s1, const void *s2);
911static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200912#endif
913
Bram Moolenaar33570922005-01-25 22:26:29 +0000914/*
915 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000916 */
917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100918eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000919{
Bram Moolenaar33570922005-01-25 22:26:29 +0000920 int i;
921 struct vimvar *p;
922
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200923 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
924 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200925 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000926 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000927 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000928
929 for (i = 0; i < VV_LEN; ++i)
930 {
931 p = &vimvars[i];
932 STRCPY(p->vv_di.di_key, p->vv_name);
933 if (p->vv_flags & VV_RO)
934 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
935 else if (p->vv_flags & VV_RO_SBX)
936 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
937 else
938 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000939
940 /* add to v: scope dict, unless the value is not always available */
941 if (p->vv_type != VAR_UNKNOWN)
942 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000944 /* add to compat scope dict */
945 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100947 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
948
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000949 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100950 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200951 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100952 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100953
954 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
955 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
956 set_vim_var_nr(VV_NONE, VVAL_NONE);
957 set_vim_var_nr(VV_NULL, VVAL_NULL);
958
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200959 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200960
961#ifdef EBCDIC
962 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100963 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200964 */
965 sortFunctions();
966#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000967}
968
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969#if defined(EXITFREE) || defined(PROTO)
970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100971eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972{
973 int i;
974 struct vimvar *p;
975
976 for (i = 0; i < VV_LEN; ++i)
977 {
978 p = &vimvars[i];
979 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000980 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000981 vim_free(p->vv_str);
982 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000983 }
984 else if (p->vv_di.di_tv.v_type == VAR_LIST)
985 {
986 list_unref(p->vv_list);
987 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000988 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989 }
990 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000991 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992 hash_clear(&compat_hashtab);
993
Bram Moolenaard9fba312005-06-26 22:34:35 +0000994 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100995# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200996 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100997# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000998
999 /* global variables */
1000 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001001
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001002 /* autoloaded script names */
1003 ga_clear_strings(&ga_loaded);
1004
Bram Moolenaarcca74132013-09-25 21:00:28 +02001005 /* Script-local variables. First clear all the variables and in a second
1006 * loop free the scriptvar_T, because a variable in one script might hold
1007 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001008 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001010 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001011 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001012 ga_clear(&ga_scripts);
1013
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001014 /* unreferenced lists and dicts */
1015 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001016
1017 /* functions */
1018 free_all_functions();
1019 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001020}
1021#endif
1022
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 * Return the name of the executed function.
1025 */
1026 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001027func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
1031
1032/*
1033 * Return the address holding the next breakpoint line for a funccall cookie.
1034 */
1035 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001036func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
Bram Moolenaar33570922005-01-25 22:26:29 +00001038 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039}
1040
1041/*
1042 * Return the address holding the debug tick for a funccall cookie.
1043 */
1044 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001045func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046{
Bram Moolenaar33570922005-01-25 22:26:29 +00001047 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048}
1049
1050/*
1051 * Return the nesting level for a funccall cookie.
1052 */
1053 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001054func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
Bram Moolenaar33570922005-01-25 22:26:29 +00001056 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057}
1058
1059/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001060funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001062/* pointer to list of previously used funccal, still around because some
1063 * item in it is still being used. */
1064funccall_T *previous_funccal = NULL;
1065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066/*
1067 * Return TRUE when a function was ended by a ":return" command.
1068 */
1069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001070current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 return current_funccal->returned;
1073}
1074
1075
1076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * Set an internal variable to a string value. Creates the variable if it does
1078 * not already exist.
1079 */
1080 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001081set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001083 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001084 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086 val = vim_strsave(value);
1087 if (val != NULL)
1088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001089 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001090 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001092 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001093 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 }
1095 }
1096}
1097
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100static char_u *redir_endp = NULL;
1101static char_u *redir_varname = NULL;
1102
1103/*
1104 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001105 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 * Returns OK if successfully completed the setup. FAIL otherwise.
1107 */
1108 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001109var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110{
1111 int save_emsg;
1112 int err;
1113 typval_T tv;
1114
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001115 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001116 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 {
1118 EMSG(_(e_invarg));
1119 return FAIL;
1120 }
1121
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001122 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 redir_varname = vim_strsave(name);
1124 if (redir_varname == NULL)
1125 return FAIL;
1126
1127 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1128 if (redir_lval == NULL)
1129 {
1130 var_redir_stop();
1131 return FAIL;
1132 }
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 /* The output is stored in growarray "redir_ga" until redirection ends. */
1135 ga_init2(&redir_ga, (int)sizeof(char), 500);
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001138 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001139 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1141 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001142 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 if (redir_endp != NULL && *redir_endp != NUL)
1144 /* Trailing characters are present after the variable name */
1145 EMSG(_(e_trailing));
1146 else
1147 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001148 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 var_redir_stop();
1150 return FAIL;
1151 }
1152
1153 /* check if we can write to the variable: set it to or append an empty
1154 * string */
1155 save_emsg = did_emsg;
1156 did_emsg = FALSE;
1157 tv.v_type = VAR_STRING;
1158 tv.vval.v_string = (char_u *)"";
1159 if (append)
1160 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1161 else
1162 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001163 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001165 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 if (err)
1167 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001168 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 var_redir_stop();
1170 return FAIL;
1171 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172
1173 return OK;
1174}
1175
1176/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177 * Append "value[value_len]" to the variable set by var_redir_start().
1178 * The actual appending is postponed until redirection ends, because the value
1179 * appended may in fact be the string we write to, changing it may cause freed
1180 * memory to be used:
1181 * :redir => foo
1182 * :let foo
1183 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184 */
1185 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001186var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189
1190 if (redir_lval == NULL)
1191 return;
1192
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001194 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001196 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001199 {
1200 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001201 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202 }
1203 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205}
1206
1207/*
1208 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001209 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210 */
1211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001212var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001214 typval_T tv;
1215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 if (redir_lval != NULL)
1217 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001218 /* If there was no error: assign the text to the variable. */
1219 if (redir_endp != NULL)
1220 {
1221 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1222 tv.v_type = VAR_STRING;
1223 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001224 /* Call get_lval() again, if it's inside a Dict or List it may
1225 * have changed. */
1226 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001227 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001228 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1229 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1230 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001231 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001233 /* free the collected output */
1234 vim_free(redir_ga.ga_data);
1235 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001236
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237 vim_free(redir_lval);
1238 redir_lval = NULL;
1239 }
1240 vim_free(redir_varname);
1241 redir_varname = NULL;
1242}
1243
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244# if defined(FEAT_MBYTE) || defined(PROTO)
1245 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001246eval_charconvert(
1247 char_u *enc_from,
1248 char_u *enc_to,
1249 char_u *fname_from,
1250 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251{
1252 int err = FALSE;
1253
1254 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1255 set_vim_var_string(VV_CC_TO, enc_to, -1);
1256 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1257 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1258 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1259 err = TRUE;
1260 set_vim_var_string(VV_CC_FROM, NULL, -1);
1261 set_vim_var_string(VV_CC_TO, NULL, -1);
1262 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1263 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1264
1265 if (err)
1266 return FAIL;
1267 return OK;
1268}
1269# endif
1270
1271# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001273eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 int err = FALSE;
1276
1277 set_vim_var_string(VV_FNAME_IN, fname, -1);
1278 set_vim_var_string(VV_CMDARG, args, -1);
1279 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1280 err = TRUE;
1281 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1282 set_vim_var_string(VV_CMDARG, NULL, -1);
1283
1284 if (err)
1285 {
1286 mch_remove(fname);
1287 return FAIL;
1288 }
1289 return OK;
1290}
1291# endif
1292
1293# if defined(FEAT_DIFF) || defined(PROTO)
1294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001295eval_diff(
1296 char_u *origfile,
1297 char_u *newfile,
1298 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 int err = FALSE;
1301
1302 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1303 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1304 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1305 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1306 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1307 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1308 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1309}
1310
1311 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001312eval_patch(
1313 char_u *origfile,
1314 char_u *difffile,
1315 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 int err;
1318
1319 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1320 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1321 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1322 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1323 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1324 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1325 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1326}
1327# endif
1328
1329/*
1330 * Top level evaluation function, returning a boolean.
1331 * Sets "error" to TRUE if there was an error.
1332 * Return TRUE or FALSE.
1333 */
1334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001335eval_to_bool(
1336 char_u *arg,
1337 int *error,
1338 char_u **nextcmd,
1339 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 int retval = FALSE;
1343
1344 if (skip)
1345 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 else
1349 {
1350 *error = FALSE;
1351 if (!skip)
1352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001353 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 }
1357 if (skip)
1358 --emsg_skip;
1359
1360 return retval;
1361}
1362
1363/*
1364 * Top level evaluation function, returning a string. If "skip" is TRUE,
1365 * only parsing to "nextcmd" is done, without reporting errors. Return
1366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1367 */
1368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001369eval_to_string_skip(
1370 char_u *arg,
1371 char_u **nextcmd,
1372 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
Bram Moolenaar33570922005-01-25 22:26:29 +00001374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 char_u *retval;
1376
1377 if (skip)
1378 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 retval = NULL;
1381 else
1382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 retval = vim_strsave(get_tv_string(&tv));
1384 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 if (skip)
1387 --emsg_skip;
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001393 * Skip over an expression at "*pp".
1394 * Return FAIL for an error, OK otherwise.
1395 */
1396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001397skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001398{
Bram Moolenaar33570922005-01-25 22:26:29 +00001399 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001400
1401 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001403}
1404
1405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001407 * When "convert" is TRUE convert a List into a sequence of lines and convert
1408 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 * Return pointer to allocated memory, or NULL for failure.
1410 */
1411 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001412eval_to_string(
1413 char_u *arg,
1414 char_u **nextcmd,
1415 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001419 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001420#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001421 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 retval = NULL;
1426 else
1427 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001428 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001429 {
1430 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001431 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001432 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001433 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001434 if (tv.vval.v_list->lv_len > 0)
1435 ga_append(&ga, NL);
1436 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001437 ga_append(&ga, NUL);
1438 retval = (char_u *)ga.ga_data;
1439 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001440#ifdef FEAT_FLOAT
1441 else if (convert && tv.v_type == VAR_FLOAT)
1442 {
1443 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1444 retval = vim_strsave(numbuf);
1445 }
1446#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001447 else
1448 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451
1452 return retval;
1453}
1454
1455/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001456 * Call eval_to_string() without using current local variables and using
1457 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 */
1459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001460eval_to_string_safe(
1461 char_u *arg,
1462 char_u **nextcmd,
1463 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 char_u *retval;
1466 void *save_funccalp;
1467
1468 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001469 if (use_sandbox)
1470 ++sandbox;
1471 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001472 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001473 if (use_sandbox)
1474 --sandbox;
1475 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 restore_funccal(save_funccalp);
1477 return retval;
1478}
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480/*
1481 * Top level evaluation function, returning a number.
1482 * Evaluates "expr" silently.
1483 * Returns -1 for an error.
1484 */
1485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001486eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
Bram Moolenaar33570922005-01-25 22:26:29 +00001488 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001490 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 ++emsg_off;
1493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 retval = -1;
1496 else
1497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001498 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 --emsg_off;
1502
1503 return retval;
1504}
1505
Bram Moolenaara40058a2005-07-11 22:42:07 +00001506/*
1507 * Prepare v: variable "idx" to be used.
1508 * Save the current typeval in "save_tv".
1509 * When not used yet add the variable to the v: hashtable.
1510 */
1511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001512prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001513{
1514 *save_tv = vimvars[idx].vv_tv;
1515 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1516 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1517}
1518
1519/*
1520 * Restore v: variable "idx" to typeval "save_tv".
1521 * When no longer defined, remove the variable from the v: hashtable.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525{
1526 hashitem_T *hi;
1527
Bram Moolenaara40058a2005-07-11 22:42:07 +00001528 vimvars[idx].vv_tv = *save_tv;
1529 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1530 {
1531 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1532 if (HASHITEM_EMPTY(hi))
1533 EMSG2(_(e_intern2), "restore_vimvar()");
1534 else
1535 hash_remove(&vimvarht, hi);
1536 }
1537}
1538
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001539#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540/*
1541 * Evaluate an expression to a list with suggestions.
1542 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001543 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544 */
1545 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001546eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001547{
1548 typval_T save_val;
1549 typval_T rettv;
1550 list_T *list = NULL;
1551 char_u *p = skipwhite(expr);
1552
1553 /* Set "v:val" to the bad word. */
1554 prepare_vimvar(VV_VAL, &save_val);
1555 vimvars[VV_VAL].vv_type = VAR_STRING;
1556 vimvars[VV_VAL].vv_str = badword;
1557 if (p_verbose == 0)
1558 ++emsg_off;
1559
1560 if (eval1(&p, &rettv, TRUE) == OK)
1561 {
1562 if (rettv.v_type != VAR_LIST)
1563 clear_tv(&rettv);
1564 else
1565 list = rettv.vval.v_list;
1566 }
1567
1568 if (p_verbose == 0)
1569 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001570 restore_vimvar(VV_VAL, &save_val);
1571
1572 return list;
1573}
1574
1575/*
1576 * "list" is supposed to contain two items: a word and a number. Return the
1577 * word in "pp" and the number as the return value.
1578 * Return -1 if anything isn't right.
1579 * Used to get the good word and score from the eval_spell_expr() result.
1580 */
1581 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001582get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001583{
1584 listitem_T *li;
1585
1586 li = list->lv_first;
1587 if (li == NULL)
1588 return -1;
1589 *pp = get_tv_string(&li->li_tv);
1590
1591 li = li->li_next;
1592 if (li == NULL)
1593 return -1;
1594 return get_tv_number(&li->li_tv);
1595}
1596#endif
1597
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001598/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001599 * Top level evaluation function.
1600 * Returns an allocated typval_T with the result.
1601 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602 */
1603 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001604eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605{
1606 typval_T *tv;
1607
1608 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001609 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610 {
1611 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001612 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001613 }
1614
1615 return tv;
1616}
1617
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621 * Uses argv[argc] for the function arguments. Only Number and String
1622 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001625 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001626call_vim_function(
1627 char_u *func,
1628 int argc,
1629 char_u **argv,
1630 int safe, /* use the sandbox */
1631 int str_arg_only, /* all arguments are strings */
1632 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
Bram Moolenaar33570922005-01-25 22:26:29 +00001634 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 long n;
1636 int len;
1637 int i;
1638 int doesrange;
1639 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001642 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 for (i = 0; i < argc; i++)
1647 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001648 /* Pass a NULL or empty argument as an empty string */
1649 if (argv[i] == NULL || *argv[i] == NUL)
1650 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001651 argvars[i].v_type = VAR_STRING;
1652 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001653 continue;
1654 }
1655
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001656 if (str_arg_only)
1657 len = 0;
1658 else
1659 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001660 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (len != 0 && len == (int)STRLEN(argv[i]))
1662 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001663 argvars[i].v_type = VAR_NUMBER;
1664 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 else
1667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001668 argvars[i].v_type = VAR_STRING;
1669 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
1671 }
1672
1673 if (safe)
1674 {
1675 save_funccalp = save_funccal();
1676 ++sandbox;
1677 }
1678
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1680 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001682 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (safe)
1684 {
1685 --sandbox;
1686 restore_funccal(save_funccalp);
1687 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 vim_free(argvars);
1689
1690 if (ret == FAIL)
1691 clear_tv(rettv);
1692
1693 return ret;
1694}
1695
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001696/*
1697 * Call vimL function "func" and return the result as a number.
1698 * Returns -1 when calling the function fails.
1699 * Uses argv[argc] for the function arguments.
1700 */
1701 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001702call_func_retnr(
1703 char_u *func,
1704 int argc,
1705 char_u **argv,
1706 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001707{
1708 typval_T rettv;
1709 long retval;
1710
1711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1713 return -1;
1714
1715 retval = get_tv_number_chk(&rettv, NULL);
1716 clear_tv(&rettv);
1717 return retval;
1718}
1719
1720#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1721 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1722
Bram Moolenaar4f688582007-07-24 12:34:30 +00001723# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001725 * Call vimL function "func" and return the result as a string.
1726 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727 * Uses argv[argc] for the function arguments.
1728 */
1729 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001730call_func_retstr(
1731 char_u *func,
1732 int argc,
1733 char_u **argv,
1734 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735{
1736 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001737 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001739 /* All arguments are passed as strings, no conversion to number. */
1740 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 return NULL;
1742
1743 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 return retval;
1746}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001747# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001749/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001750 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001752 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001753 */
1754 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001755call_func_retlist(
1756 char_u *func,
1757 int argc,
1758 char_u **argv,
1759 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760{
1761 typval_T rettv;
1762
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001763 /* All arguments are passed as strings, no conversion to number. */
1764 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001765 return NULL;
1766
1767 if (rettv.v_type != VAR_LIST)
1768 {
1769 clear_tv(&rettv);
1770 return NULL;
1771 }
1772
1773 return rettv.vval.v_list;
1774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
1776
1777/*
1778 * Save the current function call pointer, and set it to NULL.
1779 * Used when executing autocommands and for ":source".
1780 */
1781 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001782save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001784 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 current_funccal = NULL;
1787 return (void *)fc;
1788}
1789
1790 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001791restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001793 funccall_T *fc = (funccall_T *)vfc;
1794
1795 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796}
1797
Bram Moolenaar05159a02005-02-26 23:04:13 +00001798#if defined(FEAT_PROFILE) || defined(PROTO)
1799/*
1800 * Prepare profiling for entering a child or something else that is not
1801 * counted for the script/function itself.
1802 * Should always be called in pair with prof_child_exit().
1803 */
1804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805prof_child_enter(
1806 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001807{
1808 funccall_T *fc = current_funccal;
1809
1810 if (fc != NULL && fc->func->uf_profiling)
1811 profile_start(&fc->prof_child);
1812 script_prof_save(tm);
1813}
1814
1815/*
1816 * Take care of time spent in a child.
1817 * Should always be called after prof_child_enter().
1818 */
1819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820prof_child_exit(
1821 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001822{
1823 funccall_T *fc = current_funccal;
1824
1825 if (fc != NULL && fc->func->uf_profiling)
1826 {
1827 profile_end(&fc->prof_child);
1828 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1829 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1830 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1831 }
1832 script_prof_restore(tm);
1833}
1834#endif
1835
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837#ifdef FEAT_FOLDING
1838/*
1839 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1840 * it in "*cp". Doesn't give error messages.
1841 */
1842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001843eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int retval;
1847 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001848 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1849 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001852 if (use_sandbox)
1853 ++sandbox;
1854 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 retval = 0;
1858 else
1859 {
1860 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 if (tv.v_type == VAR_NUMBER)
1862 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001863 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 retval = 0;
1865 else
1866 {
1867 /* If the result is a string, check if there is a non-digit before
1868 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (!VIM_ISDIGIT(*s) && *s != '-')
1871 *cp = *s++;
1872 retval = atol((char *)s);
1873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001877 if (use_sandbox)
1878 --sandbox;
1879 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
1881 return retval;
1882}
1883#endif
1884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 * ":let" list all variable values
1887 * ":let var1 var2" list variable values
1888 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 * ":let var += expr" assignment command.
1890 * ":let var -= expr" assignment command.
1891 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 */
1894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001895ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 int var_count = 0;
1902 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001904 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001905 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
Bram Moolenaardb552d602006-03-23 22:59:57 +00001907 argend = skip_var_list(arg, &var_count, &semicolon);
1908 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001910 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1911 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001912 expr = skipwhite(argend);
1913 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1914 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001916 /*
1917 * ":let" without "=": list variables
1918 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 if (*arg == '[')
1920 EMSG(_(e_invarg));
1921 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001927 list_glob_vars(&first);
1928 list_buf_vars(&first);
1929 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001930#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001931 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001932#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001933 list_script_vars(&first);
1934 list_func_vars(&first);
1935 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 eap->nextcmd = check_nextcmd(arg);
1938 }
1939 else
1940 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 op[0] = '=';
1942 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001943 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001945 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1946 op[0] = *expr; /* +=, -= or .= */
1947 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001949 else
1950 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (eap->skip)
1953 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (eap->skip)
1956 {
1957 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 --emsg_skip;
1960 }
1961 else if (i != FAIL)
1962 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001965 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
1967 }
1968}
1969
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970/*
1971 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1972 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 * When "nextchars" is not NULL it points to a string with characters that
1974 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1975 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976 * Returns OK or FAIL;
1977 */
1978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001979ex_let_vars(
1980 char_u *arg_start,
1981 typval_T *tv,
1982 int copy, /* copy values from "tv", don't move */
1983 int semicolon, /* from skip_var_list() */
1984 int var_count, /* from skip_var_list() */
1985 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986{
1987 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001988 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001990 listitem_T *item;
1991 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992
1993 if (*arg != '[')
1994 {
1995 /*
1996 * ":let var = expr" or ":for var in list"
1997 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 return FAIL;
2000 return OK;
2001 }
2002
2003 /*
2004 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2005 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002006 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 {
2008 EMSG(_(e_listreq));
2009 return FAIL;
2010 }
2011
2012 i = list_len(l);
2013 if (semicolon == 0 && var_count < i)
2014 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002015 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 return FAIL;
2017 }
2018 if (var_count - semicolon > i)
2019 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002020 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 return FAIL;
2022 }
2023
2024 item = l->lv_first;
2025 while (*arg != ']')
2026 {
2027 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 item = item->li_next;
2030 if (arg == NULL)
2031 return FAIL;
2032
2033 arg = skipwhite(arg);
2034 if (*arg == ';')
2035 {
2036 /* Put the rest of the list (may be empty) in the var after ';'.
2037 * Create a new list for this. */
2038 l = list_alloc();
2039 if (l == NULL)
2040 return FAIL;
2041 while (item != NULL)
2042 {
2043 list_append_tv(l, &item->li_tv);
2044 item = item->li_next;
2045 }
2046
2047 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002048 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002049 ltv.vval.v_list = l;
2050 l->lv_refcount = 1;
2051
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002052 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2053 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 clear_tv(&ltv);
2055 if (arg == NULL)
2056 return FAIL;
2057 break;
2058 }
2059 else if (*arg != ',' && *arg != ']')
2060 {
2061 EMSG2(_(e_intern2), "ex_let_vars()");
2062 return FAIL;
2063 }
2064 }
2065
2066 return OK;
2067}
2068
2069/*
2070 * Skip over assignable variable "var" or list of variables "[var, var]".
2071 * Used for ":let varvar = expr" and ":for varvar in expr".
2072 * For "[var, var]" increment "*var_count" for each variable.
2073 * for "[var, var; var]" set "semicolon".
2074 * Return NULL for an error.
2075 */
2076 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002077skip_var_list(
2078 char_u *arg,
2079 int *var_count,
2080 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081{
2082 char_u *p, *s;
2083
2084 if (*arg == '[')
2085 {
2086 /* "[var, var]": find the matching ']'. */
2087 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002088 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002089 {
2090 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2091 s = skip_var_one(p);
2092 if (s == p)
2093 {
2094 EMSG2(_(e_invarg2), p);
2095 return NULL;
2096 }
2097 ++*var_count;
2098
2099 p = skipwhite(s);
2100 if (*p == ']')
2101 break;
2102 else if (*p == ';')
2103 {
2104 if (*semicolon == 1)
2105 {
2106 EMSG(_("Double ; in list of variables"));
2107 return NULL;
2108 }
2109 *semicolon = 1;
2110 }
2111 else if (*p != ',')
2112 {
2113 EMSG2(_(e_invarg2), p);
2114 return NULL;
2115 }
2116 }
2117 return p + 1;
2118 }
2119 else
2120 return skip_var_one(arg);
2121}
2122
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002124 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002125 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002128skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002130 if (*arg == '@' && arg[1] != NUL)
2131 return arg + 2;
2132 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2133 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002134}
2135
Bram Moolenaara7043832005-01-21 11:56:39 +00002136/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002137 * List variables for hashtab "ht" with prefix "prefix".
2138 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002141list_hashtable_vars(
2142 hashtab_T *ht,
2143 char_u *prefix,
2144 int empty,
2145 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002146{
Bram Moolenaar33570922005-01-25 22:26:29 +00002147 hashitem_T *hi;
2148 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 int todo;
2150
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002151 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002152 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2153 {
2154 if (!HASHITEM_EMPTY(hi))
2155 {
2156 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002157 di = HI2DI(hi);
2158 if (empty || di->di_tv.v_type != VAR_STRING
2159 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161 }
2162 }
2163}
2164
2165/*
2166 * List global variables.
2167 */
2168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002169list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002170{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002172}
2173
2174/*
2175 * List buffer variables.
2176 */
2177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002179{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002180 char_u numbuf[NUMBUFLEN];
2181
Bram Moolenaar429fa852013-04-15 12:27:36 +02002182 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002184
2185 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2187 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002188}
2189
2190/*
2191 * List window variables.
2192 */
2193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002194list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002195{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002196 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002198}
2199
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002200#ifdef FEAT_WINDOWS
2201/*
2202 * List tab page variables.
2203 */
2204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002205list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002206{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002207 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002209}
2210#endif
2211
Bram Moolenaara7043832005-01-21 11:56:39 +00002212/*
2213 * List Vim variables.
2214 */
2215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002216list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219}
2220
2221/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002222 * List script-local variables, if there is a script.
2223 */
2224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002225list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002226{
2227 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2229 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002230}
2231
2232/*
2233 * List function variables, if there is a function.
2234 */
2235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002236list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002237{
2238 if (current_funccal != NULL)
2239 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002241}
2242
2243/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 * List variables in "arg".
2245 */
2246 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002247list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248{
2249 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 char_u *name_start;
2253 char_u *arg_subsc;
2254 char_u *tofree;
2255 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256
2257 while (!ends_excmd(*arg) && !got_int)
2258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002261 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2263 {
2264 emsg_severe = TRUE;
2265 EMSG(_(e_trailing));
2266 break;
2267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 /* get_name_len() takes care of expanding curly braces */
2272 name_start = name = arg;
2273 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2274 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 /* This is mainly to keep test 49 working: when expanding
2277 * curly braces fails overrule the exception error message. */
2278 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 emsg_severe = TRUE;
2281 EMSG2(_(e_invarg2), arg);
2282 break;
2283 }
2284 error = TRUE;
2285 }
2286 else
2287 {
2288 if (tofree != NULL)
2289 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002290 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 else
2293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 /* handle d.key, l[idx], f(expr) */
2295 arg_subsc = arg;
2296 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 case 'g': list_glob_vars(first); break;
2305 case 'b': list_buf_vars(first); break;
2306 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002307#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002308 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002309#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002310 case 'v': list_vim_vars(first); break;
2311 case 's': list_script_vars(first); break;
2312 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 default:
2314 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002315 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002316 }
2317 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 {
2319 char_u numbuf[NUMBUFLEN];
2320 char_u *tf;
2321 int c;
2322 char_u *s;
2323
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002324 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 c = *arg;
2326 *arg = NUL;
2327 list_one_var_a((char_u *)"",
2328 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002329 tv.v_type,
2330 s == NULL ? (char_u *)"" : s,
2331 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332 *arg = c;
2333 vim_free(tf);
2334 }
2335 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 }
2338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339
2340 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342
2343 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
2345
2346 return arg;
2347}
2348
2349/*
2350 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2351 * Returns a pointer to the char just after the var name.
2352 * Returns NULL if there is an error.
2353 */
2354 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002355ex_let_one(
2356 char_u *arg, /* points to variable name */
2357 typval_T *tv, /* value to assign to variable */
2358 int copy, /* copy value from "tv" */
2359 char_u *endchars, /* valid chars after variable name or NULL */
2360 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361{
2362 int c1;
2363 char_u *name;
2364 char_u *p;
2365 char_u *arg_end = NULL;
2366 int len;
2367 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369
2370 /*
2371 * ":let $VAR = expr": Set environment variable.
2372 */
2373 if (*arg == '$')
2374 {
2375 /* Find the end of the name. */
2376 ++arg;
2377 name = arg;
2378 len = get_env_len(&arg);
2379 if (len == 0)
2380 EMSG2(_(e_invarg2), name - 1);
2381 else
2382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 if (op != NULL && (*op == '+' || *op == '-'))
2384 EMSG2(_(e_letwrong), op);
2385 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002386 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002388 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 {
2390 c1 = name[len];
2391 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 p = get_tv_string_chk(tv);
2393 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 {
2395 int mustfree = FALSE;
2396 char_u *s = vim_getenv(name, &mustfree);
2397
2398 if (s != NULL)
2399 {
2400 p = tofree = concat_str(s, p);
2401 if (mustfree)
2402 vim_free(s);
2403 }
2404 }
2405 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 if (STRICMP(name, "HOME") == 0)
2409 init_homedir();
2410 else if (didset_vim && STRICMP(name, "VIM") == 0)
2411 didset_vim = FALSE;
2412 else if (didset_vimruntime
2413 && STRICMP(name, "VIMRUNTIME") == 0)
2414 didset_vimruntime = FALSE;
2415 arg_end = arg;
2416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 }
2420 }
2421 }
2422
2423 /*
2424 * ":let &option = expr": Set option value.
2425 * ":let &l:option = expr": Set local option value.
2426 * ":let &g:option = expr": Set global option value.
2427 */
2428 else if (*arg == '&')
2429 {
2430 /* Find the end of the name. */
2431 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 if (p == NULL || (endchars != NULL
2433 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 EMSG(_(e_letunexp));
2435 else
2436 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 long n;
2438 int opt_type;
2439 long numval;
2440 char_u *stringval = NULL;
2441 char_u *s;
2442
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002443 c1 = *p;
2444 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445
2446 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 s = get_tv_string_chk(tv); /* != NULL if number or string */
2448 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 {
2450 opt_type = get_option_value(arg, &numval,
2451 &stringval, opt_flags);
2452 if ((opt_type == 1 && *op == '.')
2453 || (opt_type == 0 && *op != '.'))
2454 EMSG2(_(e_letwrong), op);
2455 else
2456 {
2457 if (opt_type == 1) /* number */
2458 {
2459 if (*op == '+')
2460 n = numval + n;
2461 else
2462 n = numval - n;
2463 }
2464 else if (opt_type == 0 && stringval != NULL) /* string */
2465 {
2466 s = concat_str(stringval, s);
2467 vim_free(stringval);
2468 stringval = s;
2469 }
2470 }
2471 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 if (s != NULL)
2473 {
2474 set_option_value(arg, n, s, opt_flags);
2475 arg_end = p;
2476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480 }
2481
2482 /*
2483 * ":let @r = expr": Set register contents.
2484 */
2485 else if (*arg == '@')
2486 {
2487 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 if (op != NULL && (*op == '+' || *op == '-'))
2489 EMSG2(_(e_letwrong), op);
2490 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002491 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 EMSG(_(e_letunexp));
2493 else
2494 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002495 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 char_u *s;
2497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 p = get_tv_string_chk(tv);
2499 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002501 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 if (s != NULL)
2503 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002504 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 vim_free(s);
2506 }
2507 }
2508 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002509 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002511 arg_end = arg + 1;
2512 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002513 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 }
2515 }
2516
2517 /*
2518 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002521 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002525 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2529 EMSG(_(e_letunexp));
2530 else
2531 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 arg_end = p;
2534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 }
2538
2539 else
2540 EMSG2(_(e_invarg2), arg);
2541
2542 return arg_end;
2543}
2544
2545/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2547 */
2548 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002549check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550{
2551 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2552 {
2553 EMSG2(_(e_readonlyvar), arg);
2554 return TRUE;
2555 }
2556 return FALSE;
2557}
2558
2559/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 * Get an lval: variable, Dict item or List item that can be assigned a value
2561 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2562 * "name.key", "name.key[expr]" etc.
2563 * Indexing only works if "name" is an existing List or Dictionary.
2564 * "name" points to the start of the name.
2565 * If "rettv" is not NULL it points to the value to be assigned.
2566 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2567 * wrong; must end in space or cmd separator.
2568 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 * flags:
2570 * GLV_QUIET: do not give error messages
2571 * GLV_NO_AUTOLOAD: do not use script autoloading
2572 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002574 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 */
2577 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002578get_lval(
2579 char_u *name,
2580 typval_T *rettv,
2581 lval_T *lp,
2582 int unlet,
2583 int skip,
2584 int flags, /* GLV_ values */
2585 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 char_u *expr_start, *expr_end;
2589 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 dictitem_T *v;
2591 typval_T var1;
2592 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002598 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002601 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602
2603 if (skip)
2604 {
2605 /* When skipping just find the end of the name. */
2606 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002607 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 }
2609
2610 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002611 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (expr_start != NULL)
2613 {
2614 /* Don't expand the name when we already know there is an error. */
2615 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2616 && *p != '[' && *p != '.')
2617 {
2618 EMSG(_(e_trailing));
2619 return NULL;
2620 }
2621
2622 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2623 if (lp->ll_exp_name == NULL)
2624 {
2625 /* Report an invalid expression in braces, unless the
2626 * expression evaluation has been cancelled due to an
2627 * aborting error, an interrupt, or an exception. */
2628 if (!aborting() && !quiet)
2629 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002630 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 EMSG2(_(e_invarg2), name);
2632 return NULL;
2633 }
2634 }
2635 lp->ll_name = lp->ll_exp_name;
2636 }
2637 else
2638 lp->ll_name = name;
2639
2640 /* Without [idx] or .key we are done. */
2641 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2642 return p;
2643
2644 cc = *p;
2645 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002646 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (v == NULL && !quiet)
2648 EMSG2(_(e_undefvar), lp->ll_name);
2649 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 if (v == NULL)
2651 return NULL;
2652
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 /*
2654 * Loop until no more [idx] or .key is following.
2655 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002656 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2660 && !(lp->ll_tv->v_type == VAR_DICT
2661 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E689: Can only index a List or Dictionary"));
2665 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!quiet)
2670 EMSG(_("E708: [:] must come last"));
2671 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002672 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 len = -1;
2675 if (*p == '.')
2676 {
2677 key = p + 1;
2678 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2679 ;
2680 if (len == 0)
2681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_(e_emptykey));
2684 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
2686 p = key + len;
2687 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 else
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 if (*p == ':')
2693 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 else
2695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 empty1 = FALSE;
2697 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002699 if (get_tv_string_chk(&var1) == NULL)
2700 {
2701 /* not a number or string */
2702 clear_tv(&var1);
2703 return NULL;
2704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706
2707 /* Optionally get the second index [ :expr]. */
2708 if (*p == ':')
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 if (!empty1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (rettv != NULL && (rettv->v_type != VAR_LIST
2719 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (!quiet)
2722 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (!empty1)
2724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727 p = skipwhite(p + 1);
2728 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 else
2731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (!empty1)
2736 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002739 if (get_tv_string_chk(&var2) == NULL)
2740 {
2741 /* not a number or string */
2742 if (!empty1)
2743 clear_tv(&var1);
2744 clear_tv(&var2);
2745 return NULL;
2746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (!quiet)
2756 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (!empty1)
2758 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 }
2763
2764 /* Skip to past ']'. */
2765 ++p;
2766 }
2767
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 {
2770 if (len == -1)
2771 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002773 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 if (*key == NUL)
2775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (!quiet)
2777 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 }
2781 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 lp->ll_list = NULL;
2783 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002784 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002785
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002786 /* When assigning to a scope dictionary check that a function and
2787 * variable name is valid (only variable name unless it is l: or
2788 * g: dictionary). Disallow overwriting a builtin function. */
2789 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 int prevval;
2792 int wrong;
2793
2794 if (len != -1)
2795 {
2796 prevval = key[len];
2797 key[len] = NUL;
2798 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002799 else
2800 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2802 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002804 || !valid_varname(key);
2805 if (len != -1)
2806 key[len] = prevval;
2807 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002808 return NULL;
2809 }
2810
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002813 /* Can't add "v:" variable. */
2814 if (lp->ll_dict == &vimvardict)
2815 {
2816 EMSG2(_(e_illvar), name);
2817 return NULL;
2818 }
2819
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002820 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002824 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 if (len == -1)
2826 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 }
2829 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 if (len == -1)
2834 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 p = NULL;
2837 break;
2838 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002839 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002840 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002841 return NULL;
2842
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 if (len == -1)
2844 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 }
2847 else
2848 {
2849 /*
2850 * Get the number and item for the only or first index of the List.
2851 */
2852 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 else
2855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002856 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 clear_tv(&var1);
2858 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002859 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_list = lp->ll_tv->vval.v_list;
2861 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2862 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002864 if (lp->ll_n1 < 0)
2865 {
2866 lp->ll_n1 = 0;
2867 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2868 }
2869 }
2870 if (lp->ll_li == NULL)
2871 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002874 if (!quiet)
2875 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 }
2878
2879 /*
2880 * May need to find the item or absolute index for the second
2881 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 * When no index given: "lp->ll_empty2" is TRUE.
2883 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002887 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 {
2894 if (!quiet)
2895 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002897 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002899 }
2900
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2902 if (lp->ll_n1 < 0)
2903 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2904 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002905 {
2906 if (!quiet)
2907 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002909 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 }
2911
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 }
2915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 return p;
2917}
2918
2919/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 */
2922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002923clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924{
2925 vim_free(lp->ll_exp_name);
2926 vim_free(lp->ll_newkey);
2927}
2928
2929/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002930 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 */
2934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935set_var_lval(
2936 lval_T *lp,
2937 char_u *endp,
2938 typval_T *rettv,
2939 int copy,
2940 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941{
2942 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 listitem_T *ri;
2944 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945
2946 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002949 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 cc = *endp;
2951 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 if (op != NULL && *op != '=')
2953 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002954 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002956 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002957 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002958 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002959 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002961 if ((di == NULL
2962 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2963 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2964 FALSE)))
2965 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002966 set_var(lp->ll_name, &tv, FALSE);
2967 clear_tv(&tv);
2968 }
2969 }
2970 else
2971 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002973 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 else if (tv_check_lock(lp->ll_newkey == NULL
2976 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002977 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002978 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 else if (lp->ll_range)
2980 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002981 listitem_T *ll_li = lp->ll_li;
2982 int ll_n1 = lp->ll_n1;
2983
2984 /*
2985 * Check whether any of the list items is locked
2986 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002987 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002988 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002989 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002990 return;
2991 ri = ri->li_next;
2992 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2993 break;
2994 ll_li = ll_li->li_next;
2995 ++ll_n1;
2996 }
2997
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 /*
2999 * Assign the List values to the list items.
3000 */
3001 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003 if (op != NULL && *op != '=')
3004 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3005 else
3006 {
3007 clear_tv(&lp->ll_li->li_tv);
3008 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 ri = ri->li_next;
3011 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3012 break;
3013 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003016 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 ri = NULL;
3019 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003020 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003021 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003022 lp->ll_li = lp->ll_li->li_next;
3023 ++lp->ll_n1;
3024 }
3025 if (ri != NULL)
3026 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 else if (lp->ll_empty2
3028 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 : lp->ll_n1 != lp->ll_n2)
3030 EMSG(_("E711: List value has not enough items"));
3031 }
3032 else
3033 {
3034 /*
3035 * Assign to a List or Dictionary item.
3036 */
3037 if (lp->ll_newkey != NULL)
3038 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003039 if (op != NULL && *op != '=')
3040 {
3041 EMSG2(_(e_letwrong), op);
3042 return;
3043 }
3044
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 if (di == NULL)
3048 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003049 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3050 {
3051 vim_free(di);
3052 return;
3053 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003055 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003056 else if (op != NULL && *op != '=')
3057 {
3058 tv_op(lp->ll_tv, rettv, op);
3059 return;
3060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003063
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 /*
3065 * Assign the value to the variable or list item.
3066 */
3067 if (copy)
3068 copy_tv(rettv, lp->ll_tv);
3069 else
3070 {
3071 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003072 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003073 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074 }
3075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003076}
3077
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003078/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3080 * Returns OK or FAIL.
3081 */
3082 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003083tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084{
3085 long n;
3086 char_u numbuf[NUMBUFLEN];
3087 char_u *s;
3088
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003089 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3090 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3091 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003092 {
3093 switch (tv1->v_type)
3094 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003095 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 case VAR_DICT:
3097 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003098 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003099 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003100 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003101 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 break;
3103
3104 case VAR_LIST:
3105 if (*op != '+' || tv2->v_type != VAR_LIST)
3106 break;
3107 /* List += List */
3108 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3109 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3110 return OK;
3111
3112 case VAR_NUMBER:
3113 case VAR_STRING:
3114 if (tv2->v_type == VAR_LIST)
3115 break;
3116 if (*op == '+' || *op == '-')
3117 {
3118 /* nr += nr or nr -= nr*/
3119 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003120#ifdef FEAT_FLOAT
3121 if (tv2->v_type == VAR_FLOAT)
3122 {
3123 float_T f = n;
3124
3125 if (*op == '+')
3126 f += tv2->vval.v_float;
3127 else
3128 f -= tv2->vval.v_float;
3129 clear_tv(tv1);
3130 tv1->v_type = VAR_FLOAT;
3131 tv1->vval.v_float = f;
3132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134#endif
3135 {
3136 if (*op == '+')
3137 n += get_tv_number(tv2);
3138 else
3139 n -= get_tv_number(tv2);
3140 clear_tv(tv1);
3141 tv1->v_type = VAR_NUMBER;
3142 tv1->vval.v_number = n;
3143 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003144 }
3145 else
3146 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147 if (tv2->v_type == VAR_FLOAT)
3148 break;
3149
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003150 /* str .= str */
3151 s = get_tv_string(tv1);
3152 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3153 clear_tv(tv1);
3154 tv1->v_type = VAR_STRING;
3155 tv1->vval.v_string = s;
3156 }
3157 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003158
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003160#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161 {
3162 float_T f;
3163
3164 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3165 && tv2->v_type != VAR_NUMBER
3166 && tv2->v_type != VAR_STRING))
3167 break;
3168 if (tv2->v_type == VAR_FLOAT)
3169 f = tv2->vval.v_float;
3170 else
3171 f = get_tv_number(tv2);
3172 if (*op == '+')
3173 tv1->vval.v_float += f;
3174 else
3175 tv1->vval.v_float -= f;
3176 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003178 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003179 }
3180 }
3181
3182 EMSG2(_(e_letwrong), op);
3183 return FAIL;
3184}
3185
3186/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 * Add a watcher to a list.
3188 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003190list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
3192 lw->lw_next = l->lv_watch;
3193 l->lv_watch = lw;
3194}
3195
3196/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003197 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 * No warning when it isn't found...
3199 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003201list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202{
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204
3205 lwp = &l->lv_watch;
3206 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3207 {
3208 if (lw == lwrem)
3209 {
3210 *lwp = lw->lw_next;
3211 break;
3212 }
3213 lwp = &lw->lw_next;
3214 }
3215}
3216
3217/*
3218 * Just before removing an item from a list: advance watchers to the next
3219 * item.
3220 */
3221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223{
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225
3226 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3227 if (lw->lw_item == item)
3228 lw->lw_item = item->li_next;
3229}
3230
3231/*
3232 * Evaluate the expression used in a ":for var in expr" command.
3233 * "arg" points to "var".
3234 * Set "*errp" to TRUE for an error, FALSE otherwise;
3235 * Return a pointer that holds the info. Null when there is an error.
3236 */
3237 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238eval_for_line(
3239 char_u *arg,
3240 int *errp,
3241 char_u **nextcmdp,
3242 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243{
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003246 typval_T tv;
3247 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248
3249 *errp = TRUE; /* default: there is an error */
3250
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 if (fi == NULL)
3253 return NULL;
3254
3255 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3256 if (expr == NULL)
3257 return fi;
3258
3259 expr = skipwhite(expr);
3260 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3261 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003262 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 return fi;
3264 }
3265
3266 if (skip)
3267 ++emsg_skip;
3268 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3269 {
3270 *errp = FALSE;
3271 if (!skip)
3272 {
3273 l = tv.vval.v_list;
3274 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003275 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 clear_tv(&tv);
3278 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 else
3280 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003281 /* No need to increment the refcount, it's already set for the
3282 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 fi->fi_list = l;
3284 list_add_watch(l, &fi->fi_lw);
3285 fi->fi_lw.lw_item = l->lv_first;
3286 }
3287 }
3288 }
3289 if (skip)
3290 --emsg_skip;
3291
3292 return fi;
3293}
3294
3295/*
3296 * Use the first item in a ":for" list. Advance to the next.
3297 * Assign the values to the variable (list). "arg" points to the first one.
3298 * Return TRUE when a valid item was found, FALSE when at end of list or
3299 * something wrong.
3300 */
3301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003304 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307
3308 item = fi->fi_lw.lw_item;
3309 if (item == NULL)
3310 result = FALSE;
3311 else
3312 {
3313 fi->fi_lw.lw_item = item->li_next;
3314 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3315 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3316 }
3317 return result;
3318}
3319
3320/*
3321 * Free the structure used to store info used by ":for".
3322 */
3323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003324free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325{
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003328 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003329 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003331 list_unref(fi->fi_list);
3332 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 vim_free(fi);
3334}
3335
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3337
3338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003339set_context_for_expression(
3340 expand_T *xp,
3341 char_u *arg,
3342 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 int got_eq = FALSE;
3345 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 if (cmdidx == CMD_let)
3349 {
3350 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003351 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 {
3353 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003354 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 {
3356 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003357 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 if (vim_iswhite(*p))
3359 break;
3360 }
3361 return;
3362 }
3363 }
3364 else
3365 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3366 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 while ((xp->xp_pattern = vim_strpbrk(arg,
3368 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3369 {
3370 c = *xp->xp_pattern;
3371 if (c == '&')
3372 {
3373 c = xp->xp_pattern[1];
3374 if (c == '&')
3375 {
3376 ++xp->xp_pattern;
3377 xp->xp_context = cmdidx != CMD_let || got_eq
3378 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3379 }
3380 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003383 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3384 xp->xp_pattern += 2;
3385
3386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
3388 else if (c == '$')
3389 {
3390 /* environment variable */
3391 xp->xp_context = EXPAND_ENV_VARS;
3392 }
3393 else if (c == '=')
3394 {
3395 got_eq = TRUE;
3396 xp->xp_context = EXPAND_EXPRESSION;
3397 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003398 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 && xp->xp_context == EXPAND_FUNCTIONS
3400 && vim_strchr(xp->xp_pattern, '(') == NULL)
3401 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003402 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 break;
3404 }
3405 else if (cmdidx != CMD_let || got_eq)
3406 {
3407 if (c == '"') /* string */
3408 {
3409 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3410 if (c == '\\' && xp->xp_pattern[1] != NUL)
3411 ++xp->xp_pattern;
3412 xp->xp_context = EXPAND_NOTHING;
3413 }
3414 else if (c == '\'') /* literal string */
3415 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003416 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3418 /* skip */ ;
3419 xp->xp_context = EXPAND_NOTHING;
3420 }
3421 else if (c == '|')
3422 {
3423 if (xp->xp_pattern[1] == '|')
3424 {
3425 ++xp->xp_pattern;
3426 xp->xp_context = EXPAND_EXPRESSION;
3427 }
3428 else
3429 xp->xp_context = EXPAND_COMMANDS;
3430 }
3431 else
3432 xp->xp_context = EXPAND_EXPRESSION;
3433 }
3434 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003435 /* Doesn't look like something valid, expand as an expression
3436 * anyway. */
3437 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 arg = xp->xp_pattern;
3439 if (*arg != NUL)
3440 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3441 /* skip */ ;
3442 }
3443 xp->xp_pattern = arg;
3444}
3445
3446#endif /* FEAT_CMDL_COMPL */
3447
3448/*
3449 * ":1,25call func(arg1, arg2)" function call.
3450 */
3451 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003452ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 char_u *arg = eap->arg;
3455 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003457 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 linenr_T lnum;
3461 int doesrange;
3462 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003464 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003466 if (eap->skip)
3467 {
3468 /* trans_function_name() doesn't work well when skipping, use eval0()
3469 * instead to skip to any following command, e.g. for:
3470 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003471 ++emsg_skip;
3472 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3473 clear_tv(&rettv);
3474 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003475 return;
3476 }
3477
Bram Moolenaar65639032016-03-16 21:40:30 +01003478 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003479 if (fudi.fd_newkey != NULL)
3480 {
3481 /* Still need to give an error message for missing key. */
3482 EMSG2(_(e_dictkey), fudi.fd_newkey);
3483 vim_free(fudi.fd_newkey);
3484 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003485 if (tofree == NULL)
3486 return;
3487
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003488 /* Increase refcount on dictionary, it could get deleted when evaluating
3489 * the arguments. */
3490 if (fudi.fd_dict != NULL)
3491 ++fudi.fd_dict->dv_refcount;
3492
Bram Moolenaar65639032016-03-16 21:40:30 +01003493 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3494 * contents. For VAR_PARTIAL get its partial, unless we already have one
3495 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003496 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003497 name = deref_func_name(tofree, &len,
3498 partial != NULL ? NULL : &partial, FALSE);
3499
Bram Moolenaar532c7802005-01-27 14:44:31 +00003500 /* Skip white space to allow ":call func ()". Not good, but required for
3501 * backward compatibility. */
3502 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
3505 if (*startarg != '(')
3506 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003507 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 goto end;
3509 }
3510
3511 /*
3512 * When skipping, evaluate the function once, to find the end of the
3513 * arguments.
3514 * When the function takes a range, this is discovered after the first
3515 * call, and the loop is broken.
3516 */
3517 if (eap->skip)
3518 {
3519 ++emsg_skip;
3520 lnum = eap->line2; /* do it once, also with an invalid range */
3521 }
3522 else
3523 lnum = eap->line1;
3524 for ( ; lnum <= eap->line2; ++lnum)
3525 {
3526 if (!eap->skip && eap->addr_count > 0)
3527 {
3528 curwin->w_cursor.lnum = lnum;
3529 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003530#ifdef FEAT_VIRTUALEDIT
3531 curwin->w_cursor.coladd = 0;
3532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003535 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003536 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003537 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
3539 failed = TRUE;
3540 break;
3541 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003542
3543 /* Handle a function returning a Funcref, Dictionary or List. */
3544 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3545 {
3546 failed = TRUE;
3547 break;
3548 }
3549
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003550 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (doesrange || eap->skip)
3552 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003555 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003556 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003557 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (aborting())
3559 break;
3560 }
3561 if (eap->skip)
3562 --emsg_skip;
3563
3564 if (!failed)
3565 {
3566 /* Check for trailing illegal characters and a following command. */
3567 if (!ends_excmd(*arg))
3568 {
3569 emsg_severe = TRUE;
3570 EMSG(_(e_trailing));
3571 }
3572 else
3573 eap->nextcmd = check_nextcmd(arg);
3574 }
3575
3576end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003577 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003578 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579}
3580
3581/*
3582 * ":unlet[!] var1 ... " command.
3583 */
3584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003585ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587 ex_unletlock(eap, eap->arg, 0);
3588}
3589
3590/*
3591 * ":lockvar" and ":unlockvar" commands
3592 */
3593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003594ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003595{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003597 int deep = 2;
3598
3599 if (eap->forceit)
3600 deep = -1;
3601 else if (vim_isdigit(*arg))
3602 {
3603 deep = getdigits(&arg);
3604 arg = skipwhite(arg);
3605 }
3606
3607 ex_unletlock(eap, arg, deep);
3608}
3609
3610/*
3611 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3612 */
3613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003614ex_unletlock(
3615 exarg_T *eap,
3616 char_u *argstart,
3617 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003618{
3619 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
3624 do
3625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003627 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003628 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 if (lv.ll_name == NULL)
3630 error = TRUE; /* error but continue parsing */
3631 if (name_end == NULL || (!vim_iswhite(*name_end)
3632 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 if (name_end != NULL)
3635 {
3636 emsg_severe = TRUE;
3637 EMSG(_(e_trailing));
3638 }
3639 if (!(eap->skip || error))
3640 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 break;
3642 }
3643
3644 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645 {
3646 if (eap->cmdidx == CMD_unlet)
3647 {
3648 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3649 error = TRUE;
3650 }
3651 else
3652 {
3653 if (do_lock_var(&lv, name_end, deep,
3654 eap->cmdidx == CMD_lockvar) == FAIL)
3655 error = TRUE;
3656 }
3657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 if (!eap->skip)
3660 clear_lval(&lv);
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 arg = skipwhite(name_end);
3663 } while (!ends_excmd(*arg));
3664
3665 eap->nextcmd = check_nextcmd(arg);
3666}
3667
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003669do_unlet_var(
3670 lval_T *lp,
3671 char_u *name_end,
3672 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 int ret = OK;
3675 int cc;
3676
3677 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 cc = *name_end;
3680 *name_end = NUL;
3681
3682 /* Normal name or expanded name. */
3683 if (check_changedtick(lp->ll_name))
3684 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003688 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003689 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003690 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003691 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003692 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003693 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694 else if (lp->ll_range)
3695 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003696 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003698 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003699
3700 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3701 {
3702 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003703 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003704 return FAIL;
3705 ll_li = li;
3706 ++ll_n1;
3707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708
3709 /* Delete a range of List items. */
3710 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3711 {
3712 li = lp->ll_li->li_next;
3713 listitem_remove(lp->ll_list, lp->ll_li);
3714 lp->ll_li = li;
3715 ++lp->ll_n1;
3716 }
3717 }
3718 else
3719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 /* unlet a List item. */
3722 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003723 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003725 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 }
3727
3728 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729}
3730
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731/*
3732 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003733 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 */
3735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003736do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 hashtab_T *ht;
3739 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003740 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003741 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003742 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 ht = find_var_ht(name, &varname);
3745 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003747 if (ht == &globvarht)
3748 d = &globvardict;
3749 else if (current_funccal != NULL
3750 && ht == &current_funccal->l_vars.dv_hashtab)
3751 d = &current_funccal->l_vars;
3752 else if (ht == &compat_hashtab)
3753 d = &vimvardict;
3754 else
3755 {
3756 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3757 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3758 }
3759 if (d == NULL)
3760 {
3761 EMSG2(_(e_intern2), "do_unlet()");
3762 return FAIL;
3763 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003764 hi = hash_find(ht, varname);
3765 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003766 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003767 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003768 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003769 || var_check_ro(di->di_flags, name, FALSE)
3770 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003771 return FAIL;
3772
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003773 delete_var(ht, hi);
3774 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003777 if (forceit)
3778 return OK;
3779 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return FAIL;
3781}
3782
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003783/*
3784 * Lock or unlock variable indicated by "lp".
3785 * "deep" is the levels to go (-1 for unlimited);
3786 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3787 */
3788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003789do_lock_var(
3790 lval_T *lp,
3791 char_u *name_end,
3792 int deep,
3793 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003794{
3795 int ret = OK;
3796 int cc;
3797 dictitem_T *di;
3798
3799 if (deep == 0) /* nothing to do */
3800 return OK;
3801
3802 if (lp->ll_tv == NULL)
3803 {
3804 cc = *name_end;
3805 *name_end = NUL;
3806
3807 /* Normal name or expanded name. */
3808 if (check_changedtick(lp->ll_name))
3809 ret = FAIL;
3810 else
3811 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003812 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003813 if (di == NULL)
3814 ret = FAIL;
3815 else
3816 {
3817 if (lock)
3818 di->di_flags |= DI_FLAGS_LOCK;
3819 else
3820 di->di_flags &= ~DI_FLAGS_LOCK;
3821 item_lock(&di->di_tv, deep, lock);
3822 }
3823 }
3824 *name_end = cc;
3825 }
3826 else if (lp->ll_range)
3827 {
3828 listitem_T *li = lp->ll_li;
3829
3830 /* (un)lock a range of List items. */
3831 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3832 {
3833 item_lock(&li->li_tv, deep, lock);
3834 li = li->li_next;
3835 ++lp->ll_n1;
3836 }
3837 }
3838 else if (lp->ll_list != NULL)
3839 /* (un)lock a List item. */
3840 item_lock(&lp->ll_li->li_tv, deep, lock);
3841 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003842 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003843 item_lock(&lp->ll_di->di_tv, deep, lock);
3844
3845 return ret;
3846}
3847
3848/*
3849 * Lock or unlock an item. "deep" is nr of levels to go.
3850 */
3851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003852item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003853{
3854 static int recurse = 0;
3855 list_T *l;
3856 listitem_T *li;
3857 dict_T *d;
3858 hashitem_T *hi;
3859 int todo;
3860
3861 if (recurse >= DICT_MAXNEST)
3862 {
3863 EMSG(_("E743: variable nested too deep for (un)lock"));
3864 return;
3865 }
3866 if (deep == 0)
3867 return;
3868 ++recurse;
3869
3870 /* lock/unlock the item itself */
3871 if (lock)
3872 tv->v_lock |= VAR_LOCKED;
3873 else
3874 tv->v_lock &= ~VAR_LOCKED;
3875
3876 switch (tv->v_type)
3877 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003878 case VAR_UNKNOWN:
3879 case VAR_NUMBER:
3880 case VAR_STRING:
3881 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003882 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003883 case VAR_FLOAT:
3884 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003885 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003886 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003887 break;
3888
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003889 case VAR_LIST:
3890 if ((l = tv->vval.v_list) != NULL)
3891 {
3892 if (lock)
3893 l->lv_lock |= VAR_LOCKED;
3894 else
3895 l->lv_lock &= ~VAR_LOCKED;
3896 if (deep < 0 || deep > 1)
3897 /* recursive: lock/unlock the items the List contains */
3898 for (li = l->lv_first; li != NULL; li = li->li_next)
3899 item_lock(&li->li_tv, deep - 1, lock);
3900 }
3901 break;
3902 case VAR_DICT:
3903 if ((d = tv->vval.v_dict) != NULL)
3904 {
3905 if (lock)
3906 d->dv_lock |= VAR_LOCKED;
3907 else
3908 d->dv_lock &= ~VAR_LOCKED;
3909 if (deep < 0 || deep > 1)
3910 {
3911 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003912 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003913 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3914 {
3915 if (!HASHITEM_EMPTY(hi))
3916 {
3917 --todo;
3918 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3919 }
3920 }
3921 }
3922 }
3923 }
3924 --recurse;
3925}
3926
Bram Moolenaara40058a2005-07-11 22:42:07 +00003927/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003928 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3929 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003930 */
3931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003932tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003933{
3934 return (tv->v_lock & VAR_LOCKED)
3935 || (tv->v_type == VAR_LIST
3936 && tv->vval.v_list != NULL
3937 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3938 || (tv->v_type == VAR_DICT
3939 && tv->vval.v_dict != NULL
3940 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3941}
3942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3944/*
3945 * Delete all "menutrans_" variables.
3946 */
3947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003948del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003951 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003954 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003956 {
3957 if (!HASHITEM_EMPTY(hi))
3958 {
3959 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3961 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003962 }
3963 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003964 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965}
3966#endif
3967
3968#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3969
3970/*
3971 * Local string buffer for the next two functions to store a variable name
3972 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3973 * get_user_var_name().
3974 */
3975
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003976static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978static char_u *varnamebuf = NULL;
3979static int varnamebuflen = 0;
3980
3981/*
3982 * Function to concatenate a prefix and a variable name.
3983 */
3984 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003985cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 int len;
3988
3989 len = (int)STRLEN(name) + 3;
3990 if (len > varnamebuflen)
3991 {
3992 vim_free(varnamebuf);
3993 len += 10; /* some additional space */
3994 varnamebuf = alloc(len);
3995 if (varnamebuf == NULL)
3996 {
3997 varnamebuflen = 0;
3998 return NULL;
3999 }
4000 varnamebuflen = len;
4001 }
4002 *varnamebuf = prefix;
4003 varnamebuf[1] = ':';
4004 STRCPY(varnamebuf + 2, name);
4005 return varnamebuf;
4006}
4007
4008/*
4009 * Function given to ExpandGeneric() to obtain the list of user defined
4010 * (global/buffer/window/built-in) variable names.
4011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004013get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004015 static long_u gdone;
4016 static long_u bdone;
4017 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018#ifdef FEAT_WINDOWS
4019 static long_u tdone;
4020#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004021 static int vidx;
4022 static hashitem_T *hi;
4023 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028#ifdef FEAT_WINDOWS
4029 tdone = 0;
4030#endif
4031 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004032
4033 /* Global variables */
4034 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004037 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004038 else
4039 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004040 while (HASHITEM_EMPTY(hi))
4041 ++hi;
4042 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4043 return cat_prefix_varname('g', hi->hi_key);
4044 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004046
4047 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004048 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004051 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004053 else
4054 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004055 while (HASHITEM_EMPTY(hi))
4056 ++hi;
4057 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 return (char_u *)"b:changedtick";
4063 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004064
4065 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004066 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004067 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004069 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004071 else
4072 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004073 while (HASHITEM_EMPTY(hi))
4074 ++hi;
4075 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004077
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004078#ifdef FEAT_WINDOWS
4079 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004080 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004081 if (tdone < ht->ht_used)
4082 {
4083 if (tdone++ == 0)
4084 hi = ht->ht_array;
4085 else
4086 ++hi;
4087 while (HASHITEM_EMPTY(hi))
4088 ++hi;
4089 return cat_prefix_varname('t', hi->hi_key);
4090 }
4091#endif
4092
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 /* v: variables */
4094 if (vidx < VV_LEN)
4095 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
4097 vim_free(varnamebuf);
4098 varnamebuf = NULL;
4099 varnamebuflen = 0;
4100 return NULL;
4101}
4102
4103#endif /* FEAT_CMDL_COMPL */
4104
4105/*
4106 * types for expressions.
4107 */
4108typedef enum
4109{
4110 TYPE_UNKNOWN = 0
4111 , TYPE_EQUAL /* == */
4112 , TYPE_NEQUAL /* != */
4113 , TYPE_GREATER /* > */
4114 , TYPE_GEQUAL /* >= */
4115 , TYPE_SMALLER /* < */
4116 , TYPE_SEQUAL /* <= */
4117 , TYPE_MATCH /* =~ */
4118 , TYPE_NOMATCH /* !~ */
4119} exptype_T;
4120
4121/*
4122 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4125 */
4126
4127/*
4128 * Handle zero level expression.
4129 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004131 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 * Return OK or FAIL.
4133 */
4134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004135eval0(
4136 char_u *arg,
4137 typval_T *rettv,
4138 char_u **nextcmd,
4139 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140{
4141 int ret;
4142 char_u *p;
4143
4144 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 if (ret == FAIL || !ends_excmd(*p))
4147 {
4148 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004149 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 /*
4151 * Report the invalid expression unless the expression evaluation has
4152 * been cancelled due to an aborting error, an interrupt, or an
4153 * exception.
4154 */
4155 if (!aborting())
4156 EMSG2(_(e_invexpr2), arg);
4157 ret = FAIL;
4158 }
4159 if (nextcmd != NULL)
4160 *nextcmd = check_nextcmd(p);
4161
4162 return ret;
4163}
4164
4165/*
4166 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004167 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 *
4169 * "arg" must point to the first non-white of the expression.
4170 * "arg" is advanced to the next non-white after the recognized expression.
4171 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004172 * Note: "rettv.v_lock" is not set.
4173 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 * Return OK or FAIL.
4175 */
4176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004177eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
4179 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004180 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
4182 /*
4183 * Get the first variable.
4184 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 return FAIL;
4187
4188 if ((*arg)[0] == '?')
4189 {
4190 result = FALSE;
4191 if (evaluate)
4192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 int error = FALSE;
4194
4195 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 if (error)
4199 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201
4202 /*
4203 * Get the second variable.
4204 */
4205 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 return FAIL;
4208
4209 /*
4210 * Check for the ":".
4211 */
4212 if ((*arg)[0] != ':')
4213 {
4214 EMSG(_("E109: Missing ':' after '?'"));
4215 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
4218 }
4219
4220 /*
4221 * Get the third variable.
4222 */
4223 *arg = skipwhite(*arg + 1);
4224 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4225 {
4226 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return FAIL;
4229 }
4230 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233
4234 return OK;
4235}
4236
4237/*
4238 * Handle first level expression:
4239 * expr2 || expr2 || expr2 logical OR
4240 *
4241 * "arg" must point to the first non-white of the expression.
4242 * "arg" is advanced to the next non-white after the recognized expression.
4243 *
4244 * Return OK or FAIL.
4245 */
4246 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004247eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248{
Bram Moolenaar33570922005-01-25 22:26:29 +00004249 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 long result;
4251 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
4254 /*
4255 * Get the first variable.
4256 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 return FAIL;
4259
4260 /*
4261 * Repeat until there is no following "||".
4262 */
4263 first = TRUE;
4264 result = FALSE;
4265 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4266 {
4267 if (evaluate && first)
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 if (error)
4273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 first = FALSE;
4275 }
4276
4277 /*
4278 * Get the second variable.
4279 */
4280 *arg = skipwhite(*arg + 2);
4281 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4282 return FAIL;
4283
4284 /*
4285 * Compute the result.
4286 */
4287 if (evaluate && !result)
4288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004289 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 if (error)
4293 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 if (evaluate)
4296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 rettv->v_type = VAR_NUMBER;
4298 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 }
4301
4302 return OK;
4303}
4304
4305/*
4306 * Handle second level expression:
4307 * expr3 && expr3 && expr3 logical AND
4308 *
4309 * "arg" must point to the first non-white of the expression.
4310 * "arg" is advanced to the next non-white after the recognized expression.
4311 *
4312 * Return OK or FAIL.
4313 */
4314 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004315eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
Bram Moolenaar33570922005-01-25 22:26:29 +00004317 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 long result;
4319 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 /*
4323 * Get the first variable.
4324 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 return FAIL;
4327
4328 /*
4329 * Repeat until there is no following "&&".
4330 */
4331 first = TRUE;
4332 result = TRUE;
4333 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4334 {
4335 if (evaluate && first)
4336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (error)
4341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 first = FALSE;
4343 }
4344
4345 /*
4346 * Get the second variable.
4347 */
4348 *arg = skipwhite(*arg + 2);
4349 if (eval4(arg, &var2, evaluate && result) == FAIL)
4350 return FAIL;
4351
4352 /*
4353 * Compute the result.
4354 */
4355 if (evaluate && result)
4356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 if (error)
4361 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 if (evaluate)
4364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004365 rettv->v_type = VAR_NUMBER;
4366 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 }
4369
4370 return OK;
4371}
4372
4373/*
4374 * Handle third level expression:
4375 * var1 == var2
4376 * var1 =~ var2
4377 * var1 != var2
4378 * var1 !~ var2
4379 * var1 > var2
4380 * var1 >= var2
4381 * var1 < var2
4382 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004383 * var1 is var2
4384 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 *
4386 * "arg" must point to the first non-white of the expression.
4387 * "arg" is advanced to the next non-white after the recognized expression.
4388 *
4389 * Return OK or FAIL.
4390 */
4391 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004392eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
Bram Moolenaar33570922005-01-25 22:26:29 +00004394 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 char_u *p;
4396 int i;
4397 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 int len = 2;
4400 long n1, n2;
4401 char_u *s1, *s2;
4402 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4403 regmatch_T regmatch;
4404 int ic;
4405 char_u *save_cpo;
4406
4407 /*
4408 * Get the first variable.
4409 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004410 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return FAIL;
4412
4413 p = *arg;
4414 switch (p[0])
4415 {
4416 case '=': if (p[1] == '=')
4417 type = TYPE_EQUAL;
4418 else if (p[1] == '~')
4419 type = TYPE_MATCH;
4420 break;
4421 case '!': if (p[1] == '=')
4422 type = TYPE_NEQUAL;
4423 else if (p[1] == '~')
4424 type = TYPE_NOMATCH;
4425 break;
4426 case '>': if (p[1] != '=')
4427 {
4428 type = TYPE_GREATER;
4429 len = 1;
4430 }
4431 else
4432 type = TYPE_GEQUAL;
4433 break;
4434 case '<': if (p[1] != '=')
4435 {
4436 type = TYPE_SMALLER;
4437 len = 1;
4438 }
4439 else
4440 type = TYPE_SEQUAL;
4441 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004442 case 'i': if (p[1] == 's')
4443 {
4444 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4445 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004446 i = p[len];
4447 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 {
4449 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4450 type_is = TRUE;
4451 }
4452 }
4453 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455
4456 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004457 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 */
4459 if (type != TYPE_UNKNOWN)
4460 {
4461 /* extra question mark appended: ignore case */
4462 if (p[len] == '?')
4463 {
4464 ic = TRUE;
4465 ++len;
4466 }
4467 /* extra '#' appended: match case */
4468 else if (p[len] == '#')
4469 {
4470 ic = FALSE;
4471 ++len;
4472 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004473 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 else
4475 ic = p_ic;
4476
4477 /*
4478 * Get the second variable.
4479 */
4480 *arg = skipwhite(p + len);
4481 if (eval5(arg, &var2, evaluate) == FAIL)
4482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 return FAIL;
4485 }
4486
4487 if (evaluate)
4488 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004489 if (type_is && rettv->v_type != var2.v_type)
4490 {
4491 /* For "is" a different type always means FALSE, for "notis"
4492 * it means TRUE. */
4493 n1 = (type == TYPE_NEQUAL);
4494 }
4495 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4496 {
4497 if (type_is)
4498 {
4499 n1 = (rettv->v_type == var2.v_type
4500 && rettv->vval.v_list == var2.vval.v_list);
4501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 else if (rettv->v_type != var2.v_type
4505 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4506 {
4507 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004508 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004510 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004511 clear_tv(rettv);
4512 clear_tv(&var2);
4513 return FAIL;
4514 }
4515 else
4516 {
4517 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004518 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4519 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004520 if (type == TYPE_NEQUAL)
4521 n1 = !n1;
4522 }
4523 }
4524
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004525 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4526 {
4527 if (type_is)
4528 {
4529 n1 = (rettv->v_type == var2.v_type
4530 && rettv->vval.v_dict == var2.vval.v_dict);
4531 if (type == TYPE_NEQUAL)
4532 n1 = !n1;
4533 }
4534 else if (rettv->v_type != var2.v_type
4535 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4536 {
4537 if (rettv->v_type != var2.v_type)
4538 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4539 else
4540 EMSG(_("E736: Invalid operation for Dictionary"));
4541 clear_tv(rettv);
4542 clear_tv(&var2);
4543 return FAIL;
4544 }
4545 else
4546 {
4547 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004548 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4549 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004550 if (type == TYPE_NEQUAL)
4551 n1 = !n1;
4552 }
4553 }
4554
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004555 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4556 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004557 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004558 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004560 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 clear_tv(rettv);
4562 clear_tv(&var2);
4563 return FAIL;
4564 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004565 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004566 if (type == TYPE_NEQUAL)
4567 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 }
4569
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004570#ifdef FEAT_FLOAT
4571 /*
4572 * If one of the two variables is a float, compare as a float.
4573 * When using "=~" or "!~", always compare as string.
4574 */
4575 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4576 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4577 {
4578 float_T f1, f2;
4579
4580 if (rettv->v_type == VAR_FLOAT)
4581 f1 = rettv->vval.v_float;
4582 else
4583 f1 = get_tv_number(rettv);
4584 if (var2.v_type == VAR_FLOAT)
4585 f2 = var2.vval.v_float;
4586 else
4587 f2 = get_tv_number(&var2);
4588 n1 = FALSE;
4589 switch (type)
4590 {
4591 case TYPE_EQUAL: n1 = (f1 == f2); break;
4592 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4593 case TYPE_GREATER: n1 = (f1 > f2); break;
4594 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4595 case TYPE_SMALLER: n1 = (f1 < f2); break;
4596 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4597 case TYPE_UNKNOWN:
4598 case TYPE_MATCH:
4599 case TYPE_NOMATCH: break; /* avoid gcc warning */
4600 }
4601 }
4602#endif
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 /*
4605 * If one of the two variables is a number, compare as a number.
4606 * When using "=~" or "!~", always compare as string.
4607 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004608 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 n1 = get_tv_number(rettv);
4612 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 switch (type)
4614 {
4615 case TYPE_EQUAL: n1 = (n1 == n2); break;
4616 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4617 case TYPE_GREATER: n1 = (n1 > n2); break;
4618 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4619 case TYPE_SMALLER: n1 = (n1 < n2); break;
4620 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4621 case TYPE_UNKNOWN:
4622 case TYPE_MATCH:
4623 case TYPE_NOMATCH: break; /* avoid gcc warning */
4624 }
4625 }
4626 else
4627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 s1 = get_tv_string_buf(rettv, buf1);
4629 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4631 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4632 else
4633 i = 0;
4634 n1 = FALSE;
4635 switch (type)
4636 {
4637 case TYPE_EQUAL: n1 = (i == 0); break;
4638 case TYPE_NEQUAL: n1 = (i != 0); break;
4639 case TYPE_GREATER: n1 = (i > 0); break;
4640 case TYPE_GEQUAL: n1 = (i >= 0); break;
4641 case TYPE_SMALLER: n1 = (i < 0); break;
4642 case TYPE_SEQUAL: n1 = (i <= 0); break;
4643
4644 case TYPE_MATCH:
4645 case TYPE_NOMATCH:
4646 /* avoid 'l' flag in 'cpoptions' */
4647 save_cpo = p_cpo;
4648 p_cpo = (char_u *)"";
4649 regmatch.regprog = vim_regcomp(s2,
4650 RE_MAGIC + RE_STRING);
4651 regmatch.rm_ic = ic;
4652 if (regmatch.regprog != NULL)
4653 {
4654 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004655 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (type == TYPE_NOMATCH)
4657 n1 = !n1;
4658 }
4659 p_cpo = save_cpo;
4660 break;
4661
4662 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4663 }
4664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 clear_tv(rettv);
4666 clear_tv(&var2);
4667 rettv->v_type = VAR_NUMBER;
4668 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
4670 }
4671
4672 return OK;
4673}
4674
4675/*
4676 * Handle fourth level expression:
4677 * + number addition
4678 * - number subtraction
4679 * . string concatenation
4680 *
4681 * "arg" must point to the first non-white of the expression.
4682 * "arg" is advanced to the next non-white after the recognized expression.
4683 *
4684 * Return OK or FAIL.
4685 */
4686 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004687eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
Bram Moolenaar33570922005-01-25 22:26:29 +00004689 typval_T var2;
4690 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 int op;
4692 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004693#ifdef FEAT_FLOAT
4694 float_T f1 = 0, f2 = 0;
4695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 char_u *s1, *s2;
4697 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4698 char_u *p;
4699
4700 /*
4701 * Get the first variable.
4702 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004703 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return FAIL;
4705
4706 /*
4707 * Repeat computing, until no '+', '-' or '.' is following.
4708 */
4709 for (;;)
4710 {
4711 op = **arg;
4712 if (op != '+' && op != '-' && op != '.')
4713 break;
4714
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 if ((op != '+' || rettv->v_type != VAR_LIST)
4716#ifdef FEAT_FLOAT
4717 && (op == '.' || rettv->v_type != VAR_FLOAT)
4718#endif
4719 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
4721 /* For "list + ...", an illegal use of the first operand as
4722 * a number cannot be determined before evaluating the 2nd
4723 * operand: if this is also a list, all is ok.
4724 * For "something . ...", "something - ..." or "non-list + ...",
4725 * we know that the first operand needs to be a string or number
4726 * without evaluating the 2nd operand. So check before to avoid
4727 * side effects after an error. */
4728 if (evaluate && get_tv_string_chk(rettv) == NULL)
4729 {
4730 clear_tv(rettv);
4731 return FAIL;
4732 }
4733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 /*
4736 * Get the second variable.
4737 */
4738 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004739 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004741 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return FAIL;
4743 }
4744
4745 if (evaluate)
4746 {
4747 /*
4748 * Compute the result.
4749 */
4750 if (op == '.')
4751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004752 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4753 s2 = get_tv_string_buf_chk(&var2, buf2);
4754 if (s2 == NULL) /* type error ? */
4755 {
4756 clear_tv(rettv);
4757 clear_tv(&var2);
4758 return FAIL;
4759 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004760 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 clear_tv(rettv);
4762 rettv->v_type = VAR_STRING;
4763 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004765 else if (op == '+' && rettv->v_type == VAR_LIST
4766 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004767 {
4768 /* concatenate Lists */
4769 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4770 &var3) == FAIL)
4771 {
4772 clear_tv(rettv);
4773 clear_tv(&var2);
4774 return FAIL;
4775 }
4776 clear_tv(rettv);
4777 *rettv = var3;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
4780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 int error = FALSE;
4782
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 f1 = rettv->vval.v_float;
4787 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 else
4790#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 n1 = get_tv_number_chk(rettv, &error);
4793 if (error)
4794 {
4795 /* This can only happen for "list + non-list". For
4796 * "non-list + ..." or "something - ...", we returned
4797 * before evaluating the 2nd operand. */
4798 clear_tv(rettv);
4799 return FAIL;
4800 }
4801#ifdef FEAT_FLOAT
4802 if (var2.v_type == VAR_FLOAT)
4803 f1 = n1;
4804#endif
4805 }
4806#ifdef FEAT_FLOAT
4807 if (var2.v_type == VAR_FLOAT)
4808 {
4809 f2 = var2.vval.v_float;
4810 n2 = 0;
4811 }
4812 else
4813#endif
4814 {
4815 n2 = get_tv_number_chk(&var2, &error);
4816 if (error)
4817 {
4818 clear_tv(rettv);
4819 clear_tv(&var2);
4820 return FAIL;
4821 }
4822#ifdef FEAT_FLOAT
4823 if (rettv->v_type == VAR_FLOAT)
4824 f2 = n2;
4825#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828
4829#ifdef FEAT_FLOAT
4830 /* If there is a float on either side the result is a float. */
4831 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4832 {
4833 if (op == '+')
4834 f1 = f1 + f2;
4835 else
4836 f1 = f1 - f2;
4837 rettv->v_type = VAR_FLOAT;
4838 rettv->vval.v_float = f1;
4839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#endif
4842 {
4843 if (op == '+')
4844 n1 = n1 + n2;
4845 else
4846 n1 = n1 - n2;
4847 rettv->v_type = VAR_NUMBER;
4848 rettv->vval.v_number = n1;
4849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 }
4854 return OK;
4855}
4856
4857/*
4858 * Handle fifth level expression:
4859 * * number multiplication
4860 * / number division
4861 * % number modulo
4862 *
4863 * "arg" must point to the first non-white of the expression.
4864 * "arg" is advanced to the next non-white after the recognized expression.
4865 *
4866 * Return OK or FAIL.
4867 */
4868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004869eval6(
4870 char_u **arg,
4871 typval_T *rettv,
4872 int evaluate,
4873 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874{
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 int op;
4877 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878#ifdef FEAT_FLOAT
4879 int use_float = FALSE;
4880 float_T f1 = 0, f2;
4881#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 /*
4885 * Get the first variable.
4886 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004887 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 return FAIL;
4889
4890 /*
4891 * Repeat computing, until no '*', '/' or '%' is following.
4892 */
4893 for (;;)
4894 {
4895 op = **arg;
4896 if (op != '*' && op != '/' && op != '%')
4897 break;
4898
4899 if (evaluate)
4900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#ifdef FEAT_FLOAT
4902 if (rettv->v_type == VAR_FLOAT)
4903 {
4904 f1 = rettv->vval.v_float;
4905 use_float = TRUE;
4906 n1 = 0;
4907 }
4908 else
4909#endif
4910 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912 if (error)
4913 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 else
4916 n1 = 0;
4917
4918 /*
4919 * Get the second variable.
4920 */
4921 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004922 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
4924
4925 if (evaluate)
4926 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927#ifdef FEAT_FLOAT
4928 if (var2.v_type == VAR_FLOAT)
4929 {
4930 if (!use_float)
4931 {
4932 f1 = n1;
4933 use_float = TRUE;
4934 }
4935 f2 = var2.vval.v_float;
4936 n2 = 0;
4937 }
4938 else
4939#endif
4940 {
4941 n2 = get_tv_number_chk(&var2, &error);
4942 clear_tv(&var2);
4943 if (error)
4944 return FAIL;
4945#ifdef FEAT_FLOAT
4946 if (use_float)
4947 f2 = n2;
4948#endif
4949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 /*
4952 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 if (op == '*')
4959 f1 = f1 * f2;
4960 else if (op == '/')
4961 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004962# ifdef VMS
4963 /* VMS crashes on divide by zero, work around it */
4964 if (f2 == 0.0)
4965 {
4966 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 }
4973 else
4974 f1 = f1 / f2;
4975# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 /* We rely on the floating point library to handle divide
4977 * by zero to result in "inf" and not a crash. */
4978 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004983 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 return FAIL;
4985 }
4986 rettv->v_type = VAR_FLOAT;
4987 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 if (op == '*')
4993 n1 = n1 * n2;
4994 else if (op == '/')
4995 {
4996 if (n2 == 0) /* give an error message? */
4997 {
4998 if (n1 == 0)
4999 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5000 else if (n1 < 0)
5001 n1 = -0x7fffffffL;
5002 else
5003 n1 = 0x7fffffffL;
5004 }
5005 else
5006 n1 = n1 / n2;
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 {
5010 if (n2 == 0) /* give an error message? */
5011 n1 = 0;
5012 else
5013 n1 = n1 % n2;
5014 }
5015 rettv->v_type = VAR_NUMBER;
5016 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 }
5020
5021 return OK;
5022}
5023
5024/*
5025 * Handle sixth level expression:
5026 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005027 * "string" string constant
5028 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 * &option-name option value
5030 * @r register contents
5031 * identifier variable value
5032 * function() function call
5033 * $VAR environment variable
5034 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005035 * [expr, expr] List
5036 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 *
5038 * Also handle:
5039 * ! in front logical NOT
5040 * - in front unary minus
5041 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 * trailing [] subscript in String or List
5043 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 *
5045 * "arg" must point to the first non-white of the expression.
5046 * "arg" is advanced to the next non-white after the recognized expression.
5047 *
5048 * Return OK or FAIL.
5049 */
5050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005051eval7(
5052 char_u **arg,
5053 typval_T *rettv,
5054 int evaluate,
5055 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 long n;
5058 int len;
5059 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u *start_leader, *end_leader;
5061 int ret = OK;
5062 char_u *alias;
5063
5064 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005066 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 /*
5071 * Skip '!' and '-' characters. They are handled later.
5072 */
5073 start_leader = *arg;
5074 while (**arg == '!' || **arg == '-' || **arg == '+')
5075 *arg = skipwhite(*arg + 1);
5076 end_leader = *arg;
5077
5078 switch (**arg)
5079 {
5080 /*
5081 * Number constant.
5082 */
5083 case '0':
5084 case '1':
5085 case '2':
5086 case '3':
5087 case '4':
5088 case '5':
5089 case '6':
5090 case '7':
5091 case '8':
5092 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 {
5094#ifdef FEAT_FLOAT
5095 char_u *p = skipdigits(*arg + 1);
5096 int get_float = FALSE;
5097
5098 /* We accept a float when the format matches
5099 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005100 * strict to avoid backwards compatibility problems.
5101 * Don't look for a float after the "." operator, so that
5102 * ":let vers = 1.2.3" doesn't fail. */
5103 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 get_float = TRUE;
5106 p = skipdigits(p + 2);
5107 if (*p == 'e' || *p == 'E')
5108 {
5109 ++p;
5110 if (*p == '-' || *p == '+')
5111 ++p;
5112 if (!vim_isdigit(*p))
5113 get_float = FALSE;
5114 else
5115 p = skipdigits(p + 1);
5116 }
5117 if (ASCII_ISALPHA(*p) || *p == '.')
5118 get_float = FALSE;
5119 }
5120 if (get_float)
5121 {
5122 float_T f;
5123
5124 *arg += string2float(*arg, &f);
5125 if (evaluate)
5126 {
5127 rettv->v_type = VAR_FLOAT;
5128 rettv->vval.v_float = f;
5129 }
5130 }
5131 else
5132#endif
5133 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005134 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005135 *arg += len;
5136 if (evaluate)
5137 {
5138 rettv->v_type = VAR_NUMBER;
5139 rettv->vval.v_number = n;
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /*
5146 * String constant: "string".
5147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 break;
5150
5151 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 break;
5156
5157 /*
5158 * List: [expr, expr]
5159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 * Dictionary: {key: val, key: val}
5165 */
5166 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5167 break;
5168
5169 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
5176 * Environment variable: $VAR.
5177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 break;
5180
5181 /*
5182 * Register contents: @r.
5183 */
5184 case '@': ++*arg;
5185 if (evaluate)
5186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005188 rettv->vval.v_string = get_reg_contents(**arg,
5189 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 if (**arg != NUL)
5192 ++*arg;
5193 break;
5194
5195 /*
5196 * nested expression: (expression).
5197 */
5198 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (**arg == ')')
5201 ++*arg;
5202 else if (ret == OK)
5203 {
5204 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ret = FAIL;
5207 }
5208 break;
5209
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
5212 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213
5214 if (ret == NOTDONE)
5215 {
5216 /*
5217 * Must be a variable or function name.
5218 * Can also be a curly-braces kind of name: {expr}.
5219 */
5220 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 if (alias != NULL)
5223 s = alias;
5224
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005225 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 ret = FAIL;
5227 else
5228 {
5229 if (**arg == '(') /* recursive! */
5230 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005231 partial_T *partial;
5232
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 /* If "s" is the name of a variable of type VAR_FUNC
5234 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005235 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236
5237 /* Invoke the function. */
5238 ret = get_func_tv(s, len, rettv, arg,
5239 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005240 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005241
5242 /* If evaluate is FALSE rettv->v_type was not set in
5243 * get_func_tv, but it's needed in handle_subscript() to parse
5244 * what follows. So set it here. */
5245 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5246 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005247 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005248 rettv->v_type = VAR_FUNC;
5249 }
5250
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 /* Stop the expression evaluation when immediately
5252 * aborting on error, or when an interrupt occurred or
5253 * an exception was thrown but not caught. */
5254 if (aborting())
5255 {
5256 if (ret == OK)
5257 clear_tv(rettv);
5258 ret = FAIL;
5259 }
5260 }
5261 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005262 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005263 else
5264 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005266 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 }
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 *arg = skipwhite(*arg);
5270
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005271 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5272 * expr(expr). */
5273 if (ret == OK)
5274 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
5276 /*
5277 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5278 */
5279 if (ret == OK && evaluate && end_leader > start_leader)
5280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005282 int val = 0;
5283#ifdef FEAT_FLOAT
5284 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005285
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005286 if (rettv->v_type == VAR_FLOAT)
5287 f = rettv->vval.v_float;
5288 else
5289#endif
5290 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293 clear_tv(rettv);
5294 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 else
5297 {
5298 while (end_leader > start_leader)
5299 {
5300 --end_leader;
5301 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005302 {
5303#ifdef FEAT_FLOAT
5304 if (rettv->v_type == VAR_FLOAT)
5305 f = !f;
5306 else
5307#endif
5308 val = !val;
5309 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005311 {
5312#ifdef FEAT_FLOAT
5313 if (rettv->v_type == VAR_FLOAT)
5314 f = -f;
5315 else
5316#endif
5317 val = -val;
5318 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005320#ifdef FEAT_FLOAT
5321 if (rettv->v_type == VAR_FLOAT)
5322 {
5323 clear_tv(rettv);
5324 rettv->vval.v_float = f;
5325 }
5326 else
5327#endif
5328 {
5329 clear_tv(rettv);
5330 rettv->v_type = VAR_NUMBER;
5331 rettv->vval.v_number = val;
5332 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335
5336 return ret;
5337}
5338
5339/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005340 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5341 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5343 */
5344 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005345eval_index(
5346 char_u **arg,
5347 typval_T *rettv,
5348 int evaluate,
5349 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350{
5351 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005354 long len = -1;
5355 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358
Bram Moolenaara03f2332016-02-06 18:09:59 +01005359 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005362 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 if (verbose)
5364 EMSG(_("E695: Cannot index a Funcref"));
5365 return FAIL;
5366 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005367#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 if (verbose)
5369 EMSG(_(e_float_as_string));
5370 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005371#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005372 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005373 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005374 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005375 if (verbose)
5376 EMSG(_("E909: Cannot index a special variable"));
5377 return FAIL;
5378 case VAR_UNKNOWN:
5379 if (evaluate)
5380 return FAIL;
5381 /* FALLTHROUGH */
5382
5383 case VAR_STRING:
5384 case VAR_NUMBER:
5385 case VAR_LIST:
5386 case VAR_DICT:
5387 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005388 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005390 init_tv(&var1);
5391 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 /*
5395 * dict.name
5396 */
5397 key = *arg + 1;
5398 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5399 ;
5400 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 /*
5407 * something[idx]
5408 *
5409 * Get the (first) variable from inside the [].
5410 */
5411 *arg = skipwhite(*arg + 1);
5412 if (**arg == ':')
5413 empty1 = TRUE;
5414 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5415 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005416 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5417 {
5418 /* not a number or string */
5419 clear_tv(&var1);
5420 return FAIL;
5421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422
5423 /*
5424 * Get the second variable from inside the [:].
5425 */
5426 if (**arg == ':')
5427 {
5428 range = TRUE;
5429 *arg = skipwhite(*arg + 1);
5430 if (**arg == ']')
5431 empty2 = TRUE;
5432 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005434 if (!empty1)
5435 clear_tv(&var1);
5436 return FAIL;
5437 }
5438 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5439 {
5440 /* not a number or string */
5441 if (!empty1)
5442 clear_tv(&var1);
5443 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 return FAIL;
5445 }
5446 }
5447
5448 /* Check for the ']'. */
5449 if (**arg != ']')
5450 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005451 if (verbose)
5452 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 clear_tv(&var1);
5454 if (range)
5455 clear_tv(&var2);
5456 return FAIL;
5457 }
5458 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460
5461 if (evaluate)
5462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 n1 = 0;
5464 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 n1 = get_tv_number(&var1);
5467 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 if (range)
5470 {
5471 if (empty2)
5472 n2 = -1;
5473 else
5474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 n2 = get_tv_number(&var2);
5476 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 }
5478 }
5479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005482 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005483 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005484 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005485 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005486 case VAR_SPECIAL:
5487 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005488 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005489 break; /* not evaluating, skipping over subscript */
5490
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 case VAR_NUMBER:
5492 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 len = (long)STRLEN(s);
5495 if (range)
5496 {
5497 /* The resulting variable is a substring. If the indexes
5498 * are out of range the result is empty. */
5499 if (n1 < 0)
5500 {
5501 n1 = len + n1;
5502 if (n1 < 0)
5503 n1 = 0;
5504 }
5505 if (n2 < 0)
5506 n2 = len + n2;
5507 else if (n2 >= len)
5508 n2 = len;
5509 if (n1 >= len || n2 < 0 || n1 > n2)
5510 s = NULL;
5511 else
5512 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5513 }
5514 else
5515 {
5516 /* The resulting variable is a string of a single
5517 * character. If the index is too big or negative the
5518 * result is empty. */
5519 if (n1 >= len || n1 < 0)
5520 s = NULL;
5521 else
5522 s = vim_strnsave(s + n1, 1);
5523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 clear_tv(rettv);
5525 rettv->v_type = VAR_STRING;
5526 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 break;
5528
5529 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 if (n1 < 0)
5532 n1 = len + n1;
5533 if (!empty1 && (n1 < 0 || n1 >= len))
5534 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005535 /* For a range we allow invalid values and return an empty
5536 * list. A list index out of range is an error. */
5537 if (!range)
5538 {
5539 if (verbose)
5540 EMSGN(_(e_listidx), n1);
5541 return FAIL;
5542 }
5543 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 }
5545 if (range)
5546 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005547 list_T *l;
5548 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549
5550 if (n2 < 0)
5551 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005552 else if (n2 >= len)
5553 n2 = len - 1;
5554 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005555 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 l = list_alloc();
5557 if (l == NULL)
5558 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 n1 <= n2; ++n1)
5561 {
5562 if (list_append_tv(l, &item->li_tv) == FAIL)
5563 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005564 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 return FAIL;
5566 }
5567 item = item->li_next;
5568 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 clear_tv(rettv);
5570 rettv->v_type = VAR_LIST;
5571 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005572 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 }
5574 else
5575 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005576 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 clear_tv(rettv);
5578 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 }
5580 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581
5582 case VAR_DICT:
5583 if (range)
5584 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005585 if (verbose)
5586 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 if (len == -1)
5588 clear_tv(&var1);
5589 return FAIL;
5590 }
5591 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005592 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593
5594 if (len == -1)
5595 {
5596 key = get_tv_string(&var1);
5597 if (*key == NUL)
5598 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005599 if (verbose)
5600 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 clear_tv(&var1);
5602 return FAIL;
5603 }
5604 }
5605
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005606 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005608 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005609 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 if (len == -1)
5611 clear_tv(&var1);
5612 if (item == NULL)
5613 return FAIL;
5614
5615 copy_tv(&item->di_tv, &var1);
5616 clear_tv(rettv);
5617 *rettv = var1;
5618 }
5619 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 }
5621 }
5622
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 return OK;
5624}
5625
5626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 * Get an option value.
5628 * "arg" points to the '&' or '+' before the option name.
5629 * "arg" is advanced to character after the option name.
5630 * Return OK or FAIL.
5631 */
5632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005633get_option_tv(
5634 char_u **arg,
5635 typval_T *rettv, /* when NULL, only check if option exists */
5636 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 char_u *option_end;
5639 long numval;
5640 char_u *stringval;
5641 int opt_type;
5642 int c;
5643 int working = (**arg == '+'); /* has("+option") */
5644 int ret = OK;
5645 int opt_flags;
5646
5647 /*
5648 * Isolate the option name and find its value.
5649 */
5650 option_end = find_option_end(arg, &opt_flags);
5651 if (option_end == NULL)
5652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 EMSG2(_("E112: Option name missing: %s"), *arg);
5655 return FAIL;
5656 }
5657
5658 if (!evaluate)
5659 {
5660 *arg = option_end;
5661 return OK;
5662 }
5663
5664 c = *option_end;
5665 *option_end = NUL;
5666 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 if (opt_type == -3) /* invalid name */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 EMSG2(_("E113: Unknown option: %s"), *arg);
5673 ret = FAIL;
5674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
5677 if (opt_type == -2) /* hidden string option */
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv->v_type = VAR_STRING;
5680 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 else if (opt_type == -1) /* hidden number option */
5683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 rettv->v_type = VAR_NUMBER;
5685 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 else if (opt_type == 1) /* number option */
5688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 rettv->v_type = VAR_NUMBER;
5690 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 else /* string option */
5693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 rettv->v_type = VAR_STRING;
5695 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 }
5698 else if (working && (opt_type == -2 || opt_type == -1))
5699 ret = FAIL;
5700
5701 *option_end = c; /* put back for error messages */
5702 *arg = option_end;
5703
5704 return ret;
5705}
5706
5707/*
5708 * Allocate a variable for a string constant.
5709 * Return OK or FAIL.
5710 */
5711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005712get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
5714 char_u *p;
5715 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 int extra = 0;
5717
5718 /*
5719 * Find the end of the string, skipping backslashed characters.
5720 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 if (*p == '\\' && p[1] != NUL)
5724 {
5725 ++p;
5726 /* A "\<x>" form occupies at least 4 characters, and produces up
5727 * to 6 characters: reserve space for 2 extra */
5728 if (*p == '<')
5729 extra += 2;
5730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 }
5732
5733 if (*p != '"')
5734 {
5735 EMSG2(_("E114: Missing quote: %s"), *arg);
5736 return FAIL;
5737 }
5738
5739 /* If only parsing, set *arg and return here */
5740 if (!evaluate)
5741 {
5742 *arg = p + 1;
5743 return OK;
5744 }
5745
5746 /*
5747 * Copy the string into allocated memory, handling backslashed
5748 * characters.
5749 */
5750 name = alloc((unsigned)(p - *arg + extra));
5751 if (name == NULL)
5752 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 rettv->v_type = VAR_STRING;
5754 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
5758 if (*p == '\\')
5759 {
5760 switch (*++p)
5761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 case 'b': *name++ = BS; ++p; break;
5763 case 'e': *name++ = ESC; ++p; break;
5764 case 'f': *name++ = FF; ++p; break;
5765 case 'n': *name++ = NL; ++p; break;
5766 case 'r': *name++ = CAR; ++p; break;
5767 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
5769 case 'X': /* hex: "\x1", "\x12" */
5770 case 'x':
5771 case 'u': /* Unicode: "\u0023" */
5772 case 'U':
5773 if (vim_isxdigit(p[1]))
5774 {
5775 int n, nr;
5776 int c = toupper(*p);
5777
5778 if (c == 'X')
5779 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005780 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005782 else
5783 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 nr = 0;
5785 while (--n >= 0 && vim_isxdigit(p[1]))
5786 {
5787 ++p;
5788 nr = (nr << 4) + hex2nr(*p);
5789 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791#ifdef FEAT_MBYTE
5792 /* For "\u" store the number according to
5793 * 'encoding'. */
5794 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 else
5797#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 break;
5801
5802 /* octal: "\1", "\12", "\123" */
5803 case '0':
5804 case '1':
5805 case '2':
5806 case '3':
5807 case '4':
5808 case '5':
5809 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 case '7': *name = *p++ - '0';
5811 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 *name = (*name << 3) + *p++ - '0';
5814 if (*p >= '0' && *p <= '7')
5815 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819
5820 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 if (extra != 0)
5823 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826 }
5827 /* FALLTHROUGH */
5828
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831 }
5832 }
5833 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 *arg = p + 1;
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 return OK;
5841}
5842
5843/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 * Return OK or FAIL.
5846 */
5847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005848get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 int reduce = 0;
5853
5854 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 */
5857 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 break;
5863 ++reduce;
5864 ++p;
5865 }
5866 }
5867
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 return FAIL;
5872 }
5873
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 if (!evaluate)
5876 {
5877 *arg = p + 1;
5878 return OK;
5879 }
5880
5881 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 */
5884 str = alloc((unsigned)((p - *arg) - reduce));
5885 if (str == NULL)
5886 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 rettv->v_type = VAR_STRING;
5888 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 break;
5896 ++p;
5897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 *arg = p + 1;
5902
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 return OK;
5904}
5905
5906/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 * Allocate a variable for a List and fill it from "*arg".
5908 * Return OK or FAIL.
5909 */
5910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005911get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912{
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 list_T *l = NULL;
5914 typval_T tv;
5915 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916
5917 if (evaluate)
5918 {
5919 l = list_alloc();
5920 if (l == NULL)
5921 return FAIL;
5922 }
5923
5924 *arg = skipwhite(*arg + 1);
5925 while (**arg != ']' && **arg != NUL)
5926 {
5927 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5928 goto failret;
5929 if (evaluate)
5930 {
5931 item = listitem_alloc();
5932 if (item != NULL)
5933 {
5934 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005935 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 list_append(l, item);
5937 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005938 else
5939 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 }
5941
5942 if (**arg == ']')
5943 break;
5944 if (**arg != ',')
5945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 goto failret;
5948 }
5949 *arg = skipwhite(*arg + 1);
5950 }
5951
5952 if (**arg != ']')
5953 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005954 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955failret:
5956 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005957 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 return FAIL;
5959 }
5960
5961 *arg = skipwhite(*arg + 1);
5962 if (evaluate)
5963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 rettv->v_type = VAR_LIST;
5965 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 ++l->lv_refcount;
5967 }
5968
5969 return OK;
5970}
5971
5972/*
5973 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005974 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005976 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005977list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005979 list_T *l;
5980
5981 l = (list_T *)alloc_clear(sizeof(list_T));
5982 if (l != NULL)
5983 {
5984 /* Prepend the list to the list of lists for garbage collection. */
5985 if (first_list != NULL)
5986 first_list->lv_used_prev = l;
5987 l->lv_used_prev = NULL;
5988 l->lv_used_next = first_list;
5989 first_list = l;
5990 }
5991 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992}
5993
5994/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005995 * Allocate an empty list for a return value.
5996 * Returns OK or FAIL.
5997 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005999rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006000{
6001 list_T *l = list_alloc();
6002
6003 if (l == NULL)
6004 return FAIL;
6005
6006 rettv->vval.v_list = l;
6007 rettv->v_type = VAR_LIST;
6008 ++l->lv_refcount;
6009 return OK;
6010}
6011
6012/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 * Unreference a list: decrement the reference count and free it when it
6014 * becomes zero.
6015 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006016 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006017list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006019 if (l != NULL && --l->lv_refcount <= 0)
6020 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021}
6022
6023/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006024 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 * Ignores the reference count.
6026 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028list_free(
6029 list_T *l,
6030 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031{
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006034 /* Remove the list from the list of lists for garbage collection. */
6035 if (l->lv_used_prev == NULL)
6036 first_list = l->lv_used_next;
6037 else
6038 l->lv_used_prev->lv_used_next = l->lv_used_next;
6039 if (l->lv_used_next != NULL)
6040 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6041
Bram Moolenaard9fba312005-06-26 22:34:35 +00006042 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006044 /* Remove the item before deleting it. */
6045 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006046 if (recurse || (item->li_tv.v_type != VAR_LIST
6047 && item->li_tv.v_type != VAR_DICT))
6048 clear_tv(&item->li_tv);
6049 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 }
6051 vim_free(l);
6052}
6053
6054/*
6055 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006056 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006058 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006059listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060{
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062}
6063
6064/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006065 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006068listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006070 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 vim_free(item);
6072}
6073
6074/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075 * Remove a list item from a List and free it. Also clears the value.
6076 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006078listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006079{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006080 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006081 listitem_free(item);
6082}
6083
6084/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 * Get the number of items in a list.
6086 */
6087 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006088list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 if (l == NULL)
6091 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006092 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093}
6094
6095/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096 * Return TRUE when two lists have exactly the same values.
6097 */
6098 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099list_equal(
6100 list_T *l1,
6101 list_T *l2,
6102 int ic, /* ignore case for strings */
6103 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104{
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006107 if (l1 == NULL || l2 == NULL)
6108 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006109 if (l1 == l2)
6110 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006111 if (list_len(l1) != list_len(l2))
6112 return FALSE;
6113
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 for (item1 = l1->lv_first, item2 = l2->lv_first;
6115 item1 != NULL && item2 != NULL;
6116 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118 return FALSE;
6119 return item1 == NULL && item2 == NULL;
6120}
6121
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006122/*
6123 * Return the dictitem that an entry in a hashtable points to.
6124 */
6125 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006126dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006127{
6128 return HI2DI(hi);
6129}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006130
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132 * Return TRUE when two dictionaries have exactly the same key/values.
6133 */
6134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135dict_equal(
6136 dict_T *d1,
6137 dict_T *d2,
6138 int ic, /* ignore case for strings */
6139 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006140{
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 hashitem_T *hi;
6142 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006143 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006145 if (d1 == NULL || d2 == NULL)
6146 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006147 if (d1 == d2)
6148 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 if (dict_len(d1) != dict_len(d2))
6150 return FALSE;
6151
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006152 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 if (!HASHITEM_EMPTY(hi))
6156 {
6157 item2 = dict_find(d2, hi->hi_key, -1);
6158 if (item2 == NULL)
6159 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006161 return FALSE;
6162 --todo;
6163 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006164 }
6165 return TRUE;
6166}
6167
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168static int tv_equal_recurse_limit;
6169
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006170/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006172 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006173 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174 */
6175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176tv_equal(
6177 typval_T *tv1,
6178 typval_T *tv2,
6179 int ic, /* ignore case */
6180 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181{
6182 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006183 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006185 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006187 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6188 if ((tv1->v_type == VAR_FUNC
6189 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6190 && (tv2->v_type == VAR_FUNC
6191 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6192 {
6193 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6194 : tv1->vval.v_partial->pt_name;
6195 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6196 : tv2->vval.v_partial->pt_name;
6197 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6198 }
6199
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006200 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006201 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006203 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006204 * recursiveness to a limit. We guess they are equal then.
6205 * A fixed limit has the problem of still taking an awful long time.
6206 * Reduce the limit every time running into it. That should work fine for
6207 * deeply linked structures that are not recursively linked and catch
6208 * recursiveness quickly. */
6209 if (!recursive)
6210 tv_equal_recurse_limit = 1000;
6211 if (recursive_cnt >= tv_equal_recurse_limit)
6212 {
6213 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006214 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006215 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216
6217 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006218 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006219 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006220 ++recursive_cnt;
6221 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6222 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006223 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006224
6225 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006226 ++recursive_cnt;
6227 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6228 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006229 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006230
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006231 case VAR_NUMBER:
6232 return tv1->vval.v_number == tv2->vval.v_number;
6233
6234 case VAR_STRING:
6235 s1 = get_tv_string_buf(tv1, buf1);
6236 s2 = get_tv_string_buf(tv2, buf2);
6237 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006238
6239 case VAR_SPECIAL:
6240 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006241
6242 case VAR_FLOAT:
6243#ifdef FEAT_FLOAT
6244 return tv1->vval.v_float == tv2->vval.v_float;
6245#endif
6246 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006247#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006248 return tv1->vval.v_job == tv2->vval.v_job;
6249#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006250 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006251#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006252 return tv1->vval.v_channel == tv2->vval.v_channel;
6253#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006254 case VAR_FUNC:
6255 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006256 case VAR_UNKNOWN:
6257 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006258 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006259
Bram Moolenaara03f2332016-02-06 18:09:59 +01006260 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6261 * does not equal anything, not even itself. */
6262 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006263}
6264
6265/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 * Locate item with index "n" in list "l" and return it.
6267 * A negative index is counted from the end; -1 is the last item.
6268 * Returns NULL when "n" is out of range.
6269 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006270 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006271list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006272{
Bram Moolenaar33570922005-01-25 22:26:29 +00006273 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006274 long idx;
6275
6276 if (l == NULL)
6277 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006278
6279 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006280 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006281 n = l->lv_len + n;
6282
6283 /* Check for index out of range. */
6284 if (n < 0 || n >= l->lv_len)
6285 return NULL;
6286
6287 /* When there is a cached index may start search from there. */
6288 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006290 if (n < l->lv_idx / 2)
6291 {
6292 /* closest to the start of the list */
6293 item = l->lv_first;
6294 idx = 0;
6295 }
6296 else if (n > (l->lv_idx + l->lv_len) / 2)
6297 {
6298 /* closest to the end of the list */
6299 item = l->lv_last;
6300 idx = l->lv_len - 1;
6301 }
6302 else
6303 {
6304 /* closest to the cached index */
6305 item = l->lv_idx_item;
6306 idx = l->lv_idx;
6307 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 }
6309 else
6310 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006311 if (n < l->lv_len / 2)
6312 {
6313 /* closest to the start of the list */
6314 item = l->lv_first;
6315 idx = 0;
6316 }
6317 else
6318 {
6319 /* closest to the end of the list */
6320 item = l->lv_last;
6321 idx = l->lv_len - 1;
6322 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006324
6325 while (n > idx)
6326 {
6327 /* search forward */
6328 item = item->li_next;
6329 ++idx;
6330 }
6331 while (n < idx)
6332 {
6333 /* search backward */
6334 item = item->li_prev;
6335 --idx;
6336 }
6337
6338 /* cache the used index */
6339 l->lv_idx = idx;
6340 l->lv_idx_item = item;
6341
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342 return item;
6343}
6344
6345/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006346 * Get list item "l[idx]" as a number.
6347 */
6348 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006349list_find_nr(
6350 list_T *l,
6351 long idx,
6352 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006353{
6354 listitem_T *li;
6355
6356 li = list_find(l, idx);
6357 if (li == NULL)
6358 {
6359 if (errorp != NULL)
6360 *errorp = TRUE;
6361 return -1L;
6362 }
6363 return get_tv_number_chk(&li->li_tv, errorp);
6364}
6365
6366/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006367 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6368 */
6369 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006370list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006371{
6372 listitem_T *li;
6373
6374 li = list_find(l, idx - 1);
6375 if (li == NULL)
6376 {
6377 EMSGN(_(e_listidx), idx);
6378 return NULL;
6379 }
6380 return get_tv_string(&li->li_tv);
6381}
6382
6383/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006384 * Locate "item" list "l" and return its index.
6385 * Returns -1 when "item" is not in the list.
6386 */
6387 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006388list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006389{
6390 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006391 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006392
6393 if (l == NULL)
6394 return -1;
6395 idx = 0;
6396 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6397 ++idx;
6398 if (li == NULL)
6399 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006400 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006401}
6402
6403/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006404 * Append item "item" to the end of list "l".
6405 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006407list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408{
6409 if (l->lv_last == NULL)
6410 {
6411 /* empty list */
6412 l->lv_first = item;
6413 l->lv_last = item;
6414 item->li_prev = NULL;
6415 }
6416 else
6417 {
6418 l->lv_last->li_next = item;
6419 item->li_prev = l->lv_last;
6420 l->lv_last = item;
6421 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006422 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 item->li_next = NULL;
6424}
6425
6426/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006427 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006431list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006433 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006434
Bram Moolenaar05159a02005-02-26 23:04:13 +00006435 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006437 copy_tv(tv, &li->li_tv);
6438 list_append(l, li);
6439 return OK;
6440}
6441
6442/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006443 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006444 * Return FAIL when out of memory.
6445 */
6446 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006447list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006448{
6449 listitem_T *li = listitem_alloc();
6450
6451 if (li == NULL)
6452 return FAIL;
6453 li->li_tv.v_type = VAR_DICT;
6454 li->li_tv.v_lock = 0;
6455 li->li_tv.vval.v_dict = dict;
6456 list_append(list, li);
6457 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006458 return OK;
6459}
6460
6461/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006462 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006463 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006464 * Returns FAIL when out of memory.
6465 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006467list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006468{
6469 listitem_T *li = listitem_alloc();
6470
6471 if (li == NULL)
6472 return FAIL;
6473 list_append(l, li);
6474 li->li_tv.v_type = VAR_STRING;
6475 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006476 if (str == NULL)
6477 li->li_tv.vval.v_string = NULL;
6478 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006479 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006480 return FAIL;
6481 return OK;
6482}
6483
6484/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006485 * Append "n" to list "l".
6486 * Returns FAIL when out of memory.
6487 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006490{
6491 listitem_T *li;
6492
6493 li = listitem_alloc();
6494 if (li == NULL)
6495 return FAIL;
6496 li->li_tv.v_type = VAR_NUMBER;
6497 li->li_tv.v_lock = 0;
6498 li->li_tv.vval.v_number = n;
6499 list_append(l, li);
6500 return OK;
6501}
6502
6503/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 * If "item" is NULL append at the end.
6506 * Return FAIL when out of memory.
6507 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006509list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510{
Bram Moolenaar33570922005-01-25 22:26:29 +00006511 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512
6513 if (ni == NULL)
6514 return FAIL;
6515 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006516 list_insert(l, ni, item);
6517 return OK;
6518}
6519
6520 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006521list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006522{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 if (item == NULL)
6524 /* Append new item at end of list. */
6525 list_append(l, ni);
6526 else
6527 {
6528 /* Insert new item before existing item. */
6529 ni->li_prev = item->li_prev;
6530 ni->li_next = item;
6531 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006532 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006534 ++l->lv_idx;
6535 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006539 l->lv_idx_item = NULL;
6540 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544}
6545
6546/*
6547 * Extend "l1" with "l2".
6548 * If "bef" is NULL append at the end, otherwise insert before this item.
6549 * Returns FAIL when out of memory.
6550 */
6551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006552list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553{
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006555 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006557 /* We also quit the loop when we have inserted the original item count of
6558 * the list, avoid a hang when we extend a list with itself. */
6559 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6561 return FAIL;
6562 return OK;
6563}
6564
6565/*
6566 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6567 * Return FAIL when out of memory.
6568 */
6569 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006570list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571{
Bram Moolenaar33570922005-01-25 22:26:29 +00006572 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006574 if (l1 == NULL || l2 == NULL)
6575 return FAIL;
6576
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006578 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 if (l == NULL)
6580 return FAIL;
6581 tv->v_type = VAR_LIST;
6582 tv->vval.v_list = l;
6583
6584 /* append all items from the second list */
6585 return list_extend(l, l2, NULL);
6586}
6587
6588/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 * Returns NULL when out of memory.
6593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006594 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006595list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596{
Bram Moolenaar33570922005-01-25 22:26:29 +00006597 list_T *copy;
6598 listitem_T *item;
6599 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600
6601 if (orig == NULL)
6602 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603
6604 copy = list_alloc();
6605 if (copy != NULL)
6606 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 if (copyID != 0)
6608 {
6609 /* Do this before adding the items, because one of the items may
6610 * refer back to this list. */
6611 orig->lv_copyID = copyID;
6612 orig->lv_copylist = copy;
6613 }
6614 for (item = orig->lv_first; item != NULL && !got_int;
6615 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 {
6617 ni = listitem_alloc();
6618 if (ni == NULL)
6619 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006620 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006621 {
6622 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6623 {
6624 vim_free(ni);
6625 break;
6626 }
6627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006629 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 list_append(copy, ni);
6631 }
6632 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006633 if (item != NULL)
6634 {
6635 list_unref(copy);
6636 copy = NULL;
6637 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 }
6639
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 return copy;
6641}
6642
6643/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006645 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006646 * This used to be called list_remove, but that conflicts with a Sun header
6647 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006650vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651{
Bram Moolenaar33570922005-01-25 22:26:29 +00006652 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006653
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006654 /* notify watchers */
6655 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006657 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006658 list_fix_watch(l, ip);
6659 if (ip == item2)
6660 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006662
6663 if (item2->li_next == NULL)
6664 l->lv_last = item->li_prev;
6665 else
6666 item2->li_next->li_prev = item->li_prev;
6667 if (item->li_prev == NULL)
6668 l->lv_first = item2->li_next;
6669 else
6670 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006671 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672}
6673
6674/*
6675 * Return an allocated string with the string representation of a list.
6676 * May return NULL.
6677 */
6678 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006679list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680{
6681 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682
6683 if (tv->vval.v_list == NULL)
6684 return NULL;
6685 ga_init2(&ga, (int)sizeof(char), 80);
6686 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006687 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006688 {
6689 vim_free(ga.ga_data);
6690 return NULL;
6691 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692 ga_append(&ga, ']');
6693 ga_append(&ga, NUL);
6694 return (char_u *)ga.ga_data;
6695}
6696
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697typedef struct join_S {
6698 char_u *s;
6699 char_u *tofree;
6700} join_T;
6701
6702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006703list_join_inner(
6704 garray_T *gap, /* to store the result in */
6705 list_T *l,
6706 char_u *sep,
6707 int echo_style,
6708 int copyID,
6709 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006710{
6711 int i;
6712 join_T *p;
6713 int len;
6714 int sumlen = 0;
6715 int first = TRUE;
6716 char_u *tofree;
6717 char_u numbuf[NUMBUFLEN];
6718 listitem_T *item;
6719 char_u *s;
6720
6721 /* Stringify each item in the list. */
6722 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6723 {
6724 if (echo_style)
6725 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6726 else
6727 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6728 if (s == NULL)
6729 return FAIL;
6730
6731 len = (int)STRLEN(s);
6732 sumlen += len;
6733
Bram Moolenaarcde88542015-08-11 19:14:00 +02006734 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006735 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6736 if (tofree != NULL || s != numbuf)
6737 {
6738 p->s = s;
6739 p->tofree = tofree;
6740 }
6741 else
6742 {
6743 p->s = vim_strnsave(s, len);
6744 p->tofree = p->s;
6745 }
6746
6747 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006748 if (did_echo_string_emsg) /* recursion error, bail out */
6749 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006750 }
6751
6752 /* Allocate result buffer with its total size, avoid re-allocation and
6753 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6754 if (join_gap->ga_len >= 2)
6755 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6756 if (ga_grow(gap, sumlen + 2) == FAIL)
6757 return FAIL;
6758
6759 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6760 {
6761 if (first)
6762 first = FALSE;
6763 else
6764 ga_concat(gap, sep);
6765 p = ((join_T *)join_gap->ga_data) + i;
6766
6767 if (p->s != NULL)
6768 ga_concat(gap, p->s);
6769 line_breakcheck();
6770 }
6771
6772 return OK;
6773}
6774
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006775/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006777 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006778 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006781list_join(
6782 garray_T *gap,
6783 list_T *l,
6784 char_u *sep,
6785 int echo_style,
6786 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006788 garray_T join_ga;
6789 int retval;
6790 join_T *p;
6791 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792
Bram Moolenaard39a7512015-04-16 22:51:22 +02006793 if (l->lv_len < 1)
6794 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006795 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6796 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6797
6798 /* Dispose each item in join_ga. */
6799 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006801 p = (join_T *)join_ga.ga_data;
6802 for (i = 0; i < join_ga.ga_len; ++i)
6803 {
6804 vim_free(p->tofree);
6805 ++p;
6806 }
6807 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006809
6810 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811}
6812
6813/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006814 * Return the next (unique) copy ID.
6815 * Used for serializing nested structures.
6816 */
6817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006818get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006819{
6820 current_copyID += COPYID_INC;
6821 return current_copyID;
6822}
6823
6824/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825 * Garbage collection for lists and dictionaries.
6826 *
6827 * We use reference counts to be able to free most items right away when they
6828 * are no longer used. But for composite items it's possible that it becomes
6829 * unused while the reference count is > 0: When there is a recursive
6830 * reference. Example:
6831 * :let l = [1, 2, 3]
6832 * :let d = {9: l}
6833 * :let l[1] = d
6834 *
6835 * Since this is quite unusual we handle this with garbage collection: every
6836 * once in a while find out which lists and dicts are not referenced from any
6837 * variable.
6838 *
6839 * Here is a good reference text about garbage collection (refers to Python
6840 * but it applies to all reference-counting mechanisms):
6841 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006842 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006843
6844/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 * Do garbage collection for lists and dicts.
6846 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006847 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006849garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006850{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006851 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006852 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853 buf_T *buf;
6854 win_T *wp;
6855 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006856 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006857 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006859#ifdef FEAT_WINDOWS
6860 tabpage_T *tp;
6861#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006863 /* Only do this once. */
6864 want_garbage_collect = FALSE;
6865 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006866 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006867
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 /* We advance by two because we add one for items referenced through
6869 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006870 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 /*
6873 * 1. Go through all accessible variables and mark all lists and dicts
6874 * with copyID.
6875 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006876
6877 /* Don't free variables in the previous_funccal list unless they are only
6878 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006879 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006880 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6881 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006882 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6883 NULL);
6884 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6885 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886 }
6887
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006888 /* script-local variables */
6889 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891
6892 /* buffer-local variables */
6893 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6895 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896
6897 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006898 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6900 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006901#ifdef FEAT_AUTOCMD
6902 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6904 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006905#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006907#ifdef FEAT_WINDOWS
6908 /* tabpage-local variables */
6909 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6911 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006912#endif
6913
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006914 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916
6917 /* function-local variables */
6918 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6919 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6921 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922 }
6923
Bram Moolenaard812df62008-11-09 12:46:09 +00006924 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006926
Bram Moolenaar1dced572012-04-05 16:54:08 +02006927#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006929#endif
6930
Bram Moolenaardb913952012-06-29 12:54:53 +02006931#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006933#endif
6934
6935#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006936 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006937#endif
6938
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006939#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006940 abort = abort || set_ref_in_channel(copyID);
6941#endif
6942
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006943 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006944 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 /*
6946 * 2. Free lists and dictionaries that are not referenced.
6947 */
6948 did_free = free_unref_items(copyID);
6949
6950 /*
6951 * 3. Check if any funccal can be freed now.
6952 */
6953 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 if (can_free_funccal(*pfc, copyID))
6956 {
6957 fc = *pfc;
6958 *pfc = fc->caller;
6959 free_funccal(fc, TRUE);
6960 did_free = TRUE;
6961 did_free_funccal = TRUE;
6962 }
6963 else
6964 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006966 if (did_free_funccal)
6967 /* When a funccal was freed some more items might be garbage
6968 * collected, so run again. */
6969 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006971 else if (p_verbose > 0)
6972 {
6973 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6974 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975
6976 return did_free;
6977}
6978
6979/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006980 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 */
6982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006983free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006985 dict_T *dd, *dd_next;
6986 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987 int did_free = FALSE;
6988
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 */
6992 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006993 {
6994 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006995 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006997 /* Free the Dictionary and ordinary items it contains, but don't
6998 * recurse into Lists and Dictionaries, they will be in the list
6999 * of dicts or list of lists. */
7000 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007003 dd = dd_next;
7004 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005
7006 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007007 * Go through the list of lists and free items without the copyID.
7008 * But don't free a list that has a watcher (used in a for loop), these
7009 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 */
7011 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007012 {
7013 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007014 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7015 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007017 /* Free the List and ordinary items it contains, but don't recurse
7018 * into Lists and Dictionaries, they will be in the list of dicts
7019 * or list of lists. */
7020 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007023 ll = ll_next;
7024 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007025
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 return did_free;
7027}
7028
7029/*
7030 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 * "list_stack" is used to add lists to be marked. Can be NULL.
7032 *
7033 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007036set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037{
7038 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 hashtab_T *cur_ht;
7042 ht_stack_T *ht_stack = NULL;
7043 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 cur_ht = ht;
7046 for (;;)
7047 {
7048 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 /* Mark each item in the hashtab. If the item contains a hashtab
7051 * it is added to ht_stack, if it contains a list it is added to
7052 * list_stack. */
7053 todo = (int)cur_ht->ht_used;
7054 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7055 if (!HASHITEM_EMPTY(hi))
7056 {
7057 --todo;
7058 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7059 &ht_stack, list_stack);
7060 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007061 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062
7063 if (ht_stack == NULL)
7064 break;
7065
7066 /* take an item from the stack */
7067 cur_ht = ht_stack->ht;
7068 tempitem = ht_stack;
7069 ht_stack = ht_stack->prev;
7070 free(tempitem);
7071 }
7072
7073 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074}
7075
7076/*
7077 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007078 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7079 *
7080 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007082 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007083set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 listitem_T *li;
7086 int abort = FALSE;
7087 list_T *cur_l;
7088 list_stack_T *list_stack = NULL;
7089 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 cur_l = l;
7092 for (;;)
7093 {
7094 if (!abort)
7095 /* Mark each item in the list. If the item contains a hashtab
7096 * it is added to ht_stack, if it contains a list it is added to
7097 * list_stack. */
7098 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7099 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7100 ht_stack, &list_stack);
7101 if (list_stack == NULL)
7102 break;
7103
7104 /* take an item from the stack */
7105 cur_l = list_stack->list;
7106 tempitem = list_stack;
7107 list_stack = list_stack->prev;
7108 free(tempitem);
7109 }
7110
7111 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112}
7113
7114/*
7115 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 * "list_stack" is used to add lists to be marked. Can be NULL.
7117 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7118 *
7119 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007121 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007122set_ref_in_item(
7123 typval_T *tv,
7124 int copyID,
7125 ht_stack_T **ht_stack,
7126 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007127{
7128 dict_T *dd;
7129 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007131
Bram Moolenaara03f2332016-02-06 18:09:59 +01007132 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007133 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007134 dd = tv->vval.v_dict;
7135 if (dd != NULL && dd->dv_copyID != copyID)
7136 {
7137 /* Didn't see this dict yet. */
7138 dd->dv_copyID = copyID;
7139 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007140 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007141 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7142 }
7143 else
7144 {
7145 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7146 if (newitem == NULL)
7147 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007148 else
7149 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007150 newitem->ht = &dd->dv_hashtab;
7151 newitem->prev = *ht_stack;
7152 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007153 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007155 }
7156 }
7157 else if (tv->v_type == VAR_LIST)
7158 {
7159 ll = tv->vval.v_list;
7160 if (ll != NULL && ll->lv_copyID != copyID)
7161 {
7162 /* Didn't see this list yet. */
7163 ll->lv_copyID = copyID;
7164 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007165 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007166 abort = set_ref_in_list(ll, copyID, ht_stack);
7167 }
7168 else
7169 {
7170 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007171 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007172 if (newitem == NULL)
7173 abort = TRUE;
7174 else
7175 {
7176 newitem->list = ll;
7177 newitem->prev = *list_stack;
7178 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007179 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007180 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007181 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007182 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007183 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007184}
7185
7186/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187 * Allocate an empty header for a dictionary.
7188 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007189 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007190dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191{
Bram Moolenaar33570922005-01-25 22:26:29 +00007192 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007196 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007197 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007198 if (first_dict != NULL)
7199 first_dict->dv_used_prev = d;
7200 d->dv_used_next = first_dict;
7201 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007202 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007203
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007205 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007206 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007207 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007209 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211}
7212
7213/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007214 * Allocate an empty dict for a return value.
7215 * Returns OK or FAIL.
7216 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007217 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007218rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007219{
7220 dict_T *d = dict_alloc();
7221
7222 if (d == NULL)
7223 return FAIL;
7224
7225 rettv->vval.v_dict = d;
7226 rettv->v_type = VAR_DICT;
7227 ++d->dv_refcount;
7228 return OK;
7229}
7230
7231
7232/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233 * Unreference a Dictionary: decrement the reference count and free it when it
7234 * becomes zero.
7235 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007237dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007239 if (d != NULL && --d->dv_refcount <= 0)
7240 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241}
7242
7243/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007244 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 * Ignores the reference count.
7246 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007248dict_free(
7249 dict_T *d,
7250 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007252 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007254 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007256 /* Remove the dict from the list of dicts for garbage collection. */
7257 if (d->dv_used_prev == NULL)
7258 first_dict = d->dv_used_next;
7259 else
7260 d->dv_used_prev->dv_used_next = d->dv_used_next;
7261 if (d->dv_used_next != NULL)
7262 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7263
7264 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007265 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007266 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 if (!HASHITEM_EMPTY(hi))
7270 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007271 /* Remove the item before deleting it, just in case there is
7272 * something recursive causing trouble. */
7273 di = HI2DI(hi);
7274 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007275 if (recurse || (di->di_tv.v_type != VAR_LIST
7276 && di->di_tv.v_type != VAR_DICT))
7277 clear_tv(&di->di_tv);
7278 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007279 --todo;
7280 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283 vim_free(d);
7284}
7285
7286/*
7287 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 * The "key" is copied to the new item.
7289 * Note that the value of the item "di_tv" still needs to be initialized!
7290 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007292 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007293dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294{
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007297 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007301 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007302 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304}
7305
7306/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007307 * Make a copy of a Dictionary item.
7308 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007310dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007311{
Bram Moolenaar33570922005-01-25 22:26:29 +00007312 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007314 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7315 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007316 if (di != NULL)
7317 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007319 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 copy_tv(&org->di_tv, &di->di_tv);
7321 }
7322 return di;
7323}
7324
7325/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326 * Remove item "item" from Dictionary "dict" and free it.
7327 */
7328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007329dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330{
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334 if (HASHITEM_EMPTY(hi))
7335 EMSG2(_(e_intern2), "dictitem_remove()");
7336 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338 dictitem_free(item);
7339}
7340
7341/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 * Free a dict item. Also clears the value.
7343 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007345dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007348 if (item->di_flags & DI_FLAGS_ALLOC)
7349 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350}
7351
7352/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7354 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007355 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007356 * Returns NULL when out of memory.
7357 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007359dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360{
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 dict_T *copy;
7362 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365
7366 if (orig == NULL)
7367 return NULL;
7368
7369 copy = dict_alloc();
7370 if (copy != NULL)
7371 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007372 if (copyID != 0)
7373 {
7374 orig->dv_copyID = copyID;
7375 orig->dv_copydict = copy;
7376 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007377 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007380 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 --todo;
7383
7384 di = dictitem_alloc(hi->hi_key);
7385 if (di == NULL)
7386 break;
7387 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007388 {
7389 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7390 copyID) == FAIL)
7391 {
7392 vim_free(di);
7393 break;
7394 }
7395 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007396 else
7397 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7398 if (dict_add(copy, di) == FAIL)
7399 {
7400 dictitem_free(di);
7401 break;
7402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007403 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007405
Bram Moolenaare9a41262005-01-15 22:18:47 +00007406 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 if (todo > 0)
7408 {
7409 dict_unref(copy);
7410 copy = NULL;
7411 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 }
7413
7414 return copy;
7415}
7416
7417/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007419 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007422dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423{
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425}
7426
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007428 * Add a number or string entry to dictionary "d".
7429 * When "str" is NULL use number "nr", otherwise use "str".
7430 * Returns FAIL when out of memory and when key already exists.
7431 */
7432 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007433dict_add_nr_str(
7434 dict_T *d,
7435 char *key,
7436 long nr,
7437 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007438{
7439 dictitem_T *item;
7440
7441 item = dictitem_alloc((char_u *)key);
7442 if (item == NULL)
7443 return FAIL;
7444 item->di_tv.v_lock = 0;
7445 if (str == NULL)
7446 {
7447 item->di_tv.v_type = VAR_NUMBER;
7448 item->di_tv.vval.v_number = nr;
7449 }
7450 else
7451 {
7452 item->di_tv.v_type = VAR_STRING;
7453 item->di_tv.vval.v_string = vim_strsave(str);
7454 }
7455 if (dict_add(d, item) == FAIL)
7456 {
7457 dictitem_free(item);
7458 return FAIL;
7459 }
7460 return OK;
7461}
7462
7463/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007464 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007465 * Returns FAIL when out of memory and when key already exists.
7466 */
7467 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007468dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007469{
7470 dictitem_T *item;
7471
7472 item = dictitem_alloc((char_u *)key);
7473 if (item == NULL)
7474 return FAIL;
7475 item->di_tv.v_lock = 0;
7476 item->di_tv.v_type = VAR_LIST;
7477 item->di_tv.vval.v_list = list;
7478 if (dict_add(d, item) == FAIL)
7479 {
7480 dictitem_free(item);
7481 return FAIL;
7482 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007483 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007484 return OK;
7485}
7486
7487/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488 * Get the number of items in a Dictionary.
7489 */
7490 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007491dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007492{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493 if (d == NULL)
7494 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007495 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007496}
7497
7498/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499 * Find item "key[len]" in Dictionary "d".
7500 * If "len" is negative use strlen(key).
7501 * Returns NULL when not found.
7502 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007503 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007504dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506#define AKEYLEN 200
7507 char_u buf[AKEYLEN];
7508 char_u *akey;
7509 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512 if (len < 0)
7513 akey = key;
7514 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007515 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 tofree = akey = vim_strnsave(key, len);
7517 if (akey == NULL)
7518 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007519 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520 else
7521 {
7522 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007523 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 akey = buf;
7525 }
7526
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007528 vim_free(tofree);
7529 if (HASHITEM_EMPTY(hi))
7530 return NULL;
7531 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532}
7533
7534/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007535 * Get a string item from a dictionary.
7536 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007537 * Returns NULL if the entry doesn't exist or out of memory.
7538 */
7539 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007540get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541{
7542 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007543 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007544
7545 di = dict_find(d, key, -1);
7546 if (di == NULL)
7547 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007548 s = get_tv_string(&di->di_tv);
7549 if (save && s != NULL)
7550 s = vim_strsave(s);
7551 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007552}
7553
7554/*
7555 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007556 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007557 */
7558 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007559get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007560{
7561 dictitem_T *di;
7562
7563 di = dict_find(d, key, -1);
7564 if (di == NULL)
7565 return 0;
7566 return get_tv_number(&di->di_tv);
7567}
7568
7569/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 * Return an allocated string with the string representation of a Dictionary.
7571 * May return NULL.
7572 */
7573 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007574dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575{
7576 garray_T ga;
7577 int first = TRUE;
7578 char_u *tofree;
7579 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007583 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007585 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 return NULL;
7587 ga_init2(&ga, (int)sizeof(char), 80);
7588 ga_append(&ga, '{');
7589
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007590 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007591 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007593 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007595 --todo;
7596
7597 if (first)
7598 first = FALSE;
7599 else
7600 ga_concat(&ga, (char_u *)", ");
7601
7602 tofree = string_quote(hi->hi_key, FALSE);
7603 if (tofree != NULL)
7604 {
7605 ga_concat(&ga, tofree);
7606 vim_free(tofree);
7607 }
7608 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007610 if (s != NULL)
7611 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007613 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007614 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007615 line_breakcheck();
7616
Bram Moolenaar8c711452005-01-14 21:53:12 +00007617 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007619 if (todo > 0)
7620 {
7621 vim_free(ga.ga_data);
7622 return NULL;
7623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624
7625 ga_append(&ga, '}');
7626 ga_append(&ga, NUL);
7627 return (char_u *)ga.ga_data;
7628}
7629
7630/*
7631 * Allocate a variable for a Dictionary and fill it from "*arg".
7632 * Return OK or FAIL. Returns NOTDONE for {expr}.
7633 */
7634 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007635get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636{
Bram Moolenaar33570922005-01-25 22:26:29 +00007637 dict_T *d = NULL;
7638 typval_T tvkey;
7639 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007640 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007641 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007643 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007644
7645 /*
7646 * First check if it's not a curly-braces thing: {expr}.
7647 * Must do this without evaluating, otherwise a function may be called
7648 * twice. Unfortunately this means we need to call eval1() twice for the
7649 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007650 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007652 if (*start != '}')
7653 {
7654 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7655 return FAIL;
7656 if (*start == '}')
7657 return NOTDONE;
7658 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659
7660 if (evaluate)
7661 {
7662 d = dict_alloc();
7663 if (d == NULL)
7664 return FAIL;
7665 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007666 tvkey.v_type = VAR_UNKNOWN;
7667 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668
7669 *arg = skipwhite(*arg + 1);
7670 while (**arg != '}' && **arg != NUL)
7671 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007672 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 goto failret;
7674 if (**arg != ':')
7675 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007676 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007677 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 goto failret;
7679 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007680 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007682 key = get_tv_string_buf_chk(&tvkey, buf);
7683 if (key == NULL || *key == NUL)
7684 {
7685 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7686 if (key != NULL)
7687 EMSG(_(e_emptykey));
7688 clear_tv(&tvkey);
7689 goto failret;
7690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692
7693 *arg = skipwhite(*arg + 1);
7694 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7695 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007696 if (evaluate)
7697 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 goto failret;
7699 }
7700 if (evaluate)
7701 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007702 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 if (item != NULL)
7704 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007705 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007706 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707 clear_tv(&tv);
7708 goto failret;
7709 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007710 item = dictitem_alloc(key);
7711 clear_tv(&tvkey);
7712 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007715 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007716 if (dict_add(d, item) == FAIL)
7717 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 }
7719 }
7720
7721 if (**arg == '}')
7722 break;
7723 if (**arg != ',')
7724 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007725 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 goto failret;
7727 }
7728 *arg = skipwhite(*arg + 1);
7729 }
7730
7731 if (**arg != '}')
7732 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007733 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734failret:
7735 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007736 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 return FAIL;
7738 }
7739
7740 *arg = skipwhite(*arg + 1);
7741 if (evaluate)
7742 {
7743 rettv->v_type = VAR_DICT;
7744 rettv->vval.v_dict = d;
7745 ++d->dv_refcount;
7746 }
7747
7748 return OK;
7749}
7750
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007751#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007752#endif
7753
Bram Moolenaar17a13432016-01-24 14:22:10 +01007754 static char *
7755get_var_special_name(int nr)
7756{
7757 switch (nr)
7758 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007759 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007760 case VVAL_TRUE: return "v:true";
7761 case VVAL_NONE: return "v:none";
7762 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007763 }
7764 EMSG2(_(e_intern2), "get_var_special_name()");
7765 return "42";
7766}
7767
Bram Moolenaar8c711452005-01-14 21:53:12 +00007768/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007769 * Return a string with the string representation of a variable.
7770 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007771 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007772 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007773 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007774 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775 */
7776 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007777echo_string(
7778 typval_T *tv,
7779 char_u **tofree,
7780 char_u *numbuf,
7781 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007782{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007783 static int recurse = 0;
7784 char_u *r = NULL;
7785
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007787 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007788 if (!did_echo_string_emsg)
7789 {
7790 /* Only give this message once for a recursive call to avoid
7791 * flooding the user with errors. And stop iterating over lists
7792 * and dicts. */
7793 did_echo_string_emsg = TRUE;
7794 EMSG(_("E724: variable nested too deep for displaying"));
7795 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007796 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007797 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007798 }
7799 ++recurse;
7800
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801 switch (tv->v_type)
7802 {
7803 case VAR_FUNC:
7804 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007805 r = tv->vval.v_string;
7806 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007807
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007808 case VAR_PARTIAL:
7809 *tofree = NULL;
7810 /* TODO: arguments */
7811 r = tv->vval.v_partial == NULL ? NULL : tv->vval.v_partial->pt_name;
7812 break;
7813
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007815 if (tv->vval.v_list == NULL)
7816 {
7817 *tofree = NULL;
7818 r = NULL;
7819 }
7820 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7821 {
7822 *tofree = NULL;
7823 r = (char_u *)"[...]";
7824 }
7825 else
7826 {
7827 tv->vval.v_list->lv_copyID = copyID;
7828 *tofree = list2string(tv, copyID);
7829 r = *tofree;
7830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007831 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007832
Bram Moolenaar8c711452005-01-14 21:53:12 +00007833 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007834 if (tv->vval.v_dict == NULL)
7835 {
7836 *tofree = NULL;
7837 r = NULL;
7838 }
7839 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7840 {
7841 *tofree = NULL;
7842 r = (char_u *)"{...}";
7843 }
7844 else
7845 {
7846 tv->vval.v_dict->dv_copyID = copyID;
7847 *tofree = dict2string(tv, copyID);
7848 r = *tofree;
7849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007851
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007852 case VAR_STRING:
7853 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007854 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007855 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007856 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007857 *tofree = NULL;
7858 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007859 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007860
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007861 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007862#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007863 *tofree = NULL;
7864 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7865 r = numbuf;
7866 break;
7867#endif
7868
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007869 case VAR_SPECIAL:
7870 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007871 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007872 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874
Bram Moolenaar8502c702014-06-17 12:51:16 +02007875 if (--recurse == 0)
7876 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878}
7879
7880/*
7881 * Return a string with the string representation of a variable.
7882 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7883 * "numbuf" is used for a number.
7884 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007885 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 */
7887 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007888tv2string(
7889 typval_T *tv,
7890 char_u **tofree,
7891 char_u *numbuf,
7892 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893{
7894 switch (tv->v_type)
7895 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896 case VAR_FUNC:
7897 *tofree = string_quote(tv->vval.v_string, TRUE);
7898 return *tofree;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007899 case VAR_PARTIAL:
Bram Moolenaar5c291542016-03-19 20:05:45 +01007900 {
7901 partial_T *pt = tv->vval.v_partial;
7902 char_u *fname = string_quote(pt == NULL ? NULL
7903 : pt->pt_name, FALSE);
7904 garray_T ga;
7905 int i;
7906 char_u *tf;
7907
7908 ga_init2(&ga, 1, 100);
7909 ga_concat(&ga, (char_u *)"function(");
7910 if (fname != NULL)
7911 {
7912 ga_concat(&ga, fname);
7913 vim_free(fname);
7914 }
7915 if (pt != NULL && pt->pt_argc > 0)
7916 {
7917 ga_concat(&ga, (char_u *)", [");
7918 for (i = 0; i < pt->pt_argc; ++i)
7919 {
7920 if (i > 0)
7921 ga_concat(&ga, (char_u *)", ");
7922 ga_concat(&ga,
7923 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7924 vim_free(tf);
7925 }
7926 ga_concat(&ga, (char_u *)"]");
7927 }
7928 if (pt != NULL && pt->pt_dict != NULL)
7929 {
7930 typval_T dtv;
7931
7932 ga_concat(&ga, (char_u *)", ");
7933 dtv.v_type = VAR_DICT;
7934 dtv.vval.v_dict = pt->pt_dict;
7935 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7936 vim_free(tf);
7937 }
7938 ga_concat(&ga, (char_u *)")");
7939
7940 *tofree = ga.ga_data;
7941 return *tofree;
7942 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007943 case VAR_STRING:
7944 *tofree = string_quote(tv->vval.v_string, FALSE);
7945 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007946 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007947#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007948 *tofree = NULL;
7949 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7950 return numbuf;
7951#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007952 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007953 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007954 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007955 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007956 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007957 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007958 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007959 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007960 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007961 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962}
7963
7964/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007965 * Return string "str" in ' quotes, doubling ' characters.
7966 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007968 */
7969 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007970string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007971{
Bram Moolenaar33570922005-01-25 22:26:29 +00007972 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007973 char_u *p, *r, *s;
7974
Bram Moolenaar33570922005-01-25 22:26:29 +00007975 len = (function ? 13 : 3);
7976 if (str != NULL)
7977 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007978 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 for (p = str; *p != NUL; mb_ptr_adv(p))
7980 if (*p == '\'')
7981 ++len;
7982 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007983 s = r = alloc(len);
7984 if (r != NULL)
7985 {
7986 if (function)
7987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007988 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007989 r += 10;
7990 }
7991 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007992 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007993 if (str != NULL)
7994 for (p = str; *p != NUL; )
7995 {
7996 if (*p == '\'')
7997 *r++ = '\'';
7998 MB_COPY_CHAR(p, r);
7999 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008000 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008001 if (function)
8002 *r++ = ')';
8003 *r++ = NUL;
8004 }
8005 return s;
8006}
8007
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008008#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008009/*
8010 * Convert the string "text" to a floating point number.
8011 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8012 * this always uses a decimal point.
8013 * Returns the length of the text that was consumed.
8014 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008015 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008016string2float(
8017 char_u *text,
8018 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008019{
8020 char *s = (char *)text;
8021 float_T f;
8022
8023 f = strtod(s, &s);
8024 *value = f;
8025 return (int)((char_u *)s - text);
8026}
8027#endif
8028
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 * Get the value of an environment variable.
8031 * "arg" is pointing to the '$'. It is advanced to after the name.
8032 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008033 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 */
8035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008036get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037{
8038 char_u *string = NULL;
8039 int len;
8040 int cc;
8041 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008042 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043
8044 ++*arg;
8045 name = *arg;
8046 len = get_env_len(arg);
8047 if (evaluate)
8048 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008049 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008050 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008051
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008052 cc = name[len];
8053 name[len] = NUL;
8054 /* first try vim_getenv(), fast for normal environment vars */
8055 string = vim_getenv(name, &mustfree);
8056 if (string != NULL && *string != NUL)
8057 {
8058 if (!mustfree)
8059 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008061 else
8062 {
8063 if (mustfree)
8064 vim_free(string);
8065
8066 /* next try expanding things like $VIM and ${HOME} */
8067 string = expand_env_save(name - 1);
8068 if (string != NULL && *string == '$')
8069 {
8070 vim_free(string);
8071 string = NULL;
8072 }
8073 }
8074 name[len] = cc;
8075
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008076 rettv->v_type = VAR_STRING;
8077 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 }
8079
8080 return OK;
8081}
8082
8083/*
8084 * Array with names and number of arguments of all internal functions
8085 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8086 */
8087static struct fst
8088{
8089 char *f_name; /* function name */
8090 char f_min_argc; /* minimal number of arguments */
8091 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008092 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008093 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094} functions[] =
8095{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008096#ifdef FEAT_FLOAT
8097 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008098 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008100 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008101 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008102 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"append", 2, 2, f_append},
8104 {"argc", 0, 0, f_argc},
8105 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008106 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008107 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008108#ifdef FEAT_FLOAT
8109 {"asin", 1, 1, f_asin}, /* WJMc */
8110#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008111 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008112 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008113 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008114 {"assert_false", 1, 2, f_assert_false},
8115 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#ifdef FEAT_FLOAT
8117 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008118 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008121 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"bufexists", 1, 1, f_bufexists},
8123 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8124 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8125 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8126 {"buflisted", 1, 1, f_buflisted},
8127 {"bufloaded", 1, 1, f_bufloaded},
8128 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008129 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"bufwinnr", 1, 1, f_bufwinnr},
8131 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008132 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008133 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008134 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008135#ifdef FEAT_FLOAT
8136 {"ceil", 1, 1, f_ceil},
8137#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008138#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008139 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008140 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8141 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008142 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008143 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008144 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008145 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008146 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008147 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008148 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008149 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8150 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008151 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008152 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008153#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008154 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008155 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008157 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008159#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008160 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008161 {"complete_add", 1, 1, f_complete_add},
8162 {"complete_check", 0, 0, f_complete_check},
8163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008165 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008166#ifdef FEAT_FLOAT
8167 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008168 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008169#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008170 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008172 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008173 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008174 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008176 {"diff_filler", 1, 1, f_diff_filler},
8177 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008178 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008179 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008181 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"eventhandler", 0, 0, f_eventhandler},
8183 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008184 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008186#ifdef FEAT_FLOAT
8187 {"exp", 1, 1, f_exp},
8188#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008189 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008190 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008191 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8193 {"filereadable", 1, 1, f_filereadable},
8194 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008195 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008196 {"finddir", 1, 3, f_finddir},
8197 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008198#ifdef FEAT_FLOAT
8199 {"float2nr", 1, 1, f_float2nr},
8200 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008201 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008202#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008203 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"fnamemodify", 2, 2, f_fnamemodify},
8205 {"foldclosed", 1, 1, f_foldclosed},
8206 {"foldclosedend", 1, 1, f_foldclosedend},
8207 {"foldlevel", 1, 1, f_foldlevel},
8208 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008209 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008211 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008212 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008213 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008214 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008215 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"getchar", 0, 1, f_getchar},
8217 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008218 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"getcmdline", 0, 0, f_getcmdline},
8220 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008221 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008222 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008223 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008224 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008225 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008226 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"getfsize", 1, 1, f_getfsize},
8228 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008229 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008230 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008231 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008232 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008233 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008234 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008235 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008236 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008238 {"gettabvar", 2, 3, f_gettabvar},
8239 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {"getwinposx", 0, 0, f_getwinposx},
8241 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008242 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008243 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008244 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008245 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008247 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008248 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008249 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8251 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8252 {"histadd", 2, 2, f_histadd},
8253 {"histdel", 1, 2, f_histdel},
8254 {"histget", 1, 2, f_histget},
8255 {"histnr", 1, 1, f_histnr},
8256 {"hlID", 1, 1, f_hlID},
8257 {"hlexists", 1, 1, f_hlexists},
8258 {"hostname", 0, 0, f_hostname},
8259 {"iconv", 3, 3, f_iconv},
8260 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008261 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008262 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008264 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {"inputrestore", 0, 0, f_inputrestore},
8266 {"inputsave", 0, 0, f_inputsave},
8267 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008269 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008271 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008272#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8273 {"isnan", 1, 1, f_isnan},
8274#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008275 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008276#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008277 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008278 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008279 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008280 {"job_start", 1, 2, f_job_start},
8281 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008282 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008283#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008284 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008285 {"js_decode", 1, 1, f_js_decode},
8286 {"js_encode", 1, 1, f_js_encode},
8287 {"json_decode", 1, 1, f_json_decode},
8288 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008289 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008291 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"libcall", 3, 3, f_libcall},
8293 {"libcallnr", 3, 3, f_libcallnr},
8294 {"line", 1, 1, f_line},
8295 {"line2byte", 1, 1, f_line2byte},
8296 {"lispindent", 1, 1, f_lispindent},
8297 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008299 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008300 {"log10", 1, 1, f_log10},
8301#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008302#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008303 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008304#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008305 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008306 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008307 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008308 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008309 {"matchadd", 2, 5, f_matchadd},
8310 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008311 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008312 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008313 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008314 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008315 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008316 {"max", 1, 1, f_max},
8317 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008318#ifdef vim_mkdir
8319 {"mkdir", 1, 3, f_mkdir},
8320#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008321 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008322#ifdef FEAT_MZSCHEME
8323 {"mzeval", 1, 1, f_mzeval},
8324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008326 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008327 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008328 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008329#ifdef FEAT_PERL
8330 {"perleval", 1, 1, f_perleval},
8331#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008332#ifdef FEAT_FLOAT
8333 {"pow", 2, 2, f_pow},
8334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008336 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008337 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008338#ifdef FEAT_PYTHON3
8339 {"py3eval", 1, 1, f_py3eval},
8340#endif
8341#ifdef FEAT_PYTHON
8342 {"pyeval", 1, 1, f_pyeval},
8343#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008344 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008345 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008346 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008347#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008348 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008349#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008350 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {"remote_expr", 2, 3, f_remote_expr},
8352 {"remote_foreground", 1, 1, f_remote_foreground},
8353 {"remote_peek", 1, 2, f_remote_peek},
8354 {"remote_read", 1, 1, f_remote_read},
8355 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008356 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008358 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008360 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008361#ifdef FEAT_FLOAT
8362 {"round", 1, 1, f_round},
8363#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008364 {"screenattr", 2, 2, f_screenattr},
8365 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008366 {"screencol", 0, 0, f_screencol},
8367 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008368 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008369 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008370 {"searchpair", 3, 7, f_searchpair},
8371 {"searchpairpos", 3, 7, f_searchpairpos},
8372 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {"server2client", 2, 2, f_server2client},
8374 {"serverlist", 0, 0, f_serverlist},
8375 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008376 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008378 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008380 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008381 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008382 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008383 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008385 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008386 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008388#ifdef FEAT_CRYPT
8389 {"sha256", 1, 1, f_sha256},
8390#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008391 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008392 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008394#ifdef FEAT_FLOAT
8395 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008396 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008397#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008398 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008399 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008400 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008401 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008402 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008403#ifdef FEAT_FLOAT
8404 {"sqrt", 1, 1, f_sqrt},
8405 {"str2float", 1, 1, f_str2float},
8406#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008407 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008408 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008409 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410#ifdef HAVE_STRFTIME
8411 {"strftime", 1, 2, f_strftime},
8412#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008413 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008414 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {"strlen", 1, 1, f_strlen},
8416 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008417 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008419 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008420 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"substitute", 4, 4, f_substitute},
8422 {"synID", 3, 3, f_synID},
8423 {"synIDattr", 2, 3, f_synIDattr},
8424 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008425 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008426 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008427 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008428 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008429 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008430 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008431 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008432 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008433 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008434#ifdef FEAT_FLOAT
8435 {"tan", 1, 1, f_tan},
8436 {"tanh", 1, 1, f_tanh},
8437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008439 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008440#ifdef FEAT_TIMERS
8441 {"timer_start", 2, 3, f_timer_start},
8442 {"timer_stop", 1, 1, f_timer_stop},
8443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {"tolower", 1, 1, f_tolower},
8445 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008446 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008447#ifdef FEAT_FLOAT
8448 {"trunc", 1, 1, f_trunc},
8449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008451 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008452 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008453 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008454 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {"virtcol", 1, 1, f_virtcol},
8456 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008457 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008458 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008459 {"win_getid", 0, 2, f_win_getid},
8460 {"win_gotoid", 1, 1, f_win_gotoid},
8461 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8462 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {"winbufnr", 1, 1, f_winbufnr},
8464 {"wincol", 0, 0, f_wincol},
8465 {"winheight", 1, 1, f_winheight},
8466 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008467 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008469 {"winrestview", 1, 1, f_winrestview},
8470 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008472 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008473 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008474 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475};
8476
8477#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8478
8479/*
8480 * Function given to ExpandGeneric() to obtain the list of internal
8481 * or user defined function names.
8482 */
8483 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008484get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
8486 static int intidx = -1;
8487 char_u *name;
8488
8489 if (idx == 0)
8490 intidx = -1;
8491 if (intidx < 0)
8492 {
8493 name = get_user_func_name(xp, idx);
8494 if (name != NULL)
8495 return name;
8496 }
8497 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8498 {
8499 STRCPY(IObuff, functions[intidx].f_name);
8500 STRCAT(IObuff, "(");
8501 if (functions[intidx].f_max_argc == 0)
8502 STRCAT(IObuff, ")");
8503 return IObuff;
8504 }
8505
8506 return NULL;
8507}
8508
8509/*
8510 * Function given to ExpandGeneric() to obtain the list of internal or
8511 * user defined variable or function names.
8512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008514get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
8516 static int intidx = -1;
8517 char_u *name;
8518
8519 if (idx == 0)
8520 intidx = -1;
8521 if (intidx < 0)
8522 {
8523 name = get_function_name(xp, idx);
8524 if (name != NULL)
8525 return name;
8526 }
8527 return get_user_var_name(xp, ++intidx);
8528}
8529
8530#endif /* FEAT_CMDL_COMPL */
8531
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008532#if defined(EBCDIC) || defined(PROTO)
8533/*
8534 * Compare struct fst by function name.
8535 */
8536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008537compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008538{
8539 struct fst *p1 = (struct fst *)s1;
8540 struct fst *p2 = (struct fst *)s2;
8541
8542 return STRCMP(p1->f_name, p2->f_name);
8543}
8544
8545/*
8546 * Sort the function table by function name.
8547 * The sorting of the table above is ASCII dependant.
8548 * On machines using EBCDIC we have to sort it.
8549 */
8550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008551sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008552{
8553 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8554
8555 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8556}
8557#endif
8558
8559
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560/*
8561 * Find internal function in table above.
8562 * Return index, or -1 if not found
8563 */
8564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008565find_internal_func(
8566 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
8568 int first = 0;
8569 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8570 int cmp;
8571 int x;
8572
8573 /*
8574 * Find the function name in the table. Binary search.
8575 */
8576 while (first <= last)
8577 {
8578 x = first + ((unsigned)(last - first) >> 1);
8579 cmp = STRCMP(name, functions[x].f_name);
8580 if (cmp < 0)
8581 last = x - 1;
8582 else if (cmp > 0)
8583 first = x + 1;
8584 else
8585 return x;
8586 }
8587 return -1;
8588}
8589
8590/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008591 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8592 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008593 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8594 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008595 */
8596 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008597deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008598{
Bram Moolenaar33570922005-01-25 22:26:29 +00008599 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008600 int cc;
8601
Bram Moolenaar65639032016-03-16 21:40:30 +01008602 if (partialp != NULL)
8603 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008605 cc = name[*lenp];
8606 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008607 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008608 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008609 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008610 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008611 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008612 {
8613 *lenp = 0;
8614 return (char_u *)""; /* just in case */
8615 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008616 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008617 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008618 }
8619
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008620 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8621 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008622 partial_T *pt = v->di_tv.vval.v_partial;
8623
8624 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008625 {
8626 *lenp = 0;
8627 return (char_u *)""; /* just in case */
8628 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008629 if (partialp != NULL)
8630 *partialp = pt;
8631 *lenp = (int)STRLEN(pt->pt_name);
8632 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008633 }
8634
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008635 return name;
8636}
8637
8638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 * Allocate a variable for the result of a function.
8640 * Return OK or FAIL.
8641 */
8642 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008643get_func_tv(
8644 char_u *name, /* name of the function */
8645 int len, /* length of "name" */
8646 typval_T *rettv,
8647 char_u **arg, /* argument, pointing to the '(' */
8648 linenr_T firstline, /* first line of range */
8649 linenr_T lastline, /* last line of range */
8650 int *doesrange, /* return: function handled range */
8651 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008652 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008653 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654{
8655 char_u *argp;
8656 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008657 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 int argcount = 0; /* number of arguments found */
8659
8660 /*
8661 * Get the arguments.
8662 */
8663 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008664 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 {
8666 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8667 if (*argp == ')' || *argp == ',' || *argp == NUL)
8668 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8670 {
8671 ret = FAIL;
8672 break;
8673 }
8674 ++argcount;
8675 if (*argp != ',')
8676 break;
8677 }
8678 if (*argp == ')')
8679 ++argp;
8680 else
8681 ret = FAIL;
8682
8683 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008685 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 {
8688 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008689 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008691 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693
8694 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
8697 *arg = skipwhite(argp);
8698 return ret;
8699}
8700
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008701#define ERROR_UNKNOWN 0
8702#define ERROR_TOOMANY 1
8703#define ERROR_TOOFEW 2
8704#define ERROR_SCRIPT 3
8705#define ERROR_DICT 4
8706#define ERROR_NONE 5
8707#define ERROR_OTHER 6
8708#define FLEN_FIXED 40
8709
8710/*
8711 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8712 * Change <SNR>123_name() to K_SNR 123_name().
8713 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8714 * (slow).
8715 */
8716 static char_u *
8717fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8718{
8719 int llen;
8720 char_u *fname;
8721 int i;
8722
8723 llen = eval_fname_script(name);
8724 if (llen > 0)
8725 {
8726 fname_buf[0] = K_SPECIAL;
8727 fname_buf[1] = KS_EXTRA;
8728 fname_buf[2] = (int)KE_SNR;
8729 i = 3;
8730 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8731 {
8732 if (current_SID <= 0)
8733 *error = ERROR_SCRIPT;
8734 else
8735 {
8736 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8737 i = (int)STRLEN(fname_buf);
8738 }
8739 }
8740 if (i + STRLEN(name + llen) < FLEN_FIXED)
8741 {
8742 STRCPY(fname_buf + i, name + llen);
8743 fname = fname_buf;
8744 }
8745 else
8746 {
8747 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8748 if (fname == NULL)
8749 *error = ERROR_OTHER;
8750 else
8751 {
8752 *tofree = fname;
8753 mch_memmove(fname, fname_buf, (size_t)i);
8754 STRCPY(fname + i, name + llen);
8755 }
8756 }
8757 }
8758 else
8759 fname = name;
8760 return fname;
8761}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762
8763/*
8764 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008765 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008766 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008768 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008769call_func(
8770 char_u *funcname, /* name of the function */
8771 int len, /* length of "name" */
8772 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008773 int argcount_in, /* number of "argvars" */
8774 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008775 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008776 linenr_T firstline, /* first line of range */
8777 linenr_T lastline, /* last line of range */
8778 int *doesrange, /* return: function handled range */
8779 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008780 partial_T *partial, /* optional, can be NULL */
8781 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
8783 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 int error = ERROR_NONE;
8785 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008788 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008790 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008791 int argcount = argcount_in;
8792 typval_T *argvars = argvars_in;
8793 dict_T *selfdict = selfdict_in;
8794 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8795 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008796
8797 /* Make a copy of the name, if it comes from a funcref variable it could
8798 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008799 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008800 if (name == NULL)
8801 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008803 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804
8805 *doesrange = FALSE;
8806
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008807 if (partial != NULL)
8808 {
8809 if (partial->pt_dict != NULL)
8810 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008811 /* When the function has a partial with a dict and there is a dict
8812 * argument, use the dict argument. That is backwards compatible.
8813 */
8814 if (selfdict_in == NULL)
8815 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008816 }
8817 if (error == ERROR_NONE && partial->pt_argc > 0)
8818 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008819 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8820 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8821 for (i = 0; i < argcount_in; ++i)
8822 argv[i + argv_clear] = argvars_in[i];
8823 argvars = argv;
8824 argcount = partial->pt_argc + argcount_in;
8825 }
8826 }
8827
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
8829 /* execute the function if no errors detected and executing */
8830 if (evaluate && error == ERROR_NONE)
8831 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008832 char_u *rfname = fname;
8833
8834 /* Ignore "g:" before a function name. */
8835 if (fname[0] == 'g' && fname[1] == ':')
8836 rfname = fname + 2;
8837
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008838 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8839 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 error = ERROR_UNKNOWN;
8841
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008842 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {
8844 /*
8845 * User defined function.
8846 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008847 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008848
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850 /* Trigger FuncUndefined event, may load the function. */
8851 if (fp == NULL
8852 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008853 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008854 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008856 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008857 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 }
8859#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008860 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008861 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008862 {
8863 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008864 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008865 }
8866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 if (fp != NULL)
8868 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008869 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008871 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008873 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008875 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008876 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 else
8878 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008879 int did_save_redo = FALSE;
8880
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 /*
8882 * Call the user function.
8883 * Save and restore search patterns, script variables and
8884 * redo buffer.
8885 */
8886 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008887#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008888 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008889#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008890 {
8891 saveRedobuff();
8892 did_save_redo = TRUE;
8893 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008894 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008896 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008897 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8898 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8899 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008900 /* Function was unreferenced while being used, free it
8901 * now. */
8902 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008903 if (did_save_redo)
8904 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 restore_search_patterns();
8906 error = ERROR_NONE;
8907 }
8908 }
8909 }
8910 else
8911 {
8912 /*
8913 * Find the function name in the table, call its implementation.
8914 */
8915 i = find_internal_func(fname);
8916 if (i >= 0)
8917 {
8918 if (argcount < functions[i].f_min_argc)
8919 error = ERROR_TOOFEW;
8920 else if (argcount > functions[i].f_max_argc)
8921 error = ERROR_TOOMANY;
8922 else
8923 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008924 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 error = ERROR_NONE;
8927 }
8928 }
8929 }
8930 /*
8931 * The function call (or "FuncUndefined" autocommand sequence) might
8932 * have been aborted by an error, an interrupt, or an explicitly thrown
8933 * exception that has not been caught so far. This situation can be
8934 * tested for by calling aborting(). For an error in an internal
8935 * function or for the "E132" error in call_user_func(), however, the
8936 * throw point at which the "force_abort" flag (temporarily reset by
8937 * emsg()) is normally updated has not been reached yet. We need to
8938 * update that flag first to make aborting() reliable.
8939 */
8940 update_force_abort();
8941 }
8942 if (error == ERROR_NONE)
8943 ret = OK;
8944
8945 /*
8946 * Report an error unless the argument evaluation or function call has been
8947 * cancelled due to an aborting error, an interrupt, or an exception.
8948 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008949 if (!aborting())
8950 {
8951 switch (error)
8952 {
8953 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008954 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008955 break;
8956 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008957 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008958 break;
8959 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008960 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008961 name);
8962 break;
8963 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008964 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008965 name);
8966 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008968 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008969 name);
8970 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008971 }
8972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008974 while (argv_clear > 0)
8975 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008976 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008977 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
8979 return ret;
8980}
8981
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008982/*
8983 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008984 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008985 */
8986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008987emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008988{
8989 char_u *p;
8990
8991 if (*name == K_SPECIAL)
8992 p = concat_str((char_u *)"<SNR>", name + 3);
8993 else
8994 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008995 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008996 if (p != name)
8997 vim_free(p);
8998}
8999
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009000/*
9001 * Return TRUE for a non-zero Number and a non-empty String.
9002 */
9003 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009004non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009005{
9006 return ((argvars[0].v_type == VAR_NUMBER
9007 && argvars[0].vval.v_number != 0)
9008 || (argvars[0].v_type == VAR_STRING
9009 && argvars[0].vval.v_string != NULL
9010 && *argvars[0].vval.v_string != NUL));
9011}
9012
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013/*********************************************
9014 * Implementation of the built-in functions
9015 */
9016
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009017#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009018static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009019
9020/*
9021 * Get the float value of "argvars[0]" into "f".
9022 * Returns FAIL when the argument is not a Number or Float.
9023 */
9024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009025get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009026{
9027 if (argvars[0].v_type == VAR_FLOAT)
9028 {
9029 *f = argvars[0].vval.v_float;
9030 return OK;
9031 }
9032 if (argvars[0].v_type == VAR_NUMBER)
9033 {
9034 *f = (float_T)argvars[0].vval.v_number;
9035 return OK;
9036 }
9037 EMSG(_("E808: Number or Float required"));
9038 return FAIL;
9039}
9040
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009041/*
9042 * "abs(expr)" function
9043 */
9044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009045f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009046{
9047 if (argvars[0].v_type == VAR_FLOAT)
9048 {
9049 rettv->v_type = VAR_FLOAT;
9050 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9051 }
9052 else
9053 {
9054 varnumber_T n;
9055 int error = FALSE;
9056
9057 n = get_tv_number_chk(&argvars[0], &error);
9058 if (error)
9059 rettv->vval.v_number = -1;
9060 else if (n > 0)
9061 rettv->vval.v_number = n;
9062 else
9063 rettv->vval.v_number = -n;
9064 }
9065}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009066
9067/*
9068 * "acos()" function
9069 */
9070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009071f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009072{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009073 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009074
9075 rettv->v_type = VAR_FLOAT;
9076 if (get_float_arg(argvars, &f) == OK)
9077 rettv->vval.v_float = acos(f);
9078 else
9079 rettv->vval.v_float = 0.0;
9080}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009081#endif
9082
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009084 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 */
9086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009087f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088{
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009092 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009094 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009095 && !tv_check_lock(l->lv_lock,
9096 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009097 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009099 }
9100 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009101 EMSG(_(e_listreq));
9102}
9103
9104/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009105 * "alloc_fail(id, countdown, repeat)" function
9106 */
9107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009108f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009109{
9110 if (argvars[0].v_type != VAR_NUMBER
9111 || argvars[0].vval.v_number <= 0
9112 || argvars[1].v_type != VAR_NUMBER
9113 || argvars[1].vval.v_number < 0
9114 || argvars[2].v_type != VAR_NUMBER)
9115 EMSG(_(e_invarg));
9116 else
9117 {
9118 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009119 if (alloc_fail_id >= aid_last)
9120 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009121 alloc_fail_countdown = argvars[1].vval.v_number;
9122 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009123 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009124 }
9125}
9126
9127/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009128 * "and(expr, expr)" function
9129 */
9130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009131f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009132{
9133 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9134 & get_tv_number_chk(&argvars[1], NULL);
9135}
9136
9137/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009138 * "append(lnum, string/list)" function
9139 */
9140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009141f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009142{
9143 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009145 list_T *l = NULL;
9146 listitem_T *li = NULL;
9147 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009148 long added = 0;
9149
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009150 /* When coming here from Insert mode, sync undo, so that this can be
9151 * undone separately from what was previously inserted. */
9152 if (u_sync_once == 2)
9153 {
9154 u_sync_once = 1; /* notify that u_sync() was called */
9155 u_sync(TRUE);
9156 }
9157
Bram Moolenaar0d660222005-01-07 21:51:51 +00009158 lnum = get_tv_lnum(argvars);
9159 if (lnum >= 0
9160 && lnum <= curbuf->b_ml.ml_line_count
9161 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009162 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009163 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009164 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009165 l = argvars[1].vval.v_list;
9166 if (l == NULL)
9167 return;
9168 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009169 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009170 for (;;)
9171 {
9172 if (l == NULL)
9173 tv = &argvars[1]; /* append a string */
9174 else if (li == NULL)
9175 break; /* end of list */
9176 else
9177 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009178 line = get_tv_string_chk(tv);
9179 if (line == NULL) /* type error */
9180 {
9181 rettv->vval.v_number = 1; /* Failed */
9182 break;
9183 }
9184 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009185 ++added;
9186 if (l == NULL)
9187 break;
9188 li = li->li_next;
9189 }
9190
9191 appended_lines_mark(lnum, added);
9192 if (curwin->w_cursor.lnum > lnum)
9193 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009195 else
9196 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197}
9198
9199/*
9200 * "argc()" function
9201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009203f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009205 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206}
9207
9208/*
9209 * "argidx()" function
9210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009212f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009214 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215}
9216
9217/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009218 * "arglistid()" function
9219 */
9220 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009221f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009222{
9223 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009224
9225 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009226 wp = find_tabwin(&argvars[0], &argvars[1]);
9227 if (wp != NULL)
9228 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009229}
9230
9231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 * "argv(nr)" function
9233 */
9234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009235f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236{
9237 int idx;
9238
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009239 if (argvars[0].v_type != VAR_UNKNOWN)
9240 {
9241 idx = get_tv_number_chk(&argvars[0], NULL);
9242 if (idx >= 0 && idx < ARGCOUNT)
9243 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9244 else
9245 rettv->vval.v_string = NULL;
9246 rettv->v_type = VAR_STRING;
9247 }
9248 else if (rettv_list_alloc(rettv) == OK)
9249 for (idx = 0; idx < ARGCOUNT; ++idx)
9250 list_append_string(rettv->vval.v_list,
9251 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252}
9253
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009254static void prepare_assert_error(garray_T*gap);
9255static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9256static void assert_error(garray_T *gap);
9257static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009258
9259/*
9260 * Prepare "gap" for an assert error and add the sourcing position.
9261 */
9262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009263prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009264{
9265 char buf[NUMBUFLEN];
9266
9267 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009268 if (sourcing_name != NULL)
9269 {
9270 ga_concat(gap, sourcing_name);
9271 if (sourcing_lnum > 0)
9272 ga_concat(gap, (char_u *)" ");
9273 }
9274 if (sourcing_lnum > 0)
9275 {
9276 sprintf(buf, "line %ld", (long)sourcing_lnum);
9277 ga_concat(gap, (char_u *)buf);
9278 }
9279 if (sourcing_name != NULL || sourcing_lnum > 0)
9280 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009281}
9282
9283/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009284 * Append "str" to "gap", escaping unprintable characters.
9285 * Changes NL to \n, CR to \r, etc.
9286 */
9287 static void
9288ga_concat_esc(garray_T *gap, char_u *str)
9289{
9290 char_u *p;
9291 char_u buf[NUMBUFLEN];
9292
Bram Moolenaarf1551962016-03-15 12:55:58 +01009293 if (str == NULL)
9294 {
9295 ga_concat(gap, (char_u *)"NULL");
9296 return;
9297 }
9298
Bram Moolenaar23689172016-02-15 22:37:37 +01009299 for (p = str; *p != NUL; ++p)
9300 switch (*p)
9301 {
9302 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9303 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9304 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9305 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9306 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9307 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9308 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9309 default:
9310 if (*p < ' ')
9311 {
9312 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9313 ga_concat(gap, buf);
9314 }
9315 else
9316 ga_append(gap, *p);
9317 break;
9318 }
9319}
9320
9321/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009322 * Fill "gap" with information about an assert error.
9323 */
9324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009325fill_assert_error(
9326 garray_T *gap,
9327 typval_T *opt_msg_tv,
9328 char_u *exp_str,
9329 typval_T *exp_tv,
9330 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009331{
9332 char_u numbuf[NUMBUFLEN];
9333 char_u *tofree;
9334
9335 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9336 {
9337 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9338 vim_free(tofree);
9339 }
9340 else
9341 {
9342 ga_concat(gap, (char_u *)"Expected ");
9343 if (exp_str == NULL)
9344 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009345 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009346 vim_free(tofree);
9347 }
9348 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009349 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009350 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009351 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009352 vim_free(tofree);
9353 }
9354}
Bram Moolenaar43345542015-11-29 17:35:35 +01009355
9356/*
9357 * Add an assert error to v:errors.
9358 */
9359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009360assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009361{
9362 struct vimvar *vp = &vimvars[VV_ERRORS];
9363
9364 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9365 /* Make sure v:errors is a list. */
9366 set_vim_var_list(VV_ERRORS, list_alloc());
9367 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9368}
9369
Bram Moolenaar43345542015-11-29 17:35:35 +01009370/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009371 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009372 */
9373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009374f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009375{
9376 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009377
9378 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9379 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009380 prepare_assert_error(&ga);
9381 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9382 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009383 ga_clear(&ga);
9384 }
9385}
9386
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009387/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009388 * "assert_exception(string[, msg])" function
9389 */
9390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009391f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009392{
9393 garray_T ga;
9394 char *error;
9395
9396 error = (char *)get_tv_string_chk(&argvars[0]);
9397 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9398 {
9399 prepare_assert_error(&ga);
9400 ga_concat(&ga, (char_u *)"v:exception is not set");
9401 assert_error(&ga);
9402 ga_clear(&ga);
9403 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009404 else if (error != NULL
9405 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009406 {
9407 prepare_assert_error(&ga);
9408 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9409 &vimvars[VV_EXCEPTION].vv_tv);
9410 assert_error(&ga);
9411 ga_clear(&ga);
9412 }
9413}
9414
9415/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009416 * "assert_fails(cmd [, error])" function
9417 */
9418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009419f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009420{
9421 char_u *cmd = get_tv_string_chk(&argvars[0]);
9422 garray_T ga;
9423
9424 called_emsg = FALSE;
9425 suppress_errthrow = TRUE;
9426 emsg_silent = TRUE;
9427 do_cmdline_cmd(cmd);
9428 if (!called_emsg)
9429 {
9430 prepare_assert_error(&ga);
9431 ga_concat(&ga, (char_u *)"command did not fail: ");
9432 ga_concat(&ga, cmd);
9433 assert_error(&ga);
9434 ga_clear(&ga);
9435 }
9436 else if (argvars[1].v_type != VAR_UNKNOWN)
9437 {
9438 char_u buf[NUMBUFLEN];
9439 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9440
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009441 if (error == NULL
9442 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009443 {
9444 prepare_assert_error(&ga);
9445 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9446 &vimvars[VV_ERRMSG].vv_tv);
9447 assert_error(&ga);
9448 ga_clear(&ga);
9449 }
9450 }
9451
9452 called_emsg = FALSE;
9453 suppress_errthrow = FALSE;
9454 emsg_silent = FALSE;
9455 emsg_on_display = FALSE;
9456 set_vim_var_string(VV_ERRMSG, NULL, 0);
9457}
9458
9459/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009460 * Common for assert_true() and assert_false().
9461 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009463assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009464{
9465 int error = FALSE;
9466 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009467
Bram Moolenaar37127922016-02-06 20:29:28 +01009468 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009469 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009470 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009471 if (argvars[0].v_type != VAR_NUMBER
9472 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9473 || error)
9474 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009475 prepare_assert_error(&ga);
9476 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009477 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009478 NULL, &argvars[0]);
9479 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009480 ga_clear(&ga);
9481 }
9482}
9483
9484/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009485 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009486 */
9487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009488f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009489{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009490 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009491}
9492
9493/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009494 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009495 */
9496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009497f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009498{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009499 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009500}
9501
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009502#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009503/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009504 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009505 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009507f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009508{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009509 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009510
9511 rettv->v_type = VAR_FLOAT;
9512 if (get_float_arg(argvars, &f) == OK)
9513 rettv->vval.v_float = asin(f);
9514 else
9515 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009516}
9517
9518/*
9519 * "atan()" function
9520 */
9521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009522f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009523{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009524 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009525
9526 rettv->v_type = VAR_FLOAT;
9527 if (get_float_arg(argvars, &f) == OK)
9528 rettv->vval.v_float = atan(f);
9529 else
9530 rettv->vval.v_float = 0.0;
9531}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009532
9533/*
9534 * "atan2()" function
9535 */
9536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009537f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009538{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009539 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009540
9541 rettv->v_type = VAR_FLOAT;
9542 if (get_float_arg(argvars, &fx) == OK
9543 && get_float_arg(&argvars[1], &fy) == OK)
9544 rettv->vval.v_float = atan2(fx, fy);
9545 else
9546 rettv->vval.v_float = 0.0;
9547}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009548#endif
9549
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550/*
9551 * "browse(save, title, initdir, default)" function
9552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009554f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556#ifdef FEAT_BROWSE
9557 int save;
9558 char_u *title;
9559 char_u *initdir;
9560 char_u *defname;
9561 char_u buf[NUMBUFLEN];
9562 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009565 save = get_tv_number_chk(&argvars[0], &error);
9566 title = get_tv_string_chk(&argvars[1]);
9567 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9568 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009570 if (error || title == NULL || initdir == NULL || defname == NULL)
9571 rettv->vval.v_string = NULL;
9572 else
9573 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574 do_browse(save ? BROWSE_SAVE : 0,
9575 title, defname, NULL, initdir, NULL, curbuf);
9576#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009578#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009580}
9581
9582/*
9583 * "browsedir(title, initdir)" function
9584 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009586f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009587{
9588#ifdef FEAT_BROWSE
9589 char_u *title;
9590 char_u *initdir;
9591 char_u buf[NUMBUFLEN];
9592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009593 title = get_tv_string_chk(&argvars[0]);
9594 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009596 if (title == NULL || initdir == NULL)
9597 rettv->vval.v_string = NULL;
9598 else
9599 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009600 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605}
9606
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009607static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009608
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609/*
9610 * Find a buffer by number or exact name.
9611 */
9612 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009613find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009617 if (avar->v_type == VAR_NUMBER)
9618 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009619 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009621 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009622 if (buf == NULL)
9623 {
9624 /* No full path name match, try a match with a URL or a "nofile"
9625 * buffer, these don't use the full path. */
9626 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9627 if (buf->b_fname != NULL
9628 && (path_with_url(buf->b_fname)
9629#ifdef FEAT_QUICKFIX
9630 || bt_nofile(buf)
9631#endif
9632 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009633 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009634 break;
9635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 }
9637 return buf;
9638}
9639
9640/*
9641 * "bufexists(expr)" function
9642 */
9643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009644f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009646 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647}
9648
9649/*
9650 * "buflisted(expr)" function
9651 */
9652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009653f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
9655 buf_T *buf;
9656
9657 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659}
9660
9661/*
9662 * "bufloaded(expr)" function
9663 */
9664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009665f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666{
9667 buf_T *buf;
9668
9669 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671}
9672
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009673 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009674buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 int save_magic;
9677 char_u *save_cpo;
9678 buf_T *buf;
9679
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9681 save_magic = p_magic;
9682 p_magic = TRUE;
9683 save_cpo = p_cpo;
9684 p_cpo = (char_u *)"";
9685
9686 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009687 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688
9689 p_magic = save_magic;
9690 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009691 return buf;
9692}
9693
9694/*
9695 * Get buffer by number or pattern.
9696 */
9697 static buf_T *
9698get_buf_tv(typval_T *tv, int curtab_only)
9699{
9700 char_u *name = tv->vval.v_string;
9701 buf_T *buf;
9702
9703 if (tv->v_type == VAR_NUMBER)
9704 return buflist_findnr((int)tv->vval.v_number);
9705 if (tv->v_type != VAR_STRING)
9706 return NULL;
9707 if (name == NULL || *name == NUL)
9708 return curbuf;
9709 if (name[0] == '$' && name[1] == NUL)
9710 return lastbuf;
9711
9712 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713
9714 /* If not found, try expanding the name, like done for bufexists(). */
9715 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717
9718 return buf;
9719}
9720
9721/*
9722 * "bufname(expr)" function
9723 */
9724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009725f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726{
9727 buf_T *buf;
9728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009729 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009731 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009734 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 --emsg_off;
9738}
9739
9740/*
9741 * "bufnr(expr)" function
9742 */
9743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009744f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745{
9746 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009747 int error = FALSE;
9748 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009750 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009752 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009753 --emsg_off;
9754
9755 /* If the buffer isn't found and the second argument is not zero create a
9756 * new buffer. */
9757 if (buf == NULL
9758 && argvars[1].v_type != VAR_UNKNOWN
9759 && get_tv_number_chk(&argvars[1], &error) != 0
9760 && !error
9761 && (name = get_tv_string_chk(&argvars[0])) != NULL
9762 && !error)
9763 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9764
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769}
9770
9771/*
9772 * "bufwinnr(nr)" function
9773 */
9774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009775f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776{
9777#ifdef FEAT_WINDOWS
9778 win_T *wp;
9779 int winnr = 0;
9780#endif
9781 buf_T *buf;
9782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009783 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009785 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786#ifdef FEAT_WINDOWS
9787 for (wp = firstwin; wp; wp = wp->w_next)
9788 {
9789 ++winnr;
9790 if (wp->w_buffer == buf)
9791 break;
9792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796#endif
9797 --emsg_off;
9798}
9799
9800/*
9801 * "byte2line(byte)" function
9802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009804f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808#else
9809 long boff = 0;
9810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 (linenr_T)0, &boff);
9817#endif
9818}
9819
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009821byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009822{
9823#ifdef FEAT_MBYTE
9824 char_u *t;
9825#endif
9826 char_u *str;
9827 long idx;
9828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009829 str = get_tv_string_chk(&argvars[0]);
9830 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009832 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009833 return;
9834
9835#ifdef FEAT_MBYTE
9836 t = str;
9837 for ( ; idx > 0; idx--)
9838 {
9839 if (*t == NUL) /* EOL reached */
9840 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009841 if (enc_utf8 && comp)
9842 t += utf_ptr2len(t);
9843 else
9844 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009845 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009846 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009847#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009848 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009850#endif
9851}
9852
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009853/*
9854 * "byteidx()" function
9855 */
9856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009857f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009858{
9859 byteidx(argvars, rettv, FALSE);
9860}
9861
9862/*
9863 * "byteidxcomp()" function
9864 */
9865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009866f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009867{
9868 byteidx(argvars, rettv, TRUE);
9869}
9870
Bram Moolenaardb913952012-06-29 12:54:53 +02009871 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009872func_call(
9873 char_u *name,
9874 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009875 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009876 dict_T *selfdict,
9877 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009878{
9879 listitem_T *item;
9880 typval_T argv[MAX_FUNC_ARGS + 1];
9881 int argc = 0;
9882 int dummy;
9883 int r = 0;
9884
9885 for (item = args->vval.v_list->lv_first; item != NULL;
9886 item = item->li_next)
9887 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009888 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009889 {
9890 EMSG(_("E699: Too many arguments"));
9891 break;
9892 }
9893 /* Make a copy of each argument. This is needed to be able to set
9894 * v_lock to VAR_FIXED in the copy without changing the original list.
9895 */
9896 copy_tv(&item->li_tv, &argv[argc++]);
9897 }
9898
9899 if (item == NULL)
9900 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9901 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009902 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009903
9904 /* Free the arguments. */
9905 while (argc > 0)
9906 clear_tv(&argv[--argc]);
9907
9908 return r;
9909}
9910
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009911/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009912 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009913 */
9914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009915f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009916{
9917 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009918 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009919 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009920
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009921 if (argvars[1].v_type != VAR_LIST)
9922 {
9923 EMSG(_(e_listreq));
9924 return;
9925 }
9926 if (argvars[1].vval.v_list == NULL)
9927 return;
9928
9929 if (argvars[0].v_type == VAR_FUNC)
9930 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009931 else if (argvars[0].v_type == VAR_PARTIAL)
9932 {
9933 partial = argvars[0].vval.v_partial;
9934 func = partial->pt_name;
9935 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009936 else
9937 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 if (*func == NUL)
9939 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009940
Bram Moolenaare9a41262005-01-15 22:18:47 +00009941 if (argvars[2].v_type != VAR_UNKNOWN)
9942 {
9943 if (argvars[2].v_type != VAR_DICT)
9944 {
9945 EMSG(_(e_dictreq));
9946 return;
9947 }
9948 selfdict = argvars[2].vval.v_dict;
9949 }
9950
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009951 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009952}
9953
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009954#ifdef FEAT_FLOAT
9955/*
9956 * "ceil({float})" function
9957 */
9958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009959f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009960{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009961 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009962
9963 rettv->v_type = VAR_FLOAT;
9964 if (get_float_arg(argvars, &f) == OK)
9965 rettv->vval.v_float = ceil(f);
9966 else
9967 rettv->vval.v_float = 0.0;
9968}
9969#endif
9970
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009971#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009972/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009973 * "ch_close()" function
9974 */
9975 static void
9976f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9977{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009978 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009979
9980 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009981 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009982 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009983 channel_clear(channel);
9984 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009985}
9986
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009987/*
9988 * "ch_getbufnr()" function
9989 */
9990 static void
9991f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9992{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009993 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009994
9995 rettv->vval.v_number = -1;
9996 if (channel != NULL)
9997 {
9998 char_u *what = get_tv_string(&argvars[1]);
9999 int part;
10000
10001 if (STRCMP(what, "err") == 0)
10002 part = PART_ERR;
10003 else if (STRCMP(what, "out") == 0)
10004 part = PART_OUT;
10005 else if (STRCMP(what, "in") == 0)
10006 part = PART_IN;
10007 else
10008 part = PART_SOCK;
10009 if (channel->ch_part[part].ch_buffer != NULL)
10010 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10011 }
10012}
10013
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010014/*
10015 * "ch_getjob()" function
10016 */
10017 static void
10018f_ch_getjob(typval_T *argvars, typval_T *rettv)
10019{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010020 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010021
10022 if (channel != NULL)
10023 {
10024 rettv->v_type = VAR_JOB;
10025 rettv->vval.v_job = channel->ch_job;
10026 if (channel->ch_job != NULL)
10027 ++channel->ch_job->jv_refcount;
10028 }
10029}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010030
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010031/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010032 * "ch_log()" function
10033 */
10034 static void
10035f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10036{
10037 char_u *msg = get_tv_string(&argvars[0]);
10038 channel_T *channel = NULL;
10039
10040 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010041 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010042
10043 ch_log(channel, (char *)msg);
10044}
10045
10046/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010047 * "ch_logfile()" function
10048 */
10049 static void
10050f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10051{
10052 char_u *fname;
10053 char_u *opt = (char_u *)"";
10054 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010055
10056 fname = get_tv_string(&argvars[0]);
10057 if (argvars[1].v_type == VAR_STRING)
10058 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010059 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010060}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010061
10062/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010063 * "ch_open()" function
10064 */
10065 static void
10066f_ch_open(typval_T *argvars, typval_T *rettv)
10067{
Bram Moolenaar77073442016-02-13 23:23:53 +010010068 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010069 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010070}
10071
10072/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010073 * "ch_read()" function
10074 */
10075 static void
10076f_ch_read(typval_T *argvars, typval_T *rettv)
10077{
10078 common_channel_read(argvars, rettv, FALSE);
10079}
10080
10081/*
10082 * "ch_readraw()" function
10083 */
10084 static void
10085f_ch_readraw(typval_T *argvars, typval_T *rettv)
10086{
10087 common_channel_read(argvars, rettv, TRUE);
10088}
10089
10090/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010091 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010092 */
10093 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010094f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10095{
10096 ch_expr_common(argvars, rettv, TRUE);
10097}
10098
10099/*
10100 * "ch_sendexpr()" function
10101 */
10102 static void
10103f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10104{
10105 ch_expr_common(argvars, rettv, FALSE);
10106}
10107
10108/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010109 * "ch_evalraw()" function
10110 */
10111 static void
10112f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10113{
10114 ch_raw_common(argvars, rettv, TRUE);
10115}
10116
10117/*
10118 * "ch_sendraw()" function
10119 */
10120 static void
10121f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10122{
10123 ch_raw_common(argvars, rettv, FALSE);
10124}
10125
10126/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010127 * "ch_setoptions()" function
10128 */
10129 static void
10130f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10131{
10132 channel_T *channel;
10133 jobopt_T opt;
10134
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010135 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010136 if (channel == NULL)
10137 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010138 clear_job_options(&opt);
10139 if (get_job_options(&argvars[1], &opt,
10140 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010141 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010142 channel_set_options(channel, &opt);
10143}
10144
10145/*
10146 * "ch_status()" function
10147 */
10148 static void
10149f_ch_status(typval_T *argvars, typval_T *rettv)
10150{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010151 channel_T *channel;
10152
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010153 /* return an empty string by default */
10154 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010155 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010156
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010157 channel = get_channel_arg(&argvars[0], FALSE);
10158 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010159}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010160#endif
10161
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010162/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010163 * "changenr()" function
10164 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010166f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010167{
10168 rettv->vval.v_number = curbuf->b_u_seq_cur;
10169}
10170
10171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 * "char2nr(string)" function
10173 */
10174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010175f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176{
10177#ifdef FEAT_MBYTE
10178 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010179 {
10180 int utf8 = 0;
10181
10182 if (argvars[1].v_type != VAR_UNKNOWN)
10183 utf8 = get_tv_number_chk(&argvars[1], NULL);
10184
10185 if (utf8)
10186 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10187 else
10188 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 else
10191#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010192 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193}
10194
10195/*
10196 * "cindent(lnum)" function
10197 */
10198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010199f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200{
10201#ifdef FEAT_CINDENT
10202 pos_T pos;
10203 linenr_T lnum;
10204
10205 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010206 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10208 {
10209 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010210 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 curwin->w_cursor = pos;
10212 }
10213 else
10214#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010215 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216}
10217
10218/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010219 * "clearmatches()" function
10220 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010222f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010223{
10224#ifdef FEAT_SEARCH_EXTRA
10225 clear_matches(curwin);
10226#endif
10227}
10228
10229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 * "col(string)" function
10231 */
10232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010233f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234{
10235 colnr_T col = 0;
10236 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010237 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010239 fp = var2fpos(&argvars[0], FALSE, &fnum);
10240 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 {
10242 if (fp->col == MAXCOL)
10243 {
10244 /* '> can be MAXCOL, get the length of the line then */
10245 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010246 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 else
10248 col = MAXCOL;
10249 }
10250 else
10251 {
10252 col = fp->col + 1;
10253#ifdef FEAT_VIRTUALEDIT
10254 /* col(".") when the cursor is on the NUL at the end of the line
10255 * because of "coladd" can be seen as an extra column. */
10256 if (virtual_active() && fp == &curwin->w_cursor)
10257 {
10258 char_u *p = ml_get_cursor();
10259
10260 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10261 curwin->w_virtcol - curwin->w_cursor.coladd))
10262 {
10263# ifdef FEAT_MBYTE
10264 int l;
10265
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010266 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 col += l;
10268# else
10269 if (*p != NUL && p[1] == NUL)
10270 ++col;
10271# endif
10272 }
10273 }
10274#endif
10275 }
10276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278}
10279
Bram Moolenaar572cb562005-08-05 21:35:02 +000010280#if defined(FEAT_INS_EXPAND)
10281/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010282 * "complete()" function
10283 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010285f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010286{
10287 int startcol;
10288
10289 if ((State & INSERT) == 0)
10290 {
10291 EMSG(_("E785: complete() can only be used in Insert mode"));
10292 return;
10293 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010294
10295 /* Check for undo allowed here, because if something was already inserted
10296 * the line was already saved for undo and this check isn't done. */
10297 if (!undo_allowed())
10298 return;
10299
Bram Moolenaarade00832006-03-10 21:46:58 +000010300 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10301 {
10302 EMSG(_(e_invarg));
10303 return;
10304 }
10305
10306 startcol = get_tv_number_chk(&argvars[0], NULL);
10307 if (startcol <= 0)
10308 return;
10309
10310 set_completion(startcol - 1, argvars[1].vval.v_list);
10311}
10312
10313/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010314 * "complete_add()" function
10315 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010317f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010318{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010319 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010320}
10321
10322/*
10323 * "complete_check()" function
10324 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010326f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010327{
10328 int saved = RedrawingDisabled;
10329
10330 RedrawingDisabled = 0;
10331 ins_compl_check_keys(0);
10332 rettv->vval.v_number = compl_interrupted;
10333 RedrawingDisabled = saved;
10334}
10335#endif
10336
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337/*
10338 * "confirm(message, buttons[, default [, type]])" function
10339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010341f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342{
10343#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10344 char_u *message;
10345 char_u *buttons = NULL;
10346 char_u buf[NUMBUFLEN];
10347 char_u buf2[NUMBUFLEN];
10348 int def = 1;
10349 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010350 char_u *typestr;
10351 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010353 message = get_tv_string_chk(&argvars[0]);
10354 if (message == NULL)
10355 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010356 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010358 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10359 if (buttons == NULL)
10360 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010361 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010363 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010364 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010366 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10367 if (typestr == NULL)
10368 error = TRUE;
10369 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010371 switch (TOUPPER_ASC(*typestr))
10372 {
10373 case 'E': type = VIM_ERROR; break;
10374 case 'Q': type = VIM_QUESTION; break;
10375 case 'I': type = VIM_INFO; break;
10376 case 'W': type = VIM_WARNING; break;
10377 case 'G': type = VIM_GENERIC; break;
10378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 }
10380 }
10381 }
10382 }
10383
10384 if (buttons == NULL || *buttons == NUL)
10385 buttons = (char_u *)_("&Ok");
10386
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010387 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010388 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010389 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390#endif
10391}
10392
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010393/*
10394 * "copy()" function
10395 */
10396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010397f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010398{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010399 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010400}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010402#ifdef FEAT_FLOAT
10403/*
10404 * "cos()" function
10405 */
10406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010407f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010408{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010409 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010410
10411 rettv->v_type = VAR_FLOAT;
10412 if (get_float_arg(argvars, &f) == OK)
10413 rettv->vval.v_float = cos(f);
10414 else
10415 rettv->vval.v_float = 0.0;
10416}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010417
10418/*
10419 * "cosh()" function
10420 */
10421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010422f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010423{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010424 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010425
10426 rettv->v_type = VAR_FLOAT;
10427 if (get_float_arg(argvars, &f) == OK)
10428 rettv->vval.v_float = cosh(f);
10429 else
10430 rettv->vval.v_float = 0.0;
10431}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010432#endif
10433
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010435 * "count()" function
10436 */
10437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010438f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010439{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010440 long n = 0;
10441 int ic = FALSE;
10442
Bram Moolenaare9a41262005-01-15 22:18:47 +000010443 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010444 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010445 listitem_T *li;
10446 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010447 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010448
Bram Moolenaare9a41262005-01-15 22:18:47 +000010449 if ((l = argvars[0].vval.v_list) != NULL)
10450 {
10451 li = l->lv_first;
10452 if (argvars[2].v_type != VAR_UNKNOWN)
10453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010454 int error = FALSE;
10455
10456 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010457 if (argvars[3].v_type != VAR_UNKNOWN)
10458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010459 idx = get_tv_number_chk(&argvars[3], &error);
10460 if (!error)
10461 {
10462 li = list_find(l, idx);
10463 if (li == NULL)
10464 EMSGN(_(e_listidx), idx);
10465 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010466 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010467 if (error)
10468 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010469 }
10470
10471 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010472 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010473 ++n;
10474 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010475 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010476 else if (argvars[0].v_type == VAR_DICT)
10477 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010478 int todo;
10479 dict_T *d;
10480 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010482 if ((d = argvars[0].vval.v_dict) != NULL)
10483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 int error = FALSE;
10485
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486 if (argvars[2].v_type != VAR_UNKNOWN)
10487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010489 if (argvars[3].v_type != VAR_UNKNOWN)
10490 EMSG(_(e_invarg));
10491 }
10492
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010493 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010494 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010495 {
10496 if (!HASHITEM_EMPTY(hi))
10497 {
10498 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010499 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010500 ++n;
10501 }
10502 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 }
10504 }
10505 else
10506 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010507 rettv->vval.v_number = n;
10508}
10509
10510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10512 *
10513 * Checks the existence of a cscope connection.
10514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010516f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517{
10518#ifdef FEAT_CSCOPE
10519 int num = 0;
10520 char_u *dbpath = NULL;
10521 char_u *prepend = NULL;
10522 char_u buf[NUMBUFLEN];
10523
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010524 if (argvars[0].v_type != VAR_UNKNOWN
10525 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010527 num = (int)get_tv_number(&argvars[0]);
10528 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 }
10532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010533 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534#endif
10535}
10536
10537/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010538 * "cursor(lnum, col)" function, or
10539 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010541 * Moves the cursor to the specified line and column.
10542 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010545f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546{
10547 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010548#ifdef FEAT_VIRTUALEDIT
10549 long coladd = 0;
10550#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010551 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010553 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010554 if (argvars[1].v_type == VAR_UNKNOWN)
10555 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010556 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010557 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010558
Bram Moolenaar493c1782014-05-28 14:34:46 +020010559 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010560 {
10561 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010562 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010563 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010564 line = pos.lnum;
10565 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010566#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010567 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010568#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010569 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010570 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010571 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010572 set_curswant = FALSE;
10573 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010574 }
10575 else
10576 {
10577 line = get_tv_lnum(argvars);
10578 col = get_tv_number_chk(&argvars[1], NULL);
10579#ifdef FEAT_VIRTUALEDIT
10580 if (argvars[2].v_type != VAR_UNKNOWN)
10581 coladd = get_tv_number_chk(&argvars[2], NULL);
10582#endif
10583 }
10584 if (line < 0 || col < 0
10585#ifdef FEAT_VIRTUALEDIT
10586 || coladd < 0
10587#endif
10588 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 if (line > 0)
10591 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 if (col > 0)
10593 curwin->w_cursor.col = col - 1;
10594#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010595 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596#endif
10597
10598 /* Make sure the cursor is in a valid position. */
10599 check_cursor();
10600#ifdef FEAT_MBYTE
10601 /* Correct cursor for multi-byte character. */
10602 if (has_mbyte)
10603 mb_adjust_cursor();
10604#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010605
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010606 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010607 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608}
10609
10610/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010611 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 */
10613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010614f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010616 int noref = 0;
10617
10618 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010619 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010620 if (noref < 0 || noref > 1)
10621 EMSG(_(e_invarg));
10622 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010623 {
10624 current_copyID += COPYID_INC;
10625 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627}
10628
10629/*
10630 * "delete()" function
10631 */
10632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010633f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010635 char_u nbuf[NUMBUFLEN];
10636 char_u *name;
10637 char_u *flags;
10638
10639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010641 return;
10642
10643 name = get_tv_string(&argvars[0]);
10644 if (name == NULL || *name == NUL)
10645 {
10646 EMSG(_(e_invarg));
10647 return;
10648 }
10649
10650 if (argvars[1].v_type != VAR_UNKNOWN)
10651 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010653 flags = (char_u *)"";
10654
10655 if (*flags == NUL)
10656 /* delete a file */
10657 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10658 else if (STRCMP(flags, "d") == 0)
10659 /* delete an empty directory */
10660 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10661 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010662 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010663 rettv->vval.v_number = delete_recursive(name);
10664 else
10665 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666}
10667
10668/*
10669 * "did_filetype()" function
10670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010672f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673{
10674#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010675 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010676#endif
10677}
10678
10679/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010680 * "diff_filler()" function
10681 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010683f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010684{
10685#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010686 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010687#endif
10688}
10689
10690/*
10691 * "diff_hlID()" function
10692 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010694f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010695{
10696#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010698 static linenr_T prev_lnum = 0;
10699 static int changedtick = 0;
10700 static int fnum = 0;
10701 static int change_start = 0;
10702 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010703 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010704 int filler_lines;
10705 int col;
10706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 if (lnum < 0) /* ignore type error in {lnum} arg */
10708 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010709 if (lnum != prev_lnum
10710 || changedtick != curbuf->b_changedtick
10711 || fnum != curbuf->b_fnum)
10712 {
10713 /* New line, buffer, change: need to get the values. */
10714 filler_lines = diff_check(curwin, lnum);
10715 if (filler_lines < 0)
10716 {
10717 if (filler_lines == -1)
10718 {
10719 change_start = MAXCOL;
10720 change_end = -1;
10721 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10722 hlID = HLF_ADD; /* added line */
10723 else
10724 hlID = HLF_CHD; /* changed line */
10725 }
10726 else
10727 hlID = HLF_ADD; /* added line */
10728 }
10729 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010730 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010731 prev_lnum = lnum;
10732 changedtick = curbuf->b_changedtick;
10733 fnum = curbuf->b_fnum;
10734 }
10735
10736 if (hlID == HLF_CHD || hlID == HLF_TXD)
10737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010738 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010739 if (col >= change_start && col <= change_end)
10740 hlID = HLF_TXD; /* changed text */
10741 else
10742 hlID = HLF_CHD; /* changed line */
10743 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010744 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010745#endif
10746}
10747
10748/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010749 * "disable_char_avail_for_testing({expr})" function
10750 */
10751 static void
10752f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10753{
10754 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10755}
10756
10757/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010758 * "empty({expr})" function
10759 */
10760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010761f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010762{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010763 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010764
10765 switch (argvars[0].v_type)
10766 {
10767 case VAR_STRING:
10768 case VAR_FUNC:
10769 n = argvars[0].vval.v_string == NULL
10770 || *argvars[0].vval.v_string == NUL;
10771 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010772 case VAR_PARTIAL:
10773 n = FALSE;
10774 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010775 case VAR_NUMBER:
10776 n = argvars[0].vval.v_number == 0;
10777 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010778 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010779#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010780 n = argvars[0].vval.v_float == 0.0;
10781 break;
10782#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010783 case VAR_LIST:
10784 n = argvars[0].vval.v_list == NULL
10785 || argvars[0].vval.v_list->lv_first == NULL;
10786 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010787 case VAR_DICT:
10788 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010790 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010791 case VAR_SPECIAL:
10792 n = argvars[0].vval.v_number != VVAL_TRUE;
10793 break;
10794
Bram Moolenaar835dc632016-02-07 14:27:38 +010010795 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010796#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010797 n = argvars[0].vval.v_job == NULL
10798 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10799 break;
10800#endif
10801 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010802#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010803 n = argvars[0].vval.v_channel == NULL
10804 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010805 break;
10806#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010807 case VAR_UNKNOWN:
10808 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10809 n = TRUE;
10810 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010811 }
10812
10813 rettv->vval.v_number = n;
10814}
10815
10816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 * "escape({string}, {chars})" function
10818 */
10819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010820f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821{
10822 char_u buf[NUMBUFLEN];
10823
Bram Moolenaar758711c2005-02-02 23:11:38 +000010824 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10825 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827}
10828
10829/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010830 * "eval()" function
10831 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010833f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010834{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010835 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010836
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010837 s = get_tv_string_chk(&argvars[0]);
10838 if (s != NULL)
10839 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010840
Bram Moolenaar615b9972015-01-14 17:15:05 +010010841 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10843 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010844 if (p != NULL && !aborting())
10845 EMSG2(_(e_invexpr2), p);
10846 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010847 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010848 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010850 else if (*s != NUL)
10851 EMSG(_(e_trailing));
10852}
10853
10854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 * "eventhandler()" function
10856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010858f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010860 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861}
10862
10863/*
10864 * "executable()" function
10865 */
10866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010867f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010869 char_u *name = get_tv_string(&argvars[0]);
10870
10871 /* Check in $PATH and also check directly if there is a directory name. */
10872 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10873 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010874}
10875
10876/*
10877 * "exepath()" function
10878 */
10879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010880f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010881{
10882 char_u *p = NULL;
10883
Bram Moolenaarb5971142015-03-21 17:32:19 +010010884 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010885 rettv->v_type = VAR_STRING;
10886 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887}
10888
10889/*
10890 * "exists()" function
10891 */
10892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010893f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894{
10895 char_u *p;
10896 char_u *name;
10897 int n = FALSE;
10898 int len = 0;
10899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010900 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 if (*p == '$') /* environment variable */
10902 {
10903 /* first try "normal" environment variables (fast) */
10904 if (mch_getenv(p + 1) != NULL)
10905 n = TRUE;
10906 else
10907 {
10908 /* try expanding things like $VIM and ${HOME} */
10909 p = expand_env_save(p);
10910 if (p != NULL && *p != '$')
10911 n = TRUE;
10912 vim_free(p);
10913 }
10914 }
10915 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010916 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010918 if (*skipwhite(p) != NUL)
10919 n = FALSE; /* trailing garbage */
10920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 else if (*p == '*') /* internal or user defined function */
10922 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010923 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 }
10925 else if (*p == ':')
10926 {
10927 n = cmd_exists(p + 1);
10928 }
10929 else if (*p == '#')
10930 {
10931#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010932 if (p[1] == '#')
10933 n = autocmd_supported(p + 2);
10934 else
10935 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936#endif
10937 }
10938 else /* internal variable */
10939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010940 char_u *tofree;
10941 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010943 /* get_name_len() takes care of expanding curly braces */
10944 name = p;
10945 len = get_name_len(&p, &tofree, TRUE, FALSE);
10946 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010948 if (tofree != NULL)
10949 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010950 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010951 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010953 /* handle d.key, l[idx], f(expr) */
10954 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10955 if (n)
10956 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 }
10958 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010959 if (*p != NUL)
10960 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010962 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 }
10964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966}
10967
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010968#ifdef FEAT_FLOAT
10969/*
10970 * "exp()" function
10971 */
10972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010973f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010974{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010975 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010976
10977 rettv->v_type = VAR_FLOAT;
10978 if (get_float_arg(argvars, &f) == OK)
10979 rettv->vval.v_float = exp(f);
10980 else
10981 rettv->vval.v_float = 0.0;
10982}
10983#endif
10984
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985/*
10986 * "expand()" function
10987 */
10988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010989f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990{
10991 char_u *s;
10992 int len;
10993 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010994 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010997 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011000 if (argvars[1].v_type != VAR_UNKNOWN
11001 && argvars[2].v_type != VAR_UNKNOWN
11002 && get_tv_number_chk(&argvars[2], &error)
11003 && !error)
11004 {
11005 rettv->v_type = VAR_LIST;
11006 rettv->vval.v_list = NULL;
11007 }
11008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 if (*s == '%' || *s == '#' || *s == '<')
11011 {
11012 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011013 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011015 if (rettv->v_type == VAR_LIST)
11016 {
11017 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11018 list_append_string(rettv->vval.v_list, result, -1);
11019 else
11020 vim_free(result);
11021 }
11022 else
11023 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 }
11025 else
11026 {
11027 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011028 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011029 if (argvars[1].v_type != VAR_UNKNOWN
11030 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011031 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032 if (!error)
11033 {
11034 ExpandInit(&xpc);
11035 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011036 if (p_wic)
11037 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011038 if (rettv->v_type == VAR_STRING)
11039 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11040 options, WILD_ALL);
11041 else if (rettv_list_alloc(rettv) != FAIL)
11042 {
11043 int i;
11044
11045 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11046 for (i = 0; i < xpc.xp_numfiles; i++)
11047 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11048 ExpandCleanup(&xpc);
11049 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 }
11051 else
11052 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 }
11054}
11055
11056/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011057 * Go over all entries in "d2" and add them to "d1".
11058 * When "action" is "error" then a duplicate key is an error.
11059 * When "action" is "force" then a duplicate key is overwritten.
11060 * Otherwise duplicate keys are ignored ("action" is "keep").
11061 */
11062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011063dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011064{
11065 dictitem_T *di1;
11066 hashitem_T *hi2;
11067 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011068 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011069
11070 todo = (int)d2->dv_hashtab.ht_used;
11071 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11072 {
11073 if (!HASHITEM_EMPTY(hi2))
11074 {
11075 --todo;
11076 di1 = dict_find(d1, hi2->hi_key, -1);
11077 if (d1->dv_scope != 0)
11078 {
11079 /* Disallow replacing a builtin function in l: and g:.
11080 * Check the key to be valid when adding to any
11081 * scope. */
11082 if (d1->dv_scope == VAR_DEF_SCOPE
11083 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11084 && var_check_func_name(hi2->hi_key,
11085 di1 == NULL))
11086 break;
11087 if (!valid_varname(hi2->hi_key))
11088 break;
11089 }
11090 if (di1 == NULL)
11091 {
11092 di1 = dictitem_copy(HI2DI(hi2));
11093 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11094 dictitem_free(di1);
11095 }
11096 else if (*action == 'e')
11097 {
11098 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11099 break;
11100 }
11101 else if (*action == 'f' && HI2DI(hi2) != di1)
11102 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011103 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11104 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011105 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011106 clear_tv(&di1->di_tv);
11107 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11108 }
11109 }
11110 }
11111}
11112
11113/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011114 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011115 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011116 */
11117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011118f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011119{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011120 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011121
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011123 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011124 list_T *l1, *l2;
11125 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011126 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011127 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011128
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 l1 = argvars[0].vval.v_list;
11130 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011131 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011132 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011133 {
11134 if (argvars[2].v_type != VAR_UNKNOWN)
11135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 before = get_tv_number_chk(&argvars[2], &error);
11137 if (error)
11138 return; /* type error; errmsg already given */
11139
Bram Moolenaar758711c2005-02-02 23:11:38 +000011140 if (before == l1->lv_len)
11141 item = NULL;
11142 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011143 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011144 item = list_find(l1, before);
11145 if (item == NULL)
11146 {
11147 EMSGN(_(e_listidx), before);
11148 return;
11149 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011150 }
11151 }
11152 else
11153 item = NULL;
11154 list_extend(l1, l2, item);
11155
Bram Moolenaare9a41262005-01-15 22:18:47 +000011156 copy_tv(&argvars[0], rettv);
11157 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011158 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011159 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11160 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011161 dict_T *d1, *d2;
11162 char_u *action;
11163 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011164
11165 d1 = argvars[0].vval.v_dict;
11166 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011167 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011168 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011169 {
11170 /* Check the third argument. */
11171 if (argvars[2].v_type != VAR_UNKNOWN)
11172 {
11173 static char *(av[]) = {"keep", "force", "error"};
11174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 action = get_tv_string_chk(&argvars[2]);
11176 if (action == NULL)
11177 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011178 for (i = 0; i < 3; ++i)
11179 if (STRCMP(action, av[i]) == 0)
11180 break;
11181 if (i == 3)
11182 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011183 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011184 return;
11185 }
11186 }
11187 else
11188 action = (char_u *)"force";
11189
Bram Moolenaara9922d62013-05-30 13:01:18 +020011190 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011191
Bram Moolenaare9a41262005-01-15 22:18:47 +000011192 copy_tv(&argvars[0], rettv);
11193 }
11194 }
11195 else
11196 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011197}
11198
11199/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011200 * "feedkeys()" function
11201 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011203f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011204{
11205 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011206 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011207 char_u *keys, *flags;
11208 char_u nbuf[NUMBUFLEN];
11209 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011210 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011211 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011212
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011213 /* This is not allowed in the sandbox. If the commands would still be
11214 * executed in the sandbox it would be OK, but it probably happens later,
11215 * when "sandbox" is no longer set. */
11216 if (check_secure())
11217 return;
11218
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011219 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011220
11221 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011222 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011223 flags = get_tv_string_buf(&argvars[1], nbuf);
11224 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011225 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011226 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011227 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011228 case 'n': remap = FALSE; break;
11229 case 'm': remap = TRUE; break;
11230 case 't': typed = TRUE; break;
11231 case 'i': insert = TRUE; break;
11232 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011233 }
11234 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011235 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011236
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011237 if (*keys != NUL || execute)
11238 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011239 /* Need to escape K_SPECIAL and CSI before putting the string in the
11240 * typeahead buffer. */
11241 keys_esc = vim_strsave_escape_csi(keys);
11242 if (keys_esc != NULL)
11243 {
11244 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011245 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011246 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011247 if (vgetc_busy)
11248 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011249 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011250 {
11251 int save_msg_scroll = msg_scroll;
11252
11253 /* Avoid a 1 second delay when the keys start Insert mode. */
11254 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011255 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011256 msg_scroll |= save_msg_scroll;
11257 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011258 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011259 }
11260}
11261
11262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 * "filereadable()" function
11264 */
11265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011266f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011268 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 char_u *p;
11270 int n;
11271
Bram Moolenaarc236c162008-07-13 17:41:49 +000011272#ifndef O_NONBLOCK
11273# define O_NONBLOCK 0
11274#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011276 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11277 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 {
11279 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011280 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 }
11282 else
11283 n = FALSE;
11284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286}
11287
11288/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011289 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 * rights to write into.
11291 */
11292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011293f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011295 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296}
11297
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011298 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011299findfilendir(
11300 typval_T *argvars UNUSED,
11301 typval_T *rettv,
11302 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011303{
11304#ifdef FEAT_SEARCHPATH
11305 char_u *fname;
11306 char_u *fresult = NULL;
11307 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11308 char_u *p;
11309 char_u pathbuf[NUMBUFLEN];
11310 int count = 1;
11311 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011312 int error = FALSE;
11313#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011314
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011315 rettv->vval.v_string = NULL;
11316 rettv->v_type = VAR_STRING;
11317
11318#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011320
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011321 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011323 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11324 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011325 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011326 else
11327 {
11328 if (*p != NUL)
11329 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011332 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011333 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011334 }
11335
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011336 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11337 error = TRUE;
11338
11339 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011341 do
11342 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011343 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011344 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011345 fresult = find_file_in_path_option(first ? fname : NULL,
11346 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011347 0, first, path,
11348 find_what,
11349 curbuf->b_ffname,
11350 find_what == FINDFILE_DIR
11351 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011352 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011353
11354 if (fresult != NULL && rettv->v_type == VAR_LIST)
11355 list_append_string(rettv->vval.v_list, fresult, -1);
11356
11357 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011358 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011359
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011360 if (rettv->v_type == VAR_STRING)
11361 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011362#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011363}
11364
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011365static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11366static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011367
11368/*
11369 * Implementation of map() and filter().
11370 */
11371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011372filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011373{
11374 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011375 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 listitem_T *li, *nli;
11377 list_T *l = NULL;
11378 dictitem_T *di;
11379 hashtab_T *ht;
11380 hashitem_T *hi;
11381 dict_T *d = NULL;
11382 typval_T save_val;
11383 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011384 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011385 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011386 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011387 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011388 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011389 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011390 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011391
Bram Moolenaare9a41262005-01-15 22:18:47 +000011392 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011393 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011394 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011395 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011396 return;
11397 }
11398 else if (argvars[0].v_type == VAR_DICT)
11399 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011400 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011401 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011402 return;
11403 }
11404 else
11405 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011406 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011407 return;
11408 }
11409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 expr = get_tv_string_buf_chk(&argvars[1], buf);
11411 /* On type errors, the preceding call has already displayed an error
11412 * message. Avoid a misleading error message for an empty string that
11413 * was not passed as argument. */
11414 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 prepare_vimvar(VV_VAL, &save_val);
11417 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011418
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011419 /* We reset "did_emsg" to be able to detect whether an error
11420 * occurred during evaluation of the expression. */
11421 save_did_emsg = did_emsg;
11422 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011423
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011424 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011425 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011427 vimvars[VV_KEY].vv_type = VAR_STRING;
11428
11429 ht = &d->dv_hashtab;
11430 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011431 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011432 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011434 if (!HASHITEM_EMPTY(hi))
11435 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011436 int r;
11437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 --todo;
11439 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011440 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011441 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11442 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 break;
11444 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011445 r = filter_map_one(&di->di_tv, expr, map, &rem);
11446 clear_tv(&vimvars[VV_KEY].vv_tv);
11447 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 break;
11449 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011450 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011451 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11452 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011453 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011454 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011455 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011456 }
11457 }
11458 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011459 }
11460 else
11461 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011462 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464 for (li = l->lv_first; li != NULL; li = nli)
11465 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011466 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011467 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011469 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011470 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011471 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011472 break;
11473 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011474 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011475 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011476 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011477 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011478
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011479 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011481
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011482 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011483 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011484
11485 copy_tv(&argvars[0], rettv);
11486}
11487
11488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011489filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011490{
Bram Moolenaar33570922005-01-25 22:26:29 +000011491 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011492 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011493 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011494
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011496 s = expr;
11497 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011498 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011499 if (*s != NUL) /* check for trailing chars after expr */
11500 {
11501 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011502 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011503 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011504 }
11505 if (map)
11506 {
11507 /* map(): replace the list item value */
11508 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011509 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011510 *tv = rettv;
11511 }
11512 else
11513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 int error = FALSE;
11515
Bram Moolenaare9a41262005-01-15 22:18:47 +000011516 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 /* On type error, nothing has been removed; return FAIL to stop the
11520 * loop. The error message was given by get_tv_number_chk(). */
11521 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011522 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011523 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011524 retval = OK;
11525theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011526 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011527 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011528}
11529
11530/*
11531 * "filter()" function
11532 */
11533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011534f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011535{
11536 filter_map(argvars, rettv, FALSE);
11537}
11538
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011539/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540 * "finddir({fname}[, {path}[, {count}]])" function
11541 */
11542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011543f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011544{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011545 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011546}
11547
11548/*
11549 * "findfile({fname}[, {path}[, {count}]])" function
11550 */
11551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011552f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011553{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011554 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011555}
11556
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011557#ifdef FEAT_FLOAT
11558/*
11559 * "float2nr({float})" function
11560 */
11561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011562f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011563{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011564 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011565
11566 if (get_float_arg(argvars, &f) == OK)
11567 {
11568 if (f < -0x7fffffff)
11569 rettv->vval.v_number = -0x7fffffff;
11570 else if (f > 0x7fffffff)
11571 rettv->vval.v_number = 0x7fffffff;
11572 else
11573 rettv->vval.v_number = (varnumber_T)f;
11574 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011575}
11576
11577/*
11578 * "floor({float})" function
11579 */
11580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011581f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011582{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011583 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011584
11585 rettv->v_type = VAR_FLOAT;
11586 if (get_float_arg(argvars, &f) == OK)
11587 rettv->vval.v_float = floor(f);
11588 else
11589 rettv->vval.v_float = 0.0;
11590}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011591
11592/*
11593 * "fmod()" function
11594 */
11595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011596f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011597{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011598 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011599
11600 rettv->v_type = VAR_FLOAT;
11601 if (get_float_arg(argvars, &fx) == OK
11602 && get_float_arg(&argvars[1], &fy) == OK)
11603 rettv->vval.v_float = fmod(fx, fy);
11604 else
11605 rettv->vval.v_float = 0.0;
11606}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011607#endif
11608
Bram Moolenaar0d660222005-01-07 21:51:51 +000011609/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011610 * "fnameescape({string})" function
11611 */
11612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011613f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011614{
11615 rettv->vval.v_string = vim_strsave_fnameescape(
11616 get_tv_string(&argvars[0]), FALSE);
11617 rettv->v_type = VAR_STRING;
11618}
11619
11620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 * "fnamemodify({fname}, {mods})" function
11622 */
11623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011624f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625{
11626 char_u *fname;
11627 char_u *mods;
11628 int usedlen = 0;
11629 int len;
11630 char_u *fbuf = NULL;
11631 char_u buf[NUMBUFLEN];
11632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011633 fname = get_tv_string_chk(&argvars[0]);
11634 mods = get_tv_string_buf_chk(&argvars[1], buf);
11635 if (fname == NULL || mods == NULL)
11636 fname = NULL;
11637 else
11638 {
11639 len = (int)STRLEN(fname);
11640 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 vim_free(fbuf);
11649}
11650
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011651static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652
11653/*
11654 * "foldclosed()" function
11655 */
11656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011657foldclosed_both(
11658 typval_T *argvars UNUSED,
11659 typval_T *rettv,
11660 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661{
11662#ifdef FEAT_FOLDING
11663 linenr_T lnum;
11664 linenr_T first, last;
11665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11668 {
11669 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11670 {
11671 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011672 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011674 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 return;
11676 }
11677 }
11678#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680}
11681
11682/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011683 * "foldclosed()" function
11684 */
11685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011686f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011687{
11688 foldclosed_both(argvars, rettv, FALSE);
11689}
11690
11691/*
11692 * "foldclosedend()" function
11693 */
11694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011695f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011696{
11697 foldclosed_both(argvars, rettv, TRUE);
11698}
11699
11700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 * "foldlevel()" function
11702 */
11703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011704f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705{
11706#ifdef FEAT_FOLDING
11707 linenr_T lnum;
11708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011711 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713}
11714
11715/*
11716 * "foldtext()" function
11717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011719f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720{
11721#ifdef FEAT_FOLDING
11722 linenr_T lnum;
11723 char_u *s;
11724 char_u *r;
11725 int len;
11726 char *txt;
11727#endif
11728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->v_type = VAR_STRING;
11730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11733 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11734 <= curbuf->b_ml.ml_line_count
11735 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 {
11737 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011738 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11739 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 {
11741 if (!linewhite(lnum))
11742 break;
11743 ++lnum;
11744 }
11745
11746 /* Find interesting text in this line. */
11747 s = skipwhite(ml_get(lnum));
11748 /* skip C comment-start */
11749 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011750 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011752 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011753 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011754 {
11755 s = skipwhite(ml_get(lnum + 1));
11756 if (*s == '*')
11757 s = skipwhite(s + 1);
11758 }
11759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 txt = _("+-%s%3ld lines: ");
11761 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011762 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 + 20 /* for %3ld */
11764 + STRLEN(s))); /* concatenated */
11765 if (r != NULL)
11766 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011767 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11768 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11769 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 len = (int)STRLEN(r);
11771 STRCAT(r, s);
11772 /* remove 'foldmarker' and 'commentstring' */
11773 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011774 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 }
11776 }
11777#endif
11778}
11779
11780/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011781 * "foldtextresult(lnum)" function
11782 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011784f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011785{
11786#ifdef FEAT_FOLDING
11787 linenr_T lnum;
11788 char_u *text;
11789 char_u buf[51];
11790 foldinfo_T foldinfo;
11791 int fold_count;
11792#endif
11793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 rettv->v_type = VAR_STRING;
11795 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011796#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011798 /* treat illegal types and illegal string values for {lnum} the same */
11799 if (lnum < 0)
11800 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011801 fold_count = foldedCount(curwin, lnum, &foldinfo);
11802 if (fold_count > 0)
11803 {
11804 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11805 &foldinfo, buf);
11806 if (text == buf)
11807 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011809 }
11810#endif
11811}
11812
11813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814 * "foreground()" function
11815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011817f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819#ifdef FEAT_GUI
11820 if (gui.in_use)
11821 gui_mch_set_foreground();
11822#else
11823# ifdef WIN32
11824 win32_set_foreground();
11825# endif
11826#endif
11827}
11828
11829/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011830 * "function()" function
11831 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011833f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011834{
11835 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011836 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011837 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011838 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011839
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011840 if (argvars[0].v_type == VAR_FUNC)
11841 {
11842 /* function(MyFunc, [arg], dict) */
11843 s = argvars[0].vval.v_string;
11844 }
11845 else if (argvars[0].v_type == VAR_PARTIAL
11846 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011847 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011848 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011849 arg_pt = argvars[0].vval.v_partial;
11850 s = arg_pt->pt_name;
11851 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011852 else
11853 {
11854 /* function('MyFunc', [arg], dict) */
11855 s = get_tv_string(&argvars[0]);
11856 use_string = TRUE;
11857 }
11858
11859 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011860 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011861 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011862 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11863 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011864 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011865 else
11866 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011867 int dict_idx = 0;
11868 int arg_idx = 0;
11869 list_T *list = NULL;
11870
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011871 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011872 {
11873 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011874 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011875
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011876 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11877 * also be called from another script. Using trans_function_name()
11878 * would also work, but some plugins depend on the name being
11879 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011880 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011881 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11882 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011883 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011884 STRCPY(name, sid_buf);
11885 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011886 }
11887 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011888 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011889 name = vim_strsave(s);
11890
11891 if (argvars[1].v_type != VAR_UNKNOWN)
11892 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011893 if (argvars[2].v_type != VAR_UNKNOWN)
11894 {
11895 /* function(name, [args], dict) */
11896 arg_idx = 1;
11897 dict_idx = 2;
11898 }
11899 else if (argvars[1].v_type == VAR_DICT)
11900 /* function(name, dict) */
11901 dict_idx = 1;
11902 else
11903 /* function(name, [args]) */
11904 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011905 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011906 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011907 if (argvars[dict_idx].v_type != VAR_DICT)
11908 {
11909 EMSG(_("E922: expected a dict"));
11910 vim_free(name);
11911 return;
11912 }
11913 if (argvars[dict_idx].vval.v_dict == NULL)
11914 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011915 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011916 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011917 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011918 if (argvars[arg_idx].v_type != VAR_LIST)
11919 {
11920 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11921 vim_free(name);
11922 return;
11923 }
11924 list = argvars[arg_idx].vval.v_list;
11925 if (list == NULL || list->lv_len == 0)
11926 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011927 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011928 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011929 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010011930 {
11931 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011932
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011933 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010011934 if (pt == NULL)
11935 vim_free(name);
11936 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011937 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011938 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011939 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011940 listitem_T *li;
11941 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011942 int arg_len = 0;
11943 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011944
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011945 if (arg_pt != NULL)
11946 arg_len = arg_pt->pt_argc;
11947 if (list != NULL)
11948 lv_len = list->lv_len;
11949 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011950 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011951 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011952 if (pt->pt_argv == NULL)
11953 {
11954 vim_free(pt);
11955 vim_free(name);
11956 return;
11957 }
11958 else
11959 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011960 for (i = 0; i < arg_len; i++)
11961 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
11962 if (lv_len > 0)
11963 for (li = list->lv_first; li != NULL;
11964 li = li->li_next)
11965 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011966 }
11967 }
11968
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011969 /* For "function(dict.func, [], dict)" and "func" is a partial
11970 * use "dict". That is backwards compatible. */
11971 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011972 {
11973 pt->pt_dict = argvars[dict_idx].vval.v_dict;
11974 ++pt->pt_dict->dv_refcount;
11975 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011976 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011977 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011978 pt->pt_dict = arg_pt->pt_dict;
11979 if (pt->pt_dict != NULL)
11980 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011981 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011982
11983 pt->pt_refcount = 1;
11984 pt->pt_name = name;
11985 func_ref(pt->pt_name);
11986 }
11987 rettv->v_type = VAR_PARTIAL;
11988 rettv->vval.v_partial = pt;
11989 }
11990 else
11991 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011992 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011993 rettv->v_type = VAR_FUNC;
11994 rettv->vval.v_string = name;
11995 func_ref(name);
11996 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011997 }
11998}
11999
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012000 static void
12001partial_free(partial_T *pt)
12002{
12003 int i;
12004
12005 for (i = 0; i < pt->pt_argc; ++i)
12006 clear_tv(&pt->pt_argv[i]);
12007 vim_free(pt->pt_argv);
12008 func_unref(pt->pt_name);
12009 vim_free(pt->pt_name);
12010 vim_free(pt);
12011}
12012
12013/*
12014 * Unreference a closure: decrement the reference count and free it when it
12015 * becomes zero.
12016 */
12017 void
12018partial_unref(partial_T *pt)
12019{
12020 if (pt != NULL && --pt->pt_refcount <= 0)
12021 partial_free(pt);
12022}
12023
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012024/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012025 * "garbagecollect()" function
12026 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012028f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012029{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012030 /* This is postponed until we are back at the toplevel, because we may be
12031 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12032 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012033
12034 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12035 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012036}
12037
12038/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012039 * "get()" function
12040 */
12041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012042f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012043{
Bram Moolenaar33570922005-01-25 22:26:29 +000012044 listitem_T *li;
12045 list_T *l;
12046 dictitem_T *di;
12047 dict_T *d;
12048 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012049
Bram Moolenaare9a41262005-01-15 22:18:47 +000012050 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012051 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012052 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012054 int error = FALSE;
12055
12056 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12057 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012058 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012059 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012060 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012061 else if (argvars[0].v_type == VAR_DICT)
12062 {
12063 if ((d = argvars[0].vval.v_dict) != NULL)
12064 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012065 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012066 if (di != NULL)
12067 tv = &di->di_tv;
12068 }
12069 }
12070 else
12071 EMSG2(_(e_listdictarg), "get()");
12072
12073 if (tv == NULL)
12074 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012075 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012076 copy_tv(&argvars[2], rettv);
12077 }
12078 else
12079 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012080}
12081
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012082static 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 +000012083
12084/*
12085 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012086 * Return a range (from start to end) of lines in rettv from the specified
12087 * buffer.
12088 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012089 */
12090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012091get_buffer_lines(
12092 buf_T *buf,
12093 linenr_T start,
12094 linenr_T end,
12095 int retlist,
12096 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012097{
12098 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012099
Bram Moolenaar959a1432013-12-14 12:17:38 +010012100 rettv->v_type = VAR_STRING;
12101 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012102 if (retlist && rettv_list_alloc(rettv) == FAIL)
12103 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012104
12105 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12106 return;
12107
12108 if (!retlist)
12109 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012110 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12111 p = ml_get_buf(buf, start, FALSE);
12112 else
12113 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012114 rettv->vval.v_string = vim_strsave(p);
12115 }
12116 else
12117 {
12118 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012119 return;
12120
12121 if (start < 1)
12122 start = 1;
12123 if (end > buf->b_ml.ml_line_count)
12124 end = buf->b_ml.ml_line_count;
12125 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012126 if (list_append_string(rettv->vval.v_list,
12127 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012128 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012129 }
12130}
12131
12132/*
12133 * "getbufline()" function
12134 */
12135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012136f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012137{
12138 linenr_T lnum;
12139 linenr_T end;
12140 buf_T *buf;
12141
12142 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12143 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012144 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012145 --emsg_off;
12146
Bram Moolenaar661b1822005-07-28 22:36:45 +000012147 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012148 if (argvars[2].v_type == VAR_UNKNOWN)
12149 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012150 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012151 end = get_tv_lnum_buf(&argvars[2], buf);
12152
Bram Moolenaar342337a2005-07-21 21:11:17 +000012153 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012154}
12155
Bram Moolenaar0d660222005-01-07 21:51:51 +000012156/*
12157 * "getbufvar()" function
12158 */
12159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012160f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161{
12162 buf_T *buf;
12163 buf_T *save_curbuf;
12164 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012165 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012166 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012168 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12169 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012170 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012171 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012172
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012173 rettv->v_type = VAR_STRING;
12174 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012175
12176 if (buf != NULL && varname != NULL)
12177 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012178 /* set curbuf to be our buf, temporarily */
12179 save_curbuf = curbuf;
12180 curbuf = buf;
12181
Bram Moolenaar0d660222005-01-07 21:51:51 +000012182 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012183 {
12184 if (get_option_tv(&varname, rettv, TRUE) == OK)
12185 done = TRUE;
12186 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012187 else if (STRCMP(varname, "changedtick") == 0)
12188 {
12189 rettv->v_type = VAR_NUMBER;
12190 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012191 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012192 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012193 else
12194 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012195 /* Look up the variable. */
12196 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12197 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12198 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012199 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012200 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012201 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012202 done = TRUE;
12203 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012204 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012205
12206 /* restore previous notion of curbuf */
12207 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012208 }
12209
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012210 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12211 /* use the default value */
12212 copy_tv(&argvars[2], rettv);
12213
Bram Moolenaar0d660222005-01-07 21:51:51 +000012214 --emsg_off;
12215}
12216
12217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 * "getchar()" function
12219 */
12220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012221f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222{
12223 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012224 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012226 /* Position the cursor. Needed after a message that ends in a space. */
12227 windgoto(msg_row, msg_col);
12228
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 ++no_mapping;
12230 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012231 for (;;)
12232 {
12233 if (argvars[0].v_type == VAR_UNKNOWN)
12234 /* getchar(): blocking wait. */
12235 n = safe_vgetc();
12236 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12237 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012238 n = vpeekc_any();
12239 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012240 /* illegal argument or getchar(0) and no char avail: return zero */
12241 n = 0;
12242 else
12243 /* getchar(0) and char avail: return char */
12244 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012245
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012246 if (n == K_IGNORE)
12247 continue;
12248 break;
12249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250 --no_mapping;
12251 --allow_keys;
12252
Bram Moolenaar219b8702006-11-01 14:32:36 +000012253 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12254 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12255 vimvars[VV_MOUSE_COL].vv_nr = 0;
12256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 if (IS_SPECIAL(n) || mod_mask != 0)
12259 {
12260 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12261 int i = 0;
12262
12263 /* Turn a special key into three bytes, plus modifier. */
12264 if (mod_mask != 0)
12265 {
12266 temp[i++] = K_SPECIAL;
12267 temp[i++] = KS_MODIFIER;
12268 temp[i++] = mod_mask;
12269 }
12270 if (IS_SPECIAL(n))
12271 {
12272 temp[i++] = K_SPECIAL;
12273 temp[i++] = K_SECOND(n);
12274 temp[i++] = K_THIRD(n);
12275 }
12276#ifdef FEAT_MBYTE
12277 else if (has_mbyte)
12278 i += (*mb_char2bytes)(n, temp + i);
12279#endif
12280 else
12281 temp[i++] = n;
12282 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283 rettv->v_type = VAR_STRING;
12284 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012285
12286#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012287 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012288 {
12289 int row = mouse_row;
12290 int col = mouse_col;
12291 win_T *win;
12292 linenr_T lnum;
12293# ifdef FEAT_WINDOWS
12294 win_T *wp;
12295# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012296 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012297
12298 if (row >= 0 && col >= 0)
12299 {
12300 /* Find the window at the mouse coordinates and compute the
12301 * text position. */
12302 win = mouse_find_win(&row, &col);
12303 (void)mouse_comp_pos(win, &row, &col, &lnum);
12304# ifdef FEAT_WINDOWS
12305 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012306 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012307# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012308 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012309 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12310 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12311 }
12312 }
12313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 }
12315}
12316
12317/*
12318 * "getcharmod()" function
12319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012321f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324}
12325
12326/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012327 * "getcharsearch()" function
12328 */
12329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012330f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012331{
12332 if (rettv_dict_alloc(rettv) != FAIL)
12333 {
12334 dict_T *dict = rettv->vval.v_dict;
12335
12336 dict_add_nr_str(dict, "char", 0L, last_csearch());
12337 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12338 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12339 }
12340}
12341
12342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 * "getcmdline()" function
12344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012346f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->v_type = VAR_STRING;
12349 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350}
12351
12352/*
12353 * "getcmdpos()" function
12354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012356f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012358 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359}
12360
12361/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012362 * "getcmdtype()" function
12363 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012365f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012366{
12367 rettv->v_type = VAR_STRING;
12368 rettv->vval.v_string = alloc(2);
12369 if (rettv->vval.v_string != NULL)
12370 {
12371 rettv->vval.v_string[0] = get_cmdline_type();
12372 rettv->vval.v_string[1] = NUL;
12373 }
12374}
12375
12376/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012377 * "getcmdwintype()" function
12378 */
12379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012380f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012381{
12382 rettv->v_type = VAR_STRING;
12383 rettv->vval.v_string = NULL;
12384#ifdef FEAT_CMDWIN
12385 rettv->vval.v_string = alloc(2);
12386 if (rettv->vval.v_string != NULL)
12387 {
12388 rettv->vval.v_string[0] = cmdwin_type;
12389 rettv->vval.v_string[1] = NUL;
12390 }
12391#endif
12392}
12393
12394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 * "getcwd()" function
12396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012398f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012400 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012401 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012404 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012405
12406 wp = find_tabwin(&argvars[0], &argvars[1]);
12407 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012409 if (wp->w_localdir != NULL)
12410 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12411 else if(globaldir != NULL)
12412 rettv->vval.v_string = vim_strsave(globaldir);
12413 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012414 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012415 cwd = alloc(MAXPATHL);
12416 if (cwd != NULL)
12417 {
12418 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12419 rettv->vval.v_string = vim_strsave(cwd);
12420 vim_free(cwd);
12421 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012422 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012423#ifdef BACKSLASH_IN_FILENAME
12424 if (rettv->vval.v_string != NULL)
12425 slash_adjust(rettv->vval.v_string);
12426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 }
12428}
12429
12430/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012431 * "getfontname()" function
12432 */
12433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012434f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012435{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012436 rettv->v_type = VAR_STRING;
12437 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012438#ifdef FEAT_GUI
12439 if (gui.in_use)
12440 {
12441 GuiFont font;
12442 char_u *name = NULL;
12443
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012444 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012445 {
12446 /* Get the "Normal" font. Either the name saved by
12447 * hl_set_font_name() or from the font ID. */
12448 font = gui.norm_font;
12449 name = hl_get_font_name();
12450 }
12451 else
12452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012453 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012454 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12455 return;
12456 font = gui_mch_get_font(name, FALSE);
12457 if (font == NOFONT)
12458 return; /* Invalid font name, return empty string. */
12459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012460 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012461 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012462 gui_mch_free_font(font);
12463 }
12464#endif
12465}
12466
12467/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012468 * "getfperm({fname})" function
12469 */
12470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012471f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012472{
12473 char_u *fname;
12474 struct stat st;
12475 char_u *perm = NULL;
12476 char_u flags[] = "rwx";
12477 int i;
12478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012482 if (mch_stat((char *)fname, &st) >= 0)
12483 {
12484 perm = vim_strsave((char_u *)"---------");
12485 if (perm != NULL)
12486 {
12487 for (i = 0; i < 9; i++)
12488 {
12489 if (st.st_mode & (1 << (8 - i)))
12490 perm[i] = flags[i % 3];
12491 }
12492 }
12493 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012495}
12496
12497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 * "getfsize({fname})" function
12499 */
12500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012501f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502{
12503 char_u *fname;
12504 struct stat st;
12505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012508 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509
12510 if (mch_stat((char *)fname, &st) >= 0)
12511 {
12512 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012516 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012517
12518 /* non-perfect check for overflow */
12519 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12520 rettv->vval.v_number = -2;
12521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 }
12523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012525}
12526
12527/*
12528 * "getftime({fname})" function
12529 */
12530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012531f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532{
12533 char_u *fname;
12534 struct stat st;
12535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012536 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537
12538 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012541 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542}
12543
12544/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012545 * "getftype({fname})" function
12546 */
12547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012548f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012549{
12550 char_u *fname;
12551 struct stat st;
12552 char_u *type = NULL;
12553 char *t;
12554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012555 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012557 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012558 if (mch_lstat((char *)fname, &st) >= 0)
12559 {
12560#ifdef S_ISREG
12561 if (S_ISREG(st.st_mode))
12562 t = "file";
12563 else if (S_ISDIR(st.st_mode))
12564 t = "dir";
12565# ifdef S_ISLNK
12566 else if (S_ISLNK(st.st_mode))
12567 t = "link";
12568# endif
12569# ifdef S_ISBLK
12570 else if (S_ISBLK(st.st_mode))
12571 t = "bdev";
12572# endif
12573# ifdef S_ISCHR
12574 else if (S_ISCHR(st.st_mode))
12575 t = "cdev";
12576# endif
12577# ifdef S_ISFIFO
12578 else if (S_ISFIFO(st.st_mode))
12579 t = "fifo";
12580# endif
12581# ifdef S_ISSOCK
12582 else if (S_ISSOCK(st.st_mode))
12583 t = "fifo";
12584# endif
12585 else
12586 t = "other";
12587#else
12588# ifdef S_IFMT
12589 switch (st.st_mode & S_IFMT)
12590 {
12591 case S_IFREG: t = "file"; break;
12592 case S_IFDIR: t = "dir"; break;
12593# ifdef S_IFLNK
12594 case S_IFLNK: t = "link"; break;
12595# endif
12596# ifdef S_IFBLK
12597 case S_IFBLK: t = "bdev"; break;
12598# endif
12599# ifdef S_IFCHR
12600 case S_IFCHR: t = "cdev"; break;
12601# endif
12602# ifdef S_IFIFO
12603 case S_IFIFO: t = "fifo"; break;
12604# endif
12605# ifdef S_IFSOCK
12606 case S_IFSOCK: t = "socket"; break;
12607# endif
12608 default: t = "other";
12609 }
12610# else
12611 if (mch_isdir(fname))
12612 t = "dir";
12613 else
12614 t = "file";
12615# endif
12616#endif
12617 type = vim_strsave((char_u *)t);
12618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012619 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012620}
12621
12622/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012623 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012624 */
12625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012626f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012627{
12628 linenr_T lnum;
12629 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012630 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012631
12632 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012633 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012634 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012635 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012636 retlist = FALSE;
12637 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012638 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012639 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012640 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012641 retlist = TRUE;
12642 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012643
Bram Moolenaar342337a2005-07-21 21:11:17 +000012644 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012645}
12646
12647/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012648 * "getmatches()" function
12649 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012651f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012652{
12653#ifdef FEAT_SEARCH_EXTRA
12654 dict_T *dict;
12655 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012656 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012657
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012658 if (rettv_list_alloc(rettv) == OK)
12659 {
12660 while (cur != NULL)
12661 {
12662 dict = dict_alloc();
12663 if (dict == NULL)
12664 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012665 if (cur->match.regprog == NULL)
12666 {
12667 /* match added with matchaddpos() */
12668 for (i = 0; i < MAXPOSMATCH; ++i)
12669 {
12670 llpos_T *llpos;
12671 char buf[6];
12672 list_T *l;
12673
12674 llpos = &cur->pos.pos[i];
12675 if (llpos->lnum == 0)
12676 break;
12677 l = list_alloc();
12678 if (l == NULL)
12679 break;
12680 list_append_number(l, (varnumber_T)llpos->lnum);
12681 if (llpos->col > 0)
12682 {
12683 list_append_number(l, (varnumber_T)llpos->col);
12684 list_append_number(l, (varnumber_T)llpos->len);
12685 }
12686 sprintf(buf, "pos%d", i + 1);
12687 dict_add_list(dict, buf, l);
12688 }
12689 }
12690 else
12691 {
12692 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12693 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012694 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012695 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12696 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012697# ifdef FEAT_CONCEAL
12698 if (cur->conceal_char)
12699 {
12700 char_u buf[MB_MAXBYTES + 1];
12701
12702 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12703 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12704 }
12705# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012706 list_append_dict(rettv->vval.v_list, dict);
12707 cur = cur->next;
12708 }
12709 }
12710#endif
12711}
12712
12713/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012714 * "getpid()" function
12715 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012717f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012718{
12719 rettv->vval.v_number = mch_get_pid();
12720}
12721
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012722static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012723
12724/*
12725 * "getcurpos()" function
12726 */
12727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012728f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012729{
12730 getpos_both(argvars, rettv, TRUE);
12731}
12732
Bram Moolenaar18081e32008-02-20 19:11:07 +000012733/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012734 * "getpos(string)" function
12735 */
12736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012737f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012738{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012739 getpos_both(argvars, rettv, FALSE);
12740}
12741
12742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012743getpos_both(
12744 typval_T *argvars,
12745 typval_T *rettv,
12746 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012747{
Bram Moolenaara5525202006-03-02 22:52:09 +000012748 pos_T *fp;
12749 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012750 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012751
12752 if (rettv_list_alloc(rettv) == OK)
12753 {
12754 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012755 if (getcurpos)
12756 fp = &curwin->w_cursor;
12757 else
12758 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012759 if (fnum != -1)
12760 list_append_number(l, (varnumber_T)fnum);
12761 else
12762 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012763 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12764 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012765 list_append_number(l, (fp != NULL)
12766 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012767 : (varnumber_T)0);
12768 list_append_number(l,
12769#ifdef FEAT_VIRTUALEDIT
12770 (fp != NULL) ? (varnumber_T)fp->coladd :
12771#endif
12772 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012773 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012774 {
12775 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012776 list_append_number(l, curwin->w_curswant == MAXCOL ?
12777 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012778 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012779 }
12780 else
12781 rettv->vval.v_number = FALSE;
12782}
12783
12784/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012785 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012786 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012788f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012789{
12790#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012791 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012792#endif
12793
Bram Moolenaar2641f772005-03-25 21:58:17 +000012794#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012795 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012796 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012797 wp = NULL;
12798 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12799 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012800 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012801 if (wp == NULL)
12802 return;
12803 }
12804
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012805 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012806 }
12807#endif
12808}
12809
12810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 * "getreg()" function
12812 */
12813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012814f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815{
12816 char_u *strregname;
12817 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012818 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012819 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012820 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012822 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012824 strregname = get_tv_string_chk(&argvars[0]);
12825 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012826 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012828 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012829 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12830 return_list = get_tv_number_chk(&argvars[2], &error);
12831 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012834 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012835
12836 if (error)
12837 return;
12838
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 regname = (strregname == NULL ? '"' : *strregname);
12840 if (regname == 0)
12841 regname = '"';
12842
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012843 if (return_list)
12844 {
12845 rettv->v_type = VAR_LIST;
12846 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12847 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012848 if (rettv->vval.v_list != NULL)
12849 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012850 }
12851 else
12852 {
12853 rettv->v_type = VAR_STRING;
12854 rettv->vval.v_string = get_reg_contents(regname,
12855 arg2 ? GREG_EXPR_SRC : 0);
12856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857}
12858
12859/*
12860 * "getregtype()" function
12861 */
12862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012863f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864{
12865 char_u *strregname;
12866 int regname;
12867 char_u buf[NUMBUFLEN + 2];
12868 long reglen = 0;
12869
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012870 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012871 {
12872 strregname = get_tv_string_chk(&argvars[0]);
12873 if (strregname == NULL) /* type error; errmsg already given */
12874 {
12875 rettv->v_type = VAR_STRING;
12876 rettv->vval.v_string = NULL;
12877 return;
12878 }
12879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 else
12881 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012882 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883
12884 regname = (strregname == NULL ? '"' : *strregname);
12885 if (regname == 0)
12886 regname = '"';
12887
12888 buf[0] = NUL;
12889 buf[1] = NUL;
12890 switch (get_reg_type(regname, &reglen))
12891 {
12892 case MLINE: buf[0] = 'V'; break;
12893 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894 case MBLOCK:
12895 buf[0] = Ctrl_V;
12896 sprintf((char *)buf + 1, "%ld", reglen + 1);
12897 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899 rettv->v_type = VAR_STRING;
12900 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901}
12902
12903/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012904 * "gettabvar()" function
12905 */
12906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012907f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012908{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012909 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012910 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012911 dictitem_T *v;
12912 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012913 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012914
12915 rettv->v_type = VAR_STRING;
12916 rettv->vval.v_string = NULL;
12917
12918 varname = get_tv_string_chk(&argvars[1]);
12919 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12920 if (tp != NULL && varname != NULL)
12921 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012922 /* Set tp to be our tabpage, temporarily. Also set the window to the
12923 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012924 if (switch_win(&oldcurwin, &oldtabpage,
12925 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012926 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012927 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012928 /* look up the variable */
12929 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12930 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12931 if (v != NULL)
12932 {
12933 copy_tv(&v->di_tv, rettv);
12934 done = TRUE;
12935 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012936 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012937
12938 /* restore previous notion of curwin */
12939 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012940 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012941
12942 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012943 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012944}
12945
12946/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012947 * "gettabwinvar()" function
12948 */
12949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012950f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012951{
12952 getwinvar(argvars, rettv, 1);
12953}
12954
12955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956 * "getwinposx()" function
12957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012959f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012961 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962#ifdef FEAT_GUI
12963 if (gui.in_use)
12964 {
12965 int x, y;
12966
12967 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012968 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969 }
12970#endif
12971}
12972
12973/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012974 * "win_findbuf()" function
12975 */
12976 static void
12977f_win_findbuf(typval_T *argvars, typval_T *rettv)
12978{
12979 if (rettv_list_alloc(rettv) != FAIL)
12980 win_findbuf(argvars, rettv->vval.v_list);
12981}
12982
12983/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012984 * "win_getid()" function
12985 */
12986 static void
12987f_win_getid(typval_T *argvars, typval_T *rettv)
12988{
12989 rettv->vval.v_number = win_getid(argvars);
12990}
12991
12992/*
12993 * "win_gotoid()" function
12994 */
12995 static void
12996f_win_gotoid(typval_T *argvars, typval_T *rettv)
12997{
12998 rettv->vval.v_number = win_gotoid(argvars);
12999}
13000
13001/*
13002 * "win_id2tabwin()" function
13003 */
13004 static void
13005f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13006{
13007 if (rettv_list_alloc(rettv) != FAIL)
13008 win_id2tabwin(argvars, rettv->vval.v_list);
13009}
13010
13011/*
13012 * "win_id2win()" function
13013 */
13014 static void
13015f_win_id2win(typval_T *argvars, typval_T *rettv)
13016{
13017 rettv->vval.v_number = win_id2win(argvars);
13018}
13019
13020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 * "getwinposy()" function
13022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013024f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027#ifdef FEAT_GUI
13028 if (gui.in_use)
13029 {
13030 int x, y;
13031
13032 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013033 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 }
13035#endif
13036}
13037
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013038/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013039 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013040 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013041 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013042find_win_by_nr(
13043 typval_T *vp,
13044 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013045{
13046#ifdef FEAT_WINDOWS
13047 win_T *wp;
13048#endif
13049 int nr;
13050
13051 nr = get_tv_number_chk(vp, NULL);
13052
13053#ifdef FEAT_WINDOWS
13054 if (nr < 0)
13055 return NULL;
13056 if (nr == 0)
13057 return curwin;
13058
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013059 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13060 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013061 if (--nr <= 0)
13062 break;
13063 return wp;
13064#else
13065 if (nr == 0 || nr == 1)
13066 return curwin;
13067 return NULL;
13068#endif
13069}
13070
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013072 * Find window specified by "wvp" in tabpage "tvp".
13073 */
13074 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013075find_tabwin(
13076 typval_T *wvp, /* VAR_UNKNOWN for current window */
13077 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013078{
13079 win_T *wp = NULL;
13080 tabpage_T *tp = NULL;
13081 long n;
13082
13083 if (wvp->v_type != VAR_UNKNOWN)
13084 {
13085 if (tvp->v_type != VAR_UNKNOWN)
13086 {
13087 n = get_tv_number(tvp);
13088 if (n >= 0)
13089 tp = find_tabpage(n);
13090 }
13091 else
13092 tp = curtab;
13093
13094 if (tp != NULL)
13095 wp = find_win_by_nr(wvp, tp);
13096 }
13097 else
13098 wp = curwin;
13099
13100 return wp;
13101}
13102
13103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 * "getwinvar()" function
13105 */
13106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013107f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013109 getwinvar(argvars, rettv, 0);
13110}
13111
13112/*
13113 * getwinvar() and gettabwinvar()
13114 */
13115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013116getwinvar(
13117 typval_T *argvars,
13118 typval_T *rettv,
13119 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013120{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013121 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013123 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013124 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013125 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013126#ifdef FEAT_WINDOWS
13127 win_T *oldcurwin;
13128 tabpage_T *oldtabpage;
13129 int need_switch_win;
13130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013132#ifdef FEAT_WINDOWS
13133 if (off == 1)
13134 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13135 else
13136 tp = curtab;
13137#endif
13138 win = find_win_by_nr(&argvars[off], tp);
13139 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013140 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013142 rettv->v_type = VAR_STRING;
13143 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144
13145 if (win != NULL && varname != NULL)
13146 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013147#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013148 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013149 * otherwise the window is not valid. Only do this when needed,
13150 * autocommands get blocked. */
13151 need_switch_win = !(tp == curtab && win == curwin);
13152 if (!need_switch_win
13153 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13154#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013155 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013156 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013157 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013158 if (get_option_tv(&varname, rettv, 1) == OK)
13159 done = TRUE;
13160 }
13161 else
13162 {
13163 /* Look up the variable. */
13164 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13165 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13166 varname, FALSE);
13167 if (v != NULL)
13168 {
13169 copy_tv(&v->di_tv, rettv);
13170 done = TRUE;
13171 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013174
Bram Moolenaarba117c22015-09-29 16:53:22 +020013175#ifdef FEAT_WINDOWS
13176 if (need_switch_win)
13177 /* restore previous notion of curwin */
13178 restore_win(oldcurwin, oldtabpage, TRUE);
13179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180 }
13181
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013182 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13183 /* use the default return value */
13184 copy_tv(&argvars[off + 2], rettv);
13185
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 --emsg_off;
13187}
13188
13189/*
13190 * "glob()" function
13191 */
13192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013193f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013195 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013197 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013199 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013200 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013201 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013202 if (argvars[1].v_type != VAR_UNKNOWN)
13203 {
13204 if (get_tv_number_chk(&argvars[1], &error))
13205 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013206 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013207 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013208 if (get_tv_number_chk(&argvars[2], &error))
13209 {
13210 rettv->v_type = VAR_LIST;
13211 rettv->vval.v_list = NULL;
13212 }
13213 if (argvars[3].v_type != VAR_UNKNOWN
13214 && get_tv_number_chk(&argvars[3], &error))
13215 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013216 }
13217 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013218 if (!error)
13219 {
13220 ExpandInit(&xpc);
13221 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013222 if (p_wic)
13223 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013224 if (rettv->v_type == VAR_STRING)
13225 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013226 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013227 else if (rettv_list_alloc(rettv) != FAIL)
13228 {
13229 int i;
13230
13231 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13232 NULL, options, WILD_ALL_KEEP);
13233 for (i = 0; i < xpc.xp_numfiles; i++)
13234 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13235
13236 ExpandCleanup(&xpc);
13237 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013238 }
13239 else
13240 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241}
13242
13243/*
13244 * "globpath()" function
13245 */
13246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013247f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013249 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013251 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013252 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013253 garray_T ga;
13254 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013256 /* When the optional second argument is non-zero, don't remove matches
13257 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013258 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013259 if (argvars[2].v_type != VAR_UNKNOWN)
13260 {
13261 if (get_tv_number_chk(&argvars[2], &error))
13262 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013263 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013264 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013265 if (get_tv_number_chk(&argvars[3], &error))
13266 {
13267 rettv->v_type = VAR_LIST;
13268 rettv->vval.v_list = NULL;
13269 }
13270 if (argvars[4].v_type != VAR_UNKNOWN
13271 && get_tv_number_chk(&argvars[4], &error))
13272 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013273 }
13274 }
13275 if (file != NULL && !error)
13276 {
13277 ga_init2(&ga, (int)sizeof(char_u *), 10);
13278 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13279 if (rettv->v_type == VAR_STRING)
13280 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13281 else if (rettv_list_alloc(rettv) != FAIL)
13282 for (i = 0; i < ga.ga_len; ++i)
13283 list_append_string(rettv->vval.v_list,
13284 ((char_u **)(ga.ga_data))[i], -1);
13285 ga_clear_strings(&ga);
13286 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013288 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289}
13290
13291/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013292 * "glob2regpat()" function
13293 */
13294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013295f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013296{
13297 char_u *pat = get_tv_string_chk(&argvars[0]);
13298
13299 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013300 rettv->vval.v_string = (pat == NULL)
13301 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013302}
13303
13304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305 * "has()" function
13306 */
13307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013308f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309{
13310 int i;
13311 char_u *name;
13312 int n = FALSE;
13313 static char *(has_list[]) =
13314 {
13315#ifdef AMIGA
13316 "amiga",
13317# ifdef FEAT_ARP
13318 "arp",
13319# endif
13320#endif
13321#ifdef __BEOS__
13322 "beos",
13323#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013324#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 "mac",
13326#endif
13327#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013328 "macunix", /* built with 'darwin' enabled */
13329#endif
13330#if defined(__APPLE__) && __APPLE__ == 1
13331 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333#ifdef __QNX__
13334 "qnx",
13335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336#ifdef UNIX
13337 "unix",
13338#endif
13339#ifdef VMS
13340 "vms",
13341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342#ifdef WIN32
13343 "win32",
13344#endif
13345#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13346 "win32unix",
13347#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013348#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 "win64",
13350#endif
13351#ifdef EBCDIC
13352 "ebcdic",
13353#endif
13354#ifndef CASE_INSENSITIVE_FILENAME
13355 "fname_case",
13356#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013357#ifdef HAVE_ACL
13358 "acl",
13359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360#ifdef FEAT_ARABIC
13361 "arabic",
13362#endif
13363#ifdef FEAT_AUTOCMD
13364 "autocmd",
13365#endif
13366#ifdef FEAT_BEVAL
13367 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013368# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13369 "balloon_multiline",
13370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371#endif
13372#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13373 "builtin_terms",
13374# ifdef ALL_BUILTIN_TCAPS
13375 "all_builtin_terms",
13376# endif
13377#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013378#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13379 || defined(FEAT_GUI_W32) \
13380 || defined(FEAT_GUI_MOTIF))
13381 "browsefilter",
13382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383#ifdef FEAT_BYTEOFF
13384 "byte_offset",
13385#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013386#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013387 "channel",
13388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389#ifdef FEAT_CINDENT
13390 "cindent",
13391#endif
13392#ifdef FEAT_CLIENTSERVER
13393 "clientserver",
13394#endif
13395#ifdef FEAT_CLIPBOARD
13396 "clipboard",
13397#endif
13398#ifdef FEAT_CMDL_COMPL
13399 "cmdline_compl",
13400#endif
13401#ifdef FEAT_CMDHIST
13402 "cmdline_hist",
13403#endif
13404#ifdef FEAT_COMMENTS
13405 "comments",
13406#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013407#ifdef FEAT_CONCEAL
13408 "conceal",
13409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410#ifdef FEAT_CRYPT
13411 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013412 "crypt-blowfish",
13413 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414#endif
13415#ifdef FEAT_CSCOPE
13416 "cscope",
13417#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013418#ifdef FEAT_CURSORBIND
13419 "cursorbind",
13420#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013421#ifdef CURSOR_SHAPE
13422 "cursorshape",
13423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424#ifdef DEBUG
13425 "debug",
13426#endif
13427#ifdef FEAT_CON_DIALOG
13428 "dialog_con",
13429#endif
13430#ifdef FEAT_GUI_DIALOG
13431 "dialog_gui",
13432#endif
13433#ifdef FEAT_DIFF
13434 "diff",
13435#endif
13436#ifdef FEAT_DIGRAPHS
13437 "digraphs",
13438#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013439#ifdef FEAT_DIRECTX
13440 "directx",
13441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442#ifdef FEAT_DND
13443 "dnd",
13444#endif
13445#ifdef FEAT_EMACS_TAGS
13446 "emacs_tags",
13447#endif
13448 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013449 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450#ifdef FEAT_SEARCH_EXTRA
13451 "extra_search",
13452#endif
13453#ifdef FEAT_FKMAP
13454 "farsi",
13455#endif
13456#ifdef FEAT_SEARCHPATH
13457 "file_in_path",
13458#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013459#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013460 "filterpipe",
13461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462#ifdef FEAT_FIND_ID
13463 "find_in_path",
13464#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013465#ifdef FEAT_FLOAT
13466 "float",
13467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468#ifdef FEAT_FOLDING
13469 "folding",
13470#endif
13471#ifdef FEAT_FOOTER
13472 "footer",
13473#endif
13474#if !defined(USE_SYSTEM) && defined(UNIX)
13475 "fork",
13476#endif
13477#ifdef FEAT_GETTEXT
13478 "gettext",
13479#endif
13480#ifdef FEAT_GUI
13481 "gui",
13482#endif
13483#ifdef FEAT_GUI_ATHENA
13484# ifdef FEAT_GUI_NEXTAW
13485 "gui_neXtaw",
13486# else
13487 "gui_athena",
13488# endif
13489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490#ifdef FEAT_GUI_GTK
13491 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013492# ifdef USE_GTK3
13493 "gui_gtk3",
13494# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013498#ifdef FEAT_GUI_GNOME
13499 "gui_gnome",
13500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501#ifdef FEAT_GUI_MAC
13502 "gui_mac",
13503#endif
13504#ifdef FEAT_GUI_MOTIF
13505 "gui_motif",
13506#endif
13507#ifdef FEAT_GUI_PHOTON
13508 "gui_photon",
13509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510#ifdef FEAT_GUI_W32
13511 "gui_win32",
13512#endif
13513#ifdef FEAT_HANGULIN
13514 "hangul_input",
13515#endif
13516#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13517 "iconv",
13518#endif
13519#ifdef FEAT_INS_EXPAND
13520 "insert_expand",
13521#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013522#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013523 "job",
13524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525#ifdef FEAT_JUMPLIST
13526 "jumplist",
13527#endif
13528#ifdef FEAT_KEYMAP
13529 "keymap",
13530#endif
13531#ifdef FEAT_LANGMAP
13532 "langmap",
13533#endif
13534#ifdef FEAT_LIBCALL
13535 "libcall",
13536#endif
13537#ifdef FEAT_LINEBREAK
13538 "linebreak",
13539#endif
13540#ifdef FEAT_LISP
13541 "lispindent",
13542#endif
13543#ifdef FEAT_LISTCMDS
13544 "listcmds",
13545#endif
13546#ifdef FEAT_LOCALMAP
13547 "localmap",
13548#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013549#ifdef FEAT_LUA
13550# ifndef DYNAMIC_LUA
13551 "lua",
13552# endif
13553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554#ifdef FEAT_MENU
13555 "menu",
13556#endif
13557#ifdef FEAT_SESSION
13558 "mksession",
13559#endif
13560#ifdef FEAT_MODIFY_FNAME
13561 "modify_fname",
13562#endif
13563#ifdef FEAT_MOUSE
13564 "mouse",
13565#endif
13566#ifdef FEAT_MOUSESHAPE
13567 "mouseshape",
13568#endif
13569#if defined(UNIX) || defined(VMS)
13570# ifdef FEAT_MOUSE_DEC
13571 "mouse_dec",
13572# endif
13573# ifdef FEAT_MOUSE_GPM
13574 "mouse_gpm",
13575# endif
13576# ifdef FEAT_MOUSE_JSB
13577 "mouse_jsbterm",
13578# endif
13579# ifdef FEAT_MOUSE_NET
13580 "mouse_netterm",
13581# endif
13582# ifdef FEAT_MOUSE_PTERM
13583 "mouse_pterm",
13584# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013585# ifdef FEAT_MOUSE_SGR
13586 "mouse_sgr",
13587# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013588# ifdef FEAT_SYSMOUSE
13589 "mouse_sysmouse",
13590# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013591# ifdef FEAT_MOUSE_URXVT
13592 "mouse_urxvt",
13593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594# ifdef FEAT_MOUSE_XTERM
13595 "mouse_xterm",
13596# endif
13597#endif
13598#ifdef FEAT_MBYTE
13599 "multi_byte",
13600#endif
13601#ifdef FEAT_MBYTE_IME
13602 "multi_byte_ime",
13603#endif
13604#ifdef FEAT_MULTI_LANG
13605 "multi_lang",
13606#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013607#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013608#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013609 "mzscheme",
13610#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612#ifdef FEAT_OLE
13613 "ole",
13614#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013615 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616#ifdef FEAT_PATH_EXTRA
13617 "path_extra",
13618#endif
13619#ifdef FEAT_PERL
13620#ifndef DYNAMIC_PERL
13621 "perl",
13622#endif
13623#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013624#ifdef FEAT_PERSISTENT_UNDO
13625 "persistent_undo",
13626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627#ifdef FEAT_PYTHON
13628#ifndef DYNAMIC_PYTHON
13629 "python",
13630#endif
13631#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013632#ifdef FEAT_PYTHON3
13633#ifndef DYNAMIC_PYTHON3
13634 "python3",
13635#endif
13636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637#ifdef FEAT_POSTSCRIPT
13638 "postscript",
13639#endif
13640#ifdef FEAT_PRINTER
13641 "printer",
13642#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013643#ifdef FEAT_PROFILE
13644 "profile",
13645#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013646#ifdef FEAT_RELTIME
13647 "reltime",
13648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649#ifdef FEAT_QUICKFIX
13650 "quickfix",
13651#endif
13652#ifdef FEAT_RIGHTLEFT
13653 "rightleft",
13654#endif
13655#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13656 "ruby",
13657#endif
13658#ifdef FEAT_SCROLLBIND
13659 "scrollbind",
13660#endif
13661#ifdef FEAT_CMDL_INFO
13662 "showcmd",
13663 "cmdline_info",
13664#endif
13665#ifdef FEAT_SIGNS
13666 "signs",
13667#endif
13668#ifdef FEAT_SMARTINDENT
13669 "smartindent",
13670#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013671#ifdef STARTUPTIME
13672 "startuptime",
13673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674#ifdef FEAT_STL_OPT
13675 "statusline",
13676#endif
13677#ifdef FEAT_SUN_WORKSHOP
13678 "sun_workshop",
13679#endif
13680#ifdef FEAT_NETBEANS_INTG
13681 "netbeans_intg",
13682#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013683#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013684 "spell",
13685#endif
13686#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 "syntax",
13688#endif
13689#if defined(USE_SYSTEM) || !defined(UNIX)
13690 "system",
13691#endif
13692#ifdef FEAT_TAG_BINS
13693 "tag_binary",
13694#endif
13695#ifdef FEAT_TAG_OLDSTATIC
13696 "tag_old_static",
13697#endif
13698#ifdef FEAT_TAG_ANYWHITE
13699 "tag_any_white",
13700#endif
13701#ifdef FEAT_TCL
13702# ifndef DYNAMIC_TCL
13703 "tcl",
13704# endif
13705#endif
13706#ifdef TERMINFO
13707 "terminfo",
13708#endif
13709#ifdef FEAT_TERMRESPONSE
13710 "termresponse",
13711#endif
13712#ifdef FEAT_TEXTOBJ
13713 "textobjects",
13714#endif
13715#ifdef HAVE_TGETENT
13716 "tgetent",
13717#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013718#ifdef FEAT_TIMERS
13719 "timers",
13720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721#ifdef FEAT_TITLE
13722 "title",
13723#endif
13724#ifdef FEAT_TOOLBAR
13725 "toolbar",
13726#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013727#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13728 "unnamedplus",
13729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730#ifdef FEAT_USR_CMDS
13731 "user-commands", /* was accidentally included in 5.4 */
13732 "user_commands",
13733#endif
13734#ifdef FEAT_VIMINFO
13735 "viminfo",
13736#endif
13737#ifdef FEAT_VERTSPLIT
13738 "vertsplit",
13739#endif
13740#ifdef FEAT_VIRTUALEDIT
13741 "virtualedit",
13742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744#ifdef FEAT_VISUALEXTRA
13745 "visualextra",
13746#endif
13747#ifdef FEAT_VREPLACE
13748 "vreplace",
13749#endif
13750#ifdef FEAT_WILDIGN
13751 "wildignore",
13752#endif
13753#ifdef FEAT_WILDMENU
13754 "wildmenu",
13755#endif
13756#ifdef FEAT_WINDOWS
13757 "windows",
13758#endif
13759#ifdef FEAT_WAK
13760 "winaltkeys",
13761#endif
13762#ifdef FEAT_WRITEBACKUP
13763 "writebackup",
13764#endif
13765#ifdef FEAT_XIM
13766 "xim",
13767#endif
13768#ifdef FEAT_XFONTSET
13769 "xfontset",
13770#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013771#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013772 "xpm",
13773 "xpm_w32", /* for backward compatibility */
13774#else
13775# if defined(HAVE_XPM)
13776 "xpm",
13777# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779#ifdef USE_XSMP
13780 "xsmp",
13781#endif
13782#ifdef USE_XSMP_INTERACT
13783 "xsmp_interact",
13784#endif
13785#ifdef FEAT_XCLIPBOARD
13786 "xterm_clipboard",
13787#endif
13788#ifdef FEAT_XTERM_SAVE
13789 "xterm_save",
13790#endif
13791#if defined(UNIX) && defined(FEAT_X11)
13792 "X11",
13793#endif
13794 NULL
13795 };
13796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798 for (i = 0; has_list[i] != NULL; ++i)
13799 if (STRICMP(name, has_list[i]) == 0)
13800 {
13801 n = TRUE;
13802 break;
13803 }
13804
13805 if (n == FALSE)
13806 {
13807 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013808 {
13809 if (name[5] == '-'
13810 && STRLEN(name) > 11
13811 && vim_isdigit(name[6])
13812 && vim_isdigit(name[8])
13813 && vim_isdigit(name[10]))
13814 {
13815 int major = atoi((char *)name + 6);
13816 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013817
13818 /* Expect "patch-9.9.01234". */
13819 n = (major < VIM_VERSION_MAJOR
13820 || (major == VIM_VERSION_MAJOR
13821 && (minor < VIM_VERSION_MINOR
13822 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013823 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013824 }
13825 else
13826 n = has_patch(atoi((char *)name + 5));
13827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 else if (STRICMP(name, "vim_starting") == 0)
13829 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013830#ifdef FEAT_MBYTE
13831 else if (STRICMP(name, "multi_byte_encoding") == 0)
13832 n = has_mbyte;
13833#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013834#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13835 else if (STRICMP(name, "balloon_multiline") == 0)
13836 n = multiline_balloon_available();
13837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838#ifdef DYNAMIC_TCL
13839 else if (STRICMP(name, "tcl") == 0)
13840 n = tcl_enabled(FALSE);
13841#endif
13842#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13843 else if (STRICMP(name, "iconv") == 0)
13844 n = iconv_enabled(FALSE);
13845#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013846#ifdef DYNAMIC_LUA
13847 else if (STRICMP(name, "lua") == 0)
13848 n = lua_enabled(FALSE);
13849#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013850#ifdef DYNAMIC_MZSCHEME
13851 else if (STRICMP(name, "mzscheme") == 0)
13852 n = mzscheme_enabled(FALSE);
13853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854#ifdef DYNAMIC_RUBY
13855 else if (STRICMP(name, "ruby") == 0)
13856 n = ruby_enabled(FALSE);
13857#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013858#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859#ifdef DYNAMIC_PYTHON
13860 else if (STRICMP(name, "python") == 0)
13861 n = python_enabled(FALSE);
13862#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013863#endif
13864#ifdef FEAT_PYTHON3
13865#ifdef DYNAMIC_PYTHON3
13866 else if (STRICMP(name, "python3") == 0)
13867 n = python3_enabled(FALSE);
13868#endif
13869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870#ifdef DYNAMIC_PERL
13871 else if (STRICMP(name, "perl") == 0)
13872 n = perl_enabled(FALSE);
13873#endif
13874#ifdef FEAT_GUI
13875 else if (STRICMP(name, "gui_running") == 0)
13876 n = (gui.in_use || gui.starting);
13877# ifdef FEAT_GUI_W32
13878 else if (STRICMP(name, "gui_win32s") == 0)
13879 n = gui_is_win32s();
13880# endif
13881# ifdef FEAT_BROWSE
13882 else if (STRICMP(name, "browse") == 0)
13883 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13884# endif
13885#endif
13886#ifdef FEAT_SYN_HL
13887 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013888 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889#endif
13890#if defined(WIN3264)
13891 else if (STRICMP(name, "win95") == 0)
13892 n = mch_windows95();
13893#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013894#ifdef FEAT_NETBEANS_INTG
13895 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013896 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 }
13899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013900 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901}
13902
13903/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013904 * "has_key()" function
13905 */
13906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013907f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013908{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013909 if (argvars[0].v_type != VAR_DICT)
13910 {
13911 EMSG(_(e_dictreq));
13912 return;
13913 }
13914 if (argvars[0].vval.v_dict == NULL)
13915 return;
13916
13917 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013918 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013919}
13920
13921/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013922 * "haslocaldir()" function
13923 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013925f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013926{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013927 win_T *wp = NULL;
13928
13929 wp = find_tabwin(&argvars[0], &argvars[1]);
13930 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013931}
13932
13933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 * "hasmapto()" function
13935 */
13936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013937f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938{
13939 char_u *name;
13940 char_u *mode;
13941 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013942 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013945 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 mode = (char_u *)"nvo";
13947 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013949 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013950 if (argvars[2].v_type != VAR_UNKNOWN)
13951 abbr = get_tv_number(&argvars[2]);
13952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953
Bram Moolenaar2c932302006-03-18 21:42:09 +000013954 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013957 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958}
13959
13960/*
13961 * "histadd()" function
13962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013964f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965{
13966#ifdef FEAT_CMDHIST
13967 int histype;
13968 char_u *str;
13969 char_u buf[NUMBUFLEN];
13970#endif
13971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013972 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 if (check_restricted() || check_secure())
13974 return;
13975#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013976 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13977 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 if (histype >= 0)
13979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013980 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 if (*str != NUL)
13982 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013983 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 return;
13987 }
13988 }
13989#endif
13990}
13991
13992/*
13993 * "histdel()" function
13994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013996f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997{
13998#ifdef FEAT_CMDHIST
13999 int n;
14000 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014001 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014003 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14004 if (str == NULL)
14005 n = 0;
14006 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014008 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014009 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014011 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014012 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 else
14014 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014015 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014016 get_tv_string_buf(&argvars[1], buf));
14017 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018#endif
14019}
14020
14021/*
14022 * "histget()" function
14023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014025f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026{
14027#ifdef FEAT_CMDHIST
14028 int type;
14029 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14033 if (str == NULL)
14034 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014036 {
14037 type = get_histtype(str);
14038 if (argvars[1].v_type == VAR_UNKNOWN)
14039 idx = get_history_idx(type);
14040 else
14041 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14042 /* -1 on type error */
14043 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014046 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014048 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049}
14050
14051/*
14052 * "histnr()" function
14053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014055f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056{
14057 int i;
14058
14059#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014060 char_u *history = get_tv_string_chk(&argvars[0]);
14061
14062 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063 if (i >= HIST_CMD && i < HIST_COUNT)
14064 i = get_history_idx(i);
14065 else
14066#endif
14067 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014068 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069}
14070
14071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 * "highlightID(name)" function
14073 */
14074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014075f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014077 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078}
14079
14080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014081 * "highlight_exists()" function
14082 */
14083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014084f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014085{
14086 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14087}
14088
14089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 * "hostname()" function
14091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014093f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094{
14095 char_u hostname[256];
14096
14097 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014098 rettv->v_type = VAR_STRING;
14099 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100}
14101
14102/*
14103 * iconv() function
14104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014106f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107{
14108#ifdef FEAT_MBYTE
14109 char_u buf1[NUMBUFLEN];
14110 char_u buf2[NUMBUFLEN];
14111 char_u *from, *to, *str;
14112 vimconv_T vimconv;
14113#endif
14114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014115 rettv->v_type = VAR_STRING;
14116 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117
14118#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014119 str = get_tv_string(&argvars[0]);
14120 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14121 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 vimconv.vc_type = CONV_NONE;
14123 convert_setup(&vimconv, from, to);
14124
14125 /* If the encodings are equal, no conversion needed. */
14126 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014127 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130
14131 convert_setup(&vimconv, NULL, NULL);
14132 vim_free(from);
14133 vim_free(to);
14134#endif
14135}
14136
14137/*
14138 * "indent()" function
14139 */
14140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014141f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142{
14143 linenr_T lnum;
14144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014145 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014147 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150}
14151
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014152/*
14153 * "index()" function
14154 */
14155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014156f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014157{
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 list_T *l;
14159 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014160 long idx = 0;
14161 int ic = FALSE;
14162
14163 rettv->vval.v_number = -1;
14164 if (argvars[0].v_type != VAR_LIST)
14165 {
14166 EMSG(_(e_listreq));
14167 return;
14168 }
14169 l = argvars[0].vval.v_list;
14170 if (l != NULL)
14171 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014172 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014173 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 int error = FALSE;
14176
Bram Moolenaar758711c2005-02-02 23:11:38 +000014177 /* Start at specified item. Use the cached index that list_find()
14178 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014180 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014181 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 ic = get_tv_number_chk(&argvars[3], &error);
14183 if (error)
14184 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014185 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014186
Bram Moolenaar758711c2005-02-02 23:11:38 +000014187 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014188 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014189 {
14190 rettv->vval.v_number = idx;
14191 break;
14192 }
14193 }
14194}
14195
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196static int inputsecret_flag = 0;
14197
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014198static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014199
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014201 * This function is used by f_input() and f_inputdialog() functions. The third
14202 * argument to f_input() specifies the type of completion to use at the
14203 * prompt. The third argument to f_inputdialog() specifies the value to return
14204 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 */
14206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014207get_user_input(
14208 typval_T *argvars,
14209 typval_T *rettv,
14210 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 char_u *p = NULL;
14214 int c;
14215 char_u buf[NUMBUFLEN];
14216 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014217 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014218 int xp_type = EXPAND_NOTHING;
14219 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014221 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014222 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223
14224#ifdef NO_CONSOLE_INPUT
14225 /* While starting up, there is no place to enter text. */
14226 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228#endif
14229
14230 cmd_silent = FALSE; /* Want to see the prompt. */
14231 if (prompt != NULL)
14232 {
14233 /* Only the part of the message after the last NL is considered as
14234 * prompt for the command line */
14235 p = vim_strrchr(prompt, '\n');
14236 if (p == NULL)
14237 p = prompt;
14238 else
14239 {
14240 ++p;
14241 c = *p;
14242 *p = NUL;
14243 msg_start();
14244 msg_clr_eos();
14245 msg_puts_attr(prompt, echo_attr);
14246 msg_didout = FALSE;
14247 msg_starthere();
14248 *p = c;
14249 }
14250 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 if (argvars[1].v_type != VAR_UNKNOWN)
14253 {
14254 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14255 if (defstr != NULL)
14256 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014258 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014259 {
14260 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014261 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014262 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014263
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014264 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014265 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014266
Bram Moolenaar4463f292005-09-25 22:20:24 +000014267 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14268 if (xp_name == NULL)
14269 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014270
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014271 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014272
Bram Moolenaar4463f292005-09-25 22:20:24 +000014273 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14274 &xp_arg) == FAIL)
14275 return;
14276 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014277 }
14278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014279 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014280 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014281 int save_ex_normal_busy = ex_normal_busy;
14282 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014283 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014284 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14285 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014286 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014287 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014288 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014289 && argvars[1].v_type != VAR_UNKNOWN
14290 && argvars[2].v_type != VAR_UNKNOWN)
14291 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14292 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014293
14294 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 /* since the user typed this, no need to wait for return */
14297 need_wait_return = FALSE;
14298 msg_didout = FALSE;
14299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 cmd_silent = cmd_silent_save;
14301}
14302
14303/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014304 * "input()" function
14305 * Also handles inputsecret() when inputsecret is set.
14306 */
14307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014308f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014309{
14310 get_user_input(argvars, rettv, FALSE);
14311}
14312
14313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 * "inputdialog()" function
14315 */
14316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014317f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318{
14319#if defined(FEAT_GUI_TEXTDIALOG)
14320 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14321 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14322 {
14323 char_u *message;
14324 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014325 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014327 message = get_tv_string_chk(&argvars[0]);
14328 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014329 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014330 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 else
14332 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014333 if (message != NULL && defstr != NULL
14334 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014335 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014336 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 else
14338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014339 if (message != NULL && defstr != NULL
14340 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014341 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014342 rettv->vval.v_string = vim_strsave(
14343 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014347 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 }
14349 else
14350#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014351 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352}
14353
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014354/*
14355 * "inputlist()" function
14356 */
14357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014358f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014359{
14360 listitem_T *li;
14361 int selected;
14362 int mouse_used;
14363
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014364#ifdef NO_CONSOLE_INPUT
14365 /* While starting up, there is no place to enter text. */
14366 if (no_console_input())
14367 return;
14368#endif
14369 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14370 {
14371 EMSG2(_(e_listarg), "inputlist()");
14372 return;
14373 }
14374
14375 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014376 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014377 lines_left = Rows; /* avoid more prompt */
14378 msg_scroll = TRUE;
14379 msg_clr_eos();
14380
14381 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14382 {
14383 msg_puts(get_tv_string(&li->li_tv));
14384 msg_putchar('\n');
14385 }
14386
14387 /* Ask for choice. */
14388 selected = prompt_for_number(&mouse_used);
14389 if (mouse_used)
14390 selected -= lines_left;
14391
14392 rettv->vval.v_number = selected;
14393}
14394
14395
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14397
14398/*
14399 * "inputrestore()" function
14400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014402f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403{
14404 if (ga_userinput.ga_len > 0)
14405 {
14406 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14408 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014409 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410 }
14411 else if (p_verbose > 1)
14412 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014413 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014414 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 }
14416}
14417
14418/*
14419 * "inputsave()" function
14420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014422f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014424 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 if (ga_grow(&ga_userinput, 1) == OK)
14426 {
14427 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14428 + ga_userinput.ga_len);
14429 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014430 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 }
14432 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434}
14435
14436/*
14437 * "inputsecret()" function
14438 */
14439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014440f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441{
14442 ++cmdline_star;
14443 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014444 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 --cmdline_star;
14446 --inputsecret_flag;
14447}
14448
14449/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014450 * "insert()" function
14451 */
14452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014453f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014454{
14455 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014456 listitem_T *item;
14457 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014458 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014459
14460 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014461 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014462 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014463 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014464 {
14465 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014466 before = get_tv_number_chk(&argvars[2], &error);
14467 if (error)
14468 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014469
Bram Moolenaar758711c2005-02-02 23:11:38 +000014470 if (before == l->lv_len)
14471 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014472 else
14473 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014474 item = list_find(l, before);
14475 if (item == NULL)
14476 {
14477 EMSGN(_(e_listidx), before);
14478 l = NULL;
14479 }
14480 }
14481 if (l != NULL)
14482 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014483 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014484 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014485 }
14486 }
14487}
14488
14489/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014490 * "invert(expr)" function
14491 */
14492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014493f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014494{
14495 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14496}
14497
14498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 * "isdirectory()" function
14500 */
14501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014502f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505}
14506
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014507/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014508 * "islocked()" function
14509 */
14510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014511f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014512{
14513 lval_T lv;
14514 char_u *end;
14515 dictitem_T *di;
14516
14517 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014518 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14519 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014520 if (end != NULL && lv.ll_name != NULL)
14521 {
14522 if (*end != NUL)
14523 EMSG(_(e_trailing));
14524 else
14525 {
14526 if (lv.ll_tv == NULL)
14527 {
14528 if (check_changedtick(lv.ll_name))
14529 rettv->vval.v_number = 1; /* always locked */
14530 else
14531 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014532 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014533 if (di != NULL)
14534 {
14535 /* Consider a variable locked when:
14536 * 1. the variable itself is locked
14537 * 2. the value of the variable is locked.
14538 * 3. the List or Dict value is locked.
14539 */
14540 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14541 || tv_islocked(&di->di_tv));
14542 }
14543 }
14544 }
14545 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014546 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014547 else if (lv.ll_newkey != NULL)
14548 EMSG2(_(e_dictkey), lv.ll_newkey);
14549 else if (lv.ll_list != NULL)
14550 /* List item. */
14551 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14552 else
14553 /* Dictionary item. */
14554 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14555 }
14556 }
14557
14558 clear_lval(&lv);
14559}
14560
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014561#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14562/*
14563 * "isnan()" function
14564 */
14565 static void
14566f_isnan(typval_T *argvars, typval_T *rettv)
14567{
14568 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14569 && isnan(argvars[0].vval.v_float);
14570}
14571#endif
14572
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014573static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014574
14575/*
14576 * Turn a dict into a list:
14577 * "what" == 0: list of keys
14578 * "what" == 1: list of values
14579 * "what" == 2: list of items
14580 */
14581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014582dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014583{
Bram Moolenaar33570922005-01-25 22:26:29 +000014584 list_T *l2;
14585 dictitem_T *di;
14586 hashitem_T *hi;
14587 listitem_T *li;
14588 listitem_T *li2;
14589 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014590 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014591
Bram Moolenaar8c711452005-01-14 21:53:12 +000014592 if (argvars[0].v_type != VAR_DICT)
14593 {
14594 EMSG(_(e_dictreq));
14595 return;
14596 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014597 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014598 return;
14599
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014600 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014601 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014602
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014603 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014604 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014606 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014607 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014608 --todo;
14609 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014610
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014611 li = listitem_alloc();
14612 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014613 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014614 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014615
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014616 if (what == 0)
14617 {
14618 /* keys() */
14619 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014620 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014621 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14622 }
14623 else if (what == 1)
14624 {
14625 /* values() */
14626 copy_tv(&di->di_tv, &li->li_tv);
14627 }
14628 else
14629 {
14630 /* items() */
14631 l2 = list_alloc();
14632 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014633 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014634 li->li_tv.vval.v_list = l2;
14635 if (l2 == NULL)
14636 break;
14637 ++l2->lv_refcount;
14638
14639 li2 = listitem_alloc();
14640 if (li2 == NULL)
14641 break;
14642 list_append(l2, li2);
14643 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014644 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014645 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14646
14647 li2 = listitem_alloc();
14648 if (li2 == NULL)
14649 break;
14650 list_append(l2, li2);
14651 copy_tv(&di->di_tv, &li2->li_tv);
14652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014653 }
14654 }
14655}
14656
14657/*
14658 * "items(dict)" function
14659 */
14660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014661f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014662{
14663 dict_list(argvars, rettv, 2);
14664}
14665
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014666#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014667/*
14668 * Get the job from the argument.
14669 * Returns NULL if the job is invalid.
14670 */
14671 static job_T *
14672get_job_arg(typval_T *tv)
14673{
14674 job_T *job;
14675
14676 if (tv->v_type != VAR_JOB)
14677 {
14678 EMSG2(_(e_invarg2), get_tv_string(tv));
14679 return NULL;
14680 }
14681 job = tv->vval.v_job;
14682
14683 if (job == NULL)
14684 EMSG(_("E916: not a valid job"));
14685 return job;
14686}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014687
Bram Moolenaar835dc632016-02-07 14:27:38 +010014688/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014689 * "job_getchannel()" function
14690 */
14691 static void
14692f_job_getchannel(typval_T *argvars, typval_T *rettv)
14693{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014694 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014695
Bram Moolenaar65edff82016-02-21 16:40:11 +010014696 if (job != NULL)
14697 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014698 rettv->v_type = VAR_CHANNEL;
14699 rettv->vval.v_channel = job->jv_channel;
14700 if (job->jv_channel != NULL)
14701 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014702 }
14703}
14704
14705/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014706 * "job_info()" function
14707 */
14708 static void
14709f_job_info(typval_T *argvars, typval_T *rettv)
14710{
14711 job_T *job = get_job_arg(&argvars[0]);
14712
14713 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14714 job_info(job, rettv->vval.v_dict);
14715}
14716
14717/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014718 * "job_setoptions()" function
14719 */
14720 static void
14721f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14722{
14723 job_T *job = get_job_arg(&argvars[0]);
14724 jobopt_T opt;
14725
14726 if (job == NULL)
14727 return;
14728 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014729 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014730 return;
14731 job_set_options(job, &opt);
14732}
14733
14734/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014735 * "job_start()" function
14736 */
14737 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014738f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014739{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014740 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014741 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014742}
14743
14744/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014745 * "job_status()" function
14746 */
14747 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014748f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014749{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014750 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014751
Bram Moolenaar65edff82016-02-21 16:40:11 +010014752 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014753 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014754 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014755 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014756 }
14757}
14758
14759/*
14760 * "job_stop()" function
14761 */
14762 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014763f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014764{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014765 job_T *job = get_job_arg(&argvars[0]);
14766
14767 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014768 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014769}
14770#endif
14771
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014773 * "join()" function
14774 */
14775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014776f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014777{
14778 garray_T ga;
14779 char_u *sep;
14780
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014781 if (argvars[0].v_type != VAR_LIST)
14782 {
14783 EMSG(_(e_listreq));
14784 return;
14785 }
14786 if (argvars[0].vval.v_list == NULL)
14787 return;
14788 if (argvars[1].v_type == VAR_UNKNOWN)
14789 sep = (char_u *)" ";
14790 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014791 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014792
14793 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794
14795 if (sep != NULL)
14796 {
14797 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014798 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 ga_append(&ga, NUL);
14800 rettv->vval.v_string = (char_u *)ga.ga_data;
14801 }
14802 else
14803 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014804}
14805
14806/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014807 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014808 */
14809 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014810f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014811{
14812 js_read_T reader;
14813
14814 reader.js_buf = get_tv_string(&argvars[0]);
14815 reader.js_fill = NULL;
14816 reader.js_used = 0;
14817 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14818 EMSG(_(e_invarg));
14819}
14820
14821/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014822 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014823 */
14824 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014825f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014826{
14827 rettv->v_type = VAR_STRING;
14828 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14829}
14830
14831/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014832 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014833 */
14834 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014835f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014836{
14837 js_read_T reader;
14838
14839 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014840 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014841 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014842 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014843 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014844}
14845
14846/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014847 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014848 */
14849 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014850f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014851{
14852 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014853 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014854}
14855
14856/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014857 * "keys()" function
14858 */
14859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014860f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014861{
14862 dict_list(argvars, rettv, 0);
14863}
14864
14865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866 * "last_buffer_nr()" function.
14867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014869f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870{
14871 int n = 0;
14872 buf_T *buf;
14873
14874 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14875 if (n < buf->b_fnum)
14876 n = buf->b_fnum;
14877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014878 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014879}
14880
14881/*
14882 * "len()" function
14883 */
14884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014885f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014886{
14887 switch (argvars[0].v_type)
14888 {
14889 case VAR_STRING:
14890 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014891 rettv->vval.v_number = (varnumber_T)STRLEN(
14892 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014893 break;
14894 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014895 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014896 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014897 case VAR_DICT:
14898 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14899 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014900 case VAR_UNKNOWN:
14901 case VAR_SPECIAL:
14902 case VAR_FLOAT:
14903 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014904 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014905 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014906 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014907 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014908 break;
14909 }
14910}
14911
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014912static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014913
14914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014915libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014916{
14917#ifdef FEAT_LIBCALL
14918 char_u *string_in;
14919 char_u **string_result;
14920 int nr_result;
14921#endif
14922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014924 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014925 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014926
14927 if (check_restricted() || check_secure())
14928 return;
14929
14930#ifdef FEAT_LIBCALL
14931 /* The first two args must be strings, otherwise its meaningless */
14932 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14933 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014934 string_in = NULL;
14935 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014936 string_in = argvars[2].vval.v_string;
14937 if (type == VAR_NUMBER)
14938 string_result = NULL;
14939 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014940 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014941 if (mch_libcall(argvars[0].vval.v_string,
14942 argvars[1].vval.v_string,
14943 string_in,
14944 argvars[2].vval.v_number,
14945 string_result,
14946 &nr_result) == OK
14947 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014948 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014949 }
14950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951}
14952
14953/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014954 * "libcall()" function
14955 */
14956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014957f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014958{
14959 libcall_common(argvars, rettv, VAR_STRING);
14960}
14961
14962/*
14963 * "libcallnr()" function
14964 */
14965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014966f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014967{
14968 libcall_common(argvars, rettv, VAR_NUMBER);
14969}
14970
14971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 * "line(string)" function
14973 */
14974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014975f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976{
14977 linenr_T lnum = 0;
14978 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014979 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014981 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982 if (fp != NULL)
14983 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014984 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985}
14986
14987/*
14988 * "line2byte(lnum)" function
14989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014991f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992{
14993#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014994 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995#else
14996 linenr_T lnum;
14997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014998 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015000 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015002 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15003 if (rettv->vval.v_number >= 0)
15004 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005#endif
15006}
15007
15008/*
15009 * "lispindent(lnum)" function
15010 */
15011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015012f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013{
15014#ifdef FEAT_LISP
15015 pos_T pos;
15016 linenr_T lnum;
15017
15018 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15021 {
15022 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015023 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 curwin->w_cursor = pos;
15025 }
15026 else
15027#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029}
15030
15031/*
15032 * "localtime()" function
15033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015035f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015037 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038}
15039
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015040static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041
15042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015043get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044{
15045 char_u *keys;
15046 char_u *which;
15047 char_u buf[NUMBUFLEN];
15048 char_u *keys_buf = NULL;
15049 char_u *rhs;
15050 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015051 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015052 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015053 mapblock_T *mp;
15054 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055
15056 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057 rettv->v_type = VAR_STRING;
15058 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015060 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061 if (*keys == NUL)
15062 return;
15063
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015064 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015066 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015067 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015068 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015069 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015070 if (argvars[3].v_type != VAR_UNKNOWN)
15071 get_dict = get_tv_number(&argvars[3]);
15072 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074 else
15075 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015076 if (which == NULL)
15077 return;
15078
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079 mode = get_map_mode(&which, 0);
15080
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015081 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015082 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015084
15085 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015087 /* Return a string. */
15088 if (rhs != NULL)
15089 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090
Bram Moolenaarbd743252010-10-20 21:23:33 +020015091 }
15092 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15093 {
15094 /* Return a dictionary. */
15095 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15096 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15097 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098
Bram Moolenaarbd743252010-10-20 21:23:33 +020015099 dict_add_nr_str(dict, "lhs", 0L, lhs);
15100 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15101 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15102 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15103 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15104 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15105 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015106 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015107 dict_add_nr_str(dict, "mode", 0L, mapmode);
15108
15109 vim_free(lhs);
15110 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111 }
15112}
15113
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015114#ifdef FEAT_FLOAT
15115/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015116 * "log()" function
15117 */
15118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015119f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015120{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015121 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015122
15123 rettv->v_type = VAR_FLOAT;
15124 if (get_float_arg(argvars, &f) == OK)
15125 rettv->vval.v_float = log(f);
15126 else
15127 rettv->vval.v_float = 0.0;
15128}
15129
15130/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015131 * "log10()" function
15132 */
15133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015134f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015135{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015136 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015137
15138 rettv->v_type = VAR_FLOAT;
15139 if (get_float_arg(argvars, &f) == OK)
15140 rettv->vval.v_float = log10(f);
15141 else
15142 rettv->vval.v_float = 0.0;
15143}
15144#endif
15145
Bram Moolenaar1dced572012-04-05 16:54:08 +020015146#ifdef FEAT_LUA
15147/*
15148 * "luaeval()" function
15149 */
15150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015151f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015152{
15153 char_u *str;
15154 char_u buf[NUMBUFLEN];
15155
15156 str = get_tv_string_buf(&argvars[0], buf);
15157 do_luaeval(str, argvars + 1, rettv);
15158}
15159#endif
15160
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015162 * "map()" function
15163 */
15164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015165f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015166{
15167 filter_map(argvars, rettv, TRUE);
15168}
15169
15170/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172 */
15173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015174f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177}
15178
15179/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015180 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181 */
15182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015183f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015185 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186}
15187
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015188static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189
15190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015191find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015193 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015194 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015195 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196 char_u *pat;
15197 regmatch_T regmatch;
15198 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015199 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200 char_u *save_cpo;
15201 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015202 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015203 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015204 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 list_T *l = NULL;
15206 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015207 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015208 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209
15210 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15211 save_cpo = p_cpo;
15212 p_cpo = (char_u *)"";
15213
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015214 rettv->vval.v_number = -1;
15215 if (type == 3)
15216 {
15217 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015218 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015219 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015220 }
15221 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015223 rettv->v_type = VAR_STRING;
15224 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015227 if (argvars[0].v_type == VAR_LIST)
15228 {
15229 if ((l = argvars[0].vval.v_list) == NULL)
15230 goto theend;
15231 li = l->lv_first;
15232 }
15233 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015234 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015235 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015236 len = (long)STRLEN(str);
15237 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015239 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15240 if (pat == NULL)
15241 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015242
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015243 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015245 int error = FALSE;
15246
15247 start = get_tv_number_chk(&argvars[2], &error);
15248 if (error)
15249 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015250 if (l != NULL)
15251 {
15252 li = list_find(l, start);
15253 if (li == NULL)
15254 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015255 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015256 }
15257 else
15258 {
15259 if (start < 0)
15260 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015261 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015262 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015263 /* When "count" argument is there ignore matches before "start",
15264 * otherwise skip part of the string. Differs when pattern is "^"
15265 * or "\<". */
15266 if (argvars[3].v_type != VAR_UNKNOWN)
15267 startcol = start;
15268 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015269 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015270 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015271 len -= start;
15272 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015273 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015274
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015275 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015276 nth = get_tv_number_chk(&argvars[3], &error);
15277 if (error)
15278 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 }
15280
15281 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15282 if (regmatch.regprog != NULL)
15283 {
15284 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015285
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015286 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015287 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015288 if (l != NULL)
15289 {
15290 if (li == NULL)
15291 {
15292 match = FALSE;
15293 break;
15294 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015295 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015296 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015297 if (str == NULL)
15298 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015299 }
15300
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015301 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015302
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015303 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015304 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015305 if (l == NULL && !match)
15306 break;
15307
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015308 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015309 if (l != NULL)
15310 {
15311 li = li->li_next;
15312 ++idx;
15313 }
15314 else
15315 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015316#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015317 startcol = (colnr_T)(regmatch.startp[0]
15318 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015319#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015320 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015321#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015322 if (startcol > (colnr_T)len
15323 || str + startcol <= regmatch.startp[0])
15324 {
15325 match = FALSE;
15326 break;
15327 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015328 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015329 }
15330
15331 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015333 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015334 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015335 int i;
15336
15337 /* return list with matched string and submatches */
15338 for (i = 0; i < NSUBEXP; ++i)
15339 {
15340 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015341 {
15342 if (list_append_string(rettv->vval.v_list,
15343 (char_u *)"", 0) == FAIL)
15344 break;
15345 }
15346 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015347 regmatch.startp[i],
15348 (int)(regmatch.endp[i] - regmatch.startp[i]))
15349 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015350 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351 }
15352 }
15353 else if (type == 2)
15354 {
15355 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015356 if (l != NULL)
15357 copy_tv(&li->li_tv, rettv);
15358 else
15359 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015361 }
15362 else if (l != NULL)
15363 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 else
15365 {
15366 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015367 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 (varnumber_T)(regmatch.startp[0] - str);
15369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015372 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 }
15374 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015375 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 }
15377
15378theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015379 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 p_cpo = save_cpo;
15381}
15382
15383/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015384 * "match()" function
15385 */
15386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015387f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015388{
15389 find_some_match(argvars, rettv, 1);
15390}
15391
15392/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015393 * "matchadd()" function
15394 */
15395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015396f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015397{
15398#ifdef FEAT_SEARCH_EXTRA
15399 char_u buf[NUMBUFLEN];
15400 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15401 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15402 int prio = 10; /* default priority */
15403 int id = -1;
15404 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015405 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015406
15407 rettv->vval.v_number = -1;
15408
15409 if (grp == NULL || pat == NULL)
15410 return;
15411 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015412 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015413 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015414 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015415 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015416 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015417 if (argvars[4].v_type != VAR_UNKNOWN)
15418 {
15419 if (argvars[4].v_type != VAR_DICT)
15420 {
15421 EMSG(_(e_dictreq));
15422 return;
15423 }
15424 if (dict_find(argvars[4].vval.v_dict,
15425 (char_u *)"conceal", -1) != NULL)
15426 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15427 (char_u *)"conceal", FALSE);
15428 }
15429 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015430 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015431 if (error == TRUE)
15432 return;
15433 if (id >= 1 && id <= 3)
15434 {
15435 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15436 return;
15437 }
15438
Bram Moolenaar6561d522015-07-21 15:48:27 +020015439 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15440 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015441#endif
15442}
15443
15444/*
15445 * "matchaddpos()" function
15446 */
15447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015448f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015449{
15450#ifdef FEAT_SEARCH_EXTRA
15451 char_u buf[NUMBUFLEN];
15452 char_u *group;
15453 int prio = 10;
15454 int id = -1;
15455 int error = FALSE;
15456 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015457 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015458
15459 rettv->vval.v_number = -1;
15460
15461 group = get_tv_string_buf_chk(&argvars[0], buf);
15462 if (group == NULL)
15463 return;
15464
15465 if (argvars[1].v_type != VAR_LIST)
15466 {
15467 EMSG2(_(e_listarg), "matchaddpos()");
15468 return;
15469 }
15470 l = argvars[1].vval.v_list;
15471 if (l == NULL)
15472 return;
15473
15474 if (argvars[2].v_type != VAR_UNKNOWN)
15475 {
15476 prio = get_tv_number_chk(&argvars[2], &error);
15477 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015478 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015479 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015480 if (argvars[4].v_type != VAR_UNKNOWN)
15481 {
15482 if (argvars[4].v_type != VAR_DICT)
15483 {
15484 EMSG(_(e_dictreq));
15485 return;
15486 }
15487 if (dict_find(argvars[4].vval.v_dict,
15488 (char_u *)"conceal", -1) != NULL)
15489 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15490 (char_u *)"conceal", FALSE);
15491 }
15492 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015493 }
15494 if (error == TRUE)
15495 return;
15496
15497 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15498 if (id == 1 || id == 2)
15499 {
15500 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15501 return;
15502 }
15503
Bram Moolenaar6561d522015-07-21 15:48:27 +020015504 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15505 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015506#endif
15507}
15508
15509/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015510 * "matcharg()" function
15511 */
15512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015513f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015514{
15515 if (rettv_list_alloc(rettv) == OK)
15516 {
15517#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015518 int id = get_tv_number(&argvars[0]);
15519 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015520
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015521 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015522 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015523 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15524 {
15525 list_append_string(rettv->vval.v_list,
15526 syn_id2name(m->hlg_id), -1);
15527 list_append_string(rettv->vval.v_list, m->pattern, -1);
15528 }
15529 else
15530 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015531 list_append_string(rettv->vval.v_list, NULL, -1);
15532 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015533 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015534 }
15535#endif
15536 }
15537}
15538
15539/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015540 * "matchdelete()" function
15541 */
15542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015543f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015544{
15545#ifdef FEAT_SEARCH_EXTRA
15546 rettv->vval.v_number = match_delete(curwin,
15547 (int)get_tv_number(&argvars[0]), TRUE);
15548#endif
15549}
15550
15551/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015552 * "matchend()" function
15553 */
15554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015555f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015556{
15557 find_some_match(argvars, rettv, 0);
15558}
15559
15560/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015561 * "matchlist()" function
15562 */
15563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015564f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015565{
15566 find_some_match(argvars, rettv, 3);
15567}
15568
15569/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015570 * "matchstr()" function
15571 */
15572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015573f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015574{
15575 find_some_match(argvars, rettv, 2);
15576}
15577
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015578static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015579
15580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015581max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015582{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015583 long n = 0;
15584 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015585 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015586
15587 if (argvars[0].v_type == VAR_LIST)
15588 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015589 list_T *l;
15590 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015591
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015592 l = argvars[0].vval.v_list;
15593 if (l != NULL)
15594 {
15595 li = l->lv_first;
15596 if (li != NULL)
15597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015598 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015599 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015600 {
15601 li = li->li_next;
15602 if (li == NULL)
15603 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015604 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015605 if (domax ? i > n : i < n)
15606 n = i;
15607 }
15608 }
15609 }
15610 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015611 else if (argvars[0].v_type == VAR_DICT)
15612 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015613 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015614 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015615 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015616 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015617
15618 d = argvars[0].vval.v_dict;
15619 if (d != NULL)
15620 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015621 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015622 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015624 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015625 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015626 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015627 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015628 if (first)
15629 {
15630 n = i;
15631 first = FALSE;
15632 }
15633 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015634 n = i;
15635 }
15636 }
15637 }
15638 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015639 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015640 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015641 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015642}
15643
15644/*
15645 * "max()" function
15646 */
15647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015648f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015649{
15650 max_min(argvars, rettv, TRUE);
15651}
15652
15653/*
15654 * "min()" function
15655 */
15656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015657f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015658{
15659 max_min(argvars, rettv, FALSE);
15660}
15661
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015662static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015663
15664/*
15665 * Create the directory in which "dir" is located, and higher levels when
15666 * needed.
15667 */
15668 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015669mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015670{
15671 char_u *p;
15672 char_u *updir;
15673 int r = FAIL;
15674
15675 /* Get end of directory name in "dir".
15676 * We're done when it's "/" or "c:/". */
15677 p = gettail_sep(dir);
15678 if (p <= get_past_head(dir))
15679 return OK;
15680
15681 /* If the directory exists we're done. Otherwise: create it.*/
15682 updir = vim_strnsave(dir, (int)(p - dir));
15683 if (updir == NULL)
15684 return FAIL;
15685 if (mch_isdir(updir))
15686 r = OK;
15687 else if (mkdir_recurse(updir, prot) == OK)
15688 r = vim_mkdir_emsg(updir, prot);
15689 vim_free(updir);
15690 return r;
15691}
15692
15693#ifdef vim_mkdir
15694/*
15695 * "mkdir()" function
15696 */
15697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015698f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015699{
15700 char_u *dir;
15701 char_u buf[NUMBUFLEN];
15702 int prot = 0755;
15703
15704 rettv->vval.v_number = FAIL;
15705 if (check_restricted() || check_secure())
15706 return;
15707
15708 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015709 if (*dir == NUL)
15710 rettv->vval.v_number = FAIL;
15711 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015712 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015713 if (*gettail(dir) == NUL)
15714 /* remove trailing slashes */
15715 *gettail_sep(dir) = NUL;
15716
15717 if (argvars[1].v_type != VAR_UNKNOWN)
15718 {
15719 if (argvars[2].v_type != VAR_UNKNOWN)
15720 prot = get_tv_number_chk(&argvars[2], NULL);
15721 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15722 mkdir_recurse(dir, prot);
15723 }
15724 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015725 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015726}
15727#endif
15728
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 * "mode()" function
15731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015733f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015735 char_u buf[3];
15736
15737 buf[1] = NUL;
15738 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 if (VIsual_active)
15741 {
15742 if (VIsual_select)
15743 buf[0] = VIsual_mode + 's' - 'v';
15744 else
15745 buf[0] = VIsual_mode;
15746 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015747 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015748 || State == CONFIRM)
15749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015751 if (State == ASKMORE)
15752 buf[1] = 'm';
15753 else if (State == CONFIRM)
15754 buf[1] = '?';
15755 }
15756 else if (State == EXTERNCMD)
15757 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 else if (State & INSERT)
15759 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015760#ifdef FEAT_VREPLACE
15761 if (State & VREPLACE_FLAG)
15762 {
15763 buf[0] = 'R';
15764 buf[1] = 'v';
15765 }
15766 else
15767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 if (State & REPLACE_FLAG)
15769 buf[0] = 'R';
15770 else
15771 buf[0] = 'i';
15772 }
15773 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015776 if (exmode_active)
15777 buf[1] = 'v';
15778 }
15779 else if (exmode_active)
15780 {
15781 buf[0] = 'c';
15782 buf[1] = 'e';
15783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015785 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015787 if (finish_op)
15788 buf[1] = 'o';
15789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015791 /* Clear out the minor mode when the argument is not a non-zero number or
15792 * non-empty string. */
15793 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015794 buf[1] = NUL;
15795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015796 rettv->vval.v_string = vim_strsave(buf);
15797 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798}
15799
Bram Moolenaar429fa852013-04-15 12:27:36 +020015800#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015801/*
15802 * "mzeval()" function
15803 */
15804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015805f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015806{
15807 char_u *str;
15808 char_u buf[NUMBUFLEN];
15809
15810 str = get_tv_string_buf(&argvars[0], buf);
15811 do_mzeval(str, rettv);
15812}
Bram Moolenaar75676462013-01-30 14:55:42 +010015813
15814 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015815mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015816{
15817 typval_T argvars[3];
15818
15819 argvars[0].v_type = VAR_STRING;
15820 argvars[0].vval.v_string = name;
15821 copy_tv(args, &argvars[1]);
15822 argvars[2].v_type = VAR_UNKNOWN;
15823 f_call(argvars, rettv);
15824 clear_tv(&argvars[1]);
15825}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015826#endif
15827
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015829 * "nextnonblank()" function
15830 */
15831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015832f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015833{
15834 linenr_T lnum;
15835
15836 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015838 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015839 {
15840 lnum = 0;
15841 break;
15842 }
15843 if (*skipwhite(ml_get(lnum)) != NUL)
15844 break;
15845 }
15846 rettv->vval.v_number = lnum;
15847}
15848
15849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 * "nr2char()" function
15851 */
15852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015853f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854{
15855 char_u buf[NUMBUFLEN];
15856
15857#ifdef FEAT_MBYTE
15858 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015859 {
15860 int utf8 = 0;
15861
15862 if (argvars[1].v_type != VAR_UNKNOWN)
15863 utf8 = get_tv_number_chk(&argvars[1], NULL);
15864 if (utf8)
15865 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15866 else
15867 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 else
15870#endif
15871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015872 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873 buf[1] = NUL;
15874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015875 rettv->v_type = VAR_STRING;
15876 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015877}
15878
15879/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015880 * "or(expr, expr)" function
15881 */
15882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015883f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015884{
15885 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15886 | get_tv_number_chk(&argvars[1], NULL);
15887}
15888
15889/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015890 * "pathshorten()" function
15891 */
15892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015893f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015894{
15895 char_u *p;
15896
15897 rettv->v_type = VAR_STRING;
15898 p = get_tv_string_chk(&argvars[0]);
15899 if (p == NULL)
15900 rettv->vval.v_string = NULL;
15901 else
15902 {
15903 p = vim_strsave(p);
15904 rettv->vval.v_string = p;
15905 if (p != NULL)
15906 shorten_dir(p);
15907 }
15908}
15909
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015910#ifdef FEAT_PERL
15911/*
15912 * "perleval()" function
15913 */
15914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015915f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015916{
15917 char_u *str;
15918 char_u buf[NUMBUFLEN];
15919
15920 str = get_tv_string_buf(&argvars[0], buf);
15921 do_perleval(str, rettv);
15922}
15923#endif
15924
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015925#ifdef FEAT_FLOAT
15926/*
15927 * "pow()" function
15928 */
15929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015930f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015931{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015932 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015933
15934 rettv->v_type = VAR_FLOAT;
15935 if (get_float_arg(argvars, &fx) == OK
15936 && get_float_arg(&argvars[1], &fy) == OK)
15937 rettv->vval.v_float = pow(fx, fy);
15938 else
15939 rettv->vval.v_float = 0.0;
15940}
15941#endif
15942
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015943/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015944 * "prevnonblank()" function
15945 */
15946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015947f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015948{
15949 linenr_T lnum;
15950
15951 lnum = get_tv_lnum(argvars);
15952 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15953 lnum = 0;
15954 else
15955 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15956 --lnum;
15957 rettv->vval.v_number = lnum;
15958}
15959
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015960/* This dummy va_list is here because:
15961 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15962 * - locally in the function results in a "used before set" warning
15963 * - using va_start() to initialize it gives "function with fixed args" error */
15964static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015965
Bram Moolenaar8c711452005-01-14 21:53:12 +000015966/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015967 * "printf()" function
15968 */
15969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015970f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015971{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015972 char_u buf[NUMBUFLEN];
15973 int len;
15974 char_u *s;
15975 int saved_did_emsg = did_emsg;
15976 char *fmt;
15977
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015978 rettv->v_type = VAR_STRING;
15979 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015980
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015981 /* Get the required length, allocate the buffer and do it for real. */
15982 did_emsg = FALSE;
15983 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15984 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15985 if (!did_emsg)
15986 {
15987 s = alloc(len + 1);
15988 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015989 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015990 rettv->vval.v_string = s;
15991 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015992 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015993 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015994 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015995}
15996
15997/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015998 * "pumvisible()" function
15999 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016001f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016002{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016003#ifdef FEAT_INS_EXPAND
16004 if (pum_visible())
16005 rettv->vval.v_number = 1;
16006#endif
16007}
16008
Bram Moolenaardb913952012-06-29 12:54:53 +020016009#ifdef FEAT_PYTHON3
16010/*
16011 * "py3eval()" function
16012 */
16013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016014f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016015{
16016 char_u *str;
16017 char_u buf[NUMBUFLEN];
16018
16019 str = get_tv_string_buf(&argvars[0], buf);
16020 do_py3eval(str, rettv);
16021}
16022#endif
16023
16024#ifdef FEAT_PYTHON
16025/*
16026 * "pyeval()" function
16027 */
16028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016029f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016030{
16031 char_u *str;
16032 char_u buf[NUMBUFLEN];
16033
16034 str = get_tv_string_buf(&argvars[0], buf);
16035 do_pyeval(str, rettv);
16036}
16037#endif
16038
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016039/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016040 * "range()" function
16041 */
16042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016043f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016044{
16045 long start;
16046 long end;
16047 long stride = 1;
16048 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016049 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016051 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016052 if (argvars[1].v_type == VAR_UNKNOWN)
16053 {
16054 end = start - 1;
16055 start = 0;
16056 }
16057 else
16058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016059 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016060 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016061 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016062 }
16063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016064 if (error)
16065 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016066 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016067 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016068 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016069 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016070 else
16071 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016072 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016073 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016074 if (list_append_number(rettv->vval.v_list,
16075 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016076 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016077 }
16078}
16079
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016080/*
16081 * "readfile()" function
16082 */
16083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016084f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016085{
16086 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016087 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016088 char_u *fname;
16089 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016090 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16091 int io_size = sizeof(buf);
16092 int readlen; /* size of last fread() */
16093 char_u *prev = NULL; /* previously read bytes, if any */
16094 long prevlen = 0; /* length of data in prev */
16095 long prevsize = 0; /* size of prev buffer */
16096 long maxline = MAXLNUM;
16097 long cnt = 0;
16098 char_u *p; /* position in buf */
16099 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016100
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016101 if (argvars[1].v_type != VAR_UNKNOWN)
16102 {
16103 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16104 binary = TRUE;
16105 if (argvars[2].v_type != VAR_UNKNOWN)
16106 maxline = get_tv_number(&argvars[2]);
16107 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016108
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016109 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016110 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016111
16112 /* Always open the file in binary mode, library functions have a mind of
16113 * their own about CR-LF conversion. */
16114 fname = get_tv_string(&argvars[0]);
16115 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16116 {
16117 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16118 return;
16119 }
16120
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016121 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016122 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016123 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016124
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016125 /* This for loop processes what was read, but is also entered at end
16126 * of file so that either:
16127 * - an incomplete line gets written
16128 * - a "binary" file gets an empty line at the end if it ends in a
16129 * newline. */
16130 for (p = buf, start = buf;
16131 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16132 ++p)
16133 {
16134 if (*p == '\n' || readlen <= 0)
16135 {
16136 listitem_T *li;
16137 char_u *s = NULL;
16138 long_u len = p - start;
16139
16140 /* Finished a line. Remove CRs before NL. */
16141 if (readlen > 0 && !binary)
16142 {
16143 while (len > 0 && start[len - 1] == '\r')
16144 --len;
16145 /* removal may cross back to the "prev" string */
16146 if (len == 0)
16147 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16148 --prevlen;
16149 }
16150 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016151 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016152 else
16153 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016154 /* Change "prev" buffer to be the right size. This way
16155 * the bytes are only copied once, and very long lines are
16156 * allocated only once. */
16157 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016158 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016159 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016160 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016161 prev = NULL; /* the list will own the string */
16162 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016163 }
16164 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016165 if (s == NULL)
16166 {
16167 do_outofmem_msg((long_u) prevlen + len + 1);
16168 failed = TRUE;
16169 break;
16170 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016171
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016172 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016173 {
16174 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016175 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016176 break;
16177 }
16178 li->li_tv.v_type = VAR_STRING;
16179 li->li_tv.v_lock = 0;
16180 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016181 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016182
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016183 start = p + 1; /* step over newline */
16184 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016185 break;
16186 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016187 else if (*p == NUL)
16188 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016189#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016190 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16191 * when finding the BF and check the previous two bytes. */
16192 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016193 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016194 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16195 * + 1, these may be in the "prev" string. */
16196 char_u back1 = p >= buf + 1 ? p[-1]
16197 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16198 char_u back2 = p >= buf + 2 ? p[-2]
16199 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16200 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016201
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016202 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016203 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016204 char_u *dest = p - 2;
16205
16206 /* Usually a BOM is at the beginning of a file, and so at
16207 * the beginning of a line; then we can just step over it.
16208 */
16209 if (start == dest)
16210 start = p + 1;
16211 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016212 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016213 /* have to shuffle buf to close gap */
16214 int adjust_prevlen = 0;
16215
16216 if (dest < buf)
16217 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016218 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016219 dest = buf;
16220 }
16221 if (readlen > p - buf + 1)
16222 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16223 readlen -= 3 - adjust_prevlen;
16224 prevlen -= adjust_prevlen;
16225 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016226 }
16227 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016228 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016229#endif
16230 } /* for */
16231
16232 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16233 break;
16234 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016235 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016236 /* There's part of a line in buf, store it in "prev". */
16237 if (p - start + prevlen >= prevsize)
16238 {
16239 /* need bigger "prev" buffer */
16240 char_u *newprev;
16241
16242 /* A common use case is ordinary text files and "prev" gets a
16243 * fragment of a line, so the first allocation is made
16244 * small, to avoid repeatedly 'allocing' large and
16245 * 'reallocing' small. */
16246 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016247 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016248 else
16249 {
16250 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016251 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016252 prevsize = grow50pc > growmin ? grow50pc : growmin;
16253 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016254 newprev = prev == NULL ? alloc(prevsize)
16255 : vim_realloc(prev, prevsize);
16256 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016257 {
16258 do_outofmem_msg((long_u)prevsize);
16259 failed = TRUE;
16260 break;
16261 }
16262 prev = newprev;
16263 }
16264 /* Add the line part to end of "prev". */
16265 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016266 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016267 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016268 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016269
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016270 /*
16271 * For a negative line count use only the lines at the end of the file,
16272 * free the rest.
16273 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016274 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016275 while (cnt > -maxline)
16276 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016277 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016278 --cnt;
16279 }
16280
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016281 if (failed)
16282 {
16283 list_free(rettv->vval.v_list, TRUE);
16284 /* readfile doc says an empty list is returned on error */
16285 rettv->vval.v_list = list_alloc();
16286 }
16287
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016288 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016289 fclose(fd);
16290}
16291
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016292#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016293static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016294
16295/*
16296 * Convert a List to proftime_T.
16297 * Return FAIL when there is something wrong.
16298 */
16299 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016300list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016301{
16302 long n1, n2;
16303 int error = FALSE;
16304
16305 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16306 || arg->vval.v_list->lv_len != 2)
16307 return FAIL;
16308 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16309 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16310# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016311 tm->HighPart = n1;
16312 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016313# else
16314 tm->tv_sec = n1;
16315 tm->tv_usec = n2;
16316# endif
16317 return error ? FAIL : OK;
16318}
16319#endif /* FEAT_RELTIME */
16320
16321/*
16322 * "reltime()" function
16323 */
16324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016325f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016326{
16327#ifdef FEAT_RELTIME
16328 proftime_T res;
16329 proftime_T start;
16330
16331 if (argvars[0].v_type == VAR_UNKNOWN)
16332 {
16333 /* No arguments: get current time. */
16334 profile_start(&res);
16335 }
16336 else if (argvars[1].v_type == VAR_UNKNOWN)
16337 {
16338 if (list2proftime(&argvars[0], &res) == FAIL)
16339 return;
16340 profile_end(&res);
16341 }
16342 else
16343 {
16344 /* Two arguments: compute the difference. */
16345 if (list2proftime(&argvars[0], &start) == FAIL
16346 || list2proftime(&argvars[1], &res) == FAIL)
16347 return;
16348 profile_sub(&res, &start);
16349 }
16350
16351 if (rettv_list_alloc(rettv) == OK)
16352 {
16353 long n1, n2;
16354
16355# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016356 n1 = res.HighPart;
16357 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016358# else
16359 n1 = res.tv_sec;
16360 n2 = res.tv_usec;
16361# endif
16362 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16363 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16364 }
16365#endif
16366}
16367
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016368#ifdef FEAT_FLOAT
16369/*
16370 * "reltimefloat()" function
16371 */
16372 static void
16373f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16374{
16375# ifdef FEAT_RELTIME
16376 proftime_T tm;
16377# endif
16378
16379 rettv->v_type = VAR_FLOAT;
16380 rettv->vval.v_float = 0;
16381# ifdef FEAT_RELTIME
16382 if (list2proftime(&argvars[0], &tm) == OK)
16383 rettv->vval.v_float = profile_float(&tm);
16384# endif
16385}
16386#endif
16387
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016388/*
16389 * "reltimestr()" function
16390 */
16391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016392f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016393{
16394#ifdef FEAT_RELTIME
16395 proftime_T tm;
16396#endif
16397
16398 rettv->v_type = VAR_STRING;
16399 rettv->vval.v_string = NULL;
16400#ifdef FEAT_RELTIME
16401 if (list2proftime(&argvars[0], &tm) == OK)
16402 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16403#endif
16404}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016405
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016407static void make_connection(void);
16408static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409
16410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016411make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412{
16413 if (X_DISPLAY == NULL
16414# ifdef FEAT_GUI
16415 && !gui.in_use
16416# endif
16417 )
16418 {
16419 x_force_connect = TRUE;
16420 setup_term_clip();
16421 x_force_connect = FALSE;
16422 }
16423}
16424
16425 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016426check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427{
16428 make_connection();
16429 if (X_DISPLAY == NULL)
16430 {
16431 EMSG(_("E240: No connection to Vim server"));
16432 return FAIL;
16433 }
16434 return OK;
16435}
16436#endif
16437
16438#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016439static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440
16441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016442remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443{
16444 char_u *server_name;
16445 char_u *keys;
16446 char_u *r = NULL;
16447 char_u buf[NUMBUFLEN];
16448# ifdef WIN32
16449 HWND w;
16450# else
16451 Window w;
16452# endif
16453
16454 if (check_restricted() || check_secure())
16455 return;
16456
16457# ifdef FEAT_X11
16458 if (check_connection() == FAIL)
16459 return;
16460# endif
16461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016462 server_name = get_tv_string_chk(&argvars[0]);
16463 if (server_name == NULL)
16464 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016465 keys = get_tv_string_buf(&argvars[1], buf);
16466# ifdef WIN32
16467 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16468# else
16469 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16470 < 0)
16471# endif
16472 {
16473 if (r != NULL)
16474 EMSG(r); /* sending worked but evaluation failed */
16475 else
16476 EMSG2(_("E241: Unable to send to %s"), server_name);
16477 return;
16478 }
16479
16480 rettv->vval.v_string = r;
16481
16482 if (argvars[2].v_type != VAR_UNKNOWN)
16483 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016484 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016485 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016486 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016487
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016488 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016489 v.di_tv.v_type = VAR_STRING;
16490 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016491 idvar = get_tv_string_chk(&argvars[2]);
16492 if (idvar != NULL)
16493 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016494 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016495 }
16496}
16497#endif
16498
16499/*
16500 * "remote_expr()" function
16501 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016503f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016504{
16505 rettv->v_type = VAR_STRING;
16506 rettv->vval.v_string = NULL;
16507#ifdef FEAT_CLIENTSERVER
16508 remote_common(argvars, rettv, TRUE);
16509#endif
16510}
16511
16512/*
16513 * "remote_foreground()" function
16514 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016516f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016518#ifdef FEAT_CLIENTSERVER
16519# ifdef WIN32
16520 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016521 {
16522 char_u *server_name = get_tv_string_chk(&argvars[0]);
16523
16524 if (server_name != NULL)
16525 serverForeground(server_name);
16526 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016527# else
16528 /* Send a foreground() expression to the server. */
16529 argvars[1].v_type = VAR_STRING;
16530 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16531 argvars[2].v_type = VAR_UNKNOWN;
16532 remote_common(argvars, rettv, TRUE);
16533 vim_free(argvars[1].vval.v_string);
16534# endif
16535#endif
16536}
16537
Bram Moolenaar0d660222005-01-07 21:51:51 +000016538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016539f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016540{
16541#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016542 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543 char_u *s = NULL;
16544# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016545 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016546# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016547 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016548
16549 if (check_restricted() || check_secure())
16550 {
16551 rettv->vval.v_number = -1;
16552 return;
16553 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016554 serverid = get_tv_string_chk(&argvars[0]);
16555 if (serverid == NULL)
16556 {
16557 rettv->vval.v_number = -1;
16558 return; /* type error; errmsg already given */
16559 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016560# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016561 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016562 if (n == 0)
16563 rettv->vval.v_number = -1;
16564 else
16565 {
16566 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16567 rettv->vval.v_number = (s != NULL);
16568 }
16569# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016570 if (check_connection() == FAIL)
16571 return;
16572
16573 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016574 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016575# endif
16576
16577 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016579 char_u *retvar;
16580
Bram Moolenaar33570922005-01-25 22:26:29 +000016581 v.di_tv.v_type = VAR_STRING;
16582 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016583 retvar = get_tv_string_chk(&argvars[1]);
16584 if (retvar != NULL)
16585 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016586 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016587 }
16588#else
16589 rettv->vval.v_number = -1;
16590#endif
16591}
16592
Bram Moolenaar0d660222005-01-07 21:51:51 +000016593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016594f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016595{
16596 char_u *r = NULL;
16597
16598#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016599 char_u *serverid = get_tv_string_chk(&argvars[0]);
16600
16601 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016602 {
16603# ifdef WIN32
16604 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016605 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016606
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016607 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016608 if (n != 0)
16609 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16610 if (r == NULL)
16611# else
16612 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016613 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016614# endif
16615 EMSG(_("E277: Unable to read a server reply"));
16616 }
16617#endif
16618 rettv->v_type = VAR_STRING;
16619 rettv->vval.v_string = r;
16620}
16621
16622/*
16623 * "remote_send()" function
16624 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016626f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016627{
16628 rettv->v_type = VAR_STRING;
16629 rettv->vval.v_string = NULL;
16630#ifdef FEAT_CLIENTSERVER
16631 remote_common(argvars, rettv, FALSE);
16632#endif
16633}
16634
16635/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016636 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016637 */
16638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016639f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016640{
Bram Moolenaar33570922005-01-25 22:26:29 +000016641 list_T *l;
16642 listitem_T *item, *item2;
16643 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016644 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016645 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016646 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016647 dict_T *d;
16648 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016649 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016650
Bram Moolenaar8c711452005-01-14 21:53:12 +000016651 if (argvars[0].v_type == VAR_DICT)
16652 {
16653 if (argvars[2].v_type != VAR_UNKNOWN)
16654 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016655 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016656 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016658 key = get_tv_string_chk(&argvars[1]);
16659 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016661 di = dict_find(d, key, -1);
16662 if (di == NULL)
16663 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016664 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16665 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016666 {
16667 *rettv = di->di_tv;
16668 init_tv(&di->di_tv);
16669 dictitem_remove(d, di);
16670 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016671 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016672 }
16673 }
16674 else if (argvars[0].v_type != VAR_LIST)
16675 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016676 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016677 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016679 int error = FALSE;
16680
16681 idx = get_tv_number_chk(&argvars[1], &error);
16682 if (error)
16683 ; /* type error: do nothing, errmsg already given */
16684 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016685 EMSGN(_(e_listidx), idx);
16686 else
16687 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016688 if (argvars[2].v_type == VAR_UNKNOWN)
16689 {
16690 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016691 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016692 *rettv = item->li_tv;
16693 vim_free(item);
16694 }
16695 else
16696 {
16697 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016698 end = get_tv_number_chk(&argvars[2], &error);
16699 if (error)
16700 ; /* type error: do nothing */
16701 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016702 EMSGN(_(e_listidx), end);
16703 else
16704 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016705 int cnt = 0;
16706
16707 for (li = item; li != NULL; li = li->li_next)
16708 {
16709 ++cnt;
16710 if (li == item2)
16711 break;
16712 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016713 if (li == NULL) /* didn't find "item2" after "item" */
16714 EMSG(_(e_invrange));
16715 else
16716 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016717 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016718 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016719 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016720 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016721 l->lv_first = item;
16722 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016723 item->li_prev = NULL;
16724 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016725 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016726 }
16727 }
16728 }
16729 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016730 }
16731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732}
16733
16734/*
16735 * "rename({from}, {to})" function
16736 */
16737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016738f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739{
16740 char_u buf[NUMBUFLEN];
16741
16742 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016745 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16746 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747}
16748
16749/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016750 * "repeat()" function
16751 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016753f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016754{
16755 char_u *p;
16756 int n;
16757 int slen;
16758 int len;
16759 char_u *r;
16760 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016761
16762 n = get_tv_number(&argvars[1]);
16763 if (argvars[0].v_type == VAR_LIST)
16764 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016765 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016766 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016767 if (list_extend(rettv->vval.v_list,
16768 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016769 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016770 }
16771 else
16772 {
16773 p = get_tv_string(&argvars[0]);
16774 rettv->v_type = VAR_STRING;
16775 rettv->vval.v_string = NULL;
16776
16777 slen = (int)STRLEN(p);
16778 len = slen * n;
16779 if (len <= 0)
16780 return;
16781
16782 r = alloc(len + 1);
16783 if (r != NULL)
16784 {
16785 for (i = 0; i < n; i++)
16786 mch_memmove(r + i * slen, p, (size_t)slen);
16787 r[len] = NUL;
16788 }
16789
16790 rettv->vval.v_string = r;
16791 }
16792}
16793
16794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 * "resolve()" function
16796 */
16797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016798f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799{
16800 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016801#ifdef HAVE_READLINK
16802 char_u *buf = NULL;
16803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016805 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806#ifdef FEAT_SHORTCUT
16807 {
16808 char_u *v = NULL;
16809
16810 v = mch_resolve_shortcut(p);
16811 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016812 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016814 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 }
16816#else
16817# ifdef HAVE_READLINK
16818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 char_u *cpy;
16820 int len;
16821 char_u *remain = NULL;
16822 char_u *q;
16823 int is_relative_to_current = FALSE;
16824 int has_trailing_pathsep = FALSE;
16825 int limit = 100;
16826
16827 p = vim_strsave(p);
16828
16829 if (p[0] == '.' && (vim_ispathsep(p[1])
16830 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16831 is_relative_to_current = TRUE;
16832
16833 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016834 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016837 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839
16840 q = getnextcomp(p);
16841 if (*q != NUL)
16842 {
16843 /* Separate the first path component in "p", and keep the
16844 * remainder (beginning with the path separator). */
16845 remain = vim_strsave(q - 1);
16846 q[-1] = NUL;
16847 }
16848
Bram Moolenaard9462e32011-04-11 21:35:11 +020016849 buf = alloc(MAXPATHL + 1);
16850 if (buf == NULL)
16851 goto fail;
16852
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 for (;;)
16854 {
16855 for (;;)
16856 {
16857 len = readlink((char *)p, (char *)buf, MAXPATHL);
16858 if (len <= 0)
16859 break;
16860 buf[len] = NUL;
16861
16862 if (limit-- == 0)
16863 {
16864 vim_free(p);
16865 vim_free(remain);
16866 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016867 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 goto fail;
16869 }
16870
16871 /* Ensure that the result will have a trailing path separator
16872 * if the argument has one. */
16873 if (remain == NULL && has_trailing_pathsep)
16874 add_pathsep(buf);
16875
16876 /* Separate the first path component in the link value and
16877 * concatenate the remainders. */
16878 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16879 if (*q != NUL)
16880 {
16881 if (remain == NULL)
16882 remain = vim_strsave(q - 1);
16883 else
16884 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016885 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886 if (cpy != NULL)
16887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888 vim_free(remain);
16889 remain = cpy;
16890 }
16891 }
16892 q[-1] = NUL;
16893 }
16894
16895 q = gettail(p);
16896 if (q > p && *q == NUL)
16897 {
16898 /* Ignore trailing path separator. */
16899 q[-1] = NUL;
16900 q = gettail(p);
16901 }
16902 if (q > p && !mch_isFullName(buf))
16903 {
16904 /* symlink is relative to directory of argument */
16905 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16906 if (cpy != NULL)
16907 {
16908 STRCPY(cpy, p);
16909 STRCPY(gettail(cpy), buf);
16910 vim_free(p);
16911 p = cpy;
16912 }
16913 }
16914 else
16915 {
16916 vim_free(p);
16917 p = vim_strsave(buf);
16918 }
16919 }
16920
16921 if (remain == NULL)
16922 break;
16923
16924 /* Append the first path component of "remain" to "p". */
16925 q = getnextcomp(remain + 1);
16926 len = q - remain - (*q != NUL);
16927 cpy = vim_strnsave(p, STRLEN(p) + len);
16928 if (cpy != NULL)
16929 {
16930 STRNCAT(cpy, remain, len);
16931 vim_free(p);
16932 p = cpy;
16933 }
16934 /* Shorten "remain". */
16935 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016936 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 else
16938 {
16939 vim_free(remain);
16940 remain = NULL;
16941 }
16942 }
16943
16944 /* If the result is a relative path name, make it explicitly relative to
16945 * the current directory if and only if the argument had this form. */
16946 if (!vim_ispathsep(*p))
16947 {
16948 if (is_relative_to_current
16949 && *p != NUL
16950 && !(p[0] == '.'
16951 && (p[1] == NUL
16952 || vim_ispathsep(p[1])
16953 || (p[1] == '.'
16954 && (p[2] == NUL
16955 || vim_ispathsep(p[2]))))))
16956 {
16957 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016958 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 if (cpy != NULL)
16960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 vim_free(p);
16962 p = cpy;
16963 }
16964 }
16965 else if (!is_relative_to_current)
16966 {
16967 /* Strip leading "./". */
16968 q = p;
16969 while (q[0] == '.' && vim_ispathsep(q[1]))
16970 q += 2;
16971 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016972 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973 }
16974 }
16975
16976 /* Ensure that the result will have no trailing path separator
16977 * if the argument had none. But keep "/" or "//". */
16978 if (!has_trailing_pathsep)
16979 {
16980 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016981 if (after_pathsep(p, q))
16982 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 }
16984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016985 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 }
16987# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016988 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989# endif
16990#endif
16991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016992 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993
16994#ifdef HAVE_READLINK
16995fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016996 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016998 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999}
17000
17001/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 */
17004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017005f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006{
Bram Moolenaar33570922005-01-25 22:26:29 +000017007 list_T *l;
17008 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009
Bram Moolenaar0d660222005-01-07 21:51:51 +000017010 if (argvars[0].v_type != VAR_LIST)
17011 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017012 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017013 && !tv_check_lock(l->lv_lock,
17014 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015 {
17016 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017017 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017018 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 while (li != NULL)
17020 {
17021 ni = li->li_prev;
17022 list_append(l, li);
17023 li = ni;
17024 }
17025 rettv->vval.v_list = l;
17026 rettv->v_type = VAR_LIST;
17027 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017028 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030}
17031
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017032#define SP_NOMOVE 0x01 /* don't move cursor */
17033#define SP_REPEAT 0x02 /* repeat to find outer pair */
17034#define SP_RETCOUNT 0x04 /* return matchcount */
17035#define SP_SETPCMARK 0x08 /* set previous context mark */
17036#define SP_START 0x10 /* accept match at start position */
17037#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17038#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017039#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017040
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017041static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017042
17043/*
17044 * Get flags for a search function.
17045 * Possibly sets "p_ws".
17046 * Returns BACKWARD, FORWARD or zero (for an error).
17047 */
17048 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017049get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050{
17051 int dir = FORWARD;
17052 char_u *flags;
17053 char_u nbuf[NUMBUFLEN];
17054 int mask;
17055
17056 if (varp->v_type != VAR_UNKNOWN)
17057 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017058 flags = get_tv_string_buf_chk(varp, nbuf);
17059 if (flags == NULL)
17060 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061 while (*flags != NUL)
17062 {
17063 switch (*flags)
17064 {
17065 case 'b': dir = BACKWARD; break;
17066 case 'w': p_ws = TRUE; break;
17067 case 'W': p_ws = FALSE; break;
17068 default: mask = 0;
17069 if (flagsp != NULL)
17070 switch (*flags)
17071 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017072 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017073 case 'e': mask = SP_END; break;
17074 case 'm': mask = SP_RETCOUNT; break;
17075 case 'n': mask = SP_NOMOVE; break;
17076 case 'p': mask = SP_SUBPAT; break;
17077 case 'r': mask = SP_REPEAT; break;
17078 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017079 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017080 }
17081 if (mask == 0)
17082 {
17083 EMSG2(_(e_invarg2), flags);
17084 dir = 0;
17085 }
17086 else
17087 *flagsp |= mask;
17088 }
17089 if (dir == 0)
17090 break;
17091 ++flags;
17092 }
17093 }
17094 return dir;
17095}
17096
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017098 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017101search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017103 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104 char_u *pat;
17105 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017106 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 int save_p_ws = p_ws;
17108 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017109 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017110 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017111 proftime_T tm;
17112#ifdef FEAT_RELTIME
17113 long time_limit = 0;
17114#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017115 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017116 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017118 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017119 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017120 if (dir == 0)
17121 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017122 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017123 if (flags & SP_START)
17124 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017125 if (flags & SP_END)
17126 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017127 if (flags & SP_COLUMN)
17128 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017129
Bram Moolenaar76929292008-01-06 19:07:36 +000017130 /* Optional arguments: line number to stop searching and timeout. */
17131 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017132 {
17133 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17134 if (lnum_stop < 0)
17135 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017136#ifdef FEAT_RELTIME
17137 if (argvars[3].v_type != VAR_UNKNOWN)
17138 {
17139 time_limit = get_tv_number_chk(&argvars[3], NULL);
17140 if (time_limit < 0)
17141 goto theend;
17142 }
17143#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017144 }
17145
Bram Moolenaar76929292008-01-06 19:07:36 +000017146#ifdef FEAT_RELTIME
17147 /* Set the time limit, if there is one. */
17148 profile_setlimit(time_limit, &tm);
17149#endif
17150
Bram Moolenaar231334e2005-07-25 20:46:57 +000017151 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017152 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017153 * Check to make sure only those flags are set.
17154 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17155 * flags cannot be set. Check for that condition also.
17156 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017157 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017158 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017160 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017161 goto theend;
17162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017164 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017165 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017166 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017167 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017169 if (flags & SP_SUBPAT)
17170 retval = subpatnum;
17171 else
17172 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017173 if (flags & SP_SETPCMARK)
17174 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017176 if (match_pos != NULL)
17177 {
17178 /* Store the match cursor position */
17179 match_pos->lnum = pos.lnum;
17180 match_pos->col = pos.col + 1;
17181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 /* "/$" will put the cursor after the end of the line, may need to
17183 * correct that here */
17184 check_cursor();
17185 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017186
17187 /* If 'n' flag is used: restore cursor position. */
17188 if (flags & SP_NOMOVE)
17189 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017190 else
17191 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017192theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017194
17195 return retval;
17196}
17197
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017198#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017199
17200/*
17201 * round() is not in C90, use ceil() or floor() instead.
17202 */
17203 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017204vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017205{
17206 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17207}
17208
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017209/*
17210 * "round({float})" function
17211 */
17212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017213f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017214{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017215 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017216
17217 rettv->v_type = VAR_FLOAT;
17218 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017219 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017220 else
17221 rettv->vval.v_float = 0.0;
17222}
17223#endif
17224
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017225/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017226 * "screenattr()" function
17227 */
17228 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017229f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017230{
17231 int row;
17232 int col;
17233 int c;
17234
17235 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17236 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17237 if (row < 0 || row >= screen_Rows
17238 || col < 0 || col >= screen_Columns)
17239 c = -1;
17240 else
17241 c = ScreenAttrs[LineOffset[row] + col];
17242 rettv->vval.v_number = c;
17243}
17244
17245/*
17246 * "screenchar()" function
17247 */
17248 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017249f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017250{
17251 int row;
17252 int col;
17253 int off;
17254 int c;
17255
17256 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17257 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17258 if (row < 0 || row >= screen_Rows
17259 || col < 0 || col >= screen_Columns)
17260 c = -1;
17261 else
17262 {
17263 off = LineOffset[row] + col;
17264#ifdef FEAT_MBYTE
17265 if (enc_utf8 && ScreenLinesUC[off] != 0)
17266 c = ScreenLinesUC[off];
17267 else
17268#endif
17269 c = ScreenLines[off];
17270 }
17271 rettv->vval.v_number = c;
17272}
17273
17274/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017275 * "screencol()" function
17276 *
17277 * First column is 1 to be consistent with virtcol().
17278 */
17279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017280f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017281{
17282 rettv->vval.v_number = screen_screencol() + 1;
17283}
17284
17285/*
17286 * "screenrow()" function
17287 */
17288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017289f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017290{
17291 rettv->vval.v_number = screen_screenrow() + 1;
17292}
17293
17294/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017295 * "search()" function
17296 */
17297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017298f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017299{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017300 int flags = 0;
17301
17302 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303}
17304
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017306 * "searchdecl()" function
17307 */
17308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017309f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017310{
17311 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017312 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017313 int error = FALSE;
17314 char_u *name;
17315
17316 rettv->vval.v_number = 1; /* default: FAIL */
17317
17318 name = get_tv_string_chk(&argvars[0]);
17319 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017320 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017321 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017322 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17323 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17324 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017325 if (!error && name != NULL)
17326 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017327 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017328}
17329
17330/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017331 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017334searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335{
17336 char_u *spat, *mpat, *epat;
17337 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 int dir;
17340 int flags = 0;
17341 char_u nbuf1[NUMBUFLEN];
17342 char_u nbuf2[NUMBUFLEN];
17343 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017344 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017345 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017346 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017349 spat = get_tv_string_chk(&argvars[0]);
17350 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17351 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17352 if (spat == NULL || mpat == NULL || epat == NULL)
17353 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 /* Handle the optional fourth argument: flags */
17356 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017357 if (dir == 0)
17358 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017359
17360 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017361 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17362 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017363 if ((flags & (SP_END | SP_SUBPAT)) != 0
17364 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017365 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017366 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017367 goto theend;
17368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017370 /* Using 'r' implies 'W', otherwise it doesn't work. */
17371 if (flags & SP_REPEAT)
17372 p_ws = FALSE;
17373
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017374 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017375 if (argvars[3].v_type == VAR_UNKNOWN
17376 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 skip = (char_u *)"";
17378 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017380 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017381 if (argvars[5].v_type != VAR_UNKNOWN)
17382 {
17383 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17384 if (lnum_stop < 0)
17385 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017386#ifdef FEAT_RELTIME
17387 if (argvars[6].v_type != VAR_UNKNOWN)
17388 {
17389 time_limit = get_tv_number_chk(&argvars[6], NULL);
17390 if (time_limit < 0)
17391 goto theend;
17392 }
17393#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017394 }
17395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017396 if (skip == NULL)
17397 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017399 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017400 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017401
17402theend:
17403 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017404
17405 return retval;
17406}
17407
17408/*
17409 * "searchpair()" function
17410 */
17411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017412f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017413{
17414 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17415}
17416
17417/*
17418 * "searchpairpos()" function
17419 */
17420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017421f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017422{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017423 pos_T match_pos;
17424 int lnum = 0;
17425 int col = 0;
17426
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017427 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017428 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017429
17430 if (searchpair_cmn(argvars, &match_pos) > 0)
17431 {
17432 lnum = match_pos.lnum;
17433 col = match_pos.col;
17434 }
17435
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017436 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17437 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017438}
17439
17440/*
17441 * Search for a start/middle/end thing.
17442 * Used by searchpair(), see its documentation for the details.
17443 * Returns 0 or -1 for no match,
17444 */
17445 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017446do_searchpair(
17447 char_u *spat, /* start pattern */
17448 char_u *mpat, /* middle pattern */
17449 char_u *epat, /* end pattern */
17450 int dir, /* BACKWARD or FORWARD */
17451 char_u *skip, /* skip expression */
17452 int flags, /* SP_SETPCMARK and other SP_ values */
17453 pos_T *match_pos,
17454 linenr_T lnum_stop, /* stop at this line if not zero */
17455 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017456{
17457 char_u *save_cpo;
17458 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17459 long retval = 0;
17460 pos_T pos;
17461 pos_T firstpos;
17462 pos_T foundpos;
17463 pos_T save_cursor;
17464 pos_T save_pos;
17465 int n;
17466 int r;
17467 int nest = 1;
17468 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017469 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017470 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017471
17472 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17473 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017474 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017475
Bram Moolenaar76929292008-01-06 19:07:36 +000017476#ifdef FEAT_RELTIME
17477 /* Set the time limit, if there is one. */
17478 profile_setlimit(time_limit, &tm);
17479#endif
17480
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017481 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17482 * start/middle/end (pat3, for the top pair). */
17483 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17484 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17485 if (pat2 == NULL || pat3 == NULL)
17486 goto theend;
17487 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17488 if (*mpat == NUL)
17489 STRCPY(pat3, pat2);
17490 else
17491 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17492 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017493 if (flags & SP_START)
17494 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017495
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 save_cursor = curwin->w_cursor;
17497 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017498 clearpos(&firstpos);
17499 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 pat = pat3;
17501 for (;;)
17502 {
17503 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017504 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17506 /* didn't find it or found the first match again: FAIL */
17507 break;
17508
17509 if (firstpos.lnum == 0)
17510 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017511 if (equalpos(pos, foundpos))
17512 {
17513 /* Found the same position again. Can happen with a pattern that
17514 * has "\zs" at the end and searching backwards. Advance one
17515 * character and try again. */
17516 if (dir == BACKWARD)
17517 decl(&pos);
17518 else
17519 incl(&pos);
17520 }
17521 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017523 /* clear the start flag to avoid getting stuck here */
17524 options &= ~SEARCH_START;
17525
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 /* If the skip pattern matches, ignore this match. */
17527 if (*skip != NUL)
17528 {
17529 save_pos = curwin->w_cursor;
17530 curwin->w_cursor = pos;
17531 r = eval_to_bool(skip, &err, NULL, FALSE);
17532 curwin->w_cursor = save_pos;
17533 if (err)
17534 {
17535 /* Evaluating {skip} caused an error, break here. */
17536 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017537 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538 break;
17539 }
17540 if (r)
17541 continue;
17542 }
17543
17544 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17545 {
17546 /* Found end when searching backwards or start when searching
17547 * forward: nested pair. */
17548 ++nest;
17549 pat = pat2; /* nested, don't search for middle */
17550 }
17551 else
17552 {
17553 /* Found end when searching forward or start when searching
17554 * backward: end of (nested) pair; or found middle in outer pair. */
17555 if (--nest == 1)
17556 pat = pat3; /* outer level, search for middle */
17557 }
17558
17559 if (nest == 0)
17560 {
17561 /* Found the match: return matchcount or line number. */
17562 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017563 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017565 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017566 if (flags & SP_SETPCMARK)
17567 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568 curwin->w_cursor = pos;
17569 if (!(flags & SP_REPEAT))
17570 break;
17571 nest = 1; /* search for next unmatched */
17572 }
17573 }
17574
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017575 if (match_pos != NULL)
17576 {
17577 /* Store the match cursor position */
17578 match_pos->lnum = curwin->w_cursor.lnum;
17579 match_pos->col = curwin->w_cursor.col + 1;
17580 }
17581
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017583 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 curwin->w_cursor = save_cursor;
17585
17586theend:
17587 vim_free(pat2);
17588 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017589 if (p_cpo == empty_option)
17590 p_cpo = save_cpo;
17591 else
17592 /* Darn, evaluating the {skip} expression changed the value. */
17593 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017594
17595 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596}
17597
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017598/*
17599 * "searchpos()" function
17600 */
17601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017602f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017603{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017604 pos_T match_pos;
17605 int lnum = 0;
17606 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017607 int n;
17608 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017609
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017610 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017611 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017612
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017613 n = search_cmn(argvars, &match_pos, &flags);
17614 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017615 {
17616 lnum = match_pos.lnum;
17617 col = match_pos.col;
17618 }
17619
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017620 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17621 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017622 if (flags & SP_SUBPAT)
17623 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017624}
17625
Bram Moolenaar0d660222005-01-07 21:51:51 +000017626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017627f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017629#ifdef FEAT_CLIENTSERVER
17630 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017631 char_u *server = get_tv_string_chk(&argvars[0]);
17632 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017635 if (server == NULL || reply == NULL)
17636 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017637 if (check_restricted() || check_secure())
17638 return;
17639# ifdef FEAT_X11
17640 if (check_connection() == FAIL)
17641 return;
17642# endif
17643
17644 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017646 EMSG(_("E258: Unable to send to client"));
17647 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017649 rettv->vval.v_number = 0;
17650#else
17651 rettv->vval.v_number = -1;
17652#endif
17653}
17654
Bram Moolenaar0d660222005-01-07 21:51:51 +000017655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017656f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017657{
17658 char_u *r = NULL;
17659
17660#ifdef FEAT_CLIENTSERVER
17661# ifdef WIN32
17662 r = serverGetVimNames();
17663# else
17664 make_connection();
17665 if (X_DISPLAY != NULL)
17666 r = serverGetVimNames(X_DISPLAY);
17667# endif
17668#endif
17669 rettv->v_type = VAR_STRING;
17670 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671}
17672
17673/*
17674 * "setbufvar()" function
17675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017677f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678{
17679 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017682 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 char_u nbuf[NUMBUFLEN];
17684
17685 if (check_restricted() || check_secure())
17686 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017687 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17688 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017689 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 varp = &argvars[2];
17691
17692 if (buf != NULL && varname != NULL && varp != NULL)
17693 {
17694 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696
17697 if (*varname == '&')
17698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017699 long numval;
17700 char_u *strval;
17701 int error = FALSE;
17702
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017704 numval = get_tv_number_chk(varp, &error);
17705 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017706 if (!error && strval != NULL)
17707 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 }
17709 else
17710 {
17711 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17712 if (bufvarname != NULL)
17713 {
17714 STRCPY(bufvarname, "b:");
17715 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017716 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 vim_free(bufvarname);
17718 }
17719 }
17720
17721 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724}
17725
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017727f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017728{
17729 dict_T *d;
17730 dictitem_T *di;
17731 char_u *csearch;
17732
17733 if (argvars[0].v_type != VAR_DICT)
17734 {
17735 EMSG(_(e_dictreq));
17736 return;
17737 }
17738
17739 if ((d = argvars[0].vval.v_dict) != NULL)
17740 {
17741 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17742 if (csearch != NULL)
17743 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017744#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017745 if (enc_utf8)
17746 {
17747 int pcc[MAX_MCO];
17748 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017749
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017750 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17751 }
17752 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017753#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017754 set_last_csearch(PTR2CHAR(csearch),
17755 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017756 }
17757
17758 di = dict_find(d, (char_u *)"forward", -1);
17759 if (di != NULL)
17760 set_csearch_direction(get_tv_number(&di->di_tv)
17761 ? FORWARD : BACKWARD);
17762
17763 di = dict_find(d, (char_u *)"until", -1);
17764 if (di != NULL)
17765 set_csearch_until(!!get_tv_number(&di->di_tv));
17766 }
17767}
17768
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769/*
17770 * "setcmdpos()" function
17771 */
17772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017773f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017775 int pos = (int)get_tv_number(&argvars[0]) - 1;
17776
17777 if (pos >= 0)
17778 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779}
17780
17781/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017782 * "setfperm({fname}, {mode})" function
17783 */
17784 static void
17785f_setfperm(typval_T *argvars, typval_T *rettv)
17786{
17787 char_u *fname;
17788 char_u modebuf[NUMBUFLEN];
17789 char_u *mode_str;
17790 int i;
17791 int mask;
17792 int mode = 0;
17793
17794 rettv->vval.v_number = 0;
17795 fname = get_tv_string_chk(&argvars[0]);
17796 if (fname == NULL)
17797 return;
17798 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17799 if (mode_str == NULL)
17800 return;
17801 if (STRLEN(mode_str) != 9)
17802 {
17803 EMSG2(_(e_invarg2), mode_str);
17804 return;
17805 }
17806
17807 mask = 1;
17808 for (i = 8; i >= 0; --i)
17809 {
17810 if (mode_str[i] != '-')
17811 mode |= mask;
17812 mask = mask << 1;
17813 }
17814 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17815}
17816
17817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818 * "setline()" function
17819 */
17820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017821f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822{
17823 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017824 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017825 list_T *l = NULL;
17826 listitem_T *li = NULL;
17827 long added = 0;
17828 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017830 lnum = get_tv_lnum(&argvars[0]);
17831 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017833 l = argvars[1].vval.v_list;
17834 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017836 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017837 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017838
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017839 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017840 for (;;)
17841 {
17842 if (l != NULL)
17843 {
17844 /* list argument, get next string */
17845 if (li == NULL)
17846 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017847 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017848 li = li->li_next;
17849 }
17850
17851 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017852 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017853 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017854
17855 /* When coming here from Insert mode, sync undo, so that this can be
17856 * undone separately from what was previously inserted. */
17857 if (u_sync_once == 2)
17858 {
17859 u_sync_once = 1; /* notify that u_sync() was called */
17860 u_sync(TRUE);
17861 }
17862
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017863 if (lnum <= curbuf->b_ml.ml_line_count)
17864 {
17865 /* existing line, replace it */
17866 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17867 {
17868 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017869 if (lnum == curwin->w_cursor.lnum)
17870 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017871 rettv->vval.v_number = 0; /* OK */
17872 }
17873 }
17874 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17875 {
17876 /* lnum is one past the last line, append the line */
17877 ++added;
17878 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17879 rettv->vval.v_number = 0; /* OK */
17880 }
17881
17882 if (l == NULL) /* only one string argument */
17883 break;
17884 ++lnum;
17885 }
17886
17887 if (added > 0)
17888 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889}
17890
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017891static 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 +000017892
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017894 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017895 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017897set_qf_ll_list(
17898 win_T *wp UNUSED,
17899 typval_T *list_arg UNUSED,
17900 typval_T *action_arg UNUSED,
17901 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017902{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017903#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017904 char_u *act;
17905 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017906#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017907
Bram Moolenaar2641f772005-03-25 21:58:17 +000017908 rettv->vval.v_number = -1;
17909
17910#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017911 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017912 EMSG(_(e_listreq));
17913 else
17914 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017915 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017916
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017917 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017918 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017919 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017920 if (act == NULL)
17921 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017922 if (*act == 'a' || *act == 'r')
17923 action = *act;
17924 }
17925
Bram Moolenaar81484f42012-12-05 15:16:47 +010017926 if (l != NULL && set_errorlist(wp, l, action,
17927 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017928 rettv->vval.v_number = 0;
17929 }
17930#endif
17931}
17932
17933/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017934 * "setloclist()" function
17935 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017937f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017938{
17939 win_T *win;
17940
17941 rettv->vval.v_number = -1;
17942
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017943 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017944 if (win != NULL)
17945 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17946}
17947
17948/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017949 * "setmatches()" function
17950 */
17951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017952f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017953{
17954#ifdef FEAT_SEARCH_EXTRA
17955 list_T *l;
17956 listitem_T *li;
17957 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017958 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017959
17960 rettv->vval.v_number = -1;
17961 if (argvars[0].v_type != VAR_LIST)
17962 {
17963 EMSG(_(e_listreq));
17964 return;
17965 }
17966 if ((l = argvars[0].vval.v_list) != NULL)
17967 {
17968
17969 /* To some extent make sure that we are dealing with a list from
17970 * "getmatches()". */
17971 li = l->lv_first;
17972 while (li != NULL)
17973 {
17974 if (li->li_tv.v_type != VAR_DICT
17975 || (d = li->li_tv.vval.v_dict) == NULL)
17976 {
17977 EMSG(_(e_invarg));
17978 return;
17979 }
17980 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017981 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17982 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017983 && dict_find(d, (char_u *)"priority", -1) != NULL
17984 && dict_find(d, (char_u *)"id", -1) != NULL))
17985 {
17986 EMSG(_(e_invarg));
17987 return;
17988 }
17989 li = li->li_next;
17990 }
17991
17992 clear_matches(curwin);
17993 li = l->lv_first;
17994 while (li != NULL)
17995 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017996 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017997 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017998 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017999 char_u *group;
18000 int priority;
18001 int id;
18002 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018003
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018004 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018005 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18006 {
18007 if (s == NULL)
18008 {
18009 s = list_alloc();
18010 if (s == NULL)
18011 return;
18012 }
18013
18014 /* match from matchaddpos() */
18015 for (i = 1; i < 9; i++)
18016 {
18017 sprintf((char *)buf, (char *)"pos%d", i);
18018 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18019 {
18020 if (di->di_tv.v_type != VAR_LIST)
18021 return;
18022
18023 list_append_tv(s, &di->di_tv);
18024 s->lv_refcount++;
18025 }
18026 else
18027 break;
18028 }
18029 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018030
18031 group = get_dict_string(d, (char_u *)"group", FALSE);
18032 priority = (int)get_dict_number(d, (char_u *)"priority");
18033 id = (int)get_dict_number(d, (char_u *)"id");
18034 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18035 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18036 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018037 if (i == 0)
18038 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018039 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018040 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018041 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018042 }
18043 else
18044 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018045 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018046 list_unref(s);
18047 s = NULL;
18048 }
18049
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018050 li = li->li_next;
18051 }
18052 rettv->vval.v_number = 0;
18053 }
18054#endif
18055}
18056
18057/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018058 * "setpos()" function
18059 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018061f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018062{
18063 pos_T pos;
18064 int fnum;
18065 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018066 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018067
Bram Moolenaar08250432008-02-13 11:42:46 +000018068 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018069 name = get_tv_string_chk(argvars);
18070 if (name != NULL)
18071 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018072 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018073 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018074 if (--pos.col < 0)
18075 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018076 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018077 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018078 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018079 if (fnum == curbuf->b_fnum)
18080 {
18081 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018082 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018083 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018084 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018085 curwin->w_set_curswant = FALSE;
18086 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018087 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018088 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018089 }
18090 else
18091 EMSG(_(e_invarg));
18092 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018093 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18094 {
18095 /* set mark */
18096 if (setmark_pos(name[1], &pos, fnum) == OK)
18097 rettv->vval.v_number = 0;
18098 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018099 else
18100 EMSG(_(e_invarg));
18101 }
18102 }
18103}
18104
18105/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018106 * "setqflist()" function
18107 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018109f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018110{
18111 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18112}
18113
18114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 * "setreg()" function
18116 */
18117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018118f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119{
18120 int regname;
18121 char_u *strregname;
18122 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018123 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124 int append;
18125 char_u yank_type;
18126 long block_len;
18127
18128 block_len = -1;
18129 yank_type = MAUTO;
18130 append = FALSE;
18131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018132 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018133 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018135 if (strregname == NULL)
18136 return; /* type error; errmsg already given */
18137 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 if (regname == 0 || regname == '@')
18139 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018141 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018143 stropt = get_tv_string_chk(&argvars[2]);
18144 if (stropt == NULL)
18145 return; /* type error */
18146 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 switch (*stropt)
18148 {
18149 case 'a': case 'A': /* append */
18150 append = TRUE;
18151 break;
18152 case 'v': case 'c': /* character-wise selection */
18153 yank_type = MCHAR;
18154 break;
18155 case 'V': case 'l': /* line-wise selection */
18156 yank_type = MLINE;
18157 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 case 'b': case Ctrl_V: /* block-wise selection */
18159 yank_type = MBLOCK;
18160 if (VIM_ISDIGIT(stropt[1]))
18161 {
18162 ++stropt;
18163 block_len = getdigits(&stropt) - 1;
18164 --stropt;
18165 }
18166 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 }
18168 }
18169
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018170 if (argvars[1].v_type == VAR_LIST)
18171 {
18172 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018173 char_u **allocval;
18174 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018175 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018176 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018177 int len = argvars[1].vval.v_list->lv_len;
18178 listitem_T *li;
18179
Bram Moolenaar7d647822014-04-05 21:28:56 +020018180 /* First half: use for pointers to result lines; second half: use for
18181 * pointers to allocated copies. */
18182 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018183 if (lstval == NULL)
18184 return;
18185 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018186 allocval = lstval + len + 2;
18187 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018188
18189 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18190 li = li->li_next)
18191 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018192 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018193 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018194 goto free_lstval;
18195 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018196 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018197 /* Need to make a copy, next get_tv_string_buf_chk() will
18198 * overwrite the string. */
18199 strval = vim_strsave(buf);
18200 if (strval == NULL)
18201 goto free_lstval;
18202 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018203 }
18204 *curval++ = strval;
18205 }
18206 *curval++ = NULL;
18207
18208 write_reg_contents_lst(regname, lstval, -1,
18209 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018210free_lstval:
18211 while (curallocval > allocval)
18212 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018213 vim_free(lstval);
18214 }
18215 else
18216 {
18217 strval = get_tv_string_chk(&argvars[1]);
18218 if (strval == NULL)
18219 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018220 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018222 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018223 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224}
18225
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018226/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018227 * "settabvar()" function
18228 */
18229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018230f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018231{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018232#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018233 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018234 tabpage_T *tp;
18235#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018236 char_u *varname, *tabvarname;
18237 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018238
18239 rettv->vval.v_number = 0;
18240
18241 if (check_restricted() || check_secure())
18242 return;
18243
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018244#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018245 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018246#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018247 varname = get_tv_string_chk(&argvars[1]);
18248 varp = &argvars[2];
18249
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018250 if (varname != NULL && varp != NULL
18251#ifdef FEAT_WINDOWS
18252 && tp != NULL
18253#endif
18254 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018255 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018256#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018257 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018258 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018259#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018260
18261 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18262 if (tabvarname != NULL)
18263 {
18264 STRCPY(tabvarname, "t:");
18265 STRCPY(tabvarname + 2, varname);
18266 set_var(tabvarname, varp, TRUE);
18267 vim_free(tabvarname);
18268 }
18269
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018270#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018271 /* Restore current tabpage */
18272 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018273 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018274#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018275 }
18276}
18277
18278/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018279 * "settabwinvar()" function
18280 */
18281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018282f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018283{
18284 setwinvar(argvars, rettv, 1);
18285}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286
18287/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018288 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018291f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018293 setwinvar(argvars, rettv, 0);
18294}
18295
18296/*
18297 * "setwinvar()" and "settabwinvar()" functions
18298 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018299
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018301setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018302{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 win_T *win;
18304#ifdef FEAT_WINDOWS
18305 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018306 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018307 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308#endif
18309 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018310 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018312 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313
18314 if (check_restricted() || check_secure())
18315 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018316
18317#ifdef FEAT_WINDOWS
18318 if (off == 1)
18319 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18320 else
18321 tp = curtab;
18322#endif
18323 win = find_win_by_nr(&argvars[off], tp);
18324 varname = get_tv_string_chk(&argvars[off + 1]);
18325 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326
18327 if (win != NULL && varname != NULL && varp != NULL)
18328 {
18329#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018330 need_switch_win = !(tp == curtab && win == curwin);
18331 if (!need_switch_win
18332 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018335 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018337 long numval;
18338 char_u *strval;
18339 int error = FALSE;
18340
18341 ++varname;
18342 numval = get_tv_number_chk(varp, &error);
18343 strval = get_tv_string_buf_chk(varp, nbuf);
18344 if (!error && strval != NULL)
18345 set_option_value(varname, numval, strval, OPT_LOCAL);
18346 }
18347 else
18348 {
18349 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18350 if (winvarname != NULL)
18351 {
18352 STRCPY(winvarname, "w:");
18353 STRCPY(winvarname + 2, varname);
18354 set_var(winvarname, varp, TRUE);
18355 vim_free(winvarname);
18356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 }
18358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018360 if (need_switch_win)
18361 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362#endif
18363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364}
18365
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018366#ifdef FEAT_CRYPT
18367/*
18368 * "sha256({string})" function
18369 */
18370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018371f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018372{
18373 char_u *p;
18374
18375 p = get_tv_string(&argvars[0]);
18376 rettv->vval.v_string = vim_strsave(
18377 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18378 rettv->v_type = VAR_STRING;
18379}
18380#endif /* FEAT_CRYPT */
18381
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018383 * "shellescape({string})" function
18384 */
18385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018386f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018387{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018388 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018389 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018390 rettv->v_type = VAR_STRING;
18391}
18392
18393/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018394 * shiftwidth() function
18395 */
18396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018397f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018398{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018399 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018400}
18401
18402/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018403 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 */
18405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018406f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018408 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409
Bram Moolenaar0d660222005-01-07 21:51:51 +000018410 p = get_tv_string(&argvars[0]);
18411 rettv->vval.v_string = vim_strsave(p);
18412 simplify_filename(rettv->vval.v_string); /* simplify in place */
18413 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414}
18415
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018416#ifdef FEAT_FLOAT
18417/*
18418 * "sin()" function
18419 */
18420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018421f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018422{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018423 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018424
18425 rettv->v_type = VAR_FLOAT;
18426 if (get_float_arg(argvars, &f) == OK)
18427 rettv->vval.v_float = sin(f);
18428 else
18429 rettv->vval.v_float = 0.0;
18430}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018431
18432/*
18433 * "sinh()" function
18434 */
18435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018436f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018437{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018438 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018439
18440 rettv->v_type = VAR_FLOAT;
18441 if (get_float_arg(argvars, &f) == OK)
18442 rettv->vval.v_float = sinh(f);
18443 else
18444 rettv->vval.v_float = 0.0;
18445}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018446#endif
18447
Bram Moolenaar0d660222005-01-07 21:51:51 +000018448static int
18449#ifdef __BORLANDC__
18450 _RTLENTRYF
18451#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018452 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018453static int
18454#ifdef __BORLANDC__
18455 _RTLENTRYF
18456#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018457 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018458
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018459/* struct used in the array that's given to qsort() */
18460typedef struct
18461{
18462 listitem_T *item;
18463 int idx;
18464} sortItem_T;
18465
Bram Moolenaar0b962472016-02-22 22:51:33 +010018466/* struct storing information about current sort */
18467typedef struct
18468{
18469 int item_compare_ic;
18470 int item_compare_numeric;
18471 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018472#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018473 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018474#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018475 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018476 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018477 dict_T *item_compare_selfdict;
18478 int item_compare_func_err;
18479 int item_compare_keep_zero;
18480} sortinfo_T;
18481static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018482static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018483#define ITEM_COMPARE_FAIL 999
18484
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018486 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018488 static int
18489#ifdef __BORLANDC__
18490_RTLENTRYF
18491#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018492item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018494 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018495 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018496 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018497 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018498 int res;
18499 char_u numbuf1[NUMBUFLEN];
18500 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018502 si1 = (sortItem_T *)s1;
18503 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018504 tv1 = &si1->item->li_tv;
18505 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018506
Bram Moolenaar0b962472016-02-22 22:51:33 +010018507 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018508 {
18509 long v1 = get_tv_number(tv1);
18510 long v2 = get_tv_number(tv2);
18511
18512 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18513 }
18514
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018515#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018516 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018517 {
18518 float_T v1 = get_tv_float(tv1);
18519 float_T v2 = get_tv_float(tv2);
18520
18521 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18522 }
18523#endif
18524
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018525 /* tv2string() puts quotes around a string and allocates memory. Don't do
18526 * that for string variables. Use a single quote when comparing with a
18527 * non-string to do what the docs promise. */
18528 if (tv1->v_type == VAR_STRING)
18529 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018530 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018531 p1 = (char_u *)"'";
18532 else
18533 p1 = tv1->vval.v_string;
18534 }
18535 else
18536 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18537 if (tv2->v_type == VAR_STRING)
18538 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018539 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018540 p2 = (char_u *)"'";
18541 else
18542 p2 = tv2->vval.v_string;
18543 }
18544 else
18545 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018546 if (p1 == NULL)
18547 p1 = (char_u *)"";
18548 if (p2 == NULL)
18549 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018550 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018551 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018552 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018553 res = STRICMP(p1, p2);
18554 else
18555 res = STRCMP(p1, p2);
18556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018558 {
18559 double n1, n2;
18560 n1 = strtod((char *)p1, (char **)&p1);
18561 n2 = strtod((char *)p2, (char **)&p2);
18562 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18563 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018564
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018565 /* When the result would be zero, compare the item indexes. Makes the
18566 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018567 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018568 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018569
Bram Moolenaar0d660222005-01-07 21:51:51 +000018570 vim_free(tofree1);
18571 vim_free(tofree2);
18572 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573}
18574
18575 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018576#ifdef __BORLANDC__
18577_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018579item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018580{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018581 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018582 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018583 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018584 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018585 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018586 char_u *func_name;
18587 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018589 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018590 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018591 return 0;
18592
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018593 si1 = (sortItem_T *)s1;
18594 si2 = (sortItem_T *)s2;
18595
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018596 if (partial == NULL)
18597 func_name = sortinfo->item_compare_func;
18598 else
18599 func_name = partial->pt_name;
18600
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018601 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018602 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018603 copy_tv(&si1->item->li_tv, &argv[0]);
18604 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018605
18606 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018607 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018608 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018609 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018610 clear_tv(&argv[0]);
18611 clear_tv(&argv[1]);
18612
18613 if (res == FAIL)
18614 res = ITEM_COMPARE_FAIL;
18615 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018616 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18617 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018618 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018619 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018620
18621 /* When the result would be zero, compare the pointers themselves. Makes
18622 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018623 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018624 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018625
Bram Moolenaar0d660222005-01-07 21:51:51 +000018626 return res;
18627}
18628
18629/*
18630 * "sort({list})" function
18631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018633do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634{
Bram Moolenaar33570922005-01-25 22:26:29 +000018635 list_T *l;
18636 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018637 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018638 sortinfo_T *old_sortinfo;
18639 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018640 long len;
18641 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642
Bram Moolenaar0b962472016-02-22 22:51:33 +010018643 /* Pointer to current info struct used in compare function. Save and
18644 * restore the current one for nested calls. */
18645 old_sortinfo = sortinfo;
18646 sortinfo = &info;
18647
Bram Moolenaar0d660222005-01-07 21:51:51 +000018648 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018649 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 else
18651 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018652 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018653 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018654 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18655 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018656 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018657 rettv->vval.v_list = l;
18658 rettv->v_type = VAR_LIST;
18659 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660
Bram Moolenaar0d660222005-01-07 21:51:51 +000018661 len = list_len(l);
18662 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018663 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664
Bram Moolenaar0b962472016-02-22 22:51:33 +010018665 info.item_compare_ic = FALSE;
18666 info.item_compare_numeric = FALSE;
18667 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018668#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018669 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018670#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018671 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018672 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018673 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018674 if (argvars[1].v_type != VAR_UNKNOWN)
18675 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018676 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018677 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018678 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018679 else if (argvars[1].v_type == VAR_PARTIAL)
18680 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018681 else
18682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018683 int error = FALSE;
18684
18685 i = get_tv_number_chk(&argvars[1], &error);
18686 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018687 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018688 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018689 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018690 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018691 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018692 else if (i != 0)
18693 {
18694 EMSG(_(e_invarg));
18695 goto theend;
18696 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018697 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018698 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018699 if (*info.item_compare_func == NUL)
18700 {
18701 /* empty string means default sort */
18702 info.item_compare_func = NULL;
18703 }
18704 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018705 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018706 info.item_compare_func = NULL;
18707 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018708 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018709 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018710 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018711 info.item_compare_func = NULL;
18712 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018713 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018714#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018715 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018716 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018717 info.item_compare_func = NULL;
18718 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018719 }
18720#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018721 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018722 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018723 info.item_compare_func = NULL;
18724 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018725 }
18726 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018727 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018728
18729 if (argvars[2].v_type != VAR_UNKNOWN)
18730 {
18731 /* optional third argument: {dict} */
18732 if (argvars[2].v_type != VAR_DICT)
18733 {
18734 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018735 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018736 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018737 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018738 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740
Bram Moolenaar0d660222005-01-07 21:51:51 +000018741 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018742 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018743 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018744 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745
Bram Moolenaar327aa022014-03-25 18:24:23 +010018746 i = 0;
18747 if (sort)
18748 {
18749 /* sort(): ptrs will be the list to sort */
18750 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018751 {
18752 ptrs[i].item = li;
18753 ptrs[i].idx = i;
18754 ++i;
18755 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018756
Bram Moolenaar0b962472016-02-22 22:51:33 +010018757 info.item_compare_func_err = FALSE;
18758 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018759 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018760 if ((info.item_compare_func != NULL
18761 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018762 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018763 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018764 EMSG(_("E702: Sort compare function failed"));
18765 else
18766 {
18767 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018768 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018769 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018770 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018771 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018772
Bram Moolenaar0b962472016-02-22 22:51:33 +010018773 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018774 {
18775 /* Clear the List and append the items in sorted order. */
18776 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18777 l->lv_len = 0;
18778 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018779 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018780 }
18781 }
18782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018784 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018785 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018786
18787 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018788 info.item_compare_func_err = FALSE;
18789 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018790 item_compare_func_ptr = info.item_compare_func != NULL
18791 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018792 ? item_compare2 : item_compare;
18793
18794 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18795 li = li->li_next)
18796 {
18797 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18798 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018799 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018800 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018801 {
18802 EMSG(_("E882: Uniq compare function failed"));
18803 break;
18804 }
18805 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018806
Bram Moolenaar0b962472016-02-22 22:51:33 +010018807 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018808 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018809 while (--i >= 0)
18810 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018811 li = ptrs[i].item->li_next;
18812 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018813 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018814 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018815 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018816 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018817 list_fix_watch(l, li);
18818 listitem_free(li);
18819 l->lv_len--;
18820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018821 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018822 }
18823
18824 vim_free(ptrs);
18825 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018826theend:
18827 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018828}
18829
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018830/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018831 * "sort({list})" function
18832 */
18833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018834f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018835{
18836 do_sort_uniq(argvars, rettv, TRUE);
18837}
18838
18839/*
18840 * "uniq({list})" function
18841 */
18842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018843f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018844{
18845 do_sort_uniq(argvars, rettv, FALSE);
18846}
18847
18848/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018849 * "soundfold({word})" function
18850 */
18851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018852f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018853{
18854 char_u *s;
18855
18856 rettv->v_type = VAR_STRING;
18857 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018858#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018859 rettv->vval.v_string = eval_soundfold(s);
18860#else
18861 rettv->vval.v_string = vim_strsave(s);
18862#endif
18863}
18864
18865/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018866 * "spellbadword()" function
18867 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018869f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018870{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018871 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018872 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018873 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018874
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018875 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018876 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018877
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018878#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018879 if (argvars[0].v_type == VAR_UNKNOWN)
18880 {
18881 /* Find the start and length of the badly spelled word. */
18882 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18883 if (len != 0)
18884 word = ml_get_cursor();
18885 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018886 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018887 {
18888 char_u *str = get_tv_string_chk(&argvars[0]);
18889 int capcol = -1;
18890
18891 if (str != NULL)
18892 {
18893 /* Check the argument for spelling. */
18894 while (*str != NUL)
18895 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018896 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018897 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018898 {
18899 word = str;
18900 break;
18901 }
18902 str += len;
18903 }
18904 }
18905 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018906#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018907
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018908 list_append_string(rettv->vval.v_list, word, len);
18909 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018910 attr == HLF_SPB ? "bad" :
18911 attr == HLF_SPR ? "rare" :
18912 attr == HLF_SPL ? "local" :
18913 attr == HLF_SPC ? "caps" :
18914 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018915}
18916
18917/*
18918 * "spellsuggest()" function
18919 */
18920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018921f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018922{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018923#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018924 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018925 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018926 int maxcount;
18927 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018928 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018929 listitem_T *li;
18930 int need_capital = FALSE;
18931#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018932
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018933 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018934 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018935
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018936#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018937 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018938 {
18939 str = get_tv_string(&argvars[0]);
18940 if (argvars[1].v_type != VAR_UNKNOWN)
18941 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018942 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018943 if (maxcount <= 0)
18944 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018945 if (argvars[2].v_type != VAR_UNKNOWN)
18946 {
18947 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18948 if (typeerr)
18949 return;
18950 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018951 }
18952 else
18953 maxcount = 25;
18954
Bram Moolenaar4770d092006-01-12 23:22:24 +000018955 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018956
18957 for (i = 0; i < ga.ga_len; ++i)
18958 {
18959 str = ((char_u **)ga.ga_data)[i];
18960
18961 li = listitem_alloc();
18962 if (li == NULL)
18963 vim_free(str);
18964 else
18965 {
18966 li->li_tv.v_type = VAR_STRING;
18967 li->li_tv.v_lock = 0;
18968 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018969 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018970 }
18971 }
18972 ga_clear(&ga);
18973 }
18974#endif
18975}
18976
Bram Moolenaar0d660222005-01-07 21:51:51 +000018977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018978f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979{
18980 char_u *str;
18981 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018982 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018983 regmatch_T regmatch;
18984 char_u patbuf[NUMBUFLEN];
18985 char_u *save_cpo;
18986 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018987 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018988 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018989 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018990
18991 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18992 save_cpo = p_cpo;
18993 p_cpo = (char_u *)"";
18994
18995 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018996 if (argvars[1].v_type != VAR_UNKNOWN)
18997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018998 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18999 if (pat == NULL)
19000 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019001 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019002 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019003 }
19004 if (pat == NULL || *pat == NUL)
19005 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019006
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019007 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019009 if (typeerr)
19010 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011
Bram Moolenaar0d660222005-01-07 21:51:51 +000019012 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19013 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019015 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019016 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019017 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019018 if (*str == NUL)
19019 match = FALSE; /* empty item at the end */
19020 else
19021 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019022 if (match)
19023 end = regmatch.startp[0];
19024 else
19025 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019026 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19027 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019028 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019029 if (list_append_string(rettv->vval.v_list, str,
19030 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019031 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019032 }
19033 if (!match)
19034 break;
19035 /* Advance to just after the match. */
19036 if (regmatch.endp[0] > str)
19037 col = 0;
19038 else
19039 {
19040 /* Don't get stuck at the same match. */
19041#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019042 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019043#else
19044 col = 1;
19045#endif
19046 }
19047 str = regmatch.endp[0];
19048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049
Bram Moolenaar473de612013-06-08 18:19:48 +020019050 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054}
19055
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019056#ifdef FEAT_FLOAT
19057/*
19058 * "sqrt()" function
19059 */
19060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019061f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019062{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019063 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019064
19065 rettv->v_type = VAR_FLOAT;
19066 if (get_float_arg(argvars, &f) == OK)
19067 rettv->vval.v_float = sqrt(f);
19068 else
19069 rettv->vval.v_float = 0.0;
19070}
19071
19072/*
19073 * "str2float()" function
19074 */
19075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019076f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019077{
19078 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19079
19080 if (*p == '+')
19081 p = skipwhite(p + 1);
19082 (void)string2float(p, &rettv->vval.v_float);
19083 rettv->v_type = VAR_FLOAT;
19084}
19085#endif
19086
Bram Moolenaar2c932302006-03-18 21:42:09 +000019087/*
19088 * "str2nr()" function
19089 */
19090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019091f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019092{
19093 int base = 10;
19094 char_u *p;
19095 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019096 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019097
19098 if (argvars[1].v_type != VAR_UNKNOWN)
19099 {
19100 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019101 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019102 {
19103 EMSG(_(e_invarg));
19104 return;
19105 }
19106 }
19107
19108 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019109 if (*p == '+')
19110 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019111 switch (base)
19112 {
19113 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19114 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19115 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19116 default: what = 0;
19117 }
19118 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019119 rettv->vval.v_number = n;
19120}
19121
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122#ifdef HAVE_STRFTIME
19123/*
19124 * "strftime({format}[, {time}])" function
19125 */
19126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019127f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128{
19129 char_u result_buf[256];
19130 struct tm *curtime;
19131 time_t seconds;
19132 char_u *p;
19133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019134 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019137 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 seconds = time(NULL);
19139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019140 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 curtime = localtime(&seconds);
19142 /* MSVC returns NULL for an invalid value of seconds. */
19143 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019144 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 else
19146 {
19147# ifdef FEAT_MBYTE
19148 vimconv_T conv;
19149 char_u *enc;
19150
19151 conv.vc_type = CONV_NONE;
19152 enc = enc_locale();
19153 convert_setup(&conv, p_enc, enc);
19154 if (conv.vc_type != CONV_NONE)
19155 p = string_convert(&conv, p, NULL);
19156# endif
19157 if (p != NULL)
19158 (void)strftime((char *)result_buf, sizeof(result_buf),
19159 (char *)p, curtime);
19160 else
19161 result_buf[0] = NUL;
19162
19163# ifdef FEAT_MBYTE
19164 if (conv.vc_type != CONV_NONE)
19165 vim_free(p);
19166 convert_setup(&conv, enc, p_enc);
19167 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019168 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169 else
19170# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019171 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172
19173# ifdef FEAT_MBYTE
19174 /* Release conversion descriptors */
19175 convert_setup(&conv, NULL, NULL);
19176 vim_free(enc);
19177# endif
19178 }
19179}
19180#endif
19181
19182/*
19183 * "stridx()" function
19184 */
19185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019186f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187{
19188 char_u buf[NUMBUFLEN];
19189 char_u *needle;
19190 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019191 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019193 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019195 needle = get_tv_string_chk(&argvars[1]);
19196 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019197 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019198 if (needle == NULL || haystack == NULL)
19199 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200
Bram Moolenaar33570922005-01-25 22:26:29 +000019201 if (argvars[2].v_type != VAR_UNKNOWN)
19202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019203 int error = FALSE;
19204
19205 start_idx = get_tv_number_chk(&argvars[2], &error);
19206 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019207 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019208 if (start_idx >= 0)
19209 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019210 }
19211
19212 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19213 if (pos != NULL)
19214 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215}
19216
19217/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019218 * "string()" function
19219 */
19220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019221f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019222{
19223 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019224 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019226 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019227 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019228 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019229 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019230 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231}
19232
19233/*
19234 * "strlen()" function
19235 */
19236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019237f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019239 rettv->vval.v_number = (varnumber_T)(STRLEN(
19240 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241}
19242
19243/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019244 * "strchars()" function
19245 */
19246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019247f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019248{
19249 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019250 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019251#ifdef FEAT_MBYTE
19252 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019253 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019254#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019255
19256 if (argvars[1].v_type != VAR_UNKNOWN)
19257 skipcc = get_tv_number_chk(&argvars[1], NULL);
19258 if (skipcc < 0 || skipcc > 1)
19259 EMSG(_(e_invarg));
19260 else
19261 {
19262#ifdef FEAT_MBYTE
19263 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19264 while (*s != NUL)
19265 {
19266 func_mb_ptr2char_adv(&s);
19267 ++len;
19268 }
19269 rettv->vval.v_number = len;
19270#else
19271 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19272#endif
19273 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019274}
19275
19276/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019277 * "strdisplaywidth()" function
19278 */
19279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019280f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019281{
19282 char_u *s = get_tv_string(&argvars[0]);
19283 int col = 0;
19284
19285 if (argvars[1].v_type != VAR_UNKNOWN)
19286 col = get_tv_number(&argvars[1]);
19287
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019288 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019289}
19290
19291/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019292 * "strwidth()" function
19293 */
19294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019295f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019296{
19297 char_u *s = get_tv_string(&argvars[0]);
19298
19299 rettv->vval.v_number = (varnumber_T)(
19300#ifdef FEAT_MBYTE
19301 mb_string2cells(s, -1)
19302#else
19303 STRLEN(s)
19304#endif
19305 );
19306}
19307
19308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 * "strpart()" function
19310 */
19311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019312f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313{
19314 char_u *p;
19315 int n;
19316 int len;
19317 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019318 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019320 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 slen = (int)STRLEN(p);
19322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019323 n = get_tv_number_chk(&argvars[1], &error);
19324 if (error)
19325 len = 0;
19326 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019327 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328 else
19329 len = slen - n; /* default len: all bytes that are available. */
19330
19331 /*
19332 * Only return the overlap between the specified part and the actual
19333 * string.
19334 */
19335 if (n < 0)
19336 {
19337 len += n;
19338 n = 0;
19339 }
19340 else if (n > slen)
19341 n = slen;
19342 if (len < 0)
19343 len = 0;
19344 else if (n + len > slen)
19345 len = slen - n;
19346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019347 rettv->v_type = VAR_STRING;
19348 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349}
19350
19351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019352 * "strridx()" function
19353 */
19354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019355f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019356{
19357 char_u buf[NUMBUFLEN];
19358 char_u *needle;
19359 char_u *haystack;
19360 char_u *rest;
19361 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019362 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019364 needle = get_tv_string_chk(&argvars[1]);
19365 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019366
19367 rettv->vval.v_number = -1;
19368 if (needle == NULL || haystack == NULL)
19369 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019370
19371 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019372 if (argvars[2].v_type != VAR_UNKNOWN)
19373 {
19374 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019375 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019376 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019377 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019378 }
19379 else
19380 end_idx = haystack_len;
19381
Bram Moolenaar0d660222005-01-07 21:51:51 +000019382 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019383 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019384 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019385 lastmatch = haystack + end_idx;
19386 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019387 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019388 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019389 for (rest = haystack; *rest != '\0'; ++rest)
19390 {
19391 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019392 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019393 break;
19394 lastmatch = rest;
19395 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019396 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019397
19398 if (lastmatch == NULL)
19399 rettv->vval.v_number = -1;
19400 else
19401 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19402}
19403
19404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 * "strtrans()" function
19406 */
19407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019408f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019410 rettv->v_type = VAR_STRING;
19411 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412}
19413
19414/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019415 * "submatch()" function
19416 */
19417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019418f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019419{
Bram Moolenaar41571762014-04-02 19:00:58 +020019420 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019421 int no;
19422 int retList = 0;
19423
19424 no = (int)get_tv_number_chk(&argvars[0], &error);
19425 if (error)
19426 return;
19427 error = FALSE;
19428 if (argvars[1].v_type != VAR_UNKNOWN)
19429 retList = get_tv_number_chk(&argvars[1], &error);
19430 if (error)
19431 return;
19432
19433 if (retList == 0)
19434 {
19435 rettv->v_type = VAR_STRING;
19436 rettv->vval.v_string = reg_submatch(no);
19437 }
19438 else
19439 {
19440 rettv->v_type = VAR_LIST;
19441 rettv->vval.v_list = reg_submatch_list(no);
19442 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019443}
19444
19445/*
19446 * "substitute()" function
19447 */
19448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019449f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019450{
19451 char_u patbuf[NUMBUFLEN];
19452 char_u subbuf[NUMBUFLEN];
19453 char_u flagsbuf[NUMBUFLEN];
19454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019455 char_u *str = get_tv_string_chk(&argvars[0]);
19456 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19457 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19458 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19459
Bram Moolenaar0d660222005-01-07 21:51:51 +000019460 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019461 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19462 rettv->vval.v_string = NULL;
19463 else
19464 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019465}
19466
19467/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019468 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019471f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472{
19473 int id = 0;
19474#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019475 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 long col;
19477 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019478 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019480 lnum = get_tv_lnum(argvars); /* -1 on type error */
19481 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19482 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019484 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019485 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019486 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487#endif
19488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019489 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490}
19491
19492/*
19493 * "synIDattr(id, what [, mode])" function
19494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019496f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497{
19498 char_u *p = NULL;
19499#ifdef FEAT_SYN_HL
19500 int id;
19501 char_u *what;
19502 char_u *mode;
19503 char_u modebuf[NUMBUFLEN];
19504 int modec;
19505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019506 id = get_tv_number(&argvars[0]);
19507 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019508 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019510 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019512 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 modec = 0; /* replace invalid with current */
19514 }
19515 else
19516 {
19517#ifdef FEAT_GUI
19518 if (gui.in_use)
19519 modec = 'g';
19520 else
19521#endif
19522 if (t_colors > 1)
19523 modec = 'c';
19524 else
19525 modec = 't';
19526 }
19527
19528
19529 switch (TOLOWER_ASC(what[0]))
19530 {
19531 case 'b':
19532 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19533 p = highlight_color(id, what, modec);
19534 else /* bold */
19535 p = highlight_has_attr(id, HL_BOLD, modec);
19536 break;
19537
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019538 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 p = highlight_color(id, what, modec);
19540 break;
19541
19542 case 'i':
19543 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19544 p = highlight_has_attr(id, HL_INVERSE, modec);
19545 else /* italic */
19546 p = highlight_has_attr(id, HL_ITALIC, modec);
19547 break;
19548
19549 case 'n': /* name */
19550 p = get_highlight_name(NULL, id - 1);
19551 break;
19552
19553 case 'r': /* reverse */
19554 p = highlight_has_attr(id, HL_INVERSE, modec);
19555 break;
19556
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019557 case 's':
19558 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19559 p = highlight_color(id, what, modec);
19560 else /* standout */
19561 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562 break;
19563
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019564 case 'u':
19565 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19566 /* underline */
19567 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19568 else
19569 /* undercurl */
19570 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 break;
19572 }
19573
19574 if (p != NULL)
19575 p = vim_strsave(p);
19576#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019577 rettv->v_type = VAR_STRING;
19578 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579}
19580
19581/*
19582 * "synIDtrans(id)" function
19583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019585f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586{
19587 int id;
19588
19589#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019590 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591
19592 if (id > 0)
19593 id = syn_get_final_id(id);
19594 else
19595#endif
19596 id = 0;
19597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019598 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599}
19600
19601/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019602 * "synconcealed(lnum, col)" function
19603 */
19604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019605f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019606{
19607#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19608 long lnum;
19609 long col;
19610 int syntax_flags = 0;
19611 int cchar;
19612 int matchid = 0;
19613 char_u str[NUMBUFLEN];
19614#endif
19615
19616 rettv->v_type = VAR_LIST;
19617 rettv->vval.v_list = NULL;
19618
19619#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19620 lnum = get_tv_lnum(argvars); /* -1 on type error */
19621 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19622
19623 vim_memset(str, NUL, sizeof(str));
19624
19625 if (rettv_list_alloc(rettv) != FAIL)
19626 {
19627 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19628 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19629 && curwin->w_p_cole > 0)
19630 {
19631 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19632 syntax_flags = get_syntax_info(&matchid);
19633
19634 /* get the conceal character */
19635 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19636 {
19637 cchar = syn_get_sub_char();
19638 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19639 cchar = lcs_conceal;
19640 if (cchar != NUL)
19641 {
19642# ifdef FEAT_MBYTE
19643 if (has_mbyte)
19644 (*mb_char2bytes)(cchar, str);
19645 else
19646# endif
19647 str[0] = cchar;
19648 }
19649 }
19650 }
19651
19652 list_append_number(rettv->vval.v_list,
19653 (syntax_flags & HL_CONCEAL) != 0);
19654 /* -1 to auto-determine strlen */
19655 list_append_string(rettv->vval.v_list, str, -1);
19656 list_append_number(rettv->vval.v_list, matchid);
19657 }
19658#endif
19659}
19660
19661/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019662 * "synstack(lnum, col)" function
19663 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019665f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019666{
19667#ifdef FEAT_SYN_HL
19668 long lnum;
19669 long col;
19670 int i;
19671 int id;
19672#endif
19673
19674 rettv->v_type = VAR_LIST;
19675 rettv->vval.v_list = NULL;
19676
19677#ifdef FEAT_SYN_HL
19678 lnum = get_tv_lnum(argvars); /* -1 on type error */
19679 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19680
19681 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019682 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019683 && rettv_list_alloc(rettv) != FAIL)
19684 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019685 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019686 for (i = 0; ; ++i)
19687 {
19688 id = syn_get_stack_item(i);
19689 if (id < 0)
19690 break;
19691 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19692 break;
19693 }
19694 }
19695#endif
19696}
19697
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019699get_cmd_output_as_rettv(
19700 typval_T *argvars,
19701 typval_T *rettv,
19702 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019704 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019706 char_u *infile = NULL;
19707 char_u buf[NUMBUFLEN];
19708 int err = FALSE;
19709 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019710 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019711 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019713 rettv->v_type = VAR_STRING;
19714 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019715 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019716 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019717
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019718 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019719 {
19720 /*
19721 * Write the string to a temp file, to be used for input of the shell
19722 * command.
19723 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019724 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019725 {
19726 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019727 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019728 }
19729
19730 fd = mch_fopen((char *)infile, WRITEBIN);
19731 if (fd == NULL)
19732 {
19733 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019734 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019735 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019736 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019737 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019738 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19739 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019740 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019741 else
19742 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019743 size_t len;
19744
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019745 p = get_tv_string_buf_chk(&argvars[1], buf);
19746 if (p == NULL)
19747 {
19748 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019749 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019750 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019751 len = STRLEN(p);
19752 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019753 err = TRUE;
19754 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019755 if (fclose(fd) != 0)
19756 err = TRUE;
19757 if (err)
19758 {
19759 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019760 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019761 }
19762 }
19763
Bram Moolenaar52a72462014-08-29 15:53:52 +020019764 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19765 * echoes typeahead, that messes up the display. */
19766 if (!msg_silent)
19767 flags += SHELL_COOKED;
19768
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019769 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019771 int len;
19772 listitem_T *li;
19773 char_u *s = NULL;
19774 char_u *start;
19775 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019776 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777
Bram Moolenaar52a72462014-08-29 15:53:52 +020019778 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019779 if (res == NULL)
19780 goto errret;
19781
19782 list = list_alloc();
19783 if (list == NULL)
19784 goto errret;
19785
19786 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019788 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019789 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019790 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019791 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019792
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019793 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019794 if (s == NULL)
19795 goto errret;
19796
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019797 for (p = s; start < end; ++p, ++start)
19798 *p = *start == NUL ? NL : *start;
19799 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019800
19801 li = listitem_alloc();
19802 if (li == NULL)
19803 {
19804 vim_free(s);
19805 goto errret;
19806 }
19807 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019808 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019809 li->li_tv.vval.v_string = s;
19810 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019812
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019813 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019814 rettv->v_type = VAR_LIST;
19815 rettv->vval.v_list = list;
19816 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019818 else
19819 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019820 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019821#ifdef USE_CR
19822 /* translate <CR> into <NL> */
19823 if (res != NULL)
19824 {
19825 char_u *s;
19826
19827 for (s = res; *s; ++s)
19828 {
19829 if (*s == CAR)
19830 *s = NL;
19831 }
19832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833#else
19834# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019835 /* translate <CR><NL> into <NL> */
19836 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019838 char_u *s, *d;
19839
19840 d = res;
19841 for (s = res; *s; ++s)
19842 {
19843 if (s[0] == CAR && s[1] == NL)
19844 ++s;
19845 *d++ = *s;
19846 }
19847 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849# endif
19850#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019851 rettv->vval.v_string = res;
19852 res = NULL;
19853 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019854
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019855errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019856 if (infile != NULL)
19857 {
19858 mch_remove(infile);
19859 vim_free(infile);
19860 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019861 if (res != NULL)
19862 vim_free(res);
19863 if (list != NULL)
19864 list_free(list, TRUE);
19865}
19866
19867/*
19868 * "system()" function
19869 */
19870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019871f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019872{
19873 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19874}
19875
19876/*
19877 * "systemlist()" function
19878 */
19879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019880f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019881{
19882 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883}
19884
19885/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019886 * "tabpagebuflist()" function
19887 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019889f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019890{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019891#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019892 tabpage_T *tp;
19893 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019894
19895 if (argvars[0].v_type == VAR_UNKNOWN)
19896 wp = firstwin;
19897 else
19898 {
19899 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19900 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019901 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019902 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019903 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019904 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019905 for (; wp != NULL; wp = wp->w_next)
19906 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019907 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019908 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019909 }
19910#endif
19911}
19912
19913
19914/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019915 * "tabpagenr()" function
19916 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019918f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019919{
19920 int nr = 1;
19921#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019922 char_u *arg;
19923
19924 if (argvars[0].v_type != VAR_UNKNOWN)
19925 {
19926 arg = get_tv_string_chk(&argvars[0]);
19927 nr = 0;
19928 if (arg != NULL)
19929 {
19930 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019931 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019932 else
19933 EMSG2(_(e_invexpr2), arg);
19934 }
19935 }
19936 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019937 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019938#endif
19939 rettv->vval.v_number = nr;
19940}
19941
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019942
19943#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019944static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019945
19946/*
19947 * Common code for tabpagewinnr() and winnr().
19948 */
19949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019950get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019951{
19952 win_T *twin;
19953 int nr = 1;
19954 win_T *wp;
19955 char_u *arg;
19956
19957 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19958 if (argvar->v_type != VAR_UNKNOWN)
19959 {
19960 arg = get_tv_string_chk(argvar);
19961 if (arg == NULL)
19962 nr = 0; /* type error; errmsg already given */
19963 else if (STRCMP(arg, "$") == 0)
19964 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19965 else if (STRCMP(arg, "#") == 0)
19966 {
19967 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19968 if (twin == NULL)
19969 nr = 0;
19970 }
19971 else
19972 {
19973 EMSG2(_(e_invexpr2), arg);
19974 nr = 0;
19975 }
19976 }
19977
19978 if (nr > 0)
19979 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19980 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019981 {
19982 if (wp == NULL)
19983 {
19984 /* didn't find it in this tabpage */
19985 nr = 0;
19986 break;
19987 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019988 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019989 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019990 return nr;
19991}
19992#endif
19993
19994/*
19995 * "tabpagewinnr()" function
19996 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019998f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019999{
20000 int nr = 1;
20001#ifdef FEAT_WINDOWS
20002 tabpage_T *tp;
20003
20004 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20005 if (tp == NULL)
20006 nr = 0;
20007 else
20008 nr = get_winnr(tp, &argvars[1]);
20009#endif
20010 rettv->vval.v_number = nr;
20011}
20012
20013
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020014/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020015 * "tagfiles()" function
20016 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020018f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020019{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020020 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020021 tagname_T tn;
20022 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020023
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020024 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020025 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020026 fname = alloc(MAXPATHL);
20027 if (fname == NULL)
20028 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020029
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020030 for (first = TRUE; ; first = FALSE)
20031 if (get_tagfname(&tn, first, fname) == FAIL
20032 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020033 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020034 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020035 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020036}
20037
20038/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020039 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020040 */
20041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020042f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020043{
20044 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020045
20046 tag_pattern = get_tv_string(&argvars[0]);
20047
20048 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020049 if (*tag_pattern == NUL)
20050 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020051
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020052 if (rettv_list_alloc(rettv) == OK)
20053 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020054}
20055
20056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 * "tempname()" function
20058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020060f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061{
20062 static int x = 'A';
20063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020064 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020065 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066
20067 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20068 * names. Skip 'I' and 'O', they are used for shell redirection. */
20069 do
20070 {
20071 if (x == 'Z')
20072 x = '0';
20073 else if (x == '9')
20074 x = 'A';
20075 else
20076 {
20077#ifdef EBCDIC
20078 if (x == 'I')
20079 x = 'J';
20080 else if (x == 'R')
20081 x = 'S';
20082 else
20083#endif
20084 ++x;
20085 }
20086 } while (x == 'I' || x == 'O');
20087}
20088
20089/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020090 * "test(list)" function: Just checking the walls...
20091 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020093f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020094{
20095 /* Used for unit testing. Change the code below to your liking. */
20096#if 0
20097 listitem_T *li;
20098 list_T *l;
20099 char_u *bad, *good;
20100
20101 if (argvars[0].v_type != VAR_LIST)
20102 return;
20103 l = argvars[0].vval.v_list;
20104 if (l == NULL)
20105 return;
20106 li = l->lv_first;
20107 if (li == NULL)
20108 return;
20109 bad = get_tv_string(&li->li_tv);
20110 li = li->li_next;
20111 if (li == NULL)
20112 return;
20113 good = get_tv_string(&li->li_tv);
20114 rettv->vval.v_number = test_edit_score(bad, good);
20115#endif
20116}
20117
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020118#ifdef FEAT_FLOAT
20119/*
20120 * "tan()" function
20121 */
20122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020123f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020124{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020125 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020126
20127 rettv->v_type = VAR_FLOAT;
20128 if (get_float_arg(argvars, &f) == OK)
20129 rettv->vval.v_float = tan(f);
20130 else
20131 rettv->vval.v_float = 0.0;
20132}
20133
20134/*
20135 * "tanh()" function
20136 */
20137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020138f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020139{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020140 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020141
20142 rettv->v_type = VAR_FLOAT;
20143 if (get_float_arg(argvars, &f) == OK)
20144 rettv->vval.v_float = tanh(f);
20145 else
20146 rettv->vval.v_float = 0.0;
20147}
20148#endif
20149
Bram Moolenaar975b5272016-03-15 23:10:59 +010020150#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20151/*
20152 * Get a callback from "arg". It can be a Funcref or a function name.
20153 * When "arg" is zero return an empty string.
20154 * Return NULL for an invalid argument.
20155 */
20156 char_u *
20157get_callback(typval_T *arg, partial_T **pp)
20158{
20159 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20160 {
20161 *pp = arg->vval.v_partial;
20162 return (*pp)->pt_name;
20163 }
20164 *pp = NULL;
20165 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20166 return arg->vval.v_string;
20167 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20168 return (char_u *)"";
20169 EMSG(_("E921: Invalid callback argument"));
20170 return NULL;
20171}
20172#endif
20173
20174#ifdef FEAT_TIMERS
20175/*
20176 * "timer_start(time, callback [, options])" function
20177 */
20178 static void
20179f_timer_start(typval_T *argvars, typval_T *rettv)
20180{
20181 long msec = get_tv_number(&argvars[0]);
20182 timer_T *timer;
20183 int repeat = 0;
20184 char_u *callback;
20185 dict_T *dict;
20186
20187 if (argvars[2].v_type != VAR_UNKNOWN)
20188 {
20189 if (argvars[2].v_type != VAR_DICT
20190 || (dict = argvars[2].vval.v_dict) == NULL)
20191 {
20192 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20193 return;
20194 }
20195 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20196 repeat = get_dict_number(dict, (char_u *)"repeat");
20197 }
20198
20199 timer = create_timer(msec, repeat);
20200 callback = get_callback(&argvars[1], &timer->tr_partial);
20201 if (callback == NULL)
20202 {
20203 stop_timer(timer);
20204 rettv->vval.v_number = -1;
20205 }
20206 else
20207 {
20208 timer->tr_callback = vim_strsave(callback);
20209 rettv->vval.v_number = timer->tr_id;
20210 }
20211}
20212
20213/*
20214 * "timer_stop(timer)" function
20215 */
20216 static void
20217f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20218{
20219 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20220
20221 if (timer != NULL)
20222 stop_timer(timer);
20223}
20224#endif
20225
Bram Moolenaard52d9742005-08-21 22:20:28 +000020226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 * "tolower(string)" function
20228 */
20229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020230f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231{
20232 char_u *p;
20233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234 p = vim_strsave(get_tv_string(&argvars[0]));
20235 rettv->v_type = VAR_STRING;
20236 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237
20238 if (p != NULL)
20239 while (*p != NUL)
20240 {
20241#ifdef FEAT_MBYTE
20242 int l;
20243
20244 if (enc_utf8)
20245 {
20246 int c, lc;
20247
20248 c = utf_ptr2char(p);
20249 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020250 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 /* TODO: reallocate string when byte count changes. */
20252 if (utf_char2len(lc) == l)
20253 utf_char2bytes(lc, p);
20254 p += l;
20255 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020256 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 p += l; /* skip multi-byte character */
20258 else
20259#endif
20260 {
20261 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20262 ++p;
20263 }
20264 }
20265}
20266
20267/*
20268 * "toupper(string)" function
20269 */
20270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020271f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020273 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020274 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275}
20276
20277/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020278 * "tr(string, fromstr, tostr)" function
20279 */
20280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020281f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020282{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020283 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020284 char_u *fromstr;
20285 char_u *tostr;
20286 char_u *p;
20287#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020288 int inlen;
20289 int fromlen;
20290 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020291 int idx;
20292 char_u *cpstr;
20293 int cplen;
20294 int first = TRUE;
20295#endif
20296 char_u buf[NUMBUFLEN];
20297 char_u buf2[NUMBUFLEN];
20298 garray_T ga;
20299
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020300 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020301 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20302 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020303
20304 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020305 rettv->v_type = VAR_STRING;
20306 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020307 if (fromstr == NULL || tostr == NULL)
20308 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020309 ga_init2(&ga, (int)sizeof(char), 80);
20310
20311#ifdef FEAT_MBYTE
20312 if (!has_mbyte)
20313#endif
20314 /* not multi-byte: fromstr and tostr must be the same length */
20315 if (STRLEN(fromstr) != STRLEN(tostr))
20316 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020317#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020318error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020319#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020320 EMSG2(_(e_invarg2), fromstr);
20321 ga_clear(&ga);
20322 return;
20323 }
20324
20325 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020326 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020327 {
20328#ifdef FEAT_MBYTE
20329 if (has_mbyte)
20330 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020331 inlen = (*mb_ptr2len)(in_str);
20332 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020333 cplen = inlen;
20334 idx = 0;
20335 for (p = fromstr; *p != NUL; p += fromlen)
20336 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020337 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020338 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020339 {
20340 for (p = tostr; *p != NUL; p += tolen)
20341 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020342 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020343 if (idx-- == 0)
20344 {
20345 cplen = tolen;
20346 cpstr = p;
20347 break;
20348 }
20349 }
20350 if (*p == NUL) /* tostr is shorter than fromstr */
20351 goto error;
20352 break;
20353 }
20354 ++idx;
20355 }
20356
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020357 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020358 {
20359 /* Check that fromstr and tostr have the same number of
20360 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020361 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020362 first = FALSE;
20363 for (p = tostr; *p != NUL; p += tolen)
20364 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020365 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020366 --idx;
20367 }
20368 if (idx != 0)
20369 goto error;
20370 }
20371
Bram Moolenaarcde88542015-08-11 19:14:00 +020020372 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020373 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020374 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020375
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020376 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020377 }
20378 else
20379#endif
20380 {
20381 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020382 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020383 if (p != NULL)
20384 ga_append(&ga, tostr[p - fromstr]);
20385 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020386 ga_append(&ga, *in_str);
20387 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020388 }
20389 }
20390
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020391 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020392 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020393 ga_append(&ga, NUL);
20394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020395 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020396}
20397
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020398#ifdef FEAT_FLOAT
20399/*
20400 * "trunc({float})" function
20401 */
20402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020403f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020404{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020405 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020406
20407 rettv->v_type = VAR_FLOAT;
20408 if (get_float_arg(argvars, &f) == OK)
20409 /* trunc() is not in C90, use floor() or ceil() instead. */
20410 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20411 else
20412 rettv->vval.v_float = 0.0;
20413}
20414#endif
20415
Bram Moolenaar8299df92004-07-10 09:47:34 +000020416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 * "type(expr)" function
20418 */
20419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020420f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020422 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020423
20424 switch (argvars[0].v_type)
20425 {
20426 case VAR_NUMBER: n = 0; break;
20427 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020428 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020429 case VAR_FUNC: n = 2; break;
20430 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020431 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020432 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020433 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020434 if (argvars[0].vval.v_number == VVAL_FALSE
20435 || argvars[0].vval.v_number == VVAL_TRUE)
20436 n = 6;
20437 else
20438 n = 7;
20439 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020440 case VAR_JOB: n = 8; break;
20441 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020442 case VAR_UNKNOWN:
20443 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20444 n = -1;
20445 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020446 }
20447 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448}
20449
20450/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020451 * "undofile(name)" function
20452 */
20453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020454f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020455{
20456 rettv->v_type = VAR_STRING;
20457#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020458 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020459 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020460
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020461 if (*fname == NUL)
20462 {
20463 /* If there is no file name there will be no undo file. */
20464 rettv->vval.v_string = NULL;
20465 }
20466 else
20467 {
20468 char_u *ffname = FullName_save(fname, FALSE);
20469
20470 if (ffname != NULL)
20471 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20472 vim_free(ffname);
20473 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020474 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020475#else
20476 rettv->vval.v_string = NULL;
20477#endif
20478}
20479
20480/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020481 * "undotree()" function
20482 */
20483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020484f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020485{
20486 if (rettv_dict_alloc(rettv) == OK)
20487 {
20488 dict_T *dict = rettv->vval.v_dict;
20489 list_T *list;
20490
Bram Moolenaar730cde92010-06-27 05:18:54 +020020491 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020492 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020493 dict_add_nr_str(dict, "save_last",
20494 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020495 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20496 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020497 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020498
20499 list = list_alloc();
20500 if (list != NULL)
20501 {
20502 u_eval_tree(curbuf->b_u_oldhead, list);
20503 dict_add_list(dict, "entries", list);
20504 }
20505 }
20506}
20507
20508/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020509 * "values(dict)" function
20510 */
20511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020512f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020513{
20514 dict_list(argvars, rettv, 1);
20515}
20516
20517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 * "virtcol(string)" function
20519 */
20520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020521f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522{
20523 colnr_T vcol = 0;
20524 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020525 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020527 fp = var2fpos(&argvars[0], FALSE, &fnum);
20528 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20529 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 {
20531 getvvcol(curwin, fp, NULL, NULL, &vcol);
20532 ++vcol;
20533 }
20534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020535 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536}
20537
20538/*
20539 * "visualmode()" function
20540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020542f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544 char_u str[2];
20545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020546 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547 str[0] = curbuf->b_visual_mode_eval;
20548 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020549 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550
20551 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020552 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554}
20555
20556/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020557 * "wildmenumode()" function
20558 */
20559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020560f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020561{
20562#ifdef FEAT_WILDMENU
20563 if (wild_menu_showing)
20564 rettv->vval.v_number = 1;
20565#endif
20566}
20567
20568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 * "winbufnr(nr)" function
20570 */
20571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020572f_winbufnr(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
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020580 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581}
20582
20583/*
20584 * "wincol()" function
20585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020587f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588{
20589 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020590 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591}
20592
20593/*
20594 * "winheight(nr)" function
20595 */
20596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020597f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598{
20599 win_T *wp;
20600
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020601 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020603 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020605 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606}
20607
20608/*
20609 * "winline()" function
20610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020612f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613{
20614 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020615 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616}
20617
20618/*
20619 * "winnr()" function
20620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020622f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623{
20624 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020625
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020627 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020629 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630}
20631
20632/*
20633 * "winrestcmd()" function
20634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020636f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637{
20638#ifdef FEAT_WINDOWS
20639 win_T *wp;
20640 int winnr = 1;
20641 garray_T ga;
20642 char_u buf[50];
20643
20644 ga_init2(&ga, (int)sizeof(char), 70);
20645 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20646 {
20647 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20648 ga_concat(&ga, buf);
20649# ifdef FEAT_VERTSPLIT
20650 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20651 ga_concat(&ga, buf);
20652# endif
20653 ++winnr;
20654 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020655 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020657 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020659 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020661 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662}
20663
20664/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020665 * "winrestview()" function
20666 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020668f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020669{
20670 dict_T *dict;
20671
20672 if (argvars[0].v_type != VAR_DICT
20673 || (dict = argvars[0].vval.v_dict) == NULL)
20674 EMSG(_(e_invarg));
20675 else
20676 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020677 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20678 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20679 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20680 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020681#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020682 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20683 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020684#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020685 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20686 {
20687 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20688 curwin->w_set_curswant = FALSE;
20689 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020690
Bram Moolenaar82c25852014-05-28 16:47:16 +020020691 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20692 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020693#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020694 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20695 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020696#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020697 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20698 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20699 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20700 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020701
20702 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020703 win_new_height(curwin, curwin->w_height);
20704# ifdef FEAT_VERTSPLIT
20705 win_new_width(curwin, W_WIDTH(curwin));
20706# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020707 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020708
Bram Moolenaarb851a962014-10-31 15:45:52 +010020709 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020710 curwin->w_topline = 1;
20711 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20712 curwin->w_topline = curbuf->b_ml.ml_line_count;
20713#ifdef FEAT_DIFF
20714 check_topfill(curwin, TRUE);
20715#endif
20716 }
20717}
20718
20719/*
20720 * "winsaveview()" function
20721 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020723f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020724{
20725 dict_T *dict;
20726
Bram Moolenaara800b422010-06-27 01:15:55 +020020727 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020728 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020729 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020730
20731 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20732 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20733#ifdef FEAT_VIRTUALEDIT
20734 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20735#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020736 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020737 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20738
20739 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20740#ifdef FEAT_DIFF
20741 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20742#endif
20743 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20744 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20745}
20746
20747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 * "winwidth(nr)" function
20749 */
20750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020751f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752{
20753 win_T *wp;
20754
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020755 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020757 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 else
20759#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020760 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020762 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763#endif
20764}
20765
Bram Moolenaar071d4272004-06-13 20:20:40 +000020766/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020767 * "wordcount()" function
20768 */
20769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020770f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020771{
20772 if (rettv_dict_alloc(rettv) == FAIL)
20773 return;
20774 cursor_pos_info(rettv->vval.v_dict);
20775}
20776
20777/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020778 * Write list of strings to file
20779 */
20780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020781write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020782{
20783 listitem_T *li;
20784 int c;
20785 int ret = OK;
20786 char_u *s;
20787
20788 for (li = list->lv_first; li != NULL; li = li->li_next)
20789 {
20790 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20791 {
20792 if (*s == '\n')
20793 c = putc(NUL, fd);
20794 else
20795 c = putc(*s, fd);
20796 if (c == EOF)
20797 {
20798 ret = FAIL;
20799 break;
20800 }
20801 }
20802 if (!binary || li->li_next != NULL)
20803 if (putc('\n', fd) == EOF)
20804 {
20805 ret = FAIL;
20806 break;
20807 }
20808 if (ret == FAIL)
20809 {
20810 EMSG(_(e_write));
20811 break;
20812 }
20813 }
20814 return ret;
20815}
20816
20817/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020818 * "writefile()" function
20819 */
20820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020821f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020822{
20823 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020824 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020825 char_u *fname;
20826 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020827 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020828
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020829 if (check_restricted() || check_secure())
20830 return;
20831
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020832 if (argvars[0].v_type != VAR_LIST)
20833 {
20834 EMSG2(_(e_listarg), "writefile()");
20835 return;
20836 }
20837 if (argvars[0].vval.v_list == NULL)
20838 return;
20839
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020840 if (argvars[2].v_type != VAR_UNKNOWN)
20841 {
20842 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20843 binary = TRUE;
20844 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20845 append = TRUE;
20846 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020847
20848 /* Always open the file in binary mode, library functions have a mind of
20849 * their own about CR-LF conversion. */
20850 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020851 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20852 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020853 {
20854 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20855 ret = -1;
20856 }
20857 else
20858 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020859 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20860 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020861 fclose(fd);
20862 }
20863
20864 rettv->vval.v_number = ret;
20865}
20866
20867/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020868 * "xor(expr, expr)" function
20869 */
20870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020871f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020872{
20873 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20874 ^ get_tv_number_chk(&argvars[1], NULL);
20875}
20876
20877
20878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020880 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 */
20882 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020883var2fpos(
20884 typval_T *varp,
20885 int dollar_lnum, /* TRUE when $ is last line */
20886 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020888 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020890 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891
Bram Moolenaara5525202006-03-02 22:52:09 +000020892 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020893 if (varp->v_type == VAR_LIST)
20894 {
20895 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020896 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020897 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020898 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020899
20900 l = varp->vval.v_list;
20901 if (l == NULL)
20902 return NULL;
20903
20904 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020905 pos.lnum = list_find_nr(l, 0L, &error);
20906 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020907 return NULL; /* invalid line number */
20908
20909 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020910 pos.col = list_find_nr(l, 1L, &error);
20911 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020912 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020913 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020914
20915 /* We accept "$" for the column number: last column. */
20916 li = list_find(l, 1L);
20917 if (li != NULL && li->li_tv.v_type == VAR_STRING
20918 && li->li_tv.vval.v_string != NULL
20919 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20920 pos.col = len + 1;
20921
Bram Moolenaara5525202006-03-02 22:52:09 +000020922 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020923 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020924 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020925 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020926
Bram Moolenaara5525202006-03-02 22:52:09 +000020927#ifdef FEAT_VIRTUALEDIT
20928 /* Get the virtual offset. Defaults to zero. */
20929 pos.coladd = list_find_nr(l, 2L, &error);
20930 if (error)
20931 pos.coladd = 0;
20932#endif
20933
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020934 return &pos;
20935 }
20936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020937 name = get_tv_string_chk(varp);
20938 if (name == NULL)
20939 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020940 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020942 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20943 {
20944 if (VIsual_active)
20945 return &VIsual;
20946 return &curwin->w_cursor;
20947 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020948 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020950 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20952 return NULL;
20953 return pp;
20954 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020955
20956#ifdef FEAT_VIRTUALEDIT
20957 pos.coladd = 0;
20958#endif
20959
Bram Moolenaar477933c2007-07-17 14:32:23 +000020960 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020961 {
20962 pos.col = 0;
20963 if (name[1] == '0') /* "w0": first visible line */
20964 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020965 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020966 pos.lnum = curwin->w_topline;
20967 return &pos;
20968 }
20969 else if (name[1] == '$') /* "w$": last visible line */
20970 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020971 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020972 pos.lnum = curwin->w_botline - 1;
20973 return &pos;
20974 }
20975 }
20976 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020978 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 {
20980 pos.lnum = curbuf->b_ml.ml_line_count;
20981 pos.col = 0;
20982 }
20983 else
20984 {
20985 pos.lnum = curwin->w_cursor.lnum;
20986 pos.col = (colnr_T)STRLEN(ml_get_curline());
20987 }
20988 return &pos;
20989 }
20990 return NULL;
20991}
20992
20993/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020994 * Convert list in "arg" into a position and optional file number.
20995 * When "fnump" is NULL there is no file number, only 3 items.
20996 * Note that the column is passed on as-is, the caller may want to decrement
20997 * it to use 1 for the first column.
20998 * Return FAIL when conversion is not possible, doesn't check the position for
20999 * validity.
21000 */
21001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021002list2fpos(
21003 typval_T *arg,
21004 pos_T *posp,
21005 int *fnump,
21006 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021007{
21008 list_T *l = arg->vval.v_list;
21009 long i = 0;
21010 long n;
21011
Bram Moolenaar493c1782014-05-28 14:34:46 +020021012 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21013 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021014 if (arg->v_type != VAR_LIST
21015 || l == NULL
21016 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021017 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021018 return FAIL;
21019
21020 if (fnump != NULL)
21021 {
21022 n = list_find_nr(l, i++, NULL); /* fnum */
21023 if (n < 0)
21024 return FAIL;
21025 if (n == 0)
21026 n = curbuf->b_fnum; /* current buffer */
21027 *fnump = n;
21028 }
21029
21030 n = list_find_nr(l, i++, NULL); /* lnum */
21031 if (n < 0)
21032 return FAIL;
21033 posp->lnum = n;
21034
21035 n = list_find_nr(l, i++, NULL); /* col */
21036 if (n < 0)
21037 return FAIL;
21038 posp->col = n;
21039
21040#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021041 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021042 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021043 posp->coladd = 0;
21044 else
21045 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021046#endif
21047
Bram Moolenaar493c1782014-05-28 14:34:46 +020021048 if (curswantp != NULL)
21049 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21050
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021051 return OK;
21052}
21053
21054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055 * Get the length of an environment variable name.
21056 * Advance "arg" to the first character after the name.
21057 * Return 0 for error.
21058 */
21059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021060get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061{
21062 char_u *p;
21063 int len;
21064
21065 for (p = *arg; vim_isIDc(*p); ++p)
21066 ;
21067 if (p == *arg) /* no name found */
21068 return 0;
21069
21070 len = (int)(p - *arg);
21071 *arg = p;
21072 return len;
21073}
21074
21075/*
21076 * Get the length of the name of a function or internal variable.
21077 * "arg" is advanced to the first non-white character after the name.
21078 * Return 0 if something is wrong.
21079 */
21080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021081get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082{
21083 char_u *p;
21084 int len;
21085
21086 /* Find the end of the name. */
21087 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021088 {
21089 if (*p == ':')
21090 {
21091 /* "s:" is start of "s:var", but "n:" is not and can be used in
21092 * slice "[n:]". Also "xx:" is not a namespace. */
21093 len = (int)(p - *arg);
21094 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21095 || len > 1)
21096 break;
21097 }
21098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 if (p == *arg) /* no name found */
21100 return 0;
21101
21102 len = (int)(p - *arg);
21103 *arg = skipwhite(p);
21104
21105 return len;
21106}
21107
21108/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021109 * Get the length of the name of a variable or function.
21110 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021112 * Return -1 if curly braces expansion failed.
21113 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 * If the name contains 'magic' {}'s, expand them and return the
21115 * expanded name in an allocated string via 'alias' - caller must free.
21116 */
21117 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021118get_name_len(
21119 char_u **arg,
21120 char_u **alias,
21121 int evaluate,
21122 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123{
21124 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 char_u *p;
21126 char_u *expr_start;
21127 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128
21129 *alias = NULL; /* default to no alias */
21130
21131 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21132 && (*arg)[2] == (int)KE_SNR)
21133 {
21134 /* hard coded <SNR>, already translated */
21135 *arg += 3;
21136 return get_id_len(arg) + 3;
21137 }
21138 len = eval_fname_script(*arg);
21139 if (len > 0)
21140 {
21141 /* literal "<SID>", "s:" or "<SNR>" */
21142 *arg += len;
21143 }
21144
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021146 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021148 p = find_name_end(*arg, &expr_start, &expr_end,
21149 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 if (expr_start != NULL)
21151 {
21152 char_u *temp_string;
21153
21154 if (!evaluate)
21155 {
21156 len += (int)(p - *arg);
21157 *arg = skipwhite(p);
21158 return len;
21159 }
21160
21161 /*
21162 * Include any <SID> etc in the expanded string:
21163 * Thus the -len here.
21164 */
21165 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21166 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021167 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 *alias = temp_string;
21169 *arg = skipwhite(p);
21170 return (int)STRLEN(temp_string);
21171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172
21173 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021174 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 EMSG2(_(e_invexpr2), *arg);
21176
21177 return len;
21178}
21179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021180/*
21181 * Find the end of a variable or function name, taking care of magic braces.
21182 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21183 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021184 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021185 * Return a pointer to just after the name. Equal to "arg" if there is no
21186 * valid name.
21187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021189find_name_end(
21190 char_u *arg,
21191 char_u **expr_start,
21192 char_u **expr_end,
21193 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021195 int mb_nest = 0;
21196 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021198 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021200 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021202 *expr_start = NULL;
21203 *expr_end = NULL;
21204 }
21205
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021206 /* Quick check for valid starting character. */
21207 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21208 return arg;
21209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021210 for (p = arg; *p != NUL
21211 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021212 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021213 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021214 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021215 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021216 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021217 if (*p == '\'')
21218 {
21219 /* skip over 'string' to avoid counting [ and ] inside it. */
21220 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21221 ;
21222 if (*p == NUL)
21223 break;
21224 }
21225 else if (*p == '"')
21226 {
21227 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21228 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21229 if (*p == '\\' && p[1] != NUL)
21230 ++p;
21231 if (*p == NUL)
21232 break;
21233 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021234 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21235 {
21236 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021237 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021238 len = (int)(p - arg);
21239 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021240 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021241 break;
21242 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021244 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021246 if (*p == '[')
21247 ++br_nest;
21248 else if (*p == ']')
21249 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021252 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021254 if (*p == '{')
21255 {
21256 mb_nest++;
21257 if (expr_start != NULL && *expr_start == NULL)
21258 *expr_start = p;
21259 }
21260 else if (*p == '}')
21261 {
21262 mb_nest--;
21263 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21264 *expr_end = p;
21265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 }
21268
21269 return p;
21270}
21271
21272/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021273 * Expands out the 'magic' {}'s in a variable/function name.
21274 * Note that this can call itself recursively, to deal with
21275 * constructs like foo{bar}{baz}{bam}
21276 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21277 * "in_start" ^
21278 * "expr_start" ^
21279 * "expr_end" ^
21280 * "in_end" ^
21281 *
21282 * Returns a new allocated string, which the caller must free.
21283 * Returns NULL for failure.
21284 */
21285 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021286make_expanded_name(
21287 char_u *in_start,
21288 char_u *expr_start,
21289 char_u *expr_end,
21290 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021291{
21292 char_u c1;
21293 char_u *retval = NULL;
21294 char_u *temp_result;
21295 char_u *nextcmd = NULL;
21296
21297 if (expr_end == NULL || in_end == NULL)
21298 return NULL;
21299 *expr_start = NUL;
21300 *expr_end = NUL;
21301 c1 = *in_end;
21302 *in_end = NUL;
21303
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021304 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021305 if (temp_result != NULL && nextcmd == NULL)
21306 {
21307 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21308 + (in_end - expr_end) + 1));
21309 if (retval != NULL)
21310 {
21311 STRCPY(retval, in_start);
21312 STRCAT(retval, temp_result);
21313 STRCAT(retval, expr_end + 1);
21314 }
21315 }
21316 vim_free(temp_result);
21317
21318 *in_end = c1; /* put char back for error messages */
21319 *expr_start = '{';
21320 *expr_end = '}';
21321
21322 if (retval != NULL)
21323 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021324 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021325 if (expr_start != NULL)
21326 {
21327 /* Further expansion! */
21328 temp_result = make_expanded_name(retval, expr_start,
21329 expr_end, temp_result);
21330 vim_free(retval);
21331 retval = temp_result;
21332 }
21333 }
21334
21335 return retval;
21336}
21337
21338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021340 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 */
21342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021343eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021345 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21346}
21347
21348/*
21349 * Return TRUE if character "c" can be used as the first character in a
21350 * variable or function name (excluding '{' and '}').
21351 */
21352 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021353eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021354{
21355 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356}
21357
21358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 * Set number v: variable to "val".
21360 */
21361 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021362set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021364 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365}
21366
21367/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021368 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 */
21370 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021371get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021373 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374}
21375
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021376/*
21377 * Get string v: variable value. Uses a static buffer, can only be used once.
21378 */
21379 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021380get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021381{
21382 return get_tv_string(&vimvars[idx].vv_tv);
21383}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021384
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021386 * Get List v: variable value. Caller must take care of reference count when
21387 * needed.
21388 */
21389 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021390get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021391{
21392 return vimvars[idx].vv_list;
21393}
21394
21395/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021396 * Set v:char to character "c".
21397 */
21398 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021399set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021400{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021401 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021402
21403#ifdef FEAT_MBYTE
21404 if (has_mbyte)
21405 buf[(*mb_char2bytes)(c, buf)] = NUL;
21406 else
21407#endif
21408 {
21409 buf[0] = c;
21410 buf[1] = NUL;
21411 }
21412 set_vim_var_string(VV_CHAR, buf, -1);
21413}
21414
21415/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021416 * Set v:count to "count" and v:count1 to "count1".
21417 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 */
21419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021420set_vcount(
21421 long count,
21422 long count1,
21423 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021425 if (set_prevcount)
21426 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021427 vimvars[VV_COUNT].vv_nr = count;
21428 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429}
21430
21431/*
21432 * Set string v: variable to a copy of "val".
21433 */
21434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021435set_vim_var_string(
21436 int idx,
21437 char_u *val,
21438 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439{
Bram Moolenaara542c682016-01-31 16:28:04 +010021440 clear_tv(&vimvars[idx].vv_di.di_tv);
21441 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021443 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021445 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021447 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448}
21449
21450/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021451 * Set List v: variable to "val".
21452 */
21453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021454set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021455{
Bram Moolenaara542c682016-01-31 16:28:04 +010021456 clear_tv(&vimvars[idx].vv_di.di_tv);
21457 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021458 vimvars[idx].vv_list = val;
21459 if (val != NULL)
21460 ++val->lv_refcount;
21461}
21462
21463/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021464 * Set Dictionary v: variable to "val".
21465 */
21466 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021467set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021468{
21469 int todo;
21470 hashitem_T *hi;
21471
Bram Moolenaara542c682016-01-31 16:28:04 +010021472 clear_tv(&vimvars[idx].vv_di.di_tv);
21473 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021474 vimvars[idx].vv_dict = val;
21475 if (val != NULL)
21476 {
21477 ++val->dv_refcount;
21478
21479 /* Set readonly */
21480 todo = (int)val->dv_hashtab.ht_used;
21481 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21482 {
21483 if (HASHITEM_EMPTY(hi))
21484 continue;
21485 --todo;
21486 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21487 }
21488 }
21489}
21490
21491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 * Set v:register if needed.
21493 */
21494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021495set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496{
21497 char_u regname;
21498
21499 if (c == 0 || c == ' ')
21500 regname = '"';
21501 else
21502 regname = c;
21503 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021504 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505 set_vim_var_string(VV_REG, &regname, 1);
21506}
21507
21508/*
21509 * Get or set v:exception. If "oldval" == NULL, return the current value.
21510 * Otherwise, restore the value to "oldval" and return NULL.
21511 * Must always be called in pairs to save and restore v:exception! Does not
21512 * take care of memory allocations.
21513 */
21514 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021515v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516{
21517 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021518 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519
Bram Moolenaare9a41262005-01-15 22:18:47 +000021520 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521 return NULL;
21522}
21523
21524/*
21525 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21526 * Otherwise, restore the value to "oldval" and return NULL.
21527 * Must always be called in pairs to save and restore v:throwpoint! Does not
21528 * take care of memory allocations.
21529 */
21530 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021531v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532{
21533 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021534 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535
Bram Moolenaare9a41262005-01-15 22:18:47 +000021536 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 return NULL;
21538}
21539
21540#if defined(FEAT_AUTOCMD) || defined(PROTO)
21541/*
21542 * Set v:cmdarg.
21543 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21544 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21545 * Must always be called in pairs!
21546 */
21547 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021548set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549{
21550 char_u *oldval;
21551 char_u *newval;
21552 unsigned len;
21553
Bram Moolenaare9a41262005-01-15 22:18:47 +000021554 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021555 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021557 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021558 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021559 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 }
21561
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021562 if (eap->force_bin == FORCE_BIN)
21563 len = 6;
21564 else if (eap->force_bin == FORCE_NOBIN)
21565 len = 8;
21566 else
21567 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021568
21569 if (eap->read_edit)
21570 len += 7;
21571
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021572 if (eap->force_ff != 0)
21573 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21574# ifdef FEAT_MBYTE
21575 if (eap->force_enc != 0)
21576 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021577 if (eap->bad_char != 0)
21578 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021579# endif
21580
21581 newval = alloc(len + 1);
21582 if (newval == NULL)
21583 return NULL;
21584
21585 if (eap->force_bin == FORCE_BIN)
21586 sprintf((char *)newval, " ++bin");
21587 else if (eap->force_bin == FORCE_NOBIN)
21588 sprintf((char *)newval, " ++nobin");
21589 else
21590 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021591
21592 if (eap->read_edit)
21593 STRCAT(newval, " ++edit");
21594
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021595 if (eap->force_ff != 0)
21596 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21597 eap->cmd + eap->force_ff);
21598# ifdef FEAT_MBYTE
21599 if (eap->force_enc != 0)
21600 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21601 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021602 if (eap->bad_char == BAD_KEEP)
21603 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21604 else if (eap->bad_char == BAD_DROP)
21605 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21606 else if (eap->bad_char != 0)
21607 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021608# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021609 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021610 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611}
21612#endif
21613
21614/*
21615 * Get the value of internal variable "name".
21616 * Return OK or FAIL.
21617 */
21618 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021619get_var_tv(
21620 char_u *name,
21621 int len, /* length of "name" */
21622 typval_T *rettv, /* NULL when only checking existence */
21623 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21624 int verbose, /* may give error message */
21625 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626{
21627 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021628 typval_T *tv = NULL;
21629 typval_T atv;
21630 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632
21633 /* truncate the name, so that we can use strcmp() */
21634 cc = name[len];
21635 name[len] = NUL;
21636
21637 /*
21638 * Check for "b:changedtick".
21639 */
21640 if (STRCMP(name, "b:changedtick") == 0)
21641 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021642 atv.v_type = VAR_NUMBER;
21643 atv.vval.v_number = curbuf->b_changedtick;
21644 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 }
21646
21647 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 * Check for user-defined variables.
21649 */
21650 else
21651 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021652 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021654 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021655 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021656 if (dip != NULL)
21657 *dip = v;
21658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 }
21660
Bram Moolenaare9a41262005-01-15 22:18:47 +000021661 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021663 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021664 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 ret = FAIL;
21666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021667 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021668 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669
21670 name[len] = cc;
21671
21672 return ret;
21673}
21674
21675/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021676 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21677 * Also handle function call with Funcref variable: func(expr)
21678 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21679 */
21680 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021681handle_subscript(
21682 char_u **arg,
21683 typval_T *rettv,
21684 int evaluate, /* do more than finding the end */
21685 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021686{
21687 int ret = OK;
21688 dict_T *selfdict = NULL;
21689 char_u *s;
21690 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021691 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021692
21693 while (ret == OK
21694 && (**arg == '['
21695 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021696 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21697 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021698 && !vim_iswhite(*(*arg - 1)))
21699 {
21700 if (**arg == '(')
21701 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021702 partial_T *pt = NULL;
21703
Bram Moolenaard9fba312005-06-26 22:34:35 +000021704 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021705 if (evaluate)
21706 {
21707 functv = *rettv;
21708 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021709
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021710 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021711 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021712 {
21713 pt = functv.vval.v_partial;
21714 s = pt->pt_name;
21715 }
21716 else
21717 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021718 }
21719 else
21720 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021721 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021722 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021723 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021724
21725 /* Clear the funcref afterwards, so that deleting it while
21726 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021727 if (evaluate)
21728 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021729
21730 /* Stop the expression evaluation when immediately aborting on
21731 * error, or when an interrupt occurred or an exception was thrown
21732 * but not caught. */
21733 if (aborting())
21734 {
21735 if (ret == OK)
21736 clear_tv(rettv);
21737 ret = FAIL;
21738 }
21739 dict_unref(selfdict);
21740 selfdict = NULL;
21741 }
21742 else /* **arg == '[' || **arg == '.' */
21743 {
21744 dict_unref(selfdict);
21745 if (rettv->v_type == VAR_DICT)
21746 {
21747 selfdict = rettv->vval.v_dict;
21748 if (selfdict != NULL)
21749 ++selfdict->dv_refcount;
21750 }
21751 else
21752 selfdict = NULL;
21753 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21754 {
21755 clear_tv(rettv);
21756 ret = FAIL;
21757 }
21758 }
21759 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021760
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021761 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21762 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021763 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021764 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21765 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021766 char_u *tofree = NULL;
21767 ufunc_T *fp;
21768 char_u fname_buf[FLEN_FIXED + 1];
21769 int error;
21770
21771 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021772 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021773 fp = find_func(fname);
21774 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021775
21776 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021777 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021778 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021779 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21780
21781 if (pt != NULL)
21782 {
21783 pt->pt_refcount = 1;
21784 pt->pt_dict = selfdict;
21785 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021786 if (rettv->v_type == VAR_FUNC)
21787 {
21788 /* just a function: use selfdict */
21789 pt->pt_name = rettv->vval.v_string;
21790 }
21791 else
21792 {
21793 partial_T *ret_pt = rettv->vval.v_partial;
21794 int i;
21795
21796 /* partial: use selfdict and copy args */
21797 pt->pt_name = vim_strsave(ret_pt->pt_name);
21798 if (ret_pt->pt_argc > 0)
21799 {
21800 pt->pt_argv = (typval_T *)alloc(
21801 sizeof(typval_T) * ret_pt->pt_argc);
21802 if (pt->pt_argv == NULL)
21803 /* out of memory: drop the arguments */
21804 pt->pt_argc = 0;
21805 else
21806 {
21807 pt->pt_argc = ret_pt->pt_argc;
21808 for (i = 0; i < pt->pt_argc; i++)
21809 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21810 }
21811 }
21812 partial_unref(ret_pt);
21813 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021814 func_ref(pt->pt_name);
21815 rettv->v_type = VAR_PARTIAL;
21816 rettv->vval.v_partial = pt;
21817 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021818 }
21819 }
21820
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021821 dict_unref(selfdict);
21822 return ret;
21823}
21824
21825/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021826 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021827 * value).
21828 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021829 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021830alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021831{
Bram Moolenaar33570922005-01-25 22:26:29 +000021832 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021833}
21834
21835/*
21836 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 * The string "s" must have been allocated, it is consumed.
21838 * Return NULL for out of memory, the variable otherwise.
21839 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021840 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021841alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842{
Bram Moolenaar33570922005-01-25 22:26:29 +000021843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021845 rettv = alloc_tv();
21846 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021848 rettv->v_type = VAR_STRING;
21849 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 }
21851 else
21852 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021853 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854}
21855
21856/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021857 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021859 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021860free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861{
21862 if (varp != NULL)
21863 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021864 switch (varp->v_type)
21865 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021866 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021867 func_unref(varp->vval.v_string);
21868 /*FALLTHROUGH*/
21869 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021870 vim_free(varp->vval.v_string);
21871 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021872 case VAR_PARTIAL:
21873 partial_unref(varp->vval.v_partial);
21874 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021875 case VAR_LIST:
21876 list_unref(varp->vval.v_list);
21877 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021878 case VAR_DICT:
21879 dict_unref(varp->vval.v_dict);
21880 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021881 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021882#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021883 job_unref(varp->vval.v_job);
21884 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021885#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021886 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021887#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021888 channel_unref(varp->vval.v_channel);
21889 break;
21890#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021891 case VAR_NUMBER:
21892 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021893 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021894 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021895 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 vim_free(varp);
21898 }
21899}
21900
21901/*
21902 * Free the memory for a variable value and set the value to NULL or 0.
21903 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021905clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906{
21907 if (varp != NULL)
21908 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021909 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021911 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021912 func_unref(varp->vval.v_string);
21913 /*FALLTHROUGH*/
21914 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021915 vim_free(varp->vval.v_string);
21916 varp->vval.v_string = NULL;
21917 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021918 case VAR_PARTIAL:
21919 partial_unref(varp->vval.v_partial);
21920 varp->vval.v_partial = NULL;
21921 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021922 case VAR_LIST:
21923 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021924 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021925 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021926 case VAR_DICT:
21927 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021928 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021929 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021930 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021931 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021932 varp->vval.v_number = 0;
21933 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021934 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021935#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021936 varp->vval.v_float = 0.0;
21937 break;
21938#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021939 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021940#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021941 job_unref(varp->vval.v_job);
21942 varp->vval.v_job = NULL;
21943#endif
21944 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021945 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021946#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021947 channel_unref(varp->vval.v_channel);
21948 varp->vval.v_channel = NULL;
21949#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021950 case VAR_UNKNOWN:
21951 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021953 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954 }
21955}
21956
21957/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021958 * Set the value of a variable to NULL without freeing items.
21959 */
21960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021961init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021962{
21963 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021964 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021965}
21966
21967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 * Get the number value of a variable.
21969 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021970 * For incompatible types, return 0.
21971 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21972 * caller of incompatible types: it sets *denote to TRUE if "denote"
21973 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021975 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021976get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021978 int error = FALSE;
21979
21980 return get_tv_number_chk(varp, &error); /* return 0L on error */
21981}
21982
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021983 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021984get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021985{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021986 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021988 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021990 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021991 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021992 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021993#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021994 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021995 break;
21996#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021997 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021998 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021999 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022000 break;
22001 case VAR_STRING:
22002 if (varp->vval.v_string != NULL)
22003 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022004 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022005 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022006 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022007 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022008 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022009 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022010 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022011 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022012 case VAR_SPECIAL:
22013 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22014 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022015 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022016#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022017 EMSG(_("E910: Using a Job as a Number"));
22018 break;
22019#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022020 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022021#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022022 EMSG(_("E913: Using a Channel as a Number"));
22023 break;
22024#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022025 case VAR_UNKNOWN:
22026 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022027 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022029 if (denote == NULL) /* useful for values that must be unsigned */
22030 n = -1;
22031 else
22032 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022033 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034}
22035
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022036#ifdef FEAT_FLOAT
22037 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022038get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022039{
22040 switch (varp->v_type)
22041 {
22042 case VAR_NUMBER:
22043 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022044 case VAR_FLOAT:
22045 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022046 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022047 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022048 EMSG(_("E891: Using a Funcref as a Float"));
22049 break;
22050 case VAR_STRING:
22051 EMSG(_("E892: Using a String as a Float"));
22052 break;
22053 case VAR_LIST:
22054 EMSG(_("E893: Using a List as a Float"));
22055 break;
22056 case VAR_DICT:
22057 EMSG(_("E894: Using a Dictionary as a Float"));
22058 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022059 case VAR_SPECIAL:
22060 EMSG(_("E907: Using a special value as a Float"));
22061 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022062 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022063# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022064 EMSG(_("E911: Using a Job as a Float"));
22065 break;
22066# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022067 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022068# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022069 EMSG(_("E914: Using a Channel as a Float"));
22070 break;
22071# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022072 case VAR_UNKNOWN:
22073 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022074 break;
22075 }
22076 return 0;
22077}
22078#endif
22079
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022081 * Get the lnum from the first argument.
22082 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022083 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 */
22085 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022086get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087{
Bram Moolenaar33570922005-01-25 22:26:29 +000022088 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 linenr_T lnum;
22090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022091 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 if (lnum == 0) /* no valid number, try using line() */
22093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022094 rettv.v_type = VAR_NUMBER;
22095 f_line(argvars, &rettv);
22096 lnum = rettv.vval.v_number;
22097 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 }
22099 return lnum;
22100}
22101
22102/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022103 * Get the lnum from the first argument.
22104 * Also accepts "$", then "buf" is used.
22105 * Returns 0 on error.
22106 */
22107 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022108get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022109{
22110 if (argvars[0].v_type == VAR_STRING
22111 && argvars[0].vval.v_string != NULL
22112 && argvars[0].vval.v_string[0] == '$'
22113 && buf != NULL)
22114 return buf->b_ml.ml_line_count;
22115 return get_tv_number_chk(&argvars[0], NULL);
22116}
22117
22118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 * Get the string value of a variable.
22120 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022121 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22122 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 * If the String variable has never been set, return an empty string.
22124 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022125 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22126 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022128 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022129get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022130{
22131 static char_u mybuf[NUMBUFLEN];
22132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022133 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134}
22135
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022136 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022137get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022139 char_u *res = get_tv_string_buf_chk(varp, buf);
22140
22141 return res != NULL ? res : (char_u *)"";
22142}
22143
Bram Moolenaar7d647822014-04-05 21:28:56 +020022144/*
22145 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22146 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022147 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022148get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022149{
22150 static char_u mybuf[NUMBUFLEN];
22151
22152 return get_tv_string_buf_chk(varp, mybuf);
22153}
22154
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022155 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022156get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022157{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022158 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022160 case VAR_NUMBER:
22161 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22162 return buf;
22163 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022164 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022165 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022166 break;
22167 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022168 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022169 break;
22170 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022171 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022172 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022173 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022174#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022175 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022176 break;
22177#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022178 case VAR_STRING:
22179 if (varp->vval.v_string != NULL)
22180 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022181 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022182 case VAR_SPECIAL:
22183 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22184 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022185 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022186#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022187 {
22188 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022189 char *status;
22190
22191 if (job == NULL)
22192 return (char_u *)"no process";
22193 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022194 : job->jv_status == JOB_ENDED ? "dead"
22195 : "run";
22196# ifdef UNIX
22197 vim_snprintf((char *)buf, NUMBUFLEN,
22198 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022199# elif defined(WIN32)
22200 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022201 "process %ld %s",
22202 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022203 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022204# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022205 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022206 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22207# endif
22208 return buf;
22209 }
22210#endif
22211 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022212 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022213#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022214 {
22215 channel_T *channel = varp->vval.v_channel;
22216 char *status = channel_status(channel);
22217
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022218 if (channel == NULL)
22219 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22220 else
22221 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022222 "channel %d %s", channel->ch_id, status);
22223 return buf;
22224 }
22225#endif
22226 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022227 case VAR_UNKNOWN:
22228 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022231 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232}
22233
22234/*
22235 * Find variable "name" in the list of variables.
22236 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022237 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022238 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022239 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022241 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022242find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022245 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246
Bram Moolenaara7043832005-01-21 11:56:39 +000022247 ht = find_var_ht(name, &varname);
22248 if (htp != NULL)
22249 *htp = ht;
22250 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022252 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253}
22254
22255/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022256 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022257 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022259 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022260find_var_in_ht(
22261 hashtab_T *ht,
22262 int htname,
22263 char_u *varname,
22264 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022265{
Bram Moolenaar33570922005-01-25 22:26:29 +000022266 hashitem_T *hi;
22267
22268 if (*varname == NUL)
22269 {
22270 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022271 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022272 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022273 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022274 case 'g': return &globvars_var;
22275 case 'v': return &vimvars_var;
22276 case 'b': return &curbuf->b_bufvar;
22277 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022278#ifdef FEAT_WINDOWS
22279 case 't': return &curtab->tp_winvar;
22280#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022281 case 'l': return current_funccal == NULL
22282 ? NULL : &current_funccal->l_vars_var;
22283 case 'a': return current_funccal == NULL
22284 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022285 }
22286 return NULL;
22287 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022288
22289 hi = hash_find(ht, varname);
22290 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022291 {
22292 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022293 * worked find the variable again. Don't auto-load a script if it was
22294 * loaded already, otherwise it would be loaded every time when
22295 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022296 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022297 {
22298 /* Note: script_autoload() may make "hi" invalid. It must either
22299 * be obtained again or not used. */
22300 if (!script_autoload(varname, FALSE) || aborting())
22301 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022302 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022303 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022304 if (HASHITEM_EMPTY(hi))
22305 return NULL;
22306 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022307 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022308}
22309
22310/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022311 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022312 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022313 * Set "varname" to the start of name without ':'.
22314 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022315 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022316find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022318 hashitem_T *hi;
22319
Bram Moolenaar73627d02015-08-11 15:46:09 +020022320 if (name[0] == NUL)
22321 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322 if (name[1] != ':')
22323 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022324 /* The name must not start with a colon or #. */
22325 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 return NULL;
22327 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022328
22329 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022330 hi = hash_find(&compat_hashtab, name);
22331 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022332 return &compat_hashtab;
22333
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022335 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022336 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 }
22338 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022339 if (*name == 'g') /* global variable */
22340 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022341 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22342 */
22343 if (vim_strchr(name + 2, ':') != NULL
22344 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022345 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022347 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022349 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022350#ifdef FEAT_WINDOWS
22351 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022352 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022353#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022354 if (*name == 'v') /* v: variable */
22355 return &vimvarht;
22356 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022357 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022358 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022359 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360 if (*name == 's' /* script variable */
22361 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22362 return &SCRIPT_VARS(current_SID);
22363 return NULL;
22364}
22365
22366/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022367 * Get function call environment based on bactrace debug level
22368 */
22369 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022370get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022371{
22372 int i;
22373 funccall_T *funccal;
22374 funccall_T *temp_funccal;
22375
22376 funccal = current_funccal;
22377 if (debug_backtrace_level > 0)
22378 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022379 for (i = 0; i < debug_backtrace_level; i++)
22380 {
22381 temp_funccal = funccal->caller;
22382 if (temp_funccal)
22383 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022384 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022385 /* backtrace level overflow. reset to max */
22386 debug_backtrace_level = i;
22387 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022388 }
22389 return funccal;
22390}
22391
22392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022394 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 * Returns NULL when it doesn't exist.
22396 */
22397 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022398get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399{
Bram Moolenaar33570922005-01-25 22:26:29 +000022400 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022402 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 if (v == NULL)
22404 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022405 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406}
22407
22408/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022409 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 * sourcing this script and when executing functions defined in the script.
22411 */
22412 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022413new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414{
Bram Moolenaara7043832005-01-21 11:56:39 +000022415 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022416 hashtab_T *ht;
22417 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022418
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22420 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022421 /* Re-allocating ga_data means that an ht_array pointing to
22422 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022423 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022424 for (i = 1; i <= ga_scripts.ga_len; ++i)
22425 {
22426 ht = &SCRIPT_VARS(i);
22427 if (ht->ht_mask == HT_INIT_SIZE - 1)
22428 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022429 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022430 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022431 }
22432
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433 while (ga_scripts.ga_len < id)
22434 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022435 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022436 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022437 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 }
22440 }
22441}
22442
22443/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22445 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 */
22447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022448init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449{
Bram Moolenaar33570922005-01-25 22:26:29 +000022450 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022451 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022452 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022453 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022454 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022455 dict_var->di_tv.vval.v_dict = dict;
22456 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022457 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22459 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460}
22461
22462/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022463 * Unreference a dictionary initialized by init_var_dict().
22464 */
22465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022466unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022467{
22468 /* Now the dict needs to be freed if no one else is using it, go back to
22469 * normal reference counting. */
22470 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22471 dict_unref(dict);
22472}
22473
22474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022476 * Frees all allocated variables and the value they contain.
22477 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 */
22479 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022480vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022481{
22482 vars_clear_ext(ht, TRUE);
22483}
22484
22485/*
22486 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22487 */
22488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022489vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490{
Bram Moolenaara7043832005-01-21 11:56:39 +000022491 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 hashitem_T *hi;
22493 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494
Bram Moolenaar33570922005-01-25 22:26:29 +000022495 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022496 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022497 for (hi = ht->ht_array; todo > 0; ++hi)
22498 {
22499 if (!HASHITEM_EMPTY(hi))
22500 {
22501 --todo;
22502
Bram Moolenaar33570922005-01-25 22:26:29 +000022503 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022504 * ht_array might change then. hash_clear() takes care of it
22505 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022506 v = HI2DI(hi);
22507 if (free_val)
22508 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022509 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022510 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022511 }
22512 }
22513 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022514 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515}
22516
Bram Moolenaara7043832005-01-21 11:56:39 +000022517/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022518 * Delete a variable from hashtab "ht" at item "hi".
22519 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022522delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523{
Bram Moolenaar33570922005-01-25 22:26:29 +000022524 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022525
22526 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022527 clear_tv(&di->di_tv);
22528 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529}
22530
22531/*
22532 * List the value of one internal variable.
22533 */
22534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022535list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022537 char_u *tofree;
22538 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022539 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022540
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022541 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022542 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022543 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022544 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545}
22546
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022548list_one_var_a(
22549 char_u *prefix,
22550 char_u *name,
22551 int type,
22552 char_u *string,
22553 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554{
Bram Moolenaar31859182007-08-14 20:41:13 +000022555 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22556 msg_start();
22557 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 if (name != NULL) /* "a:" vars don't have a name stored */
22559 msg_puts(name);
22560 msg_putchar(' ');
22561 msg_advance(22);
22562 if (type == VAR_NUMBER)
22563 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022564 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022565 msg_putchar('*');
22566 else if (type == VAR_LIST)
22567 {
22568 msg_putchar('[');
22569 if (*string == '[')
22570 ++string;
22571 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022572 else if (type == VAR_DICT)
22573 {
22574 msg_putchar('{');
22575 if (*string == '{')
22576 ++string;
22577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 else
22579 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022580
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022582
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022583 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022584 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022585 if (*first)
22586 {
22587 msg_clr_eos();
22588 *first = FALSE;
22589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590}
22591
22592/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022593 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 * If the variable already exists, the value is updated.
22595 * Otherwise the variable is created.
22596 */
22597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022598set_var(
22599 char_u *name,
22600 typval_T *tv,
22601 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602{
Bram Moolenaar33570922005-01-25 22:26:29 +000022603 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022605 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022607 ht = find_var_ht(name, &varname);
22608 if (ht == NULL || *varname == NUL)
22609 {
22610 EMSG2(_(e_illvar), name);
22611 return;
22612 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022613 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022614
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022615 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22616 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022617 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022618
Bram Moolenaar33570922005-01-25 22:26:29 +000022619 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022621 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022622 if (var_check_ro(v->di_flags, name, FALSE)
22623 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022624 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022625
22626 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022627 * Handle setting internal v: variables separately where needed to
22628 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022629 */
22630 if (ht == &vimvarht)
22631 {
22632 if (v->di_tv.v_type == VAR_STRING)
22633 {
22634 vim_free(v->di_tv.vval.v_string);
22635 if (copy || tv->v_type != VAR_STRING)
22636 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22637 else
22638 {
22639 /* Take over the string to avoid an extra alloc/free. */
22640 v->di_tv.vval.v_string = tv->vval.v_string;
22641 tv->vval.v_string = NULL;
22642 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022643 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022644 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022645 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022646 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022647 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022648 if (STRCMP(varname, "searchforward") == 0)
22649 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022650#ifdef FEAT_SEARCH_EXTRA
22651 else if (STRCMP(varname, "hlsearch") == 0)
22652 {
22653 no_hlsearch = !v->di_tv.vval.v_number;
22654 redraw_all_later(SOME_VALID);
22655 }
22656#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022657 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022658 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022659 else if (v->di_tv.v_type != tv->v_type)
22660 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022661 }
22662
22663 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 }
22665 else /* add a new variable */
22666 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022667 /* Can't add "v:" variable. */
22668 if (ht == &vimvarht)
22669 {
22670 EMSG2(_(e_illvar), name);
22671 return;
22672 }
22673
Bram Moolenaar92124a32005-06-17 22:03:40 +000022674 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022675 if (!valid_varname(varname))
22676 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022677
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022678 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22679 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022680 if (v == NULL)
22681 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022682 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022683 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022685 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 return;
22687 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022688 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022690
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022691 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022692 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022693 else
22694 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022696 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022697 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699}
22700
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022701/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022702 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022703 * Also give an error message.
22704 */
22705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022706var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022707{
22708 if (flags & DI_FLAGS_RO)
22709 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022710 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022711 return TRUE;
22712 }
22713 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22714 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022715 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022716 return TRUE;
22717 }
22718 return FALSE;
22719}
22720
22721/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022722 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22723 * Also give an error message.
22724 */
22725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022726var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022727{
22728 if (flags & DI_FLAGS_FIX)
22729 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022730 EMSG2(_("E795: Cannot delete variable %s"),
22731 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022732 return TRUE;
22733 }
22734 return FALSE;
22735}
22736
22737/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022738 * Check if a funcref is assigned to a valid variable name.
22739 * Return TRUE and give an error if not.
22740 */
22741 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022742var_check_func_name(
22743 char_u *name, /* points to start of variable name */
22744 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022745{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022746 /* Allow for w: b: s: and t:. */
22747 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022748 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22749 ? name[2] : name[0]))
22750 {
22751 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22752 name);
22753 return TRUE;
22754 }
22755 /* Don't allow hiding a function. When "v" is not NULL we might be
22756 * assigning another function to the same var, the type is checked
22757 * below. */
22758 if (new_var && function_exists(name))
22759 {
22760 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22761 name);
22762 return TRUE;
22763 }
22764 return FALSE;
22765}
22766
22767/*
22768 * Check if a variable name is valid.
22769 * Return FALSE and give an error if not.
22770 */
22771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022772valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022773{
22774 char_u *p;
22775
22776 for (p = varname; *p != NUL; ++p)
22777 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22778 && *p != AUTOLOAD_CHAR)
22779 {
22780 EMSG2(_(e_illvar), varname);
22781 return FALSE;
22782 }
22783 return TRUE;
22784}
22785
22786/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022787 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022788 * Also give an error message, using "name" or _("name") when use_gettext is
22789 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022790 */
22791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022792tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022793{
22794 if (lock & VAR_LOCKED)
22795 {
22796 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022797 name == NULL ? (char_u *)_("Unknown")
22798 : use_gettext ? (char_u *)_(name)
22799 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022800 return TRUE;
22801 }
22802 if (lock & VAR_FIXED)
22803 {
22804 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022805 name == NULL ? (char_u *)_("Unknown")
22806 : use_gettext ? (char_u *)_(name)
22807 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022808 return TRUE;
22809 }
22810 return FALSE;
22811}
22812
22813/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022814 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022815 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022816 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022817 * It is OK for "from" and "to" to point to the same item. This is used to
22818 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022819 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022821copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022823 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022824 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022825 switch (from->v_type)
22826 {
22827 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022828 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022829 to->vval.v_number = from->vval.v_number;
22830 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022831 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022832#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022833 to->vval.v_float = from->vval.v_float;
22834 break;
22835#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022836 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022837#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022838 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022839 if (to->vval.v_job != NULL)
22840 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022841 break;
22842#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022843 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022844#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022845 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022846 if (to->vval.v_channel != NULL)
22847 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022848 break;
22849#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022850 case VAR_STRING:
22851 case VAR_FUNC:
22852 if (from->vval.v_string == NULL)
22853 to->vval.v_string = NULL;
22854 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022855 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022856 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022857 if (from->v_type == VAR_FUNC)
22858 func_ref(to->vval.v_string);
22859 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022860 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022861 case VAR_PARTIAL:
22862 if (from->vval.v_partial == NULL)
22863 to->vval.v_partial = NULL;
22864 else
22865 {
22866 to->vval.v_partial = from->vval.v_partial;
22867 ++to->vval.v_partial->pt_refcount;
22868 }
22869 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022870 case VAR_LIST:
22871 if (from->vval.v_list == NULL)
22872 to->vval.v_list = NULL;
22873 else
22874 {
22875 to->vval.v_list = from->vval.v_list;
22876 ++to->vval.v_list->lv_refcount;
22877 }
22878 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022879 case VAR_DICT:
22880 if (from->vval.v_dict == NULL)
22881 to->vval.v_dict = NULL;
22882 else
22883 {
22884 to->vval.v_dict = from->vval.v_dict;
22885 ++to->vval.v_dict->dv_refcount;
22886 }
22887 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022888 case VAR_UNKNOWN:
22889 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022890 break;
22891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892}
22893
22894/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022895 * Make a copy of an item.
22896 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022897 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22898 * reference to an already copied list/dict can be used.
22899 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022900 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022902item_copy(
22903 typval_T *from,
22904 typval_T *to,
22905 int deep,
22906 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022907{
22908 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022909 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022910
Bram Moolenaar33570922005-01-25 22:26:29 +000022911 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022912 {
22913 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022914 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022915 }
22916 ++recurse;
22917
22918 switch (from->v_type)
22919 {
22920 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022921 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022922 case VAR_STRING:
22923 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022924 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022925 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022926 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022927 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022928 copy_tv(from, to);
22929 break;
22930 case VAR_LIST:
22931 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022932 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022933 if (from->vval.v_list == NULL)
22934 to->vval.v_list = NULL;
22935 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22936 {
22937 /* use the copy made earlier */
22938 to->vval.v_list = from->vval.v_list->lv_copylist;
22939 ++to->vval.v_list->lv_refcount;
22940 }
22941 else
22942 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22943 if (to->vval.v_list == NULL)
22944 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022945 break;
22946 case VAR_DICT:
22947 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022948 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022949 if (from->vval.v_dict == NULL)
22950 to->vval.v_dict = NULL;
22951 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22952 {
22953 /* use the copy made earlier */
22954 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22955 ++to->vval.v_dict->dv_refcount;
22956 }
22957 else
22958 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22959 if (to->vval.v_dict == NULL)
22960 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022961 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022962 case VAR_UNKNOWN:
22963 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022964 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022965 }
22966 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022967 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022968}
22969
22970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 * ":echo expr1 ..." print each argument separated with a space, add a
22972 * newline at the end.
22973 * ":echon expr1 ..." print each argument plain.
22974 */
22975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022976ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977{
22978 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022979 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022980 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 char_u *p;
22982 int needclr = TRUE;
22983 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022984 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985
22986 if (eap->skip)
22987 ++emsg_skip;
22988 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22989 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022990 /* If eval1() causes an error message the text from the command may
22991 * still need to be cleared. E.g., "echo 22,44". */
22992 need_clr_eos = needclr;
22993
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022995 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 {
22997 /*
22998 * Report the invalid expression unless the expression evaluation
22999 * has been cancelled due to an aborting error, an interrupt, or an
23000 * exception.
23001 */
23002 if (!aborting())
23003 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023004 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 break;
23006 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023007 need_clr_eos = FALSE;
23008
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 if (!eap->skip)
23010 {
23011 if (atstart)
23012 {
23013 atstart = FALSE;
23014 /* Call msg_start() after eval1(), evaluating the expression
23015 * may cause a message to appear. */
23016 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023017 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023018 /* Mark the saved text as finishing the line, so that what
23019 * follows is displayed on a new line when scrolling back
23020 * at the more prompt. */
23021 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024 }
23025 else if (eap->cmdidx == CMD_echo)
23026 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023027 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023028 if (p != NULL)
23029 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023031 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023033 if (*p != TAB && needclr)
23034 {
23035 /* remove any text still there from the command */
23036 msg_clr_eos();
23037 needclr = FALSE;
23038 }
23039 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 }
23041 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023042 {
23043#ifdef FEAT_MBYTE
23044 if (has_mbyte)
23045 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023046 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023047
23048 (void)msg_outtrans_len_attr(p, i, echo_attr);
23049 p += i - 1;
23050 }
23051 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023053 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023056 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023058 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 arg = skipwhite(arg);
23060 }
23061 eap->nextcmd = check_nextcmd(arg);
23062
23063 if (eap->skip)
23064 --emsg_skip;
23065 else
23066 {
23067 /* remove text that may still be there from the command */
23068 if (needclr)
23069 msg_clr_eos();
23070 if (eap->cmdidx == CMD_echo)
23071 msg_end();
23072 }
23073}
23074
23075/*
23076 * ":echohl {name}".
23077 */
23078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023079ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080{
23081 int id;
23082
23083 id = syn_name2id(eap->arg);
23084 if (id == 0)
23085 echo_attr = 0;
23086 else
23087 echo_attr = syn_id2attr(id);
23088}
23089
23090/*
23091 * ":execute expr1 ..." execute the result of an expression.
23092 * ":echomsg expr1 ..." Print a message
23093 * ":echoerr expr1 ..." Print an error
23094 * Each gets spaces around each argument and a newline at the end for
23095 * echo commands
23096 */
23097 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023098ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099{
23100 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023101 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 int ret = OK;
23103 char_u *p;
23104 garray_T ga;
23105 int len;
23106 int save_did_emsg;
23107
23108 ga_init2(&ga, 1, 80);
23109
23110 if (eap->skip)
23111 ++emsg_skip;
23112 while (*arg != NUL && *arg != '|' && *arg != '\n')
23113 {
23114 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023115 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 {
23117 /*
23118 * Report the invalid expression unless the expression evaluation
23119 * has been cancelled due to an aborting error, an interrupt, or an
23120 * exception.
23121 */
23122 if (!aborting())
23123 EMSG2(_(e_invexpr2), p);
23124 ret = FAIL;
23125 break;
23126 }
23127
23128 if (!eap->skip)
23129 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023130 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131 len = (int)STRLEN(p);
23132 if (ga_grow(&ga, len + 2) == FAIL)
23133 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023134 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 ret = FAIL;
23136 break;
23137 }
23138 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 ga.ga_len += len;
23142 }
23143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023144 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 arg = skipwhite(arg);
23146 }
23147
23148 if (ret != FAIL && ga.ga_data != NULL)
23149 {
23150 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023153 out_flush();
23154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 else if (eap->cmdidx == CMD_echoerr)
23156 {
23157 /* We don't want to abort following commands, restore did_emsg. */
23158 save_did_emsg = did_emsg;
23159 EMSG((char_u *)ga.ga_data);
23160 if (!force_abort)
23161 did_emsg = save_did_emsg;
23162 }
23163 else if (eap->cmdidx == CMD_execute)
23164 do_cmdline((char_u *)ga.ga_data,
23165 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23166 }
23167
23168 ga_clear(&ga);
23169
23170 if (eap->skip)
23171 --emsg_skip;
23172
23173 eap->nextcmd = check_nextcmd(arg);
23174}
23175
23176/*
23177 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23178 * "arg" points to the "&" or '+' when called, to "option" when returning.
23179 * Returns NULL when no option name found. Otherwise pointer to the char
23180 * after the option name.
23181 */
23182 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023183find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184{
23185 char_u *p = *arg;
23186
23187 ++p;
23188 if (*p == 'g' && p[1] == ':')
23189 {
23190 *opt_flags = OPT_GLOBAL;
23191 p += 2;
23192 }
23193 else if (*p == 'l' && p[1] == ':')
23194 {
23195 *opt_flags = OPT_LOCAL;
23196 p += 2;
23197 }
23198 else
23199 *opt_flags = 0;
23200
23201 if (!ASCII_ISALPHA(*p))
23202 return NULL;
23203 *arg = p;
23204
23205 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23206 p += 4; /* termcap option */
23207 else
23208 while (ASCII_ISALPHA(*p))
23209 ++p;
23210 return p;
23211}
23212
23213/*
23214 * ":function"
23215 */
23216 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023217ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218{
23219 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023220 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 int j;
23222 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023224 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225 char_u *name = NULL;
23226 char_u *p;
23227 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023228 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229 garray_T newargs;
23230 garray_T newlines;
23231 int varargs = FALSE;
23232 int mustend = FALSE;
23233 int flags = 0;
23234 ufunc_T *fp;
23235 int indent;
23236 int nesting;
23237 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023238 dictitem_T *v;
23239 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023240 static int func_nr = 0; /* number for nameless function */
23241 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023242 hashtab_T *ht;
23243 int todo;
23244 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023245 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246
23247 /*
23248 * ":function" without argument: list functions.
23249 */
23250 if (ends_excmd(*eap->arg))
23251 {
23252 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023253 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023254 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023255 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023256 {
23257 if (!HASHITEM_EMPTY(hi))
23258 {
23259 --todo;
23260 fp = HI2UF(hi);
23261 if (!isdigit(*fp->uf_name))
23262 list_func_head(fp, FALSE);
23263 }
23264 }
23265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266 eap->nextcmd = check_nextcmd(eap->arg);
23267 return;
23268 }
23269
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023270 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023271 * ":function /pat": list functions matching pattern.
23272 */
23273 if (*eap->arg == '/')
23274 {
23275 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23276 if (!eap->skip)
23277 {
23278 regmatch_T regmatch;
23279
23280 c = *p;
23281 *p = NUL;
23282 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23283 *p = c;
23284 if (regmatch.regprog != NULL)
23285 {
23286 regmatch.rm_ic = p_ic;
23287
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023288 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023289 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23290 {
23291 if (!HASHITEM_EMPTY(hi))
23292 {
23293 --todo;
23294 fp = HI2UF(hi);
23295 if (!isdigit(*fp->uf_name)
23296 && vim_regexec(&regmatch, fp->uf_name, 0))
23297 list_func_head(fp, FALSE);
23298 }
23299 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023300 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023301 }
23302 }
23303 if (*p == '/')
23304 ++p;
23305 eap->nextcmd = check_nextcmd(p);
23306 return;
23307 }
23308
23309 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023310 * Get the function name. There are these situations:
23311 * func normal function name
23312 * "name" == func, "fudi.fd_dict" == NULL
23313 * dict.func new dictionary entry
23314 * "name" == NULL, "fudi.fd_dict" set,
23315 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23316 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023317 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023318 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23319 * dict.func existing dict entry that's not a Funcref
23320 * "name" == NULL, "fudi.fd_dict" set,
23321 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023322 * s:func script-local function name
23323 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023326 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023327 paren = (vim_strchr(p, '(') != NULL);
23328 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023329 {
23330 /*
23331 * Return on an invalid expression in braces, unless the expression
23332 * evaluation has been cancelled due to an aborting error, an
23333 * interrupt, or an exception.
23334 */
23335 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023336 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023337 if (!eap->skip && fudi.fd_newkey != NULL)
23338 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023339 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342 else
23343 eap->skip = TRUE;
23344 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023345
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 /* An error in a function call during evaluation of an expression in magic
23347 * braces should not cause the function not to be defined. */
23348 saved_did_emsg = did_emsg;
23349 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350
23351 /*
23352 * ":function func" with only function name: list function.
23353 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023354 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 {
23356 if (!ends_excmd(*skipwhite(p)))
23357 {
23358 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023359 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360 }
23361 eap->nextcmd = check_nextcmd(p);
23362 if (eap->nextcmd != NULL)
23363 *p = NUL;
23364 if (!eap->skip && !got_int)
23365 {
23366 fp = find_func(name);
23367 if (fp != NULL)
23368 {
23369 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023370 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023372 if (FUNCLINE(fp, j) == NULL)
23373 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023374 msg_putchar('\n');
23375 msg_outnum((long)(j + 1));
23376 if (j < 9)
23377 msg_putchar(' ');
23378 if (j < 99)
23379 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023380 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381 out_flush(); /* show a line at a time */
23382 ui_breakcheck();
23383 }
23384 if (!got_int)
23385 {
23386 msg_putchar('\n');
23387 msg_puts((char_u *)" endfunction");
23388 }
23389 }
23390 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023391 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023393 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394 }
23395
23396 /*
23397 * ":function name(arg1, arg2)" Define function.
23398 */
23399 p = skipwhite(p);
23400 if (*p != '(')
23401 {
23402 if (!eap->skip)
23403 {
23404 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023405 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 }
23407 /* attempt to continue by skipping some text */
23408 if (vim_strchr(p, '(') != NULL)
23409 p = vim_strchr(p, '(');
23410 }
23411 p = skipwhite(p + 1);
23412
23413 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23414 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23415
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023416 if (!eap->skip)
23417 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023418 /* Check the name of the function. Unless it's a dictionary function
23419 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023420 if (name != NULL)
23421 arg = name;
23422 else
23423 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023424 if (arg != NULL && (fudi.fd_di == NULL
23425 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023426 {
23427 if (*arg == K_SPECIAL)
23428 j = 3;
23429 else
23430 j = 0;
23431 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23432 : eval_isnamec(arg[j])))
23433 ++j;
23434 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023435 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023436 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023437 /* Disallow using the g: dict. */
23438 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23439 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023440 }
23441
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442 /*
23443 * Isolate the arguments: "arg1, arg2, ...)"
23444 */
23445 while (*p != ')')
23446 {
23447 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23448 {
23449 varargs = TRUE;
23450 p += 3;
23451 mustend = TRUE;
23452 }
23453 else
23454 {
23455 arg = p;
23456 while (ASCII_ISALNUM(*p) || *p == '_')
23457 ++p;
23458 if (arg == p || isdigit(*arg)
23459 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23460 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23461 {
23462 if (!eap->skip)
23463 EMSG2(_("E125: Illegal argument: %s"), arg);
23464 break;
23465 }
23466 if (ga_grow(&newargs, 1) == FAIL)
23467 goto erret;
23468 c = *p;
23469 *p = NUL;
23470 arg = vim_strsave(arg);
23471 if (arg == NULL)
23472 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023473
23474 /* Check for duplicate argument name. */
23475 for (i = 0; i < newargs.ga_len; ++i)
23476 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23477 {
23478 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023479 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023480 goto erret;
23481 }
23482
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23484 *p = c;
23485 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 if (*p == ',')
23487 ++p;
23488 else
23489 mustend = TRUE;
23490 }
23491 p = skipwhite(p);
23492 if (mustend && *p != ')')
23493 {
23494 if (!eap->skip)
23495 EMSG2(_(e_invarg2), eap->arg);
23496 break;
23497 }
23498 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023499 if (*p != ')')
23500 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 ++p; /* skip the ')' */
23502
Bram Moolenaare9a41262005-01-15 22:18:47 +000023503 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023504 for (;;)
23505 {
23506 p = skipwhite(p);
23507 if (STRNCMP(p, "range", 5) == 0)
23508 {
23509 flags |= FC_RANGE;
23510 p += 5;
23511 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023512 else if (STRNCMP(p, "dict", 4) == 0)
23513 {
23514 flags |= FC_DICT;
23515 p += 4;
23516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 else if (STRNCMP(p, "abort", 5) == 0)
23518 {
23519 flags |= FC_ABORT;
23520 p += 5;
23521 }
23522 else
23523 break;
23524 }
23525
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023526 /* When there is a line break use what follows for the function body.
23527 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23528 if (*p == '\n')
23529 line_arg = p + 1;
23530 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531 EMSG(_(e_trailing));
23532
23533 /*
23534 * Read the body of the function, until ":endfunction" is found.
23535 */
23536 if (KeyTyped)
23537 {
23538 /* Check if the function already exists, don't let the user type the
23539 * whole function before telling him it doesn't work! For a script we
23540 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023541 if (!eap->skip && !eap->forceit)
23542 {
23543 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23544 EMSG(_(e_funcdict));
23545 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023546 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023549 if (!eap->skip && did_emsg)
23550 goto erret;
23551
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 msg_putchar('\n'); /* don't overwrite the function name */
23553 cmdline_row = msg_row;
23554 }
23555
23556 indent = 2;
23557 nesting = 0;
23558 for (;;)
23559 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023560 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023561 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023562 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023563 saved_wait_return = FALSE;
23564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023566 sourcing_lnum_off = sourcing_lnum;
23567
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023568 if (line_arg != NULL)
23569 {
23570 /* Use eap->arg, split up in parts by line breaks. */
23571 theline = line_arg;
23572 p = vim_strchr(theline, '\n');
23573 if (p == NULL)
23574 line_arg += STRLEN(line_arg);
23575 else
23576 {
23577 *p = NUL;
23578 line_arg = p + 1;
23579 }
23580 }
23581 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 theline = getcmdline(':', 0L, indent);
23583 else
23584 theline = eap->getline(':', eap->cookie, indent);
23585 if (KeyTyped)
23586 lines_left = Rows - 1;
23587 if (theline == NULL)
23588 {
23589 EMSG(_("E126: Missing :endfunction"));
23590 goto erret;
23591 }
23592
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023593 /* Detect line continuation: sourcing_lnum increased more than one. */
23594 if (sourcing_lnum > sourcing_lnum_off + 1)
23595 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23596 else
23597 sourcing_lnum_off = 0;
23598
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 if (skip_until != NULL)
23600 {
23601 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23602 * don't check for ":endfunc". */
23603 if (STRCMP(theline, skip_until) == 0)
23604 {
23605 vim_free(skip_until);
23606 skip_until = NULL;
23607 }
23608 }
23609 else
23610 {
23611 /* skip ':' and blanks*/
23612 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23613 ;
23614
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023615 /* Check for "endfunction". */
23616 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023618 if (line_arg == NULL)
23619 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 break;
23621 }
23622
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023623 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 * at "end". */
23625 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23626 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023627 else if (STRNCMP(p, "if", 2) == 0
23628 || STRNCMP(p, "wh", 2) == 0
23629 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 || STRNCMP(p, "try", 3) == 0)
23631 indent += 2;
23632
23633 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023634 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023636 if (*p == '!')
23637 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023639 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023640 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023642 ++nesting;
23643 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 }
23645 }
23646
23647 /* Check for ":append" or ":insert". */
23648 p = skip_range(p, NULL);
23649 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23650 || (p[0] == 'i'
23651 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23652 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23653 skip_until = vim_strsave((char_u *)".");
23654
23655 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23656 arg = skipwhite(skiptowhite(p));
23657 if (arg[0] == '<' && arg[1] =='<'
23658 && ((p[0] == 'p' && p[1] == 'y'
23659 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23660 || (p[0] == 'p' && p[1] == 'e'
23661 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23662 || (p[0] == 't' && p[1] == 'c'
23663 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023664 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23665 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023666 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23667 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023668 || (p[0] == 'm' && p[1] == 'z'
23669 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 ))
23671 {
23672 /* ":python <<" continues until a dot, like ":append" */
23673 p = skipwhite(arg + 2);
23674 if (*p == NUL)
23675 skip_until = vim_strsave((char_u *)".");
23676 else
23677 skip_until = vim_strsave(p);
23678 }
23679 }
23680
23681 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023682 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023683 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023684 if (line_arg == NULL)
23685 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023686 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023687 }
23688
23689 /* Copy the line to newly allocated memory. get_one_sourceline()
23690 * allocates 250 bytes per line, this saves 80% on average. The cost
23691 * is an extra alloc/free. */
23692 p = vim_strsave(theline);
23693 if (p != NULL)
23694 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023695 if (line_arg == NULL)
23696 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023697 theline = p;
23698 }
23699
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023700 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23701
23702 /* Add NULL lines for continuation lines, so that the line count is
23703 * equal to the index in the growarray. */
23704 while (sourcing_lnum_off-- > 0)
23705 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023706
23707 /* Check for end of eap->arg. */
23708 if (line_arg != NULL && *line_arg == NUL)
23709 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 }
23711
23712 /* Don't define the function when skipping commands or when an error was
23713 * detected. */
23714 if (eap->skip || did_emsg)
23715 goto erret;
23716
23717 /*
23718 * If there are no errors, add the function
23719 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023720 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023721 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023722 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023723 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023724 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023725 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023726 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023727 goto erret;
23728 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023729
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023730 fp = find_func(name);
23731 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023733 if (!eap->forceit)
23734 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023735 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023736 goto erret;
23737 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023738 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023739 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023740 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023741 name);
23742 goto erret;
23743 }
23744 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023745 ga_clear_strings(&(fp->uf_args));
23746 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023747 vim_free(name);
23748 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023750 }
23751 else
23752 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023753 char numbuf[20];
23754
23755 fp = NULL;
23756 if (fudi.fd_newkey == NULL && !eap->forceit)
23757 {
23758 EMSG(_(e_funcdict));
23759 goto erret;
23760 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023761 if (fudi.fd_di == NULL)
23762 {
23763 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023764 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023765 goto erret;
23766 }
23767 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023768 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023769 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023770
23771 /* Give the function a sequential number. Can only be used with a
23772 * Funcref! */
23773 vim_free(name);
23774 sprintf(numbuf, "%d", ++func_nr);
23775 name = vim_strsave((char_u *)numbuf);
23776 if (name == NULL)
23777 goto erret;
23778 }
23779
23780 if (fp == NULL)
23781 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023782 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023783 {
23784 int slen, plen;
23785 char_u *scriptname;
23786
23787 /* Check that the autoload name matches the script name. */
23788 j = FAIL;
23789 if (sourcing_name != NULL)
23790 {
23791 scriptname = autoload_name(name);
23792 if (scriptname != NULL)
23793 {
23794 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023795 plen = (int)STRLEN(p);
23796 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023797 if (slen > plen && fnamecmp(p,
23798 sourcing_name + slen - plen) == 0)
23799 j = OK;
23800 vim_free(scriptname);
23801 }
23802 }
23803 if (j == FAIL)
23804 {
23805 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23806 goto erret;
23807 }
23808 }
23809
23810 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 if (fp == NULL)
23812 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023813
23814 if (fudi.fd_dict != NULL)
23815 {
23816 if (fudi.fd_di == NULL)
23817 {
23818 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023819 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023820 if (fudi.fd_di == NULL)
23821 {
23822 vim_free(fp);
23823 goto erret;
23824 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023825 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23826 {
23827 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023828 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023829 goto erret;
23830 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023831 }
23832 else
23833 /* overwrite existing dict entry */
23834 clear_tv(&fudi.fd_di->di_tv);
23835 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023836 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023837 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023838 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023839
23840 /* behave like "dict" was used */
23841 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023842 }
23843
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023845 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023846 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23847 {
23848 vim_free(fp);
23849 goto erret;
23850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023852 fp->uf_args = newargs;
23853 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023854#ifdef FEAT_PROFILE
23855 fp->uf_tml_count = NULL;
23856 fp->uf_tml_total = NULL;
23857 fp->uf_tml_self = NULL;
23858 fp->uf_profiling = FALSE;
23859 if (prof_def_func())
23860 func_do_profile(fp);
23861#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023862 fp->uf_varargs = varargs;
23863 fp->uf_flags = flags;
23864 fp->uf_calls = 0;
23865 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023866 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867
23868erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 ga_clear_strings(&newargs);
23870 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023871ret_free:
23872 vim_free(skip_until);
23873 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023876 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877}
23878
23879/*
23880 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023881 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023883 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023884 * TFN_INT: internal function name OK
23885 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023886 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 * Advances "pp" to just after the function name (if no error).
23888 */
23889 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023890trans_function_name(
23891 char_u **pp,
23892 int skip, /* only find the end, don't evaluate */
23893 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023894 funcdict_T *fdp, /* return: info about dictionary used */
23895 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023897 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 char_u *start;
23899 char_u *end;
23900 int lead;
23901 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023903 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023904
23905 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023906 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023908
23909 /* Check for hard coded <SNR>: already translated function ID (from a user
23910 * command). */
23911 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23912 && (*pp)[2] == (int)KE_SNR)
23913 {
23914 *pp += 3;
23915 len = get_id_len(pp) + 3;
23916 return vim_strnsave(start, len);
23917 }
23918
23919 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23920 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023921 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023922 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023924
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023925 /* Note that TFN_ flags use the same values as GLV_ flags. */
23926 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023927 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023928 if (end == start)
23929 {
23930 if (!skip)
23931 EMSG(_("E129: Function name required"));
23932 goto theend;
23933 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023934 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023935 {
23936 /*
23937 * Report an invalid expression in braces, unless the expression
23938 * evaluation has been cancelled due to an aborting error, an
23939 * interrupt, or an exception.
23940 */
23941 if (!aborting())
23942 {
23943 if (end != NULL)
23944 EMSG2(_(e_invarg2), start);
23945 }
23946 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023947 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023948 goto theend;
23949 }
23950
23951 if (lv.ll_tv != NULL)
23952 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023953 if (fdp != NULL)
23954 {
23955 fdp->fd_dict = lv.ll_dict;
23956 fdp->fd_newkey = lv.ll_newkey;
23957 lv.ll_newkey = NULL;
23958 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023959 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023960 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23961 {
23962 name = vim_strsave(lv.ll_tv->vval.v_string);
23963 *pp = end;
23964 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010023965 else if (lv.ll_tv->v_type == VAR_PARTIAL
23966 && lv.ll_tv->vval.v_partial != NULL)
23967 {
23968 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
23969 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010023970 if (partial != NULL)
23971 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010023972 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023973 else
23974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023975 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23976 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023977 EMSG(_(e_funcref));
23978 else
23979 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023980 name = NULL;
23981 }
23982 goto theend;
23983 }
23984
23985 if (lv.ll_name == NULL)
23986 {
23987 /* Error found, but continue after the function name. */
23988 *pp = end;
23989 goto theend;
23990 }
23991
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023992 /* Check if the name is a Funcref. If so, use the value. */
23993 if (lv.ll_exp_name != NULL)
23994 {
23995 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010023996 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023997 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023998 if (name == lv.ll_exp_name)
23999 name = NULL;
24000 }
24001 else
24002 {
24003 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024004 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024005 if (name == *pp)
24006 name = NULL;
24007 }
24008 if (name != NULL)
24009 {
24010 name = vim_strsave(name);
24011 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024012 if (STRNCMP(name, "<SNR>", 5) == 0)
24013 {
24014 /* Change "<SNR>" to the byte sequence. */
24015 name[0] = K_SPECIAL;
24016 name[1] = KS_EXTRA;
24017 name[2] = (int)KE_SNR;
24018 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24019 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024020 goto theend;
24021 }
24022
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024023 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024024 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024025 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024026 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24027 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24028 {
24029 /* When there was "s:" already or the name expanded to get a
24030 * leading "s:" then remove it. */
24031 lv.ll_name += 2;
24032 len -= 2;
24033 lead = 2;
24034 }
24035 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024036 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024037 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024038 /* skip over "s:" and "g:" */
24039 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024040 lv.ll_name += 2;
24041 len = (int)(end - lv.ll_name);
24042 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024043
24044 /*
24045 * Copy the function name to allocated memory.
24046 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24047 * Accept <SNR>123_name() outside a script.
24048 */
24049 if (skip)
24050 lead = 0; /* do nothing */
24051 else if (lead > 0)
24052 {
24053 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024054 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24055 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024056 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024057 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024058 if (current_SID <= 0)
24059 {
24060 EMSG(_(e_usingsid));
24061 goto theend;
24062 }
24063 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24064 lead += (int)STRLEN(sid_buf);
24065 }
24066 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024067 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024068 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024069 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024070 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024071 goto theend;
24072 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024073 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024074 {
24075 char_u *cp = vim_strchr(lv.ll_name, ':');
24076
24077 if (cp != NULL && cp < end)
24078 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024079 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024080 goto theend;
24081 }
24082 }
24083
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024084 name = alloc((unsigned)(len + lead + 1));
24085 if (name != NULL)
24086 {
24087 if (lead > 0)
24088 {
24089 name[0] = K_SPECIAL;
24090 name[1] = KS_EXTRA;
24091 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024092 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024093 STRCPY(name + 3, sid_buf);
24094 }
24095 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024096 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024097 }
24098 *pp = end;
24099
24100theend:
24101 clear_lval(&lv);
24102 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103}
24104
24105/*
24106 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24107 * Return 2 if "p" starts with "s:".
24108 * Return 0 otherwise.
24109 */
24110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024111eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024113 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24114 * the standard library function. */
24115 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24116 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117 return 5;
24118 if (p[0] == 's' && p[1] == ':')
24119 return 2;
24120 return 0;
24121}
24122
24123/*
24124 * Return TRUE if "p" starts with "<SID>" or "s:".
24125 * Only works if eval_fname_script() returned non-zero for "p"!
24126 */
24127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024128eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129{
24130 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24131}
24132
24133/*
24134 * List the head of the function: "name(arg1, arg2)".
24135 */
24136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024137list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138{
24139 int j;
24140
24141 msg_start();
24142 if (indent)
24143 MSG_PUTS(" ");
24144 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024145 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024146 {
24147 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024148 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 }
24150 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024151 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024152 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024153 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 {
24155 if (j)
24156 MSG_PUTS(", ");
24157 msg_puts(FUNCARG(fp, j));
24158 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024159 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160 {
24161 if (j)
24162 MSG_PUTS(", ");
24163 MSG_PUTS("...");
24164 }
24165 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024166 if (fp->uf_flags & FC_ABORT)
24167 MSG_PUTS(" abort");
24168 if (fp->uf_flags & FC_RANGE)
24169 MSG_PUTS(" range");
24170 if (fp->uf_flags & FC_DICT)
24171 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024172 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024173 if (p_verbose > 0)
24174 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175}
24176
24177/*
24178 * Find a function by name, return pointer to it in ufuncs.
24179 * Return NULL for unknown function.
24180 */
24181 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024182find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024184 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024186 hi = hash_find(&func_hashtab, name);
24187 if (!HASHITEM_EMPTY(hi))
24188 return HI2UF(hi);
24189 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190}
24191
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024192#if defined(EXITFREE) || defined(PROTO)
24193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024194free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024195{
24196 hashitem_T *hi;
24197
24198 /* Need to start all over every time, because func_free() may change the
24199 * hash table. */
24200 while (func_hashtab.ht_used > 0)
24201 for (hi = func_hashtab.ht_array; ; ++hi)
24202 if (!HASHITEM_EMPTY(hi))
24203 {
24204 func_free(HI2UF(hi));
24205 break;
24206 }
24207}
24208#endif
24209
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024210 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024211translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024212{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024213 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024214 return find_internal_func(name) >= 0;
24215 return find_func(name) != NULL;
24216}
24217
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024218/*
24219 * Return TRUE if a function "name" exists.
24220 */
24221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024222function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024223{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024224 char_u *nm = name;
24225 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024226 int n = FALSE;
24227
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024228 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024229 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024230 nm = skipwhite(nm);
24231
24232 /* Only accept "funcname", "funcname ", "funcname (..." and
24233 * "funcname(...", not "funcname!...". */
24234 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024235 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024236 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024237 return n;
24238}
24239
Bram Moolenaara1544c02013-05-30 12:35:52 +020024240 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024241get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024242{
24243 char_u *nm = name;
24244 char_u *p;
24245
Bram Moolenaar65639032016-03-16 21:40:30 +010024246 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024247
24248 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024249 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024250 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024251
Bram Moolenaara1544c02013-05-30 12:35:52 +020024252 vim_free(p);
24253 return NULL;
24254}
24255
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024256/*
24257 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024258 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24259 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024260 */
24261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024262builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024263{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024264 char_u *p;
24265
24266 if (!ASCII_ISLOWER(name[0]))
24267 return FALSE;
24268 p = vim_strchr(name, AUTOLOAD_CHAR);
24269 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024270}
24271
Bram Moolenaar05159a02005-02-26 23:04:13 +000024272#if defined(FEAT_PROFILE) || defined(PROTO)
24273/*
24274 * Start profiling function "fp".
24275 */
24276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024277func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024278{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024279 int len = fp->uf_lines.ga_len;
24280
24281 if (len == 0)
24282 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024283 fp->uf_tm_count = 0;
24284 profile_zero(&fp->uf_tm_self);
24285 profile_zero(&fp->uf_tm_total);
24286 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024287 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024288 if (fp->uf_tml_total == NULL)
24289 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024290 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024291 if (fp->uf_tml_self == NULL)
24292 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024293 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024294 fp->uf_tml_idx = -1;
24295 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24296 || fp->uf_tml_self == NULL)
24297 return; /* out of memory */
24298
24299 fp->uf_profiling = TRUE;
24300}
24301
24302/*
24303 * Dump the profiling results for all functions in file "fd".
24304 */
24305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024306func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024307{
24308 hashitem_T *hi;
24309 int todo;
24310 ufunc_T *fp;
24311 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024312 ufunc_T **sorttab;
24313 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024314
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024315 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024316 if (todo == 0)
24317 return; /* nothing to dump */
24318
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024319 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024320
Bram Moolenaar05159a02005-02-26 23:04:13 +000024321 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24322 {
24323 if (!HASHITEM_EMPTY(hi))
24324 {
24325 --todo;
24326 fp = HI2UF(hi);
24327 if (fp->uf_profiling)
24328 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024329 if (sorttab != NULL)
24330 sorttab[st_len++] = fp;
24331
Bram Moolenaar05159a02005-02-26 23:04:13 +000024332 if (fp->uf_name[0] == K_SPECIAL)
24333 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24334 else
24335 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24336 if (fp->uf_tm_count == 1)
24337 fprintf(fd, "Called 1 time\n");
24338 else
24339 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24340 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24341 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24342 fprintf(fd, "\n");
24343 fprintf(fd, "count total (s) self (s)\n");
24344
24345 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24346 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024347 if (FUNCLINE(fp, i) == NULL)
24348 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024349 prof_func_line(fd, fp->uf_tml_count[i],
24350 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024351 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24352 }
24353 fprintf(fd, "\n");
24354 }
24355 }
24356 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024357
24358 if (sorttab != NULL && st_len > 0)
24359 {
24360 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24361 prof_total_cmp);
24362 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24363 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24364 prof_self_cmp);
24365 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24366 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024367
24368 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024369}
Bram Moolenaar73830342005-02-28 22:48:19 +000024370
24371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024372prof_sort_list(
24373 FILE *fd,
24374 ufunc_T **sorttab,
24375 int st_len,
24376 char *title,
24377 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024378{
24379 int i;
24380 ufunc_T *fp;
24381
24382 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24383 fprintf(fd, "count total (s) self (s) function\n");
24384 for (i = 0; i < 20 && i < st_len; ++i)
24385 {
24386 fp = sorttab[i];
24387 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24388 prefer_self);
24389 if (fp->uf_name[0] == K_SPECIAL)
24390 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24391 else
24392 fprintf(fd, " %s()\n", fp->uf_name);
24393 }
24394 fprintf(fd, "\n");
24395}
24396
24397/*
24398 * Print the count and times for one function or function line.
24399 */
24400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024401prof_func_line(
24402 FILE *fd,
24403 int count,
24404 proftime_T *total,
24405 proftime_T *self,
24406 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024407{
24408 if (count > 0)
24409 {
24410 fprintf(fd, "%5d ", count);
24411 if (prefer_self && profile_equal(total, self))
24412 fprintf(fd, " ");
24413 else
24414 fprintf(fd, "%s ", profile_msg(total));
24415 if (!prefer_self && profile_equal(total, self))
24416 fprintf(fd, " ");
24417 else
24418 fprintf(fd, "%s ", profile_msg(self));
24419 }
24420 else
24421 fprintf(fd, " ");
24422}
24423
24424/*
24425 * Compare function for total time sorting.
24426 */
24427 static int
24428#ifdef __BORLANDC__
24429_RTLENTRYF
24430#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024431prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024432{
24433 ufunc_T *p1, *p2;
24434
24435 p1 = *(ufunc_T **)s1;
24436 p2 = *(ufunc_T **)s2;
24437 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24438}
24439
24440/*
24441 * Compare function for self time sorting.
24442 */
24443 static int
24444#ifdef __BORLANDC__
24445_RTLENTRYF
24446#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024447prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024448{
24449 ufunc_T *p1, *p2;
24450
24451 p1 = *(ufunc_T **)s1;
24452 p2 = *(ufunc_T **)s2;
24453 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24454}
24455
Bram Moolenaar05159a02005-02-26 23:04:13 +000024456#endif
24457
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024458/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024459 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024460 * Return TRUE if a package was loaded.
24461 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024462 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024463script_autoload(
24464 char_u *name,
24465 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024466{
24467 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024468 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024469 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024470 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024471
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024472 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024473 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024474 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024475 return FALSE;
24476
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024477 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024478
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024479 /* Find the name in the list of previously loaded package names. Skip
24480 * "autoload/", it's always the same. */
24481 for (i = 0; i < ga_loaded.ga_len; ++i)
24482 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24483 break;
24484 if (!reload && i < ga_loaded.ga_len)
24485 ret = FALSE; /* was loaded already */
24486 else
24487 {
24488 /* Remember the name if it wasn't loaded already. */
24489 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24490 {
24491 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24492 tofree = NULL;
24493 }
24494
24495 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024496 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024497 ret = TRUE;
24498 }
24499
24500 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024501 return ret;
24502}
24503
24504/*
24505 * Return the autoload script name for a function or variable name.
24506 * Returns NULL when out of memory.
24507 */
24508 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024509autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024510{
24511 char_u *p;
24512 char_u *scriptname;
24513
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024514 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024515 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24516 if (scriptname == NULL)
24517 return FALSE;
24518 STRCPY(scriptname, "autoload/");
24519 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024520 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024521 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024522 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024523 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024524 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024525}
24526
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24528
24529/*
24530 * Function given to ExpandGeneric() to obtain the list of user defined
24531 * function names.
24532 */
24533 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024534get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024535{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024536 static long_u done;
24537 static hashitem_T *hi;
24538 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539
24540 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024542 done = 0;
24543 hi = func_hashtab.ht_array;
24544 }
24545 if (done < func_hashtab.ht_used)
24546 {
24547 if (done++ > 0)
24548 ++hi;
24549 while (HASHITEM_EMPTY(hi))
24550 ++hi;
24551 fp = HI2UF(hi);
24552
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024553 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024554 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024555
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024556 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24557 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558
24559 cat_func_name(IObuff, fp);
24560 if (xp->xp_context != EXPAND_USER_FUNC)
24561 {
24562 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024563 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564 STRCAT(IObuff, ")");
24565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 return IObuff;
24567 }
24568 return NULL;
24569}
24570
24571#endif /* FEAT_CMDL_COMPL */
24572
24573/*
24574 * Copy the function name of "fp" to buffer "buf".
24575 * "buf" must be able to hold the function name plus three bytes.
24576 * Takes care of script-local function names.
24577 */
24578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024579cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024581 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024582 {
24583 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024584 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 }
24586 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024587 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588}
24589
24590/*
24591 * ":delfunction {name}"
24592 */
24593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024594ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024595{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024596 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597 char_u *p;
24598 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024599 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600
24601 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024602 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024603 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024605 {
24606 if (fudi.fd_dict != NULL && !eap->skip)
24607 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610 if (!ends_excmd(*skipwhite(p)))
24611 {
24612 vim_free(name);
24613 EMSG(_(e_trailing));
24614 return;
24615 }
24616 eap->nextcmd = check_nextcmd(p);
24617 if (eap->nextcmd != NULL)
24618 *p = NUL;
24619
24620 if (!eap->skip)
24621 fp = find_func(name);
24622 vim_free(name);
24623
24624 if (!eap->skip)
24625 {
24626 if (fp == NULL)
24627 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024628 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024629 return;
24630 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024631 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 {
24633 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24634 return;
24635 }
24636
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024637 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024639 /* Delete the dict item that refers to the function, it will
24640 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024641 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024643 else
24644 func_free(fp);
24645 }
24646}
24647
24648/*
24649 * Free a function and remove it from the list of functions.
24650 */
24651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024652func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024653{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024654 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024655
24656 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024657 ga_clear_strings(&(fp->uf_args));
24658 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024659#ifdef FEAT_PROFILE
24660 vim_free(fp->uf_tml_count);
24661 vim_free(fp->uf_tml_total);
24662 vim_free(fp->uf_tml_self);
24663#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024664
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024665 /* remove the function from the function hashtable */
24666 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24667 if (HASHITEM_EMPTY(hi))
24668 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024669 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024670 hash_remove(&func_hashtab, hi);
24671
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024672 vim_free(fp);
24673}
24674
24675/*
24676 * Unreference a Function: decrement the reference count and free it when it
24677 * becomes zero. Only for numbered functions.
24678 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024680func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024681{
24682 ufunc_T *fp;
24683
24684 if (name != NULL && isdigit(*name))
24685 {
24686 fp = find_func(name);
24687 if (fp == NULL)
24688 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024689 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024690 {
24691 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024692 * when "uf_calls" becomes zero. */
24693 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024694 func_free(fp);
24695 }
24696 }
24697}
24698
24699/*
24700 * Count a reference to a Function.
24701 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024703func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024704{
24705 ufunc_T *fp;
24706
24707 if (name != NULL && isdigit(*name))
24708 {
24709 fp = find_func(name);
24710 if (fp == NULL)
24711 EMSG2(_(e_intern2), "func_ref()");
24712 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024713 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714 }
24715}
24716
24717/*
24718 * Call a user function.
24719 */
24720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024721call_user_func(
24722 ufunc_T *fp, /* pointer to function */
24723 int argcount, /* nr of args */
24724 typval_T *argvars, /* arguments */
24725 typval_T *rettv, /* return value */
24726 linenr_T firstline, /* first line of range */
24727 linenr_T lastline, /* last line of range */
24728 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729{
Bram Moolenaar33570922005-01-25 22:26:29 +000024730 char_u *save_sourcing_name;
24731 linenr_T save_sourcing_lnum;
24732 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024733 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024734 int save_did_emsg;
24735 static int depth = 0;
24736 dictitem_T *v;
24737 int fixvar_idx = 0; /* index in fixvar[] */
24738 int i;
24739 int ai;
24740 char_u numbuf[NUMBUFLEN];
24741 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024742 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024743#ifdef FEAT_PROFILE
24744 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024745 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747
24748 /* If depth of calling is getting too high, don't execute the function */
24749 if (depth >= p_mfd)
24750 {
24751 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024752 rettv->v_type = VAR_NUMBER;
24753 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024754 return;
24755 }
24756 ++depth;
24757
24758 line_breakcheck(); /* check for CTRL-C hit */
24759
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024760 fc = (funccall_T *)alloc(sizeof(funccall_T));
24761 fc->caller = current_funccal;
24762 current_funccal = fc;
24763 fc->func = fp;
24764 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024765 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024766 fc->linenr = 0;
24767 fc->returned = FALSE;
24768 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024770 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24771 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024772
Bram Moolenaar33570922005-01-25 22:26:29 +000024773 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024774 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024775 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24776 * each argument variable and saves a lot of time.
24777 */
24778 /*
24779 * Init l: variables.
24780 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024781 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024782 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024783 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024784 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24785 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024786 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024787 name = v->di_key;
24788 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024789 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024790 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024791 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024792 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024793 v->di_tv.vval.v_dict = selfdict;
24794 ++selfdict->dv_refcount;
24795 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024796
Bram Moolenaar33570922005-01-25 22:26:29 +000024797 /*
24798 * Init a: variables.
24799 * Set a:0 to "argcount".
24800 * Set a:000 to a list with room for the "..." arguments.
24801 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024802 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024803 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024804 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024805 /* Use "name" to avoid a warning from some compiler that checks the
24806 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024807 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024808 name = v->di_key;
24809 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024810 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024811 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024812 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024813 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024814 v->di_tv.vval.v_list = &fc->l_varlist;
24815 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24816 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24817 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024818
24819 /*
24820 * Set a:firstline to "firstline" and a:lastline to "lastline".
24821 * Set a:name to named arguments.
24822 * Set a:N to the "..." arguments.
24823 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024824 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024825 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024826 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024827 (varnumber_T)lastline);
24828 for (i = 0; i < argcount; ++i)
24829 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024830 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024831 if (ai < 0)
24832 /* named argument a:name */
24833 name = FUNCARG(fp, i);
24834 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024835 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024836 /* "..." argument a:1, a:2, etc. */
24837 sprintf((char *)numbuf, "%d", ai + 1);
24838 name = numbuf;
24839 }
24840 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24841 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024842 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024843 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24844 }
24845 else
24846 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024847 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24848 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024849 if (v == NULL)
24850 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024851 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024852 }
24853 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024854 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024855
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024856 /* Note: the values are copied directly to avoid alloc/free.
24857 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024858 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024859 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024860
24861 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24862 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024863 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24864 fc->l_listitems[ai].li_tv = argvars[i];
24865 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024866 }
24867 }
24868
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869 /* Don't redraw while executing the function. */
24870 ++RedrawingDisabled;
24871 save_sourcing_name = sourcing_name;
24872 save_sourcing_lnum = sourcing_lnum;
24873 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024874 /* need space for function name + ("function " + 3) or "[number]" */
24875 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24876 + STRLEN(fp->uf_name) + 20;
24877 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 if (sourcing_name != NULL)
24879 {
24880 if (save_sourcing_name != NULL
24881 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024882 sprintf((char *)sourcing_name, "%s[%d]..",
24883 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884 else
24885 STRCPY(sourcing_name, "function ");
24886 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24887
24888 if (p_verbose >= 12)
24889 {
24890 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024891 verbose_enter_scroll();
24892
Bram Moolenaar555b2802005-05-19 21:08:39 +000024893 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 if (p_verbose >= 14)
24895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024897 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024898 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024899 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900
24901 msg_puts((char_u *)"(");
24902 for (i = 0; i < argcount; ++i)
24903 {
24904 if (i > 0)
24905 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024906 if (argvars[i].v_type == VAR_NUMBER)
24907 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 else
24909 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024910 /* Do not want errors such as E724 here. */
24911 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024912 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024913 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024914 if (s != NULL)
24915 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024916 if (vim_strsize(s) > MSG_BUF_CLEN)
24917 {
24918 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24919 s = buf;
24920 }
24921 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024922 vim_free(tofree);
24923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 }
24925 }
24926 msg_puts((char_u *)")");
24927 }
24928 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024929
24930 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 --no_wait_return;
24932 }
24933 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024934#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024935 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024936 {
24937 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24938 func_do_profile(fp);
24939 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024940 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024941 {
24942 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024943 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024944 profile_zero(&fp->uf_tm_children);
24945 }
24946 script_prof_save(&wait_start);
24947 }
24948#endif
24949
Bram Moolenaar071d4272004-06-13 20:20:40 +000024950 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024951 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024952 save_did_emsg = did_emsg;
24953 did_emsg = FALSE;
24954
24955 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024956 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24958
24959 --RedrawingDisabled;
24960
24961 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024962 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024964 clear_tv(rettv);
24965 rettv->v_type = VAR_NUMBER;
24966 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967 }
24968
Bram Moolenaar05159a02005-02-26 23:04:13 +000024969#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024970 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024971 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024972 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024973 profile_end(&call_start);
24974 profile_sub_wait(&wait_start, &call_start);
24975 profile_add(&fp->uf_tm_total, &call_start);
24976 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024977 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024978 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024979 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24980 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024981 }
24982 }
24983#endif
24984
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 /* when being verbose, mention the return value */
24986 if (p_verbose >= 12)
24987 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024989 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024992 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024993 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024994 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024995 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024996 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024997 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024998 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024999 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025000 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025001 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025002
Bram Moolenaar555b2802005-05-19 21:08:39 +000025003 /* The value may be very long. Skip the middle part, so that we
25004 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025005 * truncate it at the end. Don't want errors such as E724 here. */
25006 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025007 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025008 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025009 if (s != NULL)
25010 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025011 if (vim_strsize(s) > MSG_BUF_CLEN)
25012 {
25013 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25014 s = buf;
25015 }
25016 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025017 vim_free(tofree);
25018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019 }
25020 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025021
25022 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023 --no_wait_return;
25024 }
25025
25026 vim_free(sourcing_name);
25027 sourcing_name = save_sourcing_name;
25028 sourcing_lnum = save_sourcing_lnum;
25029 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025030#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025031 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025032 script_prof_restore(&wait_start);
25033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034
25035 if (p_verbose >= 12 && sourcing_name != NULL)
25036 {
25037 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025038 verbose_enter_scroll();
25039
Bram Moolenaar555b2802005-05-19 21:08:39 +000025040 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025042
25043 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025044 --no_wait_return;
25045 }
25046
25047 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025048 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025050
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025051 /* If the a:000 list and the l: and a: dicts are not referenced we can
25052 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025053 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25054 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25055 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25056 {
25057 free_funccal(fc, FALSE);
25058 }
25059 else
25060 {
25061 hashitem_T *hi;
25062 listitem_T *li;
25063 int todo;
25064
25065 /* "fc" is still in use. This can happen when returning "a:000" or
25066 * assigning "l:" to a global variable.
25067 * Link "fc" in the list for garbage collection later. */
25068 fc->caller = previous_funccal;
25069 previous_funccal = fc;
25070
25071 /* Make a copy of the a: variables, since we didn't do that above. */
25072 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25073 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25074 {
25075 if (!HASHITEM_EMPTY(hi))
25076 {
25077 --todo;
25078 v = HI2DI(hi);
25079 copy_tv(&v->di_tv, &v->di_tv);
25080 }
25081 }
25082
25083 /* Make a copy of the a:000 items, since we didn't do that above. */
25084 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25085 copy_tv(&li->li_tv, &li->li_tv);
25086 }
25087}
25088
25089/*
25090 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025091 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025092 */
25093 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025094can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025095{
25096 return (fc->l_varlist.lv_copyID != copyID
25097 && fc->l_vars.dv_copyID != copyID
25098 && fc->l_avars.dv_copyID != copyID);
25099}
25100
25101/*
25102 * Free "fc" and what it contains.
25103 */
25104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025105free_funccal(
25106 funccall_T *fc,
25107 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025108{
25109 listitem_T *li;
25110
25111 /* The a: variables typevals may not have been allocated, only free the
25112 * allocated variables. */
25113 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25114
25115 /* free all l: variables */
25116 vars_clear(&fc->l_vars.dv_hashtab);
25117
25118 /* Free the a:000 variables if they were allocated. */
25119 if (free_val)
25120 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25121 clear_tv(&li->li_tv);
25122
25123 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124}
25125
25126/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025127 * Add a number variable "name" to dict "dp" with value "nr".
25128 */
25129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025130add_nr_var(
25131 dict_T *dp,
25132 dictitem_T *v,
25133 char *name,
25134 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025135{
25136 STRCPY(v->di_key, name);
25137 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25138 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25139 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025140 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025141 v->di_tv.vval.v_number = nr;
25142}
25143
25144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025145 * ":return [expr]"
25146 */
25147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025148ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149{
25150 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025151 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025152 int returning = FALSE;
25153
25154 if (current_funccal == NULL)
25155 {
25156 EMSG(_("E133: :return not inside a function"));
25157 return;
25158 }
25159
25160 if (eap->skip)
25161 ++emsg_skip;
25162
25163 eap->nextcmd = NULL;
25164 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025165 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166 {
25167 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025168 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025169 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025170 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025171 }
25172 /* It's safer to return also on error. */
25173 else if (!eap->skip)
25174 {
25175 /*
25176 * Return unless the expression evaluation has been cancelled due to an
25177 * aborting error, an interrupt, or an exception.
25178 */
25179 if (!aborting())
25180 returning = do_return(eap, FALSE, TRUE, NULL);
25181 }
25182
25183 /* When skipping or the return gets pending, advance to the next command
25184 * in this line (!returning). Otherwise, ignore the rest of the line.
25185 * Following lines will be ignored by get_func_line(). */
25186 if (returning)
25187 eap->nextcmd = NULL;
25188 else if (eap->nextcmd == NULL) /* no argument */
25189 eap->nextcmd = check_nextcmd(arg);
25190
25191 if (eap->skip)
25192 --emsg_skip;
25193}
25194
25195/*
25196 * Return from a function. Possibly makes the return pending. Also called
25197 * for a pending return at the ":endtry" or after returning from an extra
25198 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025199 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025200 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201 * FALSE when the return gets pending.
25202 */
25203 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025204do_return(
25205 exarg_T *eap,
25206 int reanimate,
25207 int is_cmd,
25208 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209{
25210 int idx;
25211 struct condstack *cstack = eap->cstack;
25212
25213 if (reanimate)
25214 /* Undo the return. */
25215 current_funccal->returned = FALSE;
25216
25217 /*
25218 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25219 * not in its finally clause (which then is to be executed next) is found.
25220 * In this case, make the ":return" pending for execution at the ":endtry".
25221 * Otherwise, return normally.
25222 */
25223 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25224 if (idx >= 0)
25225 {
25226 cstack->cs_pending[idx] = CSTP_RETURN;
25227
25228 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025229 /* A pending return again gets pending. "rettv" points to an
25230 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025232 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 else
25234 {
25235 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025236 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025237 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025238 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025240 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025241 {
25242 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025243 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025244 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 else
25246 EMSG(_(e_outofmem));
25247 }
25248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025249 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025250
25251 if (reanimate)
25252 {
25253 /* The pending return value could be overwritten by a ":return"
25254 * without argument in a finally clause; reset the default
25255 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025256 current_funccal->rettv->v_type = VAR_NUMBER;
25257 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258 }
25259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025260 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261 }
25262 else
25263 {
25264 current_funccal->returned = TRUE;
25265
25266 /* If the return is carried out now, store the return value. For
25267 * a return immediately after reanimation, the value is already
25268 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025269 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025271 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025272 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025274 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 }
25276 }
25277
25278 return idx < 0;
25279}
25280
25281/*
25282 * Free the variable with a pending return value.
25283 */
25284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025285discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286{
Bram Moolenaar33570922005-01-25 22:26:29 +000025287 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288}
25289
25290/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025291 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 * is an allocated string. Used by report_pending() for verbose messages.
25293 */
25294 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025295get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025297 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025298 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025299 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025300
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025301 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025302 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025303 if (s == NULL)
25304 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025305
25306 STRCPY(IObuff, ":return ");
25307 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25308 if (STRLEN(s) + 8 >= IOSIZE)
25309 STRCPY(IObuff + IOSIZE - 4, "...");
25310 vim_free(tofree);
25311 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312}
25313
25314/*
25315 * Get next function line.
25316 * Called by do_cmdline() to get the next line.
25317 * Returns allocated string, or NULL for end of function.
25318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025320get_func_line(
25321 int c UNUSED,
25322 void *cookie,
25323 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025324{
Bram Moolenaar33570922005-01-25 22:26:29 +000025325 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025326 ufunc_T *fp = fcp->func;
25327 char_u *retval;
25328 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329
25330 /* If breakpoints have been added/deleted need to check for it. */
25331 if (fcp->dbg_tick != debug_tick)
25332 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025333 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334 sourcing_lnum);
25335 fcp->dbg_tick = debug_tick;
25336 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025337#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025338 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025339 func_line_end(cookie);
25340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025341
Bram Moolenaar05159a02005-02-26 23:04:13 +000025342 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025343 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25344 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345 retval = NULL;
25346 else
25347 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025348 /* Skip NULL lines (continuation lines). */
25349 while (fcp->linenr < gap->ga_len
25350 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25351 ++fcp->linenr;
25352 if (fcp->linenr >= gap->ga_len)
25353 retval = NULL;
25354 else
25355 {
25356 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25357 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025358#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025359 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025360 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025361#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 }
25364
25365 /* Did we encounter a breakpoint? */
25366 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25367 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025368 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025370 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371 sourcing_lnum);
25372 fcp->dbg_tick = debug_tick;
25373 }
25374
25375 return retval;
25376}
25377
Bram Moolenaar05159a02005-02-26 23:04:13 +000025378#if defined(FEAT_PROFILE) || defined(PROTO)
25379/*
25380 * Called when starting to read a function line.
25381 * "sourcing_lnum" must be correct!
25382 * When skipping lines it may not actually be executed, but we won't find out
25383 * until later and we need to store the time now.
25384 */
25385 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025386func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025387{
25388 funccall_T *fcp = (funccall_T *)cookie;
25389 ufunc_T *fp = fcp->func;
25390
25391 if (fp->uf_profiling && sourcing_lnum >= 1
25392 && sourcing_lnum <= fp->uf_lines.ga_len)
25393 {
25394 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025395 /* Skip continuation lines. */
25396 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25397 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025398 fp->uf_tml_execed = FALSE;
25399 profile_start(&fp->uf_tml_start);
25400 profile_zero(&fp->uf_tml_children);
25401 profile_get_wait(&fp->uf_tml_wait);
25402 }
25403}
25404
25405/*
25406 * Called when actually executing a function line.
25407 */
25408 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025409func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025410{
25411 funccall_T *fcp = (funccall_T *)cookie;
25412 ufunc_T *fp = fcp->func;
25413
25414 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25415 fp->uf_tml_execed = TRUE;
25416}
25417
25418/*
25419 * Called when done with a function line.
25420 */
25421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025422func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025423{
25424 funccall_T *fcp = (funccall_T *)cookie;
25425 ufunc_T *fp = fcp->func;
25426
25427 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25428 {
25429 if (fp->uf_tml_execed)
25430 {
25431 ++fp->uf_tml_count[fp->uf_tml_idx];
25432 profile_end(&fp->uf_tml_start);
25433 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025434 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025435 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25436 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025437 }
25438 fp->uf_tml_idx = -1;
25439 }
25440}
25441#endif
25442
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443/*
25444 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025445 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446 */
25447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025448func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025449{
Bram Moolenaar33570922005-01-25 22:26:29 +000025450 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451
25452 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25453 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025454 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455 || fcp->returned);
25456}
25457
25458/*
25459 * return TRUE if cookie indicates a function which "abort"s on errors.
25460 */
25461 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025462func_has_abort(
25463 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025465 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025466}
25467
25468#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25469typedef enum
25470{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025471 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25472 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25473 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474} var_flavour_T;
25475
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025476static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477
25478 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025479var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480{
25481 char_u *p = varname;
25482
25483 if (ASCII_ISUPPER(*p))
25484 {
25485 while (*(++p))
25486 if (ASCII_ISLOWER(*p))
25487 return VAR_FLAVOUR_SESSION;
25488 return VAR_FLAVOUR_VIMINFO;
25489 }
25490 else
25491 return VAR_FLAVOUR_DEFAULT;
25492}
25493#endif
25494
25495#if defined(FEAT_VIMINFO) || defined(PROTO)
25496/*
25497 * Restore global vars that start with a capital from the viminfo file
25498 */
25499 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025500read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501{
25502 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025503 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025504 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025505 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506
25507 if (!writing && (find_viminfo_parameter('!') != NULL))
25508 {
25509 tab = vim_strchr(virp->vir_line + 1, '\t');
25510 if (tab != NULL)
25511 {
25512 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025513 switch (*tab)
25514 {
25515 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025516#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025517 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025518#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025519 case 'D': type = VAR_DICT; break;
25520 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025521 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523
25524 tab = vim_strchr(tab, '\t');
25525 if (tab != NULL)
25526 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025527 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025528 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025529 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025531#ifdef FEAT_FLOAT
25532 else if (type == VAR_FLOAT)
25533 (void)string2float(tab + 1, &tv.vval.v_float);
25534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025536 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025537 if (type == VAR_DICT || type == VAR_LIST)
25538 {
25539 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25540
25541 if (etv == NULL)
25542 /* Failed to parse back the dict or list, use it as a
25543 * string. */
25544 tv.v_type = VAR_STRING;
25545 else
25546 {
25547 vim_free(tv.vval.v_string);
25548 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025549 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025550 }
25551 }
25552
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025553 /* when in a function use global variables */
25554 save_funccal = current_funccal;
25555 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025556 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025557 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025558
25559 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025560 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025561 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25562 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 }
25564 }
25565 }
25566
25567 return viminfo_readline(virp);
25568}
25569
25570/*
25571 * Write global vars that start with a capital to the viminfo file
25572 */
25573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025574write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025575{
Bram Moolenaar33570922005-01-25 22:26:29 +000025576 hashitem_T *hi;
25577 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025578 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025579 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025580 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025581 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025582 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025583
25584 if (find_viminfo_parameter('!') == NULL)
25585 return;
25586
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025587 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025588
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025589 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025590 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025592 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025593 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025594 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025595 this_var = HI2DI(hi);
25596 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025597 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025598 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025599 {
25600 case VAR_STRING: s = "STR"; break;
25601 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025602 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025603 case VAR_DICT: s = "DIC"; break;
25604 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025605 case VAR_SPECIAL: s = "XPL"; break;
25606
25607 case VAR_UNKNOWN:
25608 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025609 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025610 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025611 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025612 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025613 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025614 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025615 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025616 if (p != NULL)
25617 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025618 vim_free(tofree);
25619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620 }
25621 }
25622}
25623#endif
25624
25625#if defined(FEAT_SESSION) || defined(PROTO)
25626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025627store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025628{
Bram Moolenaar33570922005-01-25 22:26:29 +000025629 hashitem_T *hi;
25630 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025631 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025632 char_u *p, *t;
25633
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025634 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025635 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025637 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025639 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025640 this_var = HI2DI(hi);
25641 if ((this_var->di_tv.v_type == VAR_NUMBER
25642 || this_var->di_tv.v_type == VAR_STRING)
25643 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025644 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025645 /* Escape special characters with a backslash. Turn a LF and
25646 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025647 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025648 (char_u *)"\\\"\n\r");
25649 if (p == NULL) /* out of memory */
25650 break;
25651 for (t = p; *t != NUL; ++t)
25652 if (*t == '\n')
25653 *t = 'n';
25654 else if (*t == '\r')
25655 *t = 'r';
25656 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025657 this_var->di_key,
25658 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25659 : ' ',
25660 p,
25661 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25662 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025663 || put_eol(fd) == FAIL)
25664 {
25665 vim_free(p);
25666 return FAIL;
25667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025668 vim_free(p);
25669 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025670#ifdef FEAT_FLOAT
25671 else if (this_var->di_tv.v_type == VAR_FLOAT
25672 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25673 {
25674 float_T f = this_var->di_tv.vval.v_float;
25675 int sign = ' ';
25676
25677 if (f < 0)
25678 {
25679 f = -f;
25680 sign = '-';
25681 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025682 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025683 this_var->di_key, sign, f) < 0)
25684 || put_eol(fd) == FAIL)
25685 return FAIL;
25686 }
25687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025688 }
25689 }
25690 return OK;
25691}
25692#endif
25693
Bram Moolenaar661b1822005-07-28 22:36:45 +000025694/*
25695 * Display script name where an item was last set.
25696 * Should only be invoked when 'verbose' is non-zero.
25697 */
25698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025699last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025700{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025701 char_u *p;
25702
Bram Moolenaar661b1822005-07-28 22:36:45 +000025703 if (scriptID != 0)
25704 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025705 p = home_replace_save(NULL, get_scriptname(scriptID));
25706 if (p != NULL)
25707 {
25708 verbose_enter();
25709 MSG_PUTS(_("\n\tLast set from "));
25710 MSG_PUTS(p);
25711 vim_free(p);
25712 verbose_leave();
25713 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025714 }
25715}
25716
Bram Moolenaard812df62008-11-09 12:46:09 +000025717/*
25718 * List v:oldfiles in a nice way.
25719 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025721ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025722{
25723 list_T *l = vimvars[VV_OLDFILES].vv_list;
25724 listitem_T *li;
25725 int nr = 0;
25726
25727 if (l == NULL)
25728 msg((char_u *)_("No old files"));
25729 else
25730 {
25731 msg_start();
25732 msg_scroll = TRUE;
25733 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25734 {
25735 msg_outnum((long)++nr);
25736 MSG_PUTS(": ");
25737 msg_outtrans(get_tv_string(&li->li_tv));
25738 msg_putchar('\n');
25739 out_flush(); /* output one line at a time */
25740 ui_breakcheck();
25741 }
25742 /* Assume "got_int" was set to truncate the listing. */
25743 got_int = FALSE;
25744
25745#ifdef FEAT_BROWSE_CMD
25746 if (cmdmod.browse)
25747 {
25748 quit_more = FALSE;
25749 nr = prompt_for_number(FALSE);
25750 msg_starthere();
25751 if (nr > 0)
25752 {
25753 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25754 (long)nr);
25755
25756 if (p != NULL)
25757 {
25758 p = expand_env_save(p);
25759 eap->arg = p;
25760 eap->cmdidx = CMD_edit;
25761 cmdmod.browse = FALSE;
25762 do_exedit(eap, NULL);
25763 vim_free(p);
25764 }
25765 }
25766 }
25767#endif
25768 }
25769}
25770
Bram Moolenaar53744302015-07-17 17:38:22 +020025771/* reset v:option_new, v:option_old and v:option_type */
25772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025773reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025774{
25775 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25776 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25777 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25778}
25779
25780
Bram Moolenaar071d4272004-06-13 20:20:40 +000025781#endif /* FEAT_EVAL */
25782
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025784#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025785
25786#ifdef WIN3264
25787/*
25788 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25789 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025790static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25791static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25792static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793
25794/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025795 * Get the short path (8.3) for the filename in "fnamep".
25796 * Only works for a valid file name.
25797 * When the path gets longer "fnamep" is changed and the allocated buffer
25798 * is put in "bufp".
25799 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25800 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025801 */
25802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025803get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025804{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025805 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 char_u *newbuf;
25807
25808 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025809 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025810 if (l > len - 1)
25811 {
25812 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025813 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814 newbuf = vim_strnsave(*fnamep, l);
25815 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025816 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025817
25818 vim_free(*bufp);
25819 *fnamep = *bufp = newbuf;
25820
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025821 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025822 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025823 }
25824
25825 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025826 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025827}
25828
25829/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025830 * Get the short path (8.3) for the filename in "fname". The converted
25831 * path is returned in "bufp".
25832 *
25833 * Some of the directories specified in "fname" may not exist. This function
25834 * will shorten the existing directories at the beginning of the path and then
25835 * append the remaining non-existing path.
25836 *
25837 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025838 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025839 * bufp - Pointer to an allocated buffer for the filename.
25840 * fnamelen - Length of the filename pointed to by fname
25841 *
25842 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025843 */
25844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025845shortpath_for_invalid_fname(
25846 char_u **fname,
25847 char_u **bufp,
25848 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025849{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025850 char_u *short_fname, *save_fname, *pbuf_unused;
25851 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025853 int old_len, len;
25854 int new_len, sfx_len;
25855 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025856
25857 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025858 old_len = *fnamelen;
25859 save_fname = vim_strnsave(*fname, old_len);
25860 pbuf_unused = NULL;
25861 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025863 endp = save_fname + old_len - 1; /* Find the end of the copy */
25864 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025865
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025866 /*
25867 * Try shortening the supplied path till it succeeds by removing one
25868 * directory at a time from the tail of the path.
25869 */
25870 len = 0;
25871 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025873 /* go back one path-separator */
25874 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25875 --endp;
25876 if (endp <= save_fname)
25877 break; /* processed the complete path */
25878
25879 /*
25880 * Replace the path separator with a NUL and try to shorten the
25881 * resulting path.
25882 */
25883 ch = *endp;
25884 *endp = 0;
25885 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025886 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025887 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25888 {
25889 retval = FAIL;
25890 goto theend;
25891 }
25892 *endp = ch; /* preserve the string */
25893
25894 if (len > 0)
25895 break; /* successfully shortened the path */
25896
25897 /* failed to shorten the path. Skip the path separator */
25898 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025899 }
25900
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025901 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025903 /*
25904 * Succeeded in shortening the path. Now concatenate the shortened
25905 * path with the remaining path at the tail.
25906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025907
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025908 /* Compute the length of the new path. */
25909 sfx_len = (int)(save_endp - endp) + 1;
25910 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025911
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025912 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025914 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025915 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025916 /* There is not enough space in the currently allocated string,
25917 * copy it to a buffer big enough. */
25918 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025920 {
25921 retval = FAIL;
25922 goto theend;
25923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025924 }
25925 else
25926 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025927 /* Transfer short_fname to the main buffer (it's big enough),
25928 * unless get_short_pathname() did its work in-place. */
25929 *fname = *bufp = save_fname;
25930 if (short_fname != save_fname)
25931 vim_strncpy(save_fname, short_fname, len);
25932 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025933 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025934
25935 /* concat the not-shortened part of the path */
25936 vim_strncpy(*fname + len, endp, sfx_len);
25937 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025938 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025939
25940theend:
25941 vim_free(pbuf_unused);
25942 vim_free(save_fname);
25943
25944 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025945}
25946
25947/*
25948 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025949 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950 */
25951 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025952shortpath_for_partial(
25953 char_u **fnamep,
25954 char_u **bufp,
25955 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956{
25957 int sepcount, len, tflen;
25958 char_u *p;
25959 char_u *pbuf, *tfname;
25960 int hasTilde;
25961
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025962 /* Count up the path separators from the RHS.. so we know which part
25963 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025964 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025965 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966 if (vim_ispathsep(*p))
25967 ++sepcount;
25968
25969 /* Need full path first (use expand_env() to remove a "~/") */
25970 hasTilde = (**fnamep == '~');
25971 if (hasTilde)
25972 pbuf = tfname = expand_env_save(*fnamep);
25973 else
25974 pbuf = tfname = FullName_save(*fnamep, FALSE);
25975
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025976 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025978 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25979 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980
25981 if (len == 0)
25982 {
25983 /* Don't have a valid filename, so shorten the rest of the
25984 * path if we can. This CAN give us invalid 8.3 filenames, but
25985 * there's not a lot of point in guessing what it might be.
25986 */
25987 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025988 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25989 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025990 }
25991
25992 /* Count the paths backward to find the beginning of the desired string. */
25993 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025994 {
25995#ifdef FEAT_MBYTE
25996 if (has_mbyte)
25997 p -= mb_head_off(tfname, p);
25998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 if (vim_ispathsep(*p))
26000 {
26001 if (sepcount == 0 || (hasTilde && sepcount == 1))
26002 break;
26003 else
26004 sepcount --;
26005 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007 if (hasTilde)
26008 {
26009 --p;
26010 if (p >= tfname)
26011 *p = '~';
26012 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014 }
26015 else
26016 ++p;
26017
26018 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26019 vim_free(*bufp);
26020 *fnamelen = (int)STRLEN(p);
26021 *bufp = pbuf;
26022 *fnamep = p;
26023
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026024 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025}
26026#endif /* WIN3264 */
26027
26028/*
26029 * Adjust a filename, according to a string of modifiers.
26030 * *fnamep must be NUL terminated when called. When returning, the length is
26031 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026032 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026033 * When there is an error, *fnamep is set to NULL.
26034 */
26035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026036modify_fname(
26037 char_u *src, /* string with modifiers */
26038 int *usedlen, /* characters after src that are used */
26039 char_u **fnamep, /* file name so far */
26040 char_u **bufp, /* buffer for allocated file name or NULL */
26041 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026042{
26043 int valid = 0;
26044 char_u *tail;
26045 char_u *s, *p, *pbuf;
26046 char_u dirname[MAXPATHL];
26047 int c;
26048 int has_fullname = 0;
26049#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026050 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051 int has_shortname = 0;
26052#endif
26053
26054repeat:
26055 /* ":p" - full path/file_name */
26056 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26057 {
26058 has_fullname = 1;
26059
26060 valid |= VALID_PATH;
26061 *usedlen += 2;
26062
26063 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26064 if ((*fnamep)[0] == '~'
26065#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26066 && ((*fnamep)[1] == '/'
26067# ifdef BACKSLASH_IN_FILENAME
26068 || (*fnamep)[1] == '\\'
26069# endif
26070 || (*fnamep)[1] == NUL)
26071
26072#endif
26073 )
26074 {
26075 *fnamep = expand_env_save(*fnamep);
26076 vim_free(*bufp); /* free any allocated file name */
26077 *bufp = *fnamep;
26078 if (*fnamep == NULL)
26079 return -1;
26080 }
26081
26082 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026083 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026084 {
26085 if (vim_ispathsep(*p)
26086 && p[1] == '.'
26087 && (p[2] == NUL
26088 || vim_ispathsep(p[2])
26089 || (p[2] == '.'
26090 && (p[3] == NUL || vim_ispathsep(p[3])))))
26091 break;
26092 }
26093
26094 /* FullName_save() is slow, don't use it when not needed. */
26095 if (*p != NUL || !vim_isAbsName(*fnamep))
26096 {
26097 *fnamep = FullName_save(*fnamep, *p != NUL);
26098 vim_free(*bufp); /* free any allocated file name */
26099 *bufp = *fnamep;
26100 if (*fnamep == NULL)
26101 return -1;
26102 }
26103
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026104#ifdef WIN3264
26105# if _WIN32_WINNT >= 0x0500
26106 if (vim_strchr(*fnamep, '~') != NULL)
26107 {
26108 /* Expand 8.3 filename to full path. Needed to make sure the same
26109 * file does not have two different names.
26110 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26111 p = alloc(_MAX_PATH + 1);
26112 if (p != NULL)
26113 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026114 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026115 {
26116 vim_free(*bufp);
26117 *bufp = *fnamep = p;
26118 }
26119 else
26120 vim_free(p);
26121 }
26122 }
26123# endif
26124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026125 /* Append a path separator to a directory. */
26126 if (mch_isdir(*fnamep))
26127 {
26128 /* Make room for one or two extra characters. */
26129 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26130 vim_free(*bufp); /* free any allocated file name */
26131 *bufp = *fnamep;
26132 if (*fnamep == NULL)
26133 return -1;
26134 add_pathsep(*fnamep);
26135 }
26136 }
26137
26138 /* ":." - path relative to the current directory */
26139 /* ":~" - path relative to the home directory */
26140 /* ":8" - shortname path - postponed till after */
26141 while (src[*usedlen] == ':'
26142 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26143 {
26144 *usedlen += 2;
26145 if (c == '8')
26146 {
26147#ifdef WIN3264
26148 has_shortname = 1; /* Postpone this. */
26149#endif
26150 continue;
26151 }
26152 pbuf = NULL;
26153 /* Need full path first (use expand_env() to remove a "~/") */
26154 if (!has_fullname)
26155 {
26156 if (c == '.' && **fnamep == '~')
26157 p = pbuf = expand_env_save(*fnamep);
26158 else
26159 p = pbuf = FullName_save(*fnamep, FALSE);
26160 }
26161 else
26162 p = *fnamep;
26163
26164 has_fullname = 0;
26165
26166 if (p != NULL)
26167 {
26168 if (c == '.')
26169 {
26170 mch_dirname(dirname, MAXPATHL);
26171 s = shorten_fname(p, dirname);
26172 if (s != NULL)
26173 {
26174 *fnamep = s;
26175 if (pbuf != NULL)
26176 {
26177 vim_free(*bufp); /* free any allocated file name */
26178 *bufp = pbuf;
26179 pbuf = NULL;
26180 }
26181 }
26182 }
26183 else
26184 {
26185 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26186 /* Only replace it when it starts with '~' */
26187 if (*dirname == '~')
26188 {
26189 s = vim_strsave(dirname);
26190 if (s != NULL)
26191 {
26192 *fnamep = s;
26193 vim_free(*bufp);
26194 *bufp = s;
26195 }
26196 }
26197 }
26198 vim_free(pbuf);
26199 }
26200 }
26201
26202 tail = gettail(*fnamep);
26203 *fnamelen = (int)STRLEN(*fnamep);
26204
26205 /* ":h" - head, remove "/file_name", can be repeated */
26206 /* Don't remove the first "/" or "c:\" */
26207 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26208 {
26209 valid |= VALID_HEAD;
26210 *usedlen += 2;
26211 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026212 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026213 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026214 *fnamelen = (int)(tail - *fnamep);
26215#ifdef VMS
26216 if (*fnamelen > 0)
26217 *fnamelen += 1; /* the path separator is part of the path */
26218#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026219 if (*fnamelen == 0)
26220 {
26221 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26222 p = vim_strsave((char_u *)".");
26223 if (p == NULL)
26224 return -1;
26225 vim_free(*bufp);
26226 *bufp = *fnamep = tail = p;
26227 *fnamelen = 1;
26228 }
26229 else
26230 {
26231 while (tail > s && !after_pathsep(s, tail))
26232 mb_ptr_back(*fnamep, tail);
26233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026234 }
26235
26236 /* ":8" - shortname */
26237 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26238 {
26239 *usedlen += 2;
26240#ifdef WIN3264
26241 has_shortname = 1;
26242#endif
26243 }
26244
26245#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026246 /*
26247 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026248 */
26249 if (has_shortname)
26250 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026251 /* Copy the string if it is shortened by :h and when it wasn't copied
26252 * yet, because we are going to change it in place. Avoids changing
26253 * the buffer name for "%:8". */
26254 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026255 {
26256 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026257 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026258 return -1;
26259 vim_free(*bufp);
26260 *bufp = *fnamep = p;
26261 }
26262
26263 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026264 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026265 if (!has_fullname && !vim_isAbsName(*fnamep))
26266 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026267 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026268 return -1;
26269 }
26270 else
26271 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026272 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026273
Bram Moolenaardc935552011-08-17 15:23:23 +020026274 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026275 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026276 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026277 return -1;
26278
26279 if (l == 0)
26280 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026281 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026282 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026283 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026284 return -1;
26285 }
26286 *fnamelen = l;
26287 }
26288 }
26289#endif /* WIN3264 */
26290
26291 /* ":t" - tail, just the basename */
26292 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26293 {
26294 *usedlen += 2;
26295 *fnamelen -= (int)(tail - *fnamep);
26296 *fnamep = tail;
26297 }
26298
26299 /* ":e" - extension, can be repeated */
26300 /* ":r" - root, without extension, can be repeated */
26301 while (src[*usedlen] == ':'
26302 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26303 {
26304 /* find a '.' in the tail:
26305 * - for second :e: before the current fname
26306 * - otherwise: The last '.'
26307 */
26308 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26309 s = *fnamep - 2;
26310 else
26311 s = *fnamep + *fnamelen - 1;
26312 for ( ; s > tail; --s)
26313 if (s[0] == '.')
26314 break;
26315 if (src[*usedlen + 1] == 'e') /* :e */
26316 {
26317 if (s > tail)
26318 {
26319 *fnamelen += (int)(*fnamep - (s + 1));
26320 *fnamep = s + 1;
26321#ifdef VMS
26322 /* cut version from the extension */
26323 s = *fnamep + *fnamelen - 1;
26324 for ( ; s > *fnamep; --s)
26325 if (s[0] == ';')
26326 break;
26327 if (s > *fnamep)
26328 *fnamelen = s - *fnamep;
26329#endif
26330 }
26331 else if (*fnamep <= tail)
26332 *fnamelen = 0;
26333 }
26334 else /* :r */
26335 {
26336 if (s > tail) /* remove one extension */
26337 *fnamelen = (int)(s - *fnamep);
26338 }
26339 *usedlen += 2;
26340 }
26341
26342 /* ":s?pat?foo?" - substitute */
26343 /* ":gs?pat?foo?" - global substitute */
26344 if (src[*usedlen] == ':'
26345 && (src[*usedlen + 1] == 's'
26346 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26347 {
26348 char_u *str;
26349 char_u *pat;
26350 char_u *sub;
26351 int sep;
26352 char_u *flags;
26353 int didit = FALSE;
26354
26355 flags = (char_u *)"";
26356 s = src + *usedlen + 2;
26357 if (src[*usedlen + 1] == 'g')
26358 {
26359 flags = (char_u *)"g";
26360 ++s;
26361 }
26362
26363 sep = *s++;
26364 if (sep)
26365 {
26366 /* find end of pattern */
26367 p = vim_strchr(s, sep);
26368 if (p != NULL)
26369 {
26370 pat = vim_strnsave(s, (int)(p - s));
26371 if (pat != NULL)
26372 {
26373 s = p + 1;
26374 /* find end of substitution */
26375 p = vim_strchr(s, sep);
26376 if (p != NULL)
26377 {
26378 sub = vim_strnsave(s, (int)(p - s));
26379 str = vim_strnsave(*fnamep, *fnamelen);
26380 if (sub != NULL && str != NULL)
26381 {
26382 *usedlen = (int)(p + 1 - src);
26383 s = do_string_sub(str, pat, sub, flags);
26384 if (s != NULL)
26385 {
26386 *fnamep = s;
26387 *fnamelen = (int)STRLEN(s);
26388 vim_free(*bufp);
26389 *bufp = s;
26390 didit = TRUE;
26391 }
26392 }
26393 vim_free(sub);
26394 vim_free(str);
26395 }
26396 vim_free(pat);
26397 }
26398 }
26399 /* after using ":s", repeat all the modifiers */
26400 if (didit)
26401 goto repeat;
26402 }
26403 }
26404
Bram Moolenaar26df0922014-02-23 23:39:13 +010026405 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26406 {
26407 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26408 if (p == NULL)
26409 return -1;
26410 vim_free(*bufp);
26411 *bufp = *fnamep = p;
26412 *fnamelen = (int)STRLEN(p);
26413 *usedlen += 2;
26414 }
26415
Bram Moolenaar071d4272004-06-13 20:20:40 +000026416 return valid;
26417}
26418
26419/*
26420 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26421 * "flags" can be "g" to do a global substitute.
26422 * Returns an allocated string, NULL for error.
26423 */
26424 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026425do_string_sub(
26426 char_u *str,
26427 char_u *pat,
26428 char_u *sub,
26429 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026430{
26431 int sublen;
26432 regmatch_T regmatch;
26433 int i;
26434 int do_all;
26435 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026436 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026437 garray_T ga;
26438 char_u *ret;
26439 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026440 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026441
26442 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26443 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026444 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026445
26446 ga_init2(&ga, 1, 200);
26447
26448 do_all = (flags[0] == 'g');
26449
26450 regmatch.rm_ic = p_ic;
26451 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26452 if (regmatch.regprog != NULL)
26453 {
26454 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026455 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026456 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26457 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026458 /* Skip empty match except for first match. */
26459 if (regmatch.startp[0] == regmatch.endp[0])
26460 {
26461 if (zero_width == regmatch.startp[0])
26462 {
26463 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026464 i = MB_PTR2LEN(tail);
26465 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26466 (size_t)i);
26467 ga.ga_len += i;
26468 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026469 continue;
26470 }
26471 zero_width = regmatch.startp[0];
26472 }
26473
Bram Moolenaar071d4272004-06-13 20:20:40 +000026474 /*
26475 * Get some space for a temporary buffer to do the substitution
26476 * into. It will contain:
26477 * - The text up to where the match is.
26478 * - The substituted text.
26479 * - The text after the match.
26480 */
26481 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026482 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026483 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26484 {
26485 ga_clear(&ga);
26486 break;
26487 }
26488
26489 /* copy the text up to where the match is */
26490 i = (int)(regmatch.startp[0] - tail);
26491 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26492 /* add the substituted text */
26493 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26494 + ga.ga_len + i, TRUE, TRUE, FALSE);
26495 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026496 tail = regmatch.endp[0];
26497 if (*tail == NUL)
26498 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026499 if (!do_all)
26500 break;
26501 }
26502
26503 if (ga.ga_data != NULL)
26504 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26505
Bram Moolenaar473de612013-06-08 18:19:48 +020026506 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026507 }
26508
26509 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26510 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026511 if (p_cpo == empty_option)
26512 p_cpo = save_cpo;
26513 else
26514 /* Darn, evaluating {sub} expression changed the value. */
26515 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026516
26517 return ret;
26518}
26519
26520#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */