blob: edb2b258d8234dec6216d48adf0a5fd42a91cc76 [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);
437static int list_append_number(list_T *l, varnumber_T n);
438static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
439static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
440static list_T *list_copy(list_T *orig, int deep, int copyID);
441static char_u *list2string(typval_T *tv, int copyID);
442static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
443static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
444static int free_unref_items(int copyID);
445static dictitem_T *dictitem_copy(dictitem_T *org);
446static void dictitem_remove(dict_T *dict, dictitem_T *item);
447static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
448static long dict_len(dict_T *d);
449static char_u *dict2string(typval_T *tv, int copyID);
450static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
451static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *string_quote(char_u *str, int function);
454static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
455static int find_internal_func(char_u *name);
456static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
457static 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, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100458static void emsg_funcname(char *ermsg, char_u *name);
459static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000460
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static void f_abs(typval_T *argvars, typval_T *rettv);
463static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_add(typval_T *argvars, typval_T *rettv);
466static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
467static void f_and(typval_T *argvars, typval_T *rettv);
468static void f_append(typval_T *argvars, typval_T *rettv);
469static void f_argc(typval_T *argvars, typval_T *rettv);
470static void f_argidx(typval_T *argvars, typval_T *rettv);
471static void f_arglistid(typval_T *argvars, typval_T *rettv);
472static void f_argv(typval_T *argvars, typval_T *rettv);
473static void f_assert_equal(typval_T *argvars, typval_T *rettv);
474static void f_assert_exception(typval_T *argvars, typval_T *rettv);
475static void f_assert_fails(typval_T *argvars, typval_T *rettv);
476static void f_assert_false(typval_T *argvars, typval_T *rettv);
477static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100479static void f_asin(typval_T *argvars, typval_T *rettv);
480static void f_atan(typval_T *argvars, typval_T *rettv);
481static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100483static void f_browse(typval_T *argvars, typval_T *rettv);
484static void f_browsedir(typval_T *argvars, typval_T *rettv);
485static void f_bufexists(typval_T *argvars, typval_T *rettv);
486static void f_buflisted(typval_T *argvars, typval_T *rettv);
487static void f_bufloaded(typval_T *argvars, typval_T *rettv);
488static void f_bufname(typval_T *argvars, typval_T *rettv);
489static void f_bufnr(typval_T *argvars, typval_T *rettv);
490static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
491static void f_byte2line(typval_T *argvars, typval_T *rettv);
492static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
493static void f_byteidx(typval_T *argvars, typval_T *rettv);
494static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
495static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100499#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100500static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100501static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
502static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100503static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100504# ifdef FEAT_JOB
505static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
506# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100507static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
509static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100510static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100511static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100514static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100516#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_changenr(typval_T *argvars, typval_T *rettv);
518static void f_char2nr(typval_T *argvars, typval_T *rettv);
519static void f_cindent(typval_T *argvars, typval_T *rettv);
520static void f_clearmatches(typval_T *argvars, typval_T *rettv);
521static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000522#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_complete(typval_T *argvars, typval_T *rettv);
524static void f_complete_add(typval_T *argvars, typval_T *rettv);
525static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_confirm(typval_T *argvars, typval_T *rettv);
528static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_cos(typval_T *argvars, typval_T *rettv);
531static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_count(typval_T *argvars, typval_T *rettv);
534static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
535static void f_cursor(typval_T *argsvars, typval_T *rettv);
536static void f_deepcopy(typval_T *argvars, typval_T *rettv);
537static void f_delete(typval_T *argvars, typval_T *rettv);
538static void f_did_filetype(typval_T *argvars, typval_T *rettv);
539static void f_diff_filler(typval_T *argvars, typval_T *rettv);
540static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100541static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_empty(typval_T *argvars, typval_T *rettv);
543static void f_escape(typval_T *argvars, typval_T *rettv);
544static void f_eval(typval_T *argvars, typval_T *rettv);
545static void f_eventhandler(typval_T *argvars, typval_T *rettv);
546static void f_executable(typval_T *argvars, typval_T *rettv);
547static void f_exepath(typval_T *argvars, typval_T *rettv);
548static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200551#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100552static void f_expand(typval_T *argvars, typval_T *rettv);
553static void f_extend(typval_T *argvars, typval_T *rettv);
554static void f_feedkeys(typval_T *argvars, typval_T *rettv);
555static void f_filereadable(typval_T *argvars, typval_T *rettv);
556static void f_filewritable(typval_T *argvars, typval_T *rettv);
557static void f_filter(typval_T *argvars, typval_T *rettv);
558static void f_finddir(typval_T *argvars, typval_T *rettv);
559static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_float2nr(typval_T *argvars, typval_T *rettv);
562static void f_floor(typval_T *argvars, typval_T *rettv);
563static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000564#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100565static void f_fnameescape(typval_T *argvars, typval_T *rettv);
566static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
567static void f_foldclosed(typval_T *argvars, typval_T *rettv);
568static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
569static void f_foldlevel(typval_T *argvars, typval_T *rettv);
570static void f_foldtext(typval_T *argvars, typval_T *rettv);
571static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
572static void f_foreground(typval_T *argvars, typval_T *rettv);
573static void f_function(typval_T *argvars, typval_T *rettv);
574static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
575static void f_get(typval_T *argvars, typval_T *rettv);
576static void f_getbufline(typval_T *argvars, typval_T *rettv);
577static void f_getbufvar(typval_T *argvars, typval_T *rettv);
578static void f_getchar(typval_T *argvars, typval_T *rettv);
579static void f_getcharmod(typval_T *argvars, typval_T *rettv);
580static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
581static void f_getcmdline(typval_T *argvars, typval_T *rettv);
582static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
583static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
584static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
585static void f_getcwd(typval_T *argvars, typval_T *rettv);
586static void f_getfontname(typval_T *argvars, typval_T *rettv);
587static void f_getfperm(typval_T *argvars, typval_T *rettv);
588static void f_getfsize(typval_T *argvars, typval_T *rettv);
589static void f_getftime(typval_T *argvars, typval_T *rettv);
590static void f_getftype(typval_T *argvars, typval_T *rettv);
591static void f_getline(typval_T *argvars, typval_T *rettv);
592static void f_getmatches(typval_T *argvars, typval_T *rettv);
593static void f_getpid(typval_T *argvars, typval_T *rettv);
594static void f_getcurpos(typval_T *argvars, typval_T *rettv);
595static void f_getpos(typval_T *argvars, typval_T *rettv);
596static void f_getqflist(typval_T *argvars, typval_T *rettv);
597static void f_getreg(typval_T *argvars, typval_T *rettv);
598static void f_getregtype(typval_T *argvars, typval_T *rettv);
599static void f_gettabvar(typval_T *argvars, typval_T *rettv);
600static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
601static void f_getwinposx(typval_T *argvars, typval_T *rettv);
602static void f_getwinposy(typval_T *argvars, typval_T *rettv);
603static void f_getwinvar(typval_T *argvars, typval_T *rettv);
604static void f_glob(typval_T *argvars, typval_T *rettv);
605static void f_globpath(typval_T *argvars, typval_T *rettv);
606static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
607static void f_has(typval_T *argvars, typval_T *rettv);
608static void f_has_key(typval_T *argvars, typval_T *rettv);
609static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
610static void f_hasmapto(typval_T *argvars, typval_T *rettv);
611static void f_histadd(typval_T *argvars, typval_T *rettv);
612static void f_histdel(typval_T *argvars, typval_T *rettv);
613static void f_histget(typval_T *argvars, typval_T *rettv);
614static void f_histnr(typval_T *argvars, typval_T *rettv);
615static void f_hlID(typval_T *argvars, typval_T *rettv);
616static void f_hlexists(typval_T *argvars, typval_T *rettv);
617static void f_hostname(typval_T *argvars, typval_T *rettv);
618static void f_iconv(typval_T *argvars, typval_T *rettv);
619static void f_indent(typval_T *argvars, typval_T *rettv);
620static void f_index(typval_T *argvars, typval_T *rettv);
621static void f_input(typval_T *argvars, typval_T *rettv);
622static void f_inputdialog(typval_T *argvars, typval_T *rettv);
623static void f_inputlist(typval_T *argvars, typval_T *rettv);
624static void f_inputrestore(typval_T *argvars, typval_T *rettv);
625static void f_inputsave(typval_T *argvars, typval_T *rettv);
626static void f_inputsecret(typval_T *argvars, typval_T *rettv);
627static void f_insert(typval_T *argvars, typval_T *rettv);
628static void f_invert(typval_T *argvars, typval_T *rettv);
629static void f_isdirectory(typval_T *argvars, typval_T *rettv);
630static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100631#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
632static void f_isnan(typval_T *argvars, typval_T *rettv);
633#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100635#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100636# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100637static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100638# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100639static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100640static void f_job_start(typval_T *argvars, typval_T *rettv);
641static void f_job_stop(typval_T *argvars, typval_T *rettv);
642static void f_job_status(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100645static void f_js_decode(typval_T *argvars, typval_T *rettv);
646static void f_js_encode(typval_T *argvars, typval_T *rettv);
647static void f_json_decode(typval_T *argvars, typval_T *rettv);
648static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_keys(typval_T *argvars, typval_T *rettv);
650static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
651static void f_len(typval_T *argvars, typval_T *rettv);
652static void f_libcall(typval_T *argvars, typval_T *rettv);
653static void f_libcallnr(typval_T *argvars, typval_T *rettv);
654static void f_line(typval_T *argvars, typval_T *rettv);
655static void f_line2byte(typval_T *argvars, typval_T *rettv);
656static void f_lispindent(typval_T *argvars, typval_T *rettv);
657static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_log(typval_T *argvars, typval_T *rettv);
660static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000661#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200662#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100663static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200664#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_map(typval_T *argvars, typval_T *rettv);
666static void f_maparg(typval_T *argvars, typval_T *rettv);
667static void f_mapcheck(typval_T *argvars, typval_T *rettv);
668static void f_match(typval_T *argvars, typval_T *rettv);
669static void f_matchadd(typval_T *argvars, typval_T *rettv);
670static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
671static void f_matcharg(typval_T *argvars, typval_T *rettv);
672static void f_matchdelete(typval_T *argvars, typval_T *rettv);
673static void f_matchend(typval_T *argvars, typval_T *rettv);
674static void f_matchlist(typval_T *argvars, typval_T *rettv);
675static void f_matchstr(typval_T *argvars, typval_T *rettv);
676static void f_max(typval_T *argvars, typval_T *rettv);
677static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000678#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100682#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100684#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
686static void f_nr2char(typval_T *argvars, typval_T *rettv);
687static void f_or(typval_T *argvars, typval_T *rettv);
688static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100689#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100691#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
696static void f_printf(typval_T *argvars, typval_T *rettv);
697static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
701#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200703#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_range(typval_T *argvars, typval_T *rettv);
705static void f_readfile(typval_T *argvars, typval_T *rettv);
706static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100707#ifdef FEAT_FLOAT
708static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_reltimestr(typval_T *argvars, typval_T *rettv);
711static void f_remote_expr(typval_T *argvars, typval_T *rettv);
712static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
713static void f_remote_peek(typval_T *argvars, typval_T *rettv);
714static void f_remote_read(typval_T *argvars, typval_T *rettv);
715static void f_remote_send(typval_T *argvars, typval_T *rettv);
716static void f_remove(typval_T *argvars, typval_T *rettv);
717static void f_rename(typval_T *argvars, typval_T *rettv);
718static void f_repeat(typval_T *argvars, typval_T *rettv);
719static void f_resolve(typval_T *argvars, typval_T *rettv);
720static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000723#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100724static void f_screenattr(typval_T *argvars, typval_T *rettv);
725static void f_screenchar(typval_T *argvars, typval_T *rettv);
726static void f_screencol(typval_T *argvars, typval_T *rettv);
727static void f_screenrow(typval_T *argvars, typval_T *rettv);
728static void f_search(typval_T *argvars, typval_T *rettv);
729static void f_searchdecl(typval_T *argvars, typval_T *rettv);
730static void f_searchpair(typval_T *argvars, typval_T *rettv);
731static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
732static void f_searchpos(typval_T *argvars, typval_T *rettv);
733static void f_server2client(typval_T *argvars, typval_T *rettv);
734static void f_serverlist(typval_T *argvars, typval_T *rettv);
735static void f_setbufvar(typval_T *argvars, typval_T *rettv);
736static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
737static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100738static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_setline(typval_T *argvars, typval_T *rettv);
740static void f_setloclist(typval_T *argvars, typval_T *rettv);
741static void f_setmatches(typval_T *argvars, typval_T *rettv);
742static void f_setpos(typval_T *argvars, typval_T *rettv);
743static void f_setqflist(typval_T *argvars, typval_T *rettv);
744static void f_setreg(typval_T *argvars, typval_T *rettv);
745static void f_settabvar(typval_T *argvars, typval_T *rettv);
746static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
747static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100748#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100750#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_shellescape(typval_T *argvars, typval_T *rettv);
752static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
753static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000754#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100755static void f_sin(typval_T *argvars, typval_T *rettv);
756static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000757#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sort(typval_T *argvars, typval_T *rettv);
759static void f_soundfold(typval_T *argvars, typval_T *rettv);
760static void f_spellbadword(typval_T *argvars, typval_T *rettv);
761static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
762static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_sqrt(typval_T *argvars, typval_T *rettv);
765static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_str2nr(typval_T *argvars, typval_T *rettv);
768static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000769#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_stridx(typval_T *argvars, typval_T *rettv);
773static void f_string(typval_T *argvars, typval_T *rettv);
774static void f_strlen(typval_T *argvars, typval_T *rettv);
775static void f_strpart(typval_T *argvars, typval_T *rettv);
776static void f_strridx(typval_T *argvars, typval_T *rettv);
777static void f_strtrans(typval_T *argvars, typval_T *rettv);
778static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
779static void f_strwidth(typval_T *argvars, typval_T *rettv);
780static void f_submatch(typval_T *argvars, typval_T *rettv);
781static void f_substitute(typval_T *argvars, typval_T *rettv);
782static void f_synID(typval_T *argvars, typval_T *rettv);
783static void f_synIDattr(typval_T *argvars, typval_T *rettv);
784static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
785static void f_synstack(typval_T *argvars, typval_T *rettv);
786static void f_synconcealed(typval_T *argvars, typval_T *rettv);
787static void f_system(typval_T *argvars, typval_T *rettv);
788static void f_systemlist(typval_T *argvars, typval_T *rettv);
789static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
790static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
791static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
792static void f_taglist(typval_T *argvars, typval_T *rettv);
793static void f_tagfiles(typval_T *argvars, typval_T *rettv);
794static void f_tempname(typval_T *argvars, typval_T *rettv);
795static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200796#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100797static void f_tan(typval_T *argvars, typval_T *rettv);
798static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200799#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);
814static void f_winbufnr(typval_T *argvars, typval_T *rettv);
815static void f_wincol(typval_T *argvars, typval_T *rettv);
816static void f_winheight(typval_T *argvars, typval_T *rettv);
817static void f_winline(typval_T *argvars, typval_T *rettv);
818static void f_winnr(typval_T *argvars, typval_T *rettv);
819static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
820static void f_winrestview(typval_T *argvars, typval_T *rettv);
821static void f_winsaveview(typval_T *argvars, typval_T *rettv);
822static void f_winwidth(typval_T *argvars, typval_T *rettv);
823static void f_writefile(typval_T *argvars, typval_T *rettv);
824static void f_wordcount(typval_T *argvars, typval_T *rettv);
825static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000826
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
828static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
829static int get_env_len(char_u **arg);
830static int get_id_len(char_u **arg);
831static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
832static 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 +0000833#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
834#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
835 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100836static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
837static int eval_isnamec(int c);
838static int eval_isnamec1(int c);
839static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
840static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static typval_T *alloc_string_tv(char_u *string);
842static void init_tv(typval_T *varp);
843static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100844#ifdef FEAT_FLOAT
845static float_T get_tv_float(typval_T *varp);
846#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static linenr_T get_tv_lnum(typval_T *argvars);
848static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
849static char_u *get_tv_string(typval_T *varp);
850static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
851static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
852static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
853static hashtab_T *find_var_ht(char_u *name, char_u **varname);
854static funccall_T *get_funccal(void);
855static void vars_clear_ext(hashtab_T *ht, int free_val);
856static void delete_var(hashtab_T *ht, hashitem_T *hi);
857static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
858static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
859static void set_var(char_u *name, typval_T *varp, int copy);
860static int var_check_ro(int flags, char_u *name, int use_gettext);
861static int var_check_fixed(int flags, char_u *name, int use_gettext);
862static int var_check_func_name(char_u *name, int new_var);
863static int valid_varname(char_u *varname);
864static int tv_check_lock(int lock, char_u *name, int use_gettext);
865static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
866static char_u *find_option_end(char_u **arg, int *opt_flags);
867static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
868static int eval_fname_script(char_u *p);
869static int eval_fname_sid(char_u *p);
870static void list_func_head(ufunc_T *fp, int indent);
871static ufunc_T *find_func(char_u *name);
872static int function_exists(char_u *name);
873static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000874#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100875static void func_do_profile(ufunc_T *fp);
876static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
877static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000878static int
879# ifdef __BORLANDC__
880 _RTLENTRYF
881# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100882 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000883static int
884# ifdef __BORLANDC__
885 _RTLENTRYF
886# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100887 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000888#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100889static int script_autoload(char_u *name, int reload);
890static char_u *autoload_name(char_u *name);
891static void cat_func_name(char_u *buf, ufunc_T *fp);
892static void func_free(ufunc_T *fp);
893static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
894static int can_free_funccal(funccall_T *fc, int copyID) ;
895static void free_funccal(funccall_T *fc, int free_val);
896static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
897static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
898static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
899static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
900static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
901static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
902static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
903static int write_list(FILE *fd, list_T *list, int binary);
904static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000905
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200906
907#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100908static int compare_func_name(const void *s1, const void *s2);
909static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200910#endif
911
Bram Moolenaar33570922005-01-25 22:26:29 +0000912/*
913 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000914 */
915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100916eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000917{
Bram Moolenaar33570922005-01-25 22:26:29 +0000918 int i;
919 struct vimvar *p;
920
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200921 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
922 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200923 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000924 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000925 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000926
927 for (i = 0; i < VV_LEN; ++i)
928 {
929 p = &vimvars[i];
930 STRCPY(p->vv_di.di_key, p->vv_name);
931 if (p->vv_flags & VV_RO)
932 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
933 else if (p->vv_flags & VV_RO_SBX)
934 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
935 else
936 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000937
938 /* add to v: scope dict, unless the value is not always available */
939 if (p->vv_type != VAR_UNKNOWN)
940 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000941 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000942 /* add to compat scope dict */
943 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000944 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100945 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
946
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000947 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100948 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200949 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100950 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100951
952 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
953 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
954 set_vim_var_nr(VV_NONE, VVAL_NONE);
955 set_vim_var_nr(VV_NULL, VVAL_NULL);
956
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200957 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200958
959#ifdef EBCDIC
960 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100961 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200962 */
963 sortFunctions();
964#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000965}
966
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000967#if defined(EXITFREE) || defined(PROTO)
968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100969eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000970{
971 int i;
972 struct vimvar *p;
973
974 for (i = 0; i < VV_LEN; ++i)
975 {
976 p = &vimvars[i];
977 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000978 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000979 vim_free(p->vv_str);
980 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000981 }
982 else if (p->vv_di.di_tv.v_type == VAR_LIST)
983 {
984 list_unref(p->vv_list);
985 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000986 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000987 }
988 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000989 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990 hash_clear(&compat_hashtab);
991
Bram Moolenaard9fba312005-06-26 22:34:35 +0000992 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100993# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200994 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100995# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000996
997 /* global variables */
998 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000999
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001000 /* autoloaded script names */
1001 ga_clear_strings(&ga_loaded);
1002
Bram Moolenaarcca74132013-09-25 21:00:28 +02001003 /* Script-local variables. First clear all the variables and in a second
1004 * loop free the scriptvar_T, because a variable in one script might hold
1005 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001007 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001008 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001010 ga_clear(&ga_scripts);
1011
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001012 /* unreferenced lists and dicts */
1013 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001014
1015 /* functions */
1016 free_all_functions();
1017 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001018}
1019#endif
1020
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 * Return the name of the executed function.
1023 */
1024 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001025func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028}
1029
1030/*
1031 * Return the address holding the next breakpoint line for a funccall cookie.
1032 */
1033 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001034func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
Bram Moolenaar33570922005-01-25 22:26:29 +00001036 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037}
1038
1039/*
1040 * Return the address holding the debug tick for a funccall cookie.
1041 */
1042 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001043func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
Bram Moolenaar33570922005-01-25 22:26:29 +00001045 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046}
1047
1048/*
1049 * Return the nesting level for a funccall cookie.
1050 */
1051 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001052func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
Bram Moolenaar33570922005-01-25 22:26:29 +00001054 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055}
1056
1057/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001058funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001060/* pointer to list of previously used funccal, still around because some
1061 * item in it is still being used. */
1062funccall_T *previous_funccal = NULL;
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064/*
1065 * Return TRUE when a function was ended by a ":return" command.
1066 */
1067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001068current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
1070 return current_funccal->returned;
1071}
1072
1073
1074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 * Set an internal variable to a string value. Creates the variable if it does
1076 * not already exist.
1077 */
1078 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001079set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001081 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001082 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
1084 val = vim_strsave(value);
1085 if (val != NULL)
1086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001087 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001088 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001090 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001091 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
1093 }
1094}
1095
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001097static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098static char_u *redir_endp = NULL;
1099static char_u *redir_varname = NULL;
1100
1101/*
1102 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001103 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 * Returns OK if successfully completed the setup. FAIL otherwise.
1105 */
1106 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001107var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
1109 int save_emsg;
1110 int err;
1111 typval_T tv;
1112
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001113 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001114 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 {
1116 EMSG(_(e_invarg));
1117 return FAIL;
1118 }
1119
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001120 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 redir_varname = vim_strsave(name);
1122 if (redir_varname == NULL)
1123 return FAIL;
1124
1125 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1126 if (redir_lval == NULL)
1127 {
1128 var_redir_stop();
1129 return FAIL;
1130 }
1131
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132 /* The output is stored in growarray "redir_ga" until redirection ends. */
1133 ga_init2(&redir_ga, (int)sizeof(char), 500);
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001136 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001137 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1139 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001140 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_endp != NULL && *redir_endp != NUL)
1142 /* Trailing characters are present after the variable name */
1143 EMSG(_(e_trailing));
1144 else
1145 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 var_redir_stop();
1148 return FAIL;
1149 }
1150
1151 /* check if we can write to the variable: set it to or append an empty
1152 * string */
1153 save_emsg = did_emsg;
1154 did_emsg = FALSE;
1155 tv.v_type = VAR_STRING;
1156 tv.vval.v_string = (char_u *)"";
1157 if (append)
1158 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1159 else
1160 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001161 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001163 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 if (err)
1165 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001166 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 var_redir_stop();
1168 return FAIL;
1169 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170
1171 return OK;
1172}
1173
1174/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001175 * Append "value[value_len]" to the variable set by var_redir_start().
1176 * The actual appending is postponed until redirection ends, because the value
1177 * appended may in fact be the string we write to, changing it may cause freed
1178 * memory to be used:
1179 * :redir => foo
1180 * :let foo
1181 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 */
1183 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001184var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001186 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187
1188 if (redir_lval == NULL)
1189 return;
1190
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001191 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001192 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001194 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001196 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197 {
1198 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001199 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001200 }
1201 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203}
1204
1205/*
1206 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001207 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 */
1209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001210var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001212 typval_T tv;
1213
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214 if (redir_lval != NULL)
1215 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001216 /* If there was no error: assign the text to the variable. */
1217 if (redir_endp != NULL)
1218 {
1219 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1220 tv.v_type = VAR_STRING;
1221 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001222 /* Call get_lval() again, if it's inside a Dict or List it may
1223 * have changed. */
1224 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001225 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001226 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1227 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1228 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001229 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001230
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001231 /* free the collected output */
1232 vim_free(redir_ga.ga_data);
1233 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001235 vim_free(redir_lval);
1236 redir_lval = NULL;
1237 }
1238 vim_free(redir_varname);
1239 redir_varname = NULL;
1240}
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242# if defined(FEAT_MBYTE) || defined(PROTO)
1243 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001244eval_charconvert(
1245 char_u *enc_from,
1246 char_u *enc_to,
1247 char_u *fname_from,
1248 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250 int err = FALSE;
1251
1252 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1253 set_vim_var_string(VV_CC_TO, enc_to, -1);
1254 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1255 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1256 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1257 err = TRUE;
1258 set_vim_var_string(VV_CC_FROM, NULL, -1);
1259 set_vim_var_string(VV_CC_TO, NULL, -1);
1260 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262
1263 if (err)
1264 return FAIL;
1265 return OK;
1266}
1267# endif
1268
1269# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001271eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272{
1273 int err = FALSE;
1274
1275 set_vim_var_string(VV_FNAME_IN, fname, -1);
1276 set_vim_var_string(VV_CMDARG, args, -1);
1277 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1278 err = TRUE;
1279 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1280 set_vim_var_string(VV_CMDARG, NULL, -1);
1281
1282 if (err)
1283 {
1284 mch_remove(fname);
1285 return FAIL;
1286 }
1287 return OK;
1288}
1289# endif
1290
1291# if defined(FEAT_DIFF) || defined(PROTO)
1292 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001293eval_diff(
1294 char_u *origfile,
1295 char_u *newfile,
1296 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298 int err = FALSE;
1299
1300 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1301 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1302 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1303 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1304 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1305 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1306 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1307}
1308
1309 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001310eval_patch(
1311 char_u *origfile,
1312 char_u *difffile,
1313 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 int err;
1316
1317 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1318 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1319 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1320 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1321 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1322 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1323 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1324}
1325# endif
1326
1327/*
1328 * Top level evaluation function, returning a boolean.
1329 * Sets "error" to TRUE if there was an error.
1330 * Return TRUE or FALSE.
1331 */
1332 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001333eval_to_bool(
1334 char_u *arg,
1335 int *error,
1336 char_u **nextcmd,
1337 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
Bram Moolenaar33570922005-01-25 22:26:29 +00001339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 int retval = FALSE;
1341
1342 if (skip)
1343 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 else
1347 {
1348 *error = FALSE;
1349 if (!skip)
1350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001351 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354 }
1355 if (skip)
1356 --emsg_skip;
1357
1358 return retval;
1359}
1360
1361/*
1362 * Top level evaluation function, returning a string. If "skip" is TRUE,
1363 * only parsing to "nextcmd" is done, without reporting errors. Return
1364 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1365 */
1366 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367eval_to_string_skip(
1368 char_u *arg,
1369 char_u **nextcmd,
1370 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
Bram Moolenaar33570922005-01-25 22:26:29 +00001372 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 char_u *retval;
1374
1375 if (skip)
1376 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 retval = NULL;
1379 else
1380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 retval = vim_strsave(get_tv_string(&tv));
1382 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384 if (skip)
1385 --emsg_skip;
1386
1387 return retval;
1388}
1389
1390/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001391 * Skip over an expression at "*pp".
1392 * Return FAIL for an error, OK otherwise.
1393 */
1394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001395skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001398
1399 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001400 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001401}
1402
1403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001405 * When "convert" is TRUE convert a List into a sequence of lines and convert
1406 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 * Return pointer to allocated memory, or NULL for failure.
1408 */
1409 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001410eval_to_string(
1411 char_u *arg,
1412 char_u **nextcmd,
1413 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
Bram Moolenaar33570922005-01-25 22:26:29 +00001415 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001417 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001418#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001419 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 retval = NULL;
1424 else
1425 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001426 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001427 {
1428 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001429 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001430 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001431 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001432 if (tv.vval.v_list->lv_len > 0)
1433 ga_append(&ga, NL);
1434 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001435 ga_append(&ga, NUL);
1436 retval = (char_u *)ga.ga_data;
1437 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001438#ifdef FEAT_FLOAT
1439 else if (convert && tv.v_type == VAR_FLOAT)
1440 {
1441 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1442 retval = vim_strsave(numbuf);
1443 }
1444#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001445 else
1446 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449
1450 return retval;
1451}
1452
1453/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001454 * Call eval_to_string() without using current local variables and using
1455 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 */
1457 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001458eval_to_string_safe(
1459 char_u *arg,
1460 char_u **nextcmd,
1461 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 char_u *retval;
1464 void *save_funccalp;
1465
1466 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001467 if (use_sandbox)
1468 ++sandbox;
1469 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001471 if (use_sandbox)
1472 --sandbox;
1473 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 restore_funccal(save_funccalp);
1475 return retval;
1476}
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478/*
1479 * Top level evaluation function, returning a number.
1480 * Evaluates "expr" silently.
1481 * Returns -1 for an error.
1482 */
1483 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
Bram Moolenaar33570922005-01-25 22:26:29 +00001486 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001488 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
1490 ++emsg_off;
1491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001492 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 retval = -1;
1494 else
1495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001496 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499 --emsg_off;
1500
1501 return retval;
1502}
1503
Bram Moolenaara40058a2005-07-11 22:42:07 +00001504/*
1505 * Prepare v: variable "idx" to be used.
1506 * Save the current typeval in "save_tv".
1507 * When not used yet add the variable to the v: hashtable.
1508 */
1509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001510prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001511{
1512 *save_tv = vimvars[idx].vv_tv;
1513 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1514 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1515}
1516
1517/*
1518 * Restore v: variable "idx" to typeval "save_tv".
1519 * When no longer defined, remove the variable from the v: hashtable.
1520 */
1521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001522restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001523{
1524 hashitem_T *hi;
1525
Bram Moolenaara40058a2005-07-11 22:42:07 +00001526 vimvars[idx].vv_tv = *save_tv;
1527 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1528 {
1529 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1530 if (HASHITEM_EMPTY(hi))
1531 EMSG2(_(e_intern2), "restore_vimvar()");
1532 else
1533 hash_remove(&vimvarht, hi);
1534 }
1535}
1536
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001537#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001538/*
1539 * Evaluate an expression to a list with suggestions.
1540 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001541 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001542 */
1543 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001545{
1546 typval_T save_val;
1547 typval_T rettv;
1548 list_T *list = NULL;
1549 char_u *p = skipwhite(expr);
1550
1551 /* Set "v:val" to the bad word. */
1552 prepare_vimvar(VV_VAL, &save_val);
1553 vimvars[VV_VAL].vv_type = VAR_STRING;
1554 vimvars[VV_VAL].vv_str = badword;
1555 if (p_verbose == 0)
1556 ++emsg_off;
1557
1558 if (eval1(&p, &rettv, TRUE) == OK)
1559 {
1560 if (rettv.v_type != VAR_LIST)
1561 clear_tv(&rettv);
1562 else
1563 list = rettv.vval.v_list;
1564 }
1565
1566 if (p_verbose == 0)
1567 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001568 restore_vimvar(VV_VAL, &save_val);
1569
1570 return list;
1571}
1572
1573/*
1574 * "list" is supposed to contain two items: a word and a number. Return the
1575 * word in "pp" and the number as the return value.
1576 * Return -1 if anything isn't right.
1577 * Used to get the good word and score from the eval_spell_expr() result.
1578 */
1579 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001580get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001581{
1582 listitem_T *li;
1583
1584 li = list->lv_first;
1585 if (li == NULL)
1586 return -1;
1587 *pp = get_tv_string(&li->li_tv);
1588
1589 li = li->li_next;
1590 if (li == NULL)
1591 return -1;
1592 return get_tv_number(&li->li_tv);
1593}
1594#endif
1595
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001596/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001597 * Top level evaluation function.
1598 * Returns an allocated typval_T with the result.
1599 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001600 */
1601 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001602eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001603{
1604 typval_T *tv;
1605
1606 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001607 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001608 {
1609 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001610 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001611 }
1612
1613 return tv;
1614}
1615
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619 * Uses argv[argc] for the function arguments. Only Number and String
1620 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001624call_vim_function(
1625 char_u *func,
1626 int argc,
1627 char_u **argv,
1628 int safe, /* use the sandbox */
1629 int str_arg_only, /* all arguments are strings */
1630 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
Bram Moolenaar33570922005-01-25 22:26:29 +00001632 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 long n;
1634 int len;
1635 int i;
1636 int doesrange;
1637 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001640 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
1644 for (i = 0; i < argc; i++)
1645 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001646 /* Pass a NULL or empty argument as an empty string */
1647 if (argv[i] == NULL || *argv[i] == NUL)
1648 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001649 argvars[i].v_type = VAR_STRING;
1650 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001651 continue;
1652 }
1653
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001654 if (str_arg_only)
1655 len = 0;
1656 else
1657 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001658 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (len != 0 && len == (int)STRLEN(argv[i]))
1660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001661 argvars[i].v_type = VAR_NUMBER;
1662 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
1664 else
1665 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001666 argvars[i].v_type = VAR_STRING;
1667 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 }
1670
1671 if (safe)
1672 {
1673 save_funccalp = save_funccal();
1674 ++sandbox;
1675 }
1676
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1678 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (safe)
1682 {
1683 --sandbox;
1684 restore_funccal(save_funccalp);
1685 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 vim_free(argvars);
1687
1688 if (ret == FAIL)
1689 clear_tv(rettv);
1690
1691 return ret;
1692}
1693
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001694/*
1695 * Call vimL function "func" and return the result as a number.
1696 * Returns -1 when calling the function fails.
1697 * Uses argv[argc] for the function arguments.
1698 */
1699 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700call_func_retnr(
1701 char_u *func,
1702 int argc,
1703 char_u **argv,
1704 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001705{
1706 typval_T rettv;
1707 long retval;
1708
1709 /* All arguments are passed as strings, no conversion to number. */
1710 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1711 return -1;
1712
1713 retval = get_tv_number_chk(&rettv, NULL);
1714 clear_tv(&rettv);
1715 return retval;
1716}
1717
1718#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1719 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1720
Bram Moolenaar4f688582007-07-24 12:34:30 +00001721# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001723 * Call vimL function "func" and return the result as a string.
1724 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725 * Uses argv[argc] for the function arguments.
1726 */
1727 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001728call_func_retstr(
1729 char_u *func,
1730 int argc,
1731 char_u **argv,
1732 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733{
1734 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001735 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001737 /* All arguments are passed as strings, no conversion to number. */
1738 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 return NULL;
1740
1741 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001742 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 return retval;
1744}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001745# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001746
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001747/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001748 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001749 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001750 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751 */
1752 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001753call_func_retlist(
1754 char_u *func,
1755 int argc,
1756 char_u **argv,
1757 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758{
1759 typval_T rettv;
1760
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001761 /* All arguments are passed as strings, no conversion to number. */
1762 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763 return NULL;
1764
1765 if (rettv.v_type != VAR_LIST)
1766 {
1767 clear_tv(&rettv);
1768 return NULL;
1769 }
1770
1771 return rettv.vval.v_list;
1772}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#endif
1774
1775/*
1776 * Save the current function call pointer, and set it to NULL.
1777 * Used when executing autocommands and for ":source".
1778 */
1779 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001782 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 current_funccal = NULL;
1785 return (void *)fc;
1786}
1787
1788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001789restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001791 funccall_T *fc = (funccall_T *)vfc;
1792
1793 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794}
1795
Bram Moolenaar05159a02005-02-26 23:04:13 +00001796#if defined(FEAT_PROFILE) || defined(PROTO)
1797/*
1798 * Prepare profiling for entering a child or something else that is not
1799 * counted for the script/function itself.
1800 * Should always be called in pair with prof_child_exit().
1801 */
1802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803prof_child_enter(
1804 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001805{
1806 funccall_T *fc = current_funccal;
1807
1808 if (fc != NULL && fc->func->uf_profiling)
1809 profile_start(&fc->prof_child);
1810 script_prof_save(tm);
1811}
1812
1813/*
1814 * Take care of time spent in a child.
1815 * Should always be called after prof_child_enter().
1816 */
1817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818prof_child_exit(
1819 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001820{
1821 funccall_T *fc = current_funccal;
1822
1823 if (fc != NULL && fc->func->uf_profiling)
1824 {
1825 profile_end(&fc->prof_child);
1826 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1827 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1828 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1829 }
1830 script_prof_restore(tm);
1831}
1832#endif
1833
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835#ifdef FEAT_FOLDING
1836/*
1837 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1838 * it in "*cp". Doesn't give error messages.
1839 */
1840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001841eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842{
Bram Moolenaar33570922005-01-25 22:26:29 +00001843 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 int retval;
1845 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001846 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1847 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
1849 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001850 if (use_sandbox)
1851 ++sandbox;
1852 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 retval = 0;
1856 else
1857 {
1858 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 if (tv.v_type == VAR_NUMBER)
1860 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001861 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 retval = 0;
1863 else
1864 {
1865 /* If the result is a string, check if there is a non-digit before
1866 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (!VIM_ISDIGIT(*s) && *s != '-')
1869 *cp = *s++;
1870 retval = atol((char *)s);
1871 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
1874 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001875 if (use_sandbox)
1876 --sandbox;
1877 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879 return retval;
1880}
1881#endif
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 * ":let" list all variable values
1885 * ":let var1 var2" list variable values
1886 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001887 * ":let var += expr" assignment command.
1888 * ":let var -= expr" assignment command.
1889 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 */
1892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001893ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 int var_count = 0;
1900 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001902 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
Bram Moolenaardb552d602006-03-23 22:59:57 +00001905 argend = skip_var_list(arg, &var_count, &semicolon);
1906 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001908 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1909 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001910 expr = skipwhite(argend);
1911 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1912 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001914 /*
1915 * ":let" without "=": list variables
1916 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 if (*arg == '[')
1918 EMSG(_(e_invarg));
1919 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001921 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 list_glob_vars(&first);
1926 list_buf_vars(&first);
1927 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001928#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001929 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001930#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001931 list_script_vars(&first);
1932 list_func_vars(&first);
1933 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 eap->nextcmd = check_nextcmd(arg);
1936 }
1937 else
1938 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 op[0] = '=';
1940 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001941 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001943 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1944 op[0] = *expr; /* +=, -= or .= */
1945 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001947 else
1948 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (eap->skip)
1951 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (eap->skip)
1954 {
1955 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 --emsg_skip;
1958 }
1959 else if (i != FAIL)
1960 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 }
1966}
1967
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968/*
1969 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1970 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 * When "nextchars" is not NULL it points to a string with characters that
1972 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1973 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 * Returns OK or FAIL;
1975 */
1976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001977ex_let_vars(
1978 char_u *arg_start,
1979 typval_T *tv,
1980 int copy, /* copy values from "tv", don't move */
1981 int semicolon, /* from skip_var_list() */
1982 int var_count, /* from skip_var_list() */
1983 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984{
1985 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001986 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001988 listitem_T *item;
1989 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990
1991 if (*arg != '[')
1992 {
1993 /*
1994 * ":let var = expr" or ":for var in list"
1995 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 return FAIL;
1998 return OK;
1999 }
2000
2001 /*
2002 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2003 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002004 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005 {
2006 EMSG(_(e_listreq));
2007 return FAIL;
2008 }
2009
2010 i = list_len(l);
2011 if (semicolon == 0 && var_count < i)
2012 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002013 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 return FAIL;
2015 }
2016 if (var_count - semicolon > i)
2017 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002018 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 return FAIL;
2020 }
2021
2022 item = l->lv_first;
2023 while (*arg != ']')
2024 {
2025 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002026 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027 item = item->li_next;
2028 if (arg == NULL)
2029 return FAIL;
2030
2031 arg = skipwhite(arg);
2032 if (*arg == ';')
2033 {
2034 /* Put the rest of the list (may be empty) in the var after ';'.
2035 * Create a new list for this. */
2036 l = list_alloc();
2037 if (l == NULL)
2038 return FAIL;
2039 while (item != NULL)
2040 {
2041 list_append_tv(l, &item->li_tv);
2042 item = item->li_next;
2043 }
2044
2045 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002046 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 ltv.vval.v_list = l;
2048 l->lv_refcount = 1;
2049
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002050 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2051 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 clear_tv(&ltv);
2053 if (arg == NULL)
2054 return FAIL;
2055 break;
2056 }
2057 else if (*arg != ',' && *arg != ']')
2058 {
2059 EMSG2(_(e_intern2), "ex_let_vars()");
2060 return FAIL;
2061 }
2062 }
2063
2064 return OK;
2065}
2066
2067/*
2068 * Skip over assignable variable "var" or list of variables "[var, var]".
2069 * Used for ":let varvar = expr" and ":for varvar in expr".
2070 * For "[var, var]" increment "*var_count" for each variable.
2071 * for "[var, var; var]" set "semicolon".
2072 * Return NULL for an error.
2073 */
2074 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002075skip_var_list(
2076 char_u *arg,
2077 int *var_count,
2078 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079{
2080 char_u *p, *s;
2081
2082 if (*arg == '[')
2083 {
2084 /* "[var, var]": find the matching ']'. */
2085 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002086 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087 {
2088 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2089 s = skip_var_one(p);
2090 if (s == p)
2091 {
2092 EMSG2(_(e_invarg2), p);
2093 return NULL;
2094 }
2095 ++*var_count;
2096
2097 p = skipwhite(s);
2098 if (*p == ']')
2099 break;
2100 else if (*p == ';')
2101 {
2102 if (*semicolon == 1)
2103 {
2104 EMSG(_("Double ; in list of variables"));
2105 return NULL;
2106 }
2107 *semicolon = 1;
2108 }
2109 else if (*p != ',')
2110 {
2111 EMSG2(_(e_invarg2), p);
2112 return NULL;
2113 }
2114 }
2115 return p + 1;
2116 }
2117 else
2118 return skip_var_one(arg);
2119}
2120
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002122 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002123 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002125 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002126skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002128 if (*arg == '@' && arg[1] != NUL)
2129 return arg + 2;
2130 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2131 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002132}
2133
Bram Moolenaara7043832005-01-21 11:56:39 +00002134/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002135 * List variables for hashtab "ht" with prefix "prefix".
2136 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002137 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002139list_hashtable_vars(
2140 hashtab_T *ht,
2141 char_u *prefix,
2142 int empty,
2143 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002144{
Bram Moolenaar33570922005-01-25 22:26:29 +00002145 hashitem_T *hi;
2146 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147 int todo;
2148
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002149 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002150 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2151 {
2152 if (!HASHITEM_EMPTY(hi))
2153 {
2154 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002155 di = HI2DI(hi);
2156 if (empty || di->di_tv.v_type != VAR_STRING
2157 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002159 }
2160 }
2161}
2162
2163/*
2164 * List global variables.
2165 */
2166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002167list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002168{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002170}
2171
2172/*
2173 * List buffer variables.
2174 */
2175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002176list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002177{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002178 char_u numbuf[NUMBUFLEN];
2179
Bram Moolenaar429fa852013-04-15 12:27:36 +02002180 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182
2183 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2185 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002186}
2187
2188/*
2189 * List window variables.
2190 */
2191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002192list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002193{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002194 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002196}
2197
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002198#ifdef FEAT_WINDOWS
2199/*
2200 * List tab page variables.
2201 */
2202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002203list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002204{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002205 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002207}
2208#endif
2209
Bram Moolenaara7043832005-01-21 11:56:39 +00002210/*
2211 * List Vim variables.
2212 */
2213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217}
2218
2219/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220 * List script-local variables, if there is a script.
2221 */
2222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224{
2225 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002226 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2227 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002228}
2229
2230/*
2231 * List function variables, if there is a function.
2232 */
2233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002234list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002235{
2236 if (current_funccal != NULL)
2237 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002238 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002239}
2240
2241/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 * List variables in "arg".
2243 */
2244 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002245list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246{
2247 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 char_u *name_start;
2251 char_u *arg_subsc;
2252 char_u *tofree;
2253 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254
2255 while (!ends_excmd(*arg) && !got_int)
2256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002259 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2261 {
2262 emsg_severe = TRUE;
2263 EMSG(_(e_trailing));
2264 break;
2265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 /* get_name_len() takes care of expanding curly braces */
2270 name_start = name = arg;
2271 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2272 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 /* This is mainly to keep test 49 working: when expanding
2275 * curly braces fails overrule the exception error message. */
2276 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 emsg_severe = TRUE;
2279 EMSG2(_(e_invarg2), arg);
2280 break;
2281 }
2282 error = TRUE;
2283 }
2284 else
2285 {
2286 if (tofree != NULL)
2287 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002288 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 else
2291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 /* handle d.key, l[idx], f(expr) */
2293 arg_subsc = arg;
2294 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 'g': list_glob_vars(first); break;
2303 case 'b': list_buf_vars(first); break;
2304 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002305#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002306 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002307#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002308 case 'v': list_vim_vars(first); break;
2309 case 's': list_script_vars(first); break;
2310 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 default:
2312 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002314 }
2315 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 {
2317 char_u numbuf[NUMBUFLEN];
2318 char_u *tf;
2319 int c;
2320 char_u *s;
2321
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002322 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323 c = *arg;
2324 *arg = NUL;
2325 list_one_var_a((char_u *)"",
2326 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002327 tv.v_type,
2328 s == NULL ? (char_u *)"" : s,
2329 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002330 *arg = c;
2331 vim_free(tf);
2332 }
2333 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
2336 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337
2338 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002340
2341 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
2343
2344 return arg;
2345}
2346
2347/*
2348 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2349 * Returns a pointer to the char just after the var name.
2350 * Returns NULL if there is an error.
2351 */
2352 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002353ex_let_one(
2354 char_u *arg, /* points to variable name */
2355 typval_T *tv, /* value to assign to variable */
2356 int copy, /* copy value from "tv" */
2357 char_u *endchars, /* valid chars after variable name or NULL */
2358 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359{
2360 int c1;
2361 char_u *name;
2362 char_u *p;
2363 char_u *arg_end = NULL;
2364 int len;
2365 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367
2368 /*
2369 * ":let $VAR = expr": Set environment variable.
2370 */
2371 if (*arg == '$')
2372 {
2373 /* Find the end of the name. */
2374 ++arg;
2375 name = arg;
2376 len = get_env_len(&arg);
2377 if (len == 0)
2378 EMSG2(_(e_invarg2), name - 1);
2379 else
2380 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 if (op != NULL && (*op == '+' || *op == '-'))
2382 EMSG2(_(e_letwrong), op);
2383 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002384 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002386 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 {
2388 c1 = name[len];
2389 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002390 p = get_tv_string_chk(tv);
2391 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 {
2393 int mustfree = FALSE;
2394 char_u *s = vim_getenv(name, &mustfree);
2395
2396 if (s != NULL)
2397 {
2398 p = tofree = concat_str(s, p);
2399 if (mustfree)
2400 vim_free(s);
2401 }
2402 }
2403 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002406 if (STRICMP(name, "HOME") == 0)
2407 init_homedir();
2408 else if (didset_vim && STRICMP(name, "VIM") == 0)
2409 didset_vim = FALSE;
2410 else if (didset_vimruntime
2411 && STRICMP(name, "VIMRUNTIME") == 0)
2412 didset_vimruntime = FALSE;
2413 arg_end = arg;
2414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 }
2418 }
2419 }
2420
2421 /*
2422 * ":let &option = expr": Set option value.
2423 * ":let &l:option = expr": Set local option value.
2424 * ":let &g:option = expr": Set global option value.
2425 */
2426 else if (*arg == '&')
2427 {
2428 /* Find the end of the name. */
2429 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002430 if (p == NULL || (endchars != NULL
2431 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 EMSG(_(e_letunexp));
2433 else
2434 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 long n;
2436 int opt_type;
2437 long numval;
2438 char_u *stringval = NULL;
2439 char_u *s;
2440
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002441 c1 = *p;
2442 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443
2444 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 s = get_tv_string_chk(tv); /* != NULL if number or string */
2446 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 {
2448 opt_type = get_option_value(arg, &numval,
2449 &stringval, opt_flags);
2450 if ((opt_type == 1 && *op == '.')
2451 || (opt_type == 0 && *op != '.'))
2452 EMSG2(_(e_letwrong), op);
2453 else
2454 {
2455 if (opt_type == 1) /* number */
2456 {
2457 if (*op == '+')
2458 n = numval + n;
2459 else
2460 n = numval - n;
2461 }
2462 else if (opt_type == 0 && stringval != NULL) /* string */
2463 {
2464 s = concat_str(stringval, s);
2465 vim_free(stringval);
2466 stringval = s;
2467 }
2468 }
2469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 if (s != NULL)
2471 {
2472 set_option_value(arg, n, s, opt_flags);
2473 arg_end = p;
2474 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478 }
2479
2480 /*
2481 * ":let @r = expr": Set register contents.
2482 */
2483 else if (*arg == '@')
2484 {
2485 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 if (op != NULL && (*op == '+' || *op == '-'))
2487 EMSG2(_(e_letwrong), op);
2488 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002489 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 EMSG(_(e_letunexp));
2491 else
2492 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002493 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 char_u *s;
2495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 p = get_tv_string_chk(tv);
2497 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002499 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 if (s != NULL)
2501 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002502 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 vim_free(s);
2504 }
2505 }
2506 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002507 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002509 arg_end = arg + 1;
2510 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002511 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 }
2513 }
2514
2515 /*
2516 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002519 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002521 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002523 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2527 EMSG(_(e_letunexp));
2528 else
2529 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002530 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 arg_end = p;
2532 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 }
2536
2537 else
2538 EMSG2(_(e_invarg2), arg);
2539
2540 return arg_end;
2541}
2542
2543/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2545 */
2546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002547check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548{
2549 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2550 {
2551 EMSG2(_(e_readonlyvar), arg);
2552 return TRUE;
2553 }
2554 return FALSE;
2555}
2556
2557/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 * Get an lval: variable, Dict item or List item that can be assigned a value
2559 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2560 * "name.key", "name.key[expr]" etc.
2561 * Indexing only works if "name" is an existing List or Dictionary.
2562 * "name" points to the start of the name.
2563 * If "rettv" is not NULL it points to the value to be assigned.
2564 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2565 * wrong; must end in space or cmd separator.
2566 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002567 * flags:
2568 * GLV_QUIET: do not give error messages
2569 * GLV_NO_AUTOLOAD: do not use script autoloading
2570 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002572 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 */
2575 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002576get_lval(
2577 char_u *name,
2578 typval_T *rettv,
2579 lval_T *lp,
2580 int unlet,
2581 int skip,
2582 int flags, /* GLV_ values */
2583 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 char_u *expr_start, *expr_end;
2587 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 dictitem_T *v;
2589 typval_T var1;
2590 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002596 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600
2601 if (skip)
2602 {
2603 /* When skipping just find the end of the name. */
2604 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002605 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 }
2607
2608 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002609 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (expr_start != NULL)
2611 {
2612 /* Don't expand the name when we already know there is an error. */
2613 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2614 && *p != '[' && *p != '.')
2615 {
2616 EMSG(_(e_trailing));
2617 return NULL;
2618 }
2619
2620 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2621 if (lp->ll_exp_name == NULL)
2622 {
2623 /* Report an invalid expression in braces, unless the
2624 * expression evaluation has been cancelled due to an
2625 * aborting error, an interrupt, or an exception. */
2626 if (!aborting() && !quiet)
2627 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002628 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 EMSG2(_(e_invarg2), name);
2630 return NULL;
2631 }
2632 }
2633 lp->ll_name = lp->ll_exp_name;
2634 }
2635 else
2636 lp->ll_name = name;
2637
2638 /* Without [idx] or .key we are done. */
2639 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2640 return p;
2641
2642 cc = *p;
2643 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002644 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (v == NULL && !quiet)
2646 EMSG2(_(e_undefvar), lp->ll_name);
2647 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 if (v == NULL)
2649 return NULL;
2650
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 /*
2652 * Loop until no more [idx] or .key is following.
2653 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002654 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2658 && !(lp->ll_tv->v_type == VAR_DICT
2659 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_("E689: Can only index a List or Dictionary"));
2663 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
2668 EMSG(_("E708: [:] must come last"));
2669 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 len = -1;
2673 if (*p == '.')
2674 {
2675 key = p + 1;
2676 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2677 ;
2678 if (len == 0)
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (!quiet)
2681 EMSG(_(e_emptykey));
2682 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 }
2684 p = key + len;
2685 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 else
2687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (*p == ':')
2691 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 else
2693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 empty1 = FALSE;
2695 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002697 if (get_tv_string_chk(&var1) == NULL)
2698 {
2699 /* not a number or string */
2700 clear_tv(&var1);
2701 return NULL;
2702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704
2705 /* Optionally get the second index [ :expr]. */
2706 if (*p == ':')
2707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002711 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (rettv != NULL && (rettv->v_type != VAR_LIST
2717 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (!empty1)
2722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
2725 p = skipwhite(p + 1);
2726 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 else
2729 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 if (!empty1)
2734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002737 if (get_tv_string_chk(&var2) == NULL)
2738 {
2739 /* not a number or string */
2740 if (!empty1)
2741 clear_tv(&var1);
2742 clear_tv(&var2);
2743 return NULL;
2744 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (!quiet)
2754 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 if (!empty1)
2756 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
2761
2762 /* Skip to past ']'. */
2763 ++p;
2764 }
2765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 {
2768 if (len == -1)
2769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (*key == NUL)
2773 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 if (!quiet)
2775 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 }
2779 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002780 lp->ll_list = NULL;
2781 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002782 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002784 /* When assigning to a scope dictionary check that a function and
2785 * variable name is valid (only variable name unless it is l: or
2786 * g: dictionary). Disallow overwriting a builtin function. */
2787 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002789 int prevval;
2790 int wrong;
2791
2792 if (len != -1)
2793 {
2794 prevval = key[len];
2795 key[len] = NUL;
2796 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002797 else
2798 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002799 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2800 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002801 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002802 || !valid_varname(key);
2803 if (len != -1)
2804 key[len] = prevval;
2805 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002806 return NULL;
2807 }
2808
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 /* Can't add "v:" variable. */
2812 if (lp->ll_dict == &vimvardict)
2813 {
2814 EMSG2(_(e_illvar), name);
2815 return NULL;
2816 }
2817
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002818 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002822 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 if (len == -1)
2824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 }
2827 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 if (len == -1)
2832 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 p = NULL;
2835 break;
2836 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002837 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002838 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002839 return NULL;
2840
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 if (len == -1)
2842 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 }
2845 else
2846 {
2847 /*
2848 * Get the number and item for the only or first index of the List.
2849 */
2850 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 else
2853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002854 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 clear_tv(&var1);
2856 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002857 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 lp->ll_list = lp->ll_tv->vval.v_list;
2859 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2860 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002862 if (lp->ll_n1 < 0)
2863 {
2864 lp->ll_n1 = 0;
2865 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2866 }
2867 }
2868 if (lp->ll_li == NULL)
2869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002872 if (!quiet)
2873 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 }
2876
2877 /*
2878 * May need to find the item or absolute index for the second
2879 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 * When no index given: "lp->ll_empty2" is TRUE.
2881 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002885 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002891 {
2892 if (!quiet)
2893 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2900 if (lp->ll_n1 < 0)
2901 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2902 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002903 {
2904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002907 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002908 }
2909
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 }
2913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 return p;
2915}
2916
2917/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 */
2920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002921clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922{
2923 vim_free(lp->ll_exp_name);
2924 vim_free(lp->ll_newkey);
2925}
2926
2927/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 */
2932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002933set_var_lval(
2934 lval_T *lp,
2935 char_u *endp,
2936 typval_T *rettv,
2937 int copy,
2938 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939{
2940 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 listitem_T *ri;
2942 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943
2944 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 cc = *endp;
2949 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 if (op != NULL && *op != '=')
2951 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002952 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002954 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002955 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002956 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002957 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002959 if ((di == NULL
2960 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2961 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2962 FALSE)))
2963 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 set_var(lp->ll_name, &tv, FALSE);
2965 clear_tv(&tv);
2966 }
2967 }
2968 else
2969 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002971 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002973 else if (tv_check_lock(lp->ll_newkey == NULL
2974 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002975 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002976 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 else if (lp->ll_range)
2978 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002979 listitem_T *ll_li = lp->ll_li;
2980 int ll_n1 = lp->ll_n1;
2981
2982 /*
2983 * Check whether any of the list items is locked
2984 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002985 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002986 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002987 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002988 return;
2989 ri = ri->li_next;
2990 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2991 break;
2992 ll_li = ll_li->li_next;
2993 ++ll_n1;
2994 }
2995
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 /*
2997 * Assign the List values to the list items.
2998 */
2999 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 if (op != NULL && *op != '=')
3002 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3003 else
3004 {
3005 clear_tv(&lp->ll_li->li_tv);
3006 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3007 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 ri = ri->li_next;
3009 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3010 break;
3011 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003014 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 ri = NULL;
3017 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003019 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 lp->ll_li = lp->ll_li->li_next;
3021 ++lp->ll_n1;
3022 }
3023 if (ri != NULL)
3024 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003025 else if (lp->ll_empty2
3026 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 : lp->ll_n1 != lp->ll_n2)
3028 EMSG(_("E711: List value has not enough items"));
3029 }
3030 else
3031 {
3032 /*
3033 * Assign to a List or Dictionary item.
3034 */
3035 if (lp->ll_newkey != NULL)
3036 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 if (op != NULL && *op != '=')
3038 {
3039 EMSG2(_(e_letwrong), op);
3040 return;
3041 }
3042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003044 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 if (di == NULL)
3046 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003047 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3048 {
3049 vim_free(di);
3050 return;
3051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003053 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 else if (op != NULL && *op != '=')
3055 {
3056 tv_op(lp->ll_tv, rettv, op);
3057 return;
3058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003061
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 /*
3063 * Assign the value to the variable or list item.
3064 */
3065 if (copy)
3066 copy_tv(rettv, lp->ll_tv);
3067 else
3068 {
3069 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003070 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003071 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072 }
3073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074}
3075
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003076/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003077 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3078 * Returns OK or FAIL.
3079 */
3080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003081tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082{
3083 long n;
3084 char_u numbuf[NUMBUFLEN];
3085 char_u *s;
3086
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003087 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3088 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3089 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 {
3091 switch (tv1->v_type)
3092 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003093 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094 case VAR_DICT:
3095 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003096 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003097 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003098 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 break;
3100
3101 case VAR_LIST:
3102 if (*op != '+' || tv2->v_type != VAR_LIST)
3103 break;
3104 /* List += List */
3105 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3106 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3107 return OK;
3108
3109 case VAR_NUMBER:
3110 case VAR_STRING:
3111 if (tv2->v_type == VAR_LIST)
3112 break;
3113 if (*op == '+' || *op == '-')
3114 {
3115 /* nr += nr or nr -= nr*/
3116 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#ifdef FEAT_FLOAT
3118 if (tv2->v_type == VAR_FLOAT)
3119 {
3120 float_T f = n;
3121
3122 if (*op == '+')
3123 f += tv2->vval.v_float;
3124 else
3125 f -= tv2->vval.v_float;
3126 clear_tv(tv1);
3127 tv1->v_type = VAR_FLOAT;
3128 tv1->vval.v_float = f;
3129 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131#endif
3132 {
3133 if (*op == '+')
3134 n += get_tv_number(tv2);
3135 else
3136 n -= get_tv_number(tv2);
3137 clear_tv(tv1);
3138 tv1->v_type = VAR_NUMBER;
3139 tv1->vval.v_number = n;
3140 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 }
3142 else
3143 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144 if (tv2->v_type == VAR_FLOAT)
3145 break;
3146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003147 /* str .= str */
3148 s = get_tv_string(tv1);
3149 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3150 clear_tv(tv1);
3151 tv1->v_type = VAR_STRING;
3152 tv1->vval.v_string = s;
3153 }
3154 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003156 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003157#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003158 {
3159 float_T f;
3160
3161 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3162 && tv2->v_type != VAR_NUMBER
3163 && tv2->v_type != VAR_STRING))
3164 break;
3165 if (tv2->v_type == VAR_FLOAT)
3166 f = tv2->vval.v_float;
3167 else
3168 f = get_tv_number(tv2);
3169 if (*op == '+')
3170 tv1->vval.v_float += f;
3171 else
3172 tv1->vval.v_float -= f;
3173 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003174#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003175 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003176 }
3177 }
3178
3179 EMSG2(_(e_letwrong), op);
3180 return FAIL;
3181}
3182
3183/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184 * Add a watcher to a list.
3185 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003187list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188{
3189 lw->lw_next = l->lv_watch;
3190 l->lv_watch = lw;
3191}
3192
3193/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003194 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 * No warning when it isn't found...
3196 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199{
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201
3202 lwp = &l->lv_watch;
3203 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3204 {
3205 if (lw == lwrem)
3206 {
3207 *lwp = lw->lw_next;
3208 break;
3209 }
3210 lwp = &lw->lw_next;
3211 }
3212}
3213
3214/*
3215 * Just before removing an item from a list: advance watchers to the next
3216 * item.
3217 */
3218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220{
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222
3223 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3224 if (lw->lw_item == item)
3225 lw->lw_item = item->li_next;
3226}
3227
3228/*
3229 * Evaluate the expression used in a ":for var in expr" command.
3230 * "arg" points to "var".
3231 * Set "*errp" to TRUE for an error, FALSE otherwise;
3232 * Return a pointer that holds the info. Null when there is an error.
3233 */
3234 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235eval_for_line(
3236 char_u *arg,
3237 int *errp,
3238 char_u **nextcmdp,
3239 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240{
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 typval_T tv;
3244 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245
3246 *errp = TRUE; /* default: there is an error */
3247
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 if (fi == NULL)
3250 return NULL;
3251
3252 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3253 if (expr == NULL)
3254 return fi;
3255
3256 expr = skipwhite(expr);
3257 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3258 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003259 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 return fi;
3261 }
3262
3263 if (skip)
3264 ++emsg_skip;
3265 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3266 {
3267 *errp = FALSE;
3268 if (!skip)
3269 {
3270 l = tv.vval.v_list;
3271 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003272 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003274 clear_tv(&tv);
3275 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 else
3277 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003278 /* No need to increment the refcount, it's already set for the
3279 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 fi->fi_list = l;
3281 list_add_watch(l, &fi->fi_lw);
3282 fi->fi_lw.lw_item = l->lv_first;
3283 }
3284 }
3285 }
3286 if (skip)
3287 --emsg_skip;
3288
3289 return fi;
3290}
3291
3292/*
3293 * Use the first item in a ":for" list. Advance to the next.
3294 * Assign the values to the variable (list). "arg" points to the first one.
3295 * Return TRUE when a valid item was found, FALSE when at end of list or
3296 * something wrong.
3297 */
3298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003299next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003301 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304
3305 item = fi->fi_lw.lw_item;
3306 if (item == NULL)
3307 result = FALSE;
3308 else
3309 {
3310 fi->fi_lw.lw_item = item->li_next;
3311 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3312 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3313 }
3314 return result;
3315}
3316
3317/*
3318 * Free the structure used to store info used by ":for".
3319 */
3320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003321free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322{
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003325 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003328 list_unref(fi->fi_list);
3329 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 vim_free(fi);
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3334
3335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336set_context_for_expression(
3337 expand_T *xp,
3338 char_u *arg,
3339 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 int got_eq = FALSE;
3342 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 if (cmdidx == CMD_let)
3346 {
3347 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003351 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 {
3353 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003354 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 if (vim_iswhite(*p))
3356 break;
3357 }
3358 return;
3359 }
3360 }
3361 else
3362 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3363 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 while ((xp->xp_pattern = vim_strpbrk(arg,
3365 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3366 {
3367 c = *xp->xp_pattern;
3368 if (c == '&')
3369 {
3370 c = xp->xp_pattern[1];
3371 if (c == '&')
3372 {
3373 ++xp->xp_pattern;
3374 xp->xp_context = cmdidx != CMD_let || got_eq
3375 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3376 }
3377 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003380 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3381 xp->xp_pattern += 2;
3382
3383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 else if (c == '$')
3386 {
3387 /* environment variable */
3388 xp->xp_context = EXPAND_ENV_VARS;
3389 }
3390 else if (c == '=')
3391 {
3392 got_eq = TRUE;
3393 xp->xp_context = EXPAND_EXPRESSION;
3394 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003395 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 && xp->xp_context == EXPAND_FUNCTIONS
3397 && vim_strchr(xp->xp_pattern, '(') == NULL)
3398 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003399 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 break;
3401 }
3402 else if (cmdidx != CMD_let || got_eq)
3403 {
3404 if (c == '"') /* string */
3405 {
3406 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3407 if (c == '\\' && xp->xp_pattern[1] != NUL)
3408 ++xp->xp_pattern;
3409 xp->xp_context = EXPAND_NOTHING;
3410 }
3411 else if (c == '\'') /* literal string */
3412 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003413 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3415 /* skip */ ;
3416 xp->xp_context = EXPAND_NOTHING;
3417 }
3418 else if (c == '|')
3419 {
3420 if (xp->xp_pattern[1] == '|')
3421 {
3422 ++xp->xp_pattern;
3423 xp->xp_context = EXPAND_EXPRESSION;
3424 }
3425 else
3426 xp->xp_context = EXPAND_COMMANDS;
3427 }
3428 else
3429 xp->xp_context = EXPAND_EXPRESSION;
3430 }
3431 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003432 /* Doesn't look like something valid, expand as an expression
3433 * anyway. */
3434 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 arg = xp->xp_pattern;
3436 if (*arg != NUL)
3437 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3438 /* skip */ ;
3439 }
3440 xp->xp_pattern = arg;
3441}
3442
3443#endif /* FEAT_CMDL_COMPL */
3444
3445/*
3446 * ":1,25call func(arg1, arg2)" function call.
3447 */
3448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003449ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 char_u *arg = eap->arg;
3452 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003454 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 linenr_T lnum;
3458 int doesrange;
3459 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003460 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003462 if (eap->skip)
3463 {
3464 /* trans_function_name() doesn't work well when skipping, use eval0()
3465 * instead to skip to any following command, e.g. for:
3466 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003467 ++emsg_skip;
3468 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3469 clear_tv(&rettv);
3470 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003471 return;
3472 }
3473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003474 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003475 if (fudi.fd_newkey != NULL)
3476 {
3477 /* Still need to give an error message for missing key. */
3478 EMSG2(_(e_dictkey), fudi.fd_newkey);
3479 vim_free(fudi.fd_newkey);
3480 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003481 if (tofree == NULL)
3482 return;
3483
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003484 /* Increase refcount on dictionary, it could get deleted when evaluating
3485 * the arguments. */
3486 if (fudi.fd_dict != NULL)
3487 ++fudi.fd_dict->dv_refcount;
3488
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003489 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003490 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003491 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar532c7802005-01-27 14:44:31 +00003493 /* Skip white space to allow ":call func ()". Not good, but required for
3494 * backward compatibility. */
3495 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498 if (*startarg != '(')
3499 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003500 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 goto end;
3502 }
3503
3504 /*
3505 * When skipping, evaluate the function once, to find the end of the
3506 * arguments.
3507 * When the function takes a range, this is discovered after the first
3508 * call, and the loop is broken.
3509 */
3510 if (eap->skip)
3511 {
3512 ++emsg_skip;
3513 lnum = eap->line2; /* do it once, also with an invalid range */
3514 }
3515 else
3516 lnum = eap->line1;
3517 for ( ; lnum <= eap->line2; ++lnum)
3518 {
3519 if (!eap->skip && eap->addr_count > 0)
3520 {
3521 curwin->w_cursor.lnum = lnum;
3522 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003523#ifdef FEAT_VIRTUALEDIT
3524 curwin->w_cursor.coladd = 0;
3525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003529 eap->line1, eap->line2, &doesrange,
3530 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
3532 failed = TRUE;
3533 break;
3534 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003535
3536 /* Handle a function returning a Funcref, Dictionary or List. */
3537 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3538 {
3539 failed = TRUE;
3540 break;
3541 }
3542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (doesrange || eap->skip)
3545 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003549 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003550 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (aborting())
3552 break;
3553 }
3554 if (eap->skip)
3555 --emsg_skip;
3556
3557 if (!failed)
3558 {
3559 /* Check for trailing illegal characters and a following command. */
3560 if (!ends_excmd(*arg))
3561 {
3562 emsg_severe = TRUE;
3563 EMSG(_(e_trailing));
3564 }
3565 else
3566 eap->nextcmd = check_nextcmd(arg);
3567 }
3568
3569end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003570 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003571 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572}
3573
3574/*
3575 * ":unlet[!] var1 ... " command.
3576 */
3577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003578ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580 ex_unletlock(eap, eap->arg, 0);
3581}
3582
3583/*
3584 * ":lockvar" and ":unlockvar" commands
3585 */
3586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003588{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 int deep = 2;
3591
3592 if (eap->forceit)
3593 deep = -1;
3594 else if (vim_isdigit(*arg))
3595 {
3596 deep = getdigits(&arg);
3597 arg = skipwhite(arg);
3598 }
3599
3600 ex_unletlock(eap, arg, deep);
3601}
3602
3603/*
3604 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3605 */
3606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003607ex_unletlock(
3608 exarg_T *eap,
3609 char_u *argstart,
3610 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611{
3612 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 do
3618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003620 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003621 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 if (lv.ll_name == NULL)
3623 error = TRUE; /* error but continue parsing */
3624 if (name_end == NULL || (!vim_iswhite(*name_end)
3625 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (name_end != NULL)
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 if (!(eap->skip || error))
3633 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 break;
3635 }
3636
3637 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 {
3639 if (eap->cmdidx == CMD_unlet)
3640 {
3641 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3642 error = TRUE;
3643 }
3644 else
3645 {
3646 if (do_lock_var(&lv, name_end, deep,
3647 eap->cmdidx == CMD_lockvar) == FAIL)
3648 error = TRUE;
3649 }
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (!eap->skip)
3653 clear_lval(&lv);
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 arg = skipwhite(name_end);
3656 } while (!ends_excmd(*arg));
3657
3658 eap->nextcmd = check_nextcmd(arg);
3659}
3660
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003662do_unlet_var(
3663 lval_T *lp,
3664 char_u *name_end,
3665 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 int ret = OK;
3668 int cc;
3669
3670 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 cc = *name_end;
3673 *name_end = NUL;
3674
3675 /* Normal name or expanded name. */
3676 if (check_changedtick(lp->ll_name))
3677 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003684 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003685 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 else if (lp->ll_range)
3688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003691 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003692
3693 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3694 {
3695 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003696 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 return FAIL;
3698 ll_li = li;
3699 ++ll_n1;
3700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701
3702 /* Delete a range of List items. */
3703 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3704 {
3705 li = lp->ll_li->li_next;
3706 listitem_remove(lp->ll_list, lp->ll_li);
3707 lp->ll_li = li;
3708 ++lp->ll_n1;
3709 }
3710 }
3711 else
3712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a List item. */
3715 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003718 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 }
3720
3721 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003722}
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724/*
3725 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003729do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 hashtab_T *ht;
3732 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003733 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003735 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 ht = find_var_ht(name, &varname);
3738 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003740 if (ht == &globvarht)
3741 d = &globvardict;
3742 else if (current_funccal != NULL
3743 && ht == &current_funccal->l_vars.dv_hashtab)
3744 d = &current_funccal->l_vars;
3745 else if (ht == &compat_hashtab)
3746 d = &vimvardict;
3747 else
3748 {
3749 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3750 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3751 }
3752 if (d == NULL)
3753 {
3754 EMSG2(_(e_intern2), "do_unlet()");
3755 return FAIL;
3756 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003757 hi = hash_find(ht, varname);
3758 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003759 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003760 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003761 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003762 || var_check_ro(di->di_flags, name, FALSE)
3763 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003764 return FAIL;
3765
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766 delete_var(ht, hi);
3767 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003770 if (forceit)
3771 return OK;
3772 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 return FAIL;
3774}
3775
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003776/*
3777 * Lock or unlock variable indicated by "lp".
3778 * "deep" is the levels to go (-1 for unlimited);
3779 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3780 */
3781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782do_lock_var(
3783 lval_T *lp,
3784 char_u *name_end,
3785 int deep,
3786 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003787{
3788 int ret = OK;
3789 int cc;
3790 dictitem_T *di;
3791
3792 if (deep == 0) /* nothing to do */
3793 return OK;
3794
3795 if (lp->ll_tv == NULL)
3796 {
3797 cc = *name_end;
3798 *name_end = NUL;
3799
3800 /* Normal name or expanded name. */
3801 if (check_changedtick(lp->ll_name))
3802 ret = FAIL;
3803 else
3804 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003805 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003806 if (di == NULL)
3807 ret = FAIL;
3808 else
3809 {
3810 if (lock)
3811 di->di_flags |= DI_FLAGS_LOCK;
3812 else
3813 di->di_flags &= ~DI_FLAGS_LOCK;
3814 item_lock(&di->di_tv, deep, lock);
3815 }
3816 }
3817 *name_end = cc;
3818 }
3819 else if (lp->ll_range)
3820 {
3821 listitem_T *li = lp->ll_li;
3822
3823 /* (un)lock a range of List items. */
3824 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3825 {
3826 item_lock(&li->li_tv, deep, lock);
3827 li = li->li_next;
3828 ++lp->ll_n1;
3829 }
3830 }
3831 else if (lp->ll_list != NULL)
3832 /* (un)lock a List item. */
3833 item_lock(&lp->ll_li->li_tv, deep, lock);
3834 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003835 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003836 item_lock(&lp->ll_di->di_tv, deep, lock);
3837
3838 return ret;
3839}
3840
3841/*
3842 * Lock or unlock an item. "deep" is nr of levels to go.
3843 */
3844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003845item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003846{
3847 static int recurse = 0;
3848 list_T *l;
3849 listitem_T *li;
3850 dict_T *d;
3851 hashitem_T *hi;
3852 int todo;
3853
3854 if (recurse >= DICT_MAXNEST)
3855 {
3856 EMSG(_("E743: variable nested too deep for (un)lock"));
3857 return;
3858 }
3859 if (deep == 0)
3860 return;
3861 ++recurse;
3862
3863 /* lock/unlock the item itself */
3864 if (lock)
3865 tv->v_lock |= VAR_LOCKED;
3866 else
3867 tv->v_lock &= ~VAR_LOCKED;
3868
3869 switch (tv->v_type)
3870 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003871 case VAR_UNKNOWN:
3872 case VAR_NUMBER:
3873 case VAR_STRING:
3874 case VAR_FUNC:
3875 case VAR_FLOAT:
3876 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003877 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003878 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003879 break;
3880
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003881 case VAR_LIST:
3882 if ((l = tv->vval.v_list) != NULL)
3883 {
3884 if (lock)
3885 l->lv_lock |= VAR_LOCKED;
3886 else
3887 l->lv_lock &= ~VAR_LOCKED;
3888 if (deep < 0 || deep > 1)
3889 /* recursive: lock/unlock the items the List contains */
3890 for (li = l->lv_first; li != NULL; li = li->li_next)
3891 item_lock(&li->li_tv, deep - 1, lock);
3892 }
3893 break;
3894 case VAR_DICT:
3895 if ((d = tv->vval.v_dict) != NULL)
3896 {
3897 if (lock)
3898 d->dv_lock |= VAR_LOCKED;
3899 else
3900 d->dv_lock &= ~VAR_LOCKED;
3901 if (deep < 0 || deep > 1)
3902 {
3903 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003904 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003905 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3906 {
3907 if (!HASHITEM_EMPTY(hi))
3908 {
3909 --todo;
3910 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3911 }
3912 }
3913 }
3914 }
3915 }
3916 --recurse;
3917}
3918
Bram Moolenaara40058a2005-07-11 22:42:07 +00003919/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003920 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3921 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003922 */
3923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003924tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003925{
3926 return (tv->v_lock & VAR_LOCKED)
3927 || (tv->v_type == VAR_LIST
3928 && tv->vval.v_list != NULL
3929 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3930 || (tv->v_type == VAR_DICT
3931 && tv->vval.v_dict != NULL
3932 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3933}
3934
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3936/*
3937 * Delete all "menutrans_" variables.
3938 */
3939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003940del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003946 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 {
3949 if (!HASHITEM_EMPTY(hi))
3950 {
3951 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3953 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 }
3955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957}
3958#endif
3959
3960#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3961
3962/*
3963 * Local string buffer for the next two functions to store a variable name
3964 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3965 * get_user_var_name().
3966 */
3967
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003968static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970static char_u *varnamebuf = NULL;
3971static int varnamebuflen = 0;
3972
3973/*
3974 * Function to concatenate a prefix and a variable name.
3975 */
3976 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003977cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 int len;
3980
3981 len = (int)STRLEN(name) + 3;
3982 if (len > varnamebuflen)
3983 {
3984 vim_free(varnamebuf);
3985 len += 10; /* some additional space */
3986 varnamebuf = alloc(len);
3987 if (varnamebuf == NULL)
3988 {
3989 varnamebuflen = 0;
3990 return NULL;
3991 }
3992 varnamebuflen = len;
3993 }
3994 *varnamebuf = prefix;
3995 varnamebuf[1] = ':';
3996 STRCPY(varnamebuf + 2, name);
3997 return varnamebuf;
3998}
3999
4000/*
4001 * Function given to ExpandGeneric() to obtain the list of user defined
4002 * (global/buffer/window/built-in) variable names.
4003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004005get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static long_u gdone;
4008 static long_u bdone;
4009 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010#ifdef FEAT_WINDOWS
4011 static long_u tdone;
4012#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004013 static int vidx;
4014 static hashitem_T *hi;
4015 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004019 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_WINDOWS
4021 tdone = 0;
4022#endif
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024
4025 /* Global variables */
4026 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004030 else
4031 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004032 while (HASHITEM_EMPTY(hi))
4033 ++hi;
4034 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4035 return cat_prefix_varname('g', hi->hi_key);
4036 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004040 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 else
4046 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return (char_u *)"b:changedtick";
4055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
4057 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004058 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004061 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 else
4064 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004065 while (HASHITEM_EMPTY(hi))
4066 ++hi;
4067 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004069
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070#ifdef FEAT_WINDOWS
4071 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073 if (tdone < ht->ht_used)
4074 {
4075 if (tdone++ == 0)
4076 hi = ht->ht_array;
4077 else
4078 ++hi;
4079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('t', hi->hi_key);
4082 }
4083#endif
4084
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 /* v: variables */
4086 if (vidx < VV_LEN)
4087 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 vim_free(varnamebuf);
4090 varnamebuf = NULL;
4091 varnamebuflen = 0;
4092 return NULL;
4093}
4094
4095#endif /* FEAT_CMDL_COMPL */
4096
4097/*
4098 * types for expressions.
4099 */
4100typedef enum
4101{
4102 TYPE_UNKNOWN = 0
4103 , TYPE_EQUAL /* == */
4104 , TYPE_NEQUAL /* != */
4105 , TYPE_GREATER /* > */
4106 , TYPE_GEQUAL /* >= */
4107 , TYPE_SMALLER /* < */
4108 , TYPE_SEQUAL /* <= */
4109 , TYPE_MATCH /* =~ */
4110 , TYPE_NOMATCH /* !~ */
4111} exptype_T;
4112
4113/*
4114 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4117 */
4118
4119/*
4120 * Handle zero level expression.
4121 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004123 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 * Return OK or FAIL.
4125 */
4126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004127eval0(
4128 char_u *arg,
4129 typval_T *rettv,
4130 char_u **nextcmd,
4131 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
4133 int ret;
4134 char_u *p;
4135
4136 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (ret == FAIL || !ends_excmd(*p))
4139 {
4140 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 /*
4143 * Report the invalid expression unless the expression evaluation has
4144 * been cancelled due to an aborting error, an interrupt, or an
4145 * exception.
4146 */
4147 if (!aborting())
4148 EMSG2(_(e_invexpr2), arg);
4149 ret = FAIL;
4150 }
4151 if (nextcmd != NULL)
4152 *nextcmd = check_nextcmd(p);
4153
4154 return ret;
4155}
4156
4157/*
4158 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004159 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 *
4161 * "arg" must point to the first non-white of the expression.
4162 * "arg" is advanced to the next non-white after the recognized expression.
4163 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004164 * Note: "rettv.v_lock" is not set.
4165 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 * Return OK or FAIL.
4167 */
4168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004169eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004172 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
4174 /*
4175 * Get the first variable.
4176 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return FAIL;
4179
4180 if ((*arg)[0] == '?')
4181 {
4182 result = FALSE;
4183 if (evaluate)
4184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 int error = FALSE;
4186
4187 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (error)
4191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193
4194 /*
4195 * Get the second variable.
4196 */
4197 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 return FAIL;
4200
4201 /*
4202 * Check for the ":".
4203 */
4204 if ((*arg)[0] != ':')
4205 {
4206 EMSG(_("E109: Missing ':' after '?'"));
4207 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
4210 }
4211
4212 /*
4213 * Get the third variable.
4214 */
4215 *arg = skipwhite(*arg + 1);
4216 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4217 {
4218 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 return FAIL;
4221 }
4222 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225
4226 return OK;
4227}
4228
4229/*
4230 * Handle first level expression:
4231 * expr2 || expr2 || expr2 logical OR
4232 *
4233 * "arg" must point to the first non-white of the expression.
4234 * "arg" is advanced to the next non-white after the recognized expression.
4235 *
4236 * Return OK or FAIL.
4237 */
4238 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004239eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240{
Bram Moolenaar33570922005-01-25 22:26:29 +00004241 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 long result;
4243 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 /*
4247 * Get the first variable.
4248 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 return FAIL;
4251
4252 /*
4253 * Repeat until there is no following "||".
4254 */
4255 first = TRUE;
4256 result = FALSE;
4257 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4258 {
4259 if (evaluate && first)
4260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 first = FALSE;
4267 }
4268
4269 /*
4270 * Get the second variable.
4271 */
4272 *arg = skipwhite(*arg + 2);
4273 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4274 return FAIL;
4275
4276 /*
4277 * Compute the result.
4278 */
4279 if (evaluate && !result)
4280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 if (error)
4285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 if (evaluate)
4288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 rettv->v_type = VAR_NUMBER;
4290 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 }
4293
4294 return OK;
4295}
4296
4297/*
4298 * Handle second level expression:
4299 * expr3 && expr3 && expr3 logical AND
4300 *
4301 * "arg" must point to the first non-white of the expression.
4302 * "arg" is advanced to the next non-white after the recognized expression.
4303 *
4304 * Return OK or FAIL.
4305 */
4306 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004307eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
Bram Moolenaar33570922005-01-25 22:26:29 +00004309 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 long result;
4311 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
4314 /*
4315 * Get the first variable.
4316 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004317 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 return FAIL;
4319
4320 /*
4321 * Repeat until there is no following "&&".
4322 */
4323 first = TRUE;
4324 result = TRUE;
4325 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4326 {
4327 if (evaluate && first)
4328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (error)
4333 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 first = FALSE;
4335 }
4336
4337 /*
4338 * Get the second variable.
4339 */
4340 *arg = skipwhite(*arg + 2);
4341 if (eval4(arg, &var2, evaluate && result) == FAIL)
4342 return FAIL;
4343
4344 /*
4345 * Compute the result.
4346 */
4347 if (evaluate && result)
4348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004351 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004352 if (error)
4353 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355 if (evaluate)
4356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 rettv->v_type = VAR_NUMBER;
4358 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 }
4361
4362 return OK;
4363}
4364
4365/*
4366 * Handle third level expression:
4367 * var1 == var2
4368 * var1 =~ var2
4369 * var1 != var2
4370 * var1 !~ var2
4371 * var1 > var2
4372 * var1 >= var2
4373 * var1 < var2
4374 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004375 * var1 is var2
4376 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 *
4378 * "arg" must point to the first non-white of the expression.
4379 * "arg" is advanced to the next non-white after the recognized expression.
4380 *
4381 * Return OK or FAIL.
4382 */
4383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004384eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385{
Bram Moolenaar33570922005-01-25 22:26:29 +00004386 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 char_u *p;
4388 int i;
4389 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 int len = 2;
4392 long n1, n2;
4393 char_u *s1, *s2;
4394 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4395 regmatch_T regmatch;
4396 int ic;
4397 char_u *save_cpo;
4398
4399 /*
4400 * Get the first variable.
4401 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 return FAIL;
4404
4405 p = *arg;
4406 switch (p[0])
4407 {
4408 case '=': if (p[1] == '=')
4409 type = TYPE_EQUAL;
4410 else if (p[1] == '~')
4411 type = TYPE_MATCH;
4412 break;
4413 case '!': if (p[1] == '=')
4414 type = TYPE_NEQUAL;
4415 else if (p[1] == '~')
4416 type = TYPE_NOMATCH;
4417 break;
4418 case '>': if (p[1] != '=')
4419 {
4420 type = TYPE_GREATER;
4421 len = 1;
4422 }
4423 else
4424 type = TYPE_GEQUAL;
4425 break;
4426 case '<': if (p[1] != '=')
4427 {
4428 type = TYPE_SMALLER;
4429 len = 1;
4430 }
4431 else
4432 type = TYPE_SEQUAL;
4433 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 case 'i': if (p[1] == 's')
4435 {
4436 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4437 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004438 i = p[len];
4439 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 {
4441 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4442 type_is = TRUE;
4443 }
4444 }
4445 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447
4448 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004449 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 */
4451 if (type != TYPE_UNKNOWN)
4452 {
4453 /* extra question mark appended: ignore case */
4454 if (p[len] == '?')
4455 {
4456 ic = TRUE;
4457 ++len;
4458 }
4459 /* extra '#' appended: match case */
4460 else if (p[len] == '#')
4461 {
4462 ic = FALSE;
4463 ++len;
4464 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004465 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 else
4467 ic = p_ic;
4468
4469 /*
4470 * Get the second variable.
4471 */
4472 *arg = skipwhite(p + len);
4473 if (eval5(arg, &var2, evaluate) == FAIL)
4474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 return FAIL;
4477 }
4478
4479 if (evaluate)
4480 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004481 if (type_is && rettv->v_type != var2.v_type)
4482 {
4483 /* For "is" a different type always means FALSE, for "notis"
4484 * it means TRUE. */
4485 n1 = (type == TYPE_NEQUAL);
4486 }
4487 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4488 {
4489 if (type_is)
4490 {
4491 n1 = (rettv->v_type == var2.v_type
4492 && rettv->vval.v_list == var2.vval.v_list);
4493 if (type == TYPE_NEQUAL)
4494 n1 = !n1;
4495 }
4496 else if (rettv->v_type != var2.v_type
4497 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4498 {
4499 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004500 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004501 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004502 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004503 clear_tv(rettv);
4504 clear_tv(&var2);
4505 return FAIL;
4506 }
4507 else
4508 {
4509 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004510 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4511 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 if (type == TYPE_NEQUAL)
4513 n1 = !n1;
4514 }
4515 }
4516
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004517 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4518 {
4519 if (type_is)
4520 {
4521 n1 = (rettv->v_type == var2.v_type
4522 && rettv->vval.v_dict == var2.vval.v_dict);
4523 if (type == TYPE_NEQUAL)
4524 n1 = !n1;
4525 }
4526 else if (rettv->v_type != var2.v_type
4527 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4528 {
4529 if (rettv->v_type != var2.v_type)
4530 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4531 else
4532 EMSG(_("E736: Invalid operation for Dictionary"));
4533 clear_tv(rettv);
4534 clear_tv(&var2);
4535 return FAIL;
4536 }
4537 else
4538 {
4539 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004540 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4541 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004542 if (type == TYPE_NEQUAL)
4543 n1 = !n1;
4544 }
4545 }
4546
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004547 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4548 {
4549 if (rettv->v_type != var2.v_type
4550 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4551 {
4552 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004553 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004554 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004555 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004556 clear_tv(rettv);
4557 clear_tv(&var2);
4558 return FAIL;
4559 }
4560 else
4561 {
4562 /* Compare two Funcrefs for being equal or unequal. */
4563 if (rettv->vval.v_string == NULL
4564 || var2.vval.v_string == NULL)
4565 n1 = FALSE;
4566 else
4567 n1 = STRCMP(rettv->vval.v_string,
4568 var2.vval.v_string) == 0;
4569 if (type == TYPE_NEQUAL)
4570 n1 = !n1;
4571 }
4572 }
4573
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004574#ifdef FEAT_FLOAT
4575 /*
4576 * If one of the two variables is a float, compare as a float.
4577 * When using "=~" or "!~", always compare as string.
4578 */
4579 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4580 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4581 {
4582 float_T f1, f2;
4583
4584 if (rettv->v_type == VAR_FLOAT)
4585 f1 = rettv->vval.v_float;
4586 else
4587 f1 = get_tv_number(rettv);
4588 if (var2.v_type == VAR_FLOAT)
4589 f2 = var2.vval.v_float;
4590 else
4591 f2 = get_tv_number(&var2);
4592 n1 = FALSE;
4593 switch (type)
4594 {
4595 case TYPE_EQUAL: n1 = (f1 == f2); break;
4596 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4597 case TYPE_GREATER: n1 = (f1 > f2); break;
4598 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4599 case TYPE_SMALLER: n1 = (f1 < f2); break;
4600 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4601 case TYPE_UNKNOWN:
4602 case TYPE_MATCH:
4603 case TYPE_NOMATCH: break; /* avoid gcc warning */
4604 }
4605 }
4606#endif
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 /*
4609 * If one of the two variables is a number, compare as a number.
4610 * When using "=~" or "!~", always compare as string.
4611 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004612 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 n1 = get_tv_number(rettv);
4616 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 switch (type)
4618 {
4619 case TYPE_EQUAL: n1 = (n1 == n2); break;
4620 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4621 case TYPE_GREATER: n1 = (n1 > n2); break;
4622 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4623 case TYPE_SMALLER: n1 = (n1 < n2); break;
4624 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4625 case TYPE_UNKNOWN:
4626 case TYPE_MATCH:
4627 case TYPE_NOMATCH: break; /* avoid gcc warning */
4628 }
4629 }
4630 else
4631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 s1 = get_tv_string_buf(rettv, buf1);
4633 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4635 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4636 else
4637 i = 0;
4638 n1 = FALSE;
4639 switch (type)
4640 {
4641 case TYPE_EQUAL: n1 = (i == 0); break;
4642 case TYPE_NEQUAL: n1 = (i != 0); break;
4643 case TYPE_GREATER: n1 = (i > 0); break;
4644 case TYPE_GEQUAL: n1 = (i >= 0); break;
4645 case TYPE_SMALLER: n1 = (i < 0); break;
4646 case TYPE_SEQUAL: n1 = (i <= 0); break;
4647
4648 case TYPE_MATCH:
4649 case TYPE_NOMATCH:
4650 /* avoid 'l' flag in 'cpoptions' */
4651 save_cpo = p_cpo;
4652 p_cpo = (char_u *)"";
4653 regmatch.regprog = vim_regcomp(s2,
4654 RE_MAGIC + RE_STRING);
4655 regmatch.rm_ic = ic;
4656 if (regmatch.regprog != NULL)
4657 {
4658 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004659 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (type == TYPE_NOMATCH)
4661 n1 = !n1;
4662 }
4663 p_cpo = save_cpo;
4664 break;
4665
4666 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4667 }
4668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
4670 clear_tv(&var2);
4671 rettv->v_type = VAR_NUMBER;
4672 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674 }
4675
4676 return OK;
4677}
4678
4679/*
4680 * Handle fourth level expression:
4681 * + number addition
4682 * - number subtraction
4683 * . string concatenation
4684 *
4685 * "arg" must point to the first non-white of the expression.
4686 * "arg" is advanced to the next non-white after the recognized expression.
4687 *
4688 * Return OK or FAIL.
4689 */
4690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004691eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
Bram Moolenaar33570922005-01-25 22:26:29 +00004693 typval_T var2;
4694 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 int op;
4696 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697#ifdef FEAT_FLOAT
4698 float_T f1 = 0, f2 = 0;
4699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 char_u *s1, *s2;
4701 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4702 char_u *p;
4703
4704 /*
4705 * Get the first variable.
4706 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004707 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return FAIL;
4709
4710 /*
4711 * Repeat computing, until no '+', '-' or '.' is following.
4712 */
4713 for (;;)
4714 {
4715 op = **arg;
4716 if (op != '+' && op != '-' && op != '.')
4717 break;
4718
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719 if ((op != '+' || rettv->v_type != VAR_LIST)
4720#ifdef FEAT_FLOAT
4721 && (op == '.' || rettv->v_type != VAR_FLOAT)
4722#endif
4723 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 {
4725 /* For "list + ...", an illegal use of the first operand as
4726 * a number cannot be determined before evaluating the 2nd
4727 * operand: if this is also a list, all is ok.
4728 * For "something . ...", "something - ..." or "non-list + ...",
4729 * we know that the first operand needs to be a string or number
4730 * without evaluating the 2nd operand. So check before to avoid
4731 * side effects after an error. */
4732 if (evaluate && get_tv_string_chk(rettv) == NULL)
4733 {
4734 clear_tv(rettv);
4735 return FAIL;
4736 }
4737 }
4738
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 /*
4740 * Get the second variable.
4741 */
4742 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004743 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 return FAIL;
4747 }
4748
4749 if (evaluate)
4750 {
4751 /*
4752 * Compute the result.
4753 */
4754 if (op == '.')
4755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4757 s2 = get_tv_string_buf_chk(&var2, buf2);
4758 if (s2 == NULL) /* type error ? */
4759 {
4760 clear_tv(rettv);
4761 clear_tv(&var2);
4762 return FAIL;
4763 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004764 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765 clear_tv(rettv);
4766 rettv->v_type = VAR_STRING;
4767 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004769 else if (op == '+' && rettv->v_type == VAR_LIST
4770 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004771 {
4772 /* concatenate Lists */
4773 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4774 &var3) == FAIL)
4775 {
4776 clear_tv(rettv);
4777 clear_tv(&var2);
4778 return FAIL;
4779 }
4780 clear_tv(rettv);
4781 *rettv = var3;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 else
4784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 int error = FALSE;
4786
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787#ifdef FEAT_FLOAT
4788 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790 f1 = rettv->vval.v_float;
4791 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793 else
4794#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 n1 = get_tv_number_chk(rettv, &error);
4797 if (error)
4798 {
4799 /* This can only happen for "list + non-list". For
4800 * "non-list + ..." or "something - ...", we returned
4801 * before evaluating the 2nd operand. */
4802 clear_tv(rettv);
4803 return FAIL;
4804 }
4805#ifdef FEAT_FLOAT
4806 if (var2.v_type == VAR_FLOAT)
4807 f1 = n1;
4808#endif
4809 }
4810#ifdef FEAT_FLOAT
4811 if (var2.v_type == VAR_FLOAT)
4812 {
4813 f2 = var2.vval.v_float;
4814 n2 = 0;
4815 }
4816 else
4817#endif
4818 {
4819 n2 = get_tv_number_chk(&var2, &error);
4820 if (error)
4821 {
4822 clear_tv(rettv);
4823 clear_tv(&var2);
4824 return FAIL;
4825 }
4826#ifdef FEAT_FLOAT
4827 if (rettv->v_type == VAR_FLOAT)
4828 f2 = n2;
4829#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832
4833#ifdef FEAT_FLOAT
4834 /* If there is a float on either side the result is a float. */
4835 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4836 {
4837 if (op == '+')
4838 f1 = f1 + f2;
4839 else
4840 f1 = f1 - f2;
4841 rettv->v_type = VAR_FLOAT;
4842 rettv->vval.v_float = f1;
4843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845#endif
4846 {
4847 if (op == '+')
4848 n1 = n1 + n2;
4849 else
4850 n1 = n1 - n2;
4851 rettv->v_type = VAR_NUMBER;
4852 rettv->vval.v_number = n1;
4853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857 }
4858 return OK;
4859}
4860
4861/*
4862 * Handle fifth level expression:
4863 * * number multiplication
4864 * / number division
4865 * % number modulo
4866 *
4867 * "arg" must point to the first non-white of the expression.
4868 * "arg" is advanced to the next non-white after the recognized expression.
4869 *
4870 * Return OK or FAIL.
4871 */
4872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004873eval6(
4874 char_u **arg,
4875 typval_T *rettv,
4876 int evaluate,
4877 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878{
Bram Moolenaar33570922005-01-25 22:26:29 +00004879 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 int op;
4881 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882#ifdef FEAT_FLOAT
4883 int use_float = FALSE;
4884 float_T f1 = 0, f2;
4885#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 /*
4889 * Get the first variable.
4890 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004891 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 return FAIL;
4893
4894 /*
4895 * Repeat computing, until no '*', '/' or '%' is following.
4896 */
4897 for (;;)
4898 {
4899 op = **arg;
4900 if (op != '*' && op != '/' && op != '%')
4901 break;
4902
4903 if (evaluate)
4904 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905#ifdef FEAT_FLOAT
4906 if (rettv->v_type == VAR_FLOAT)
4907 {
4908 f1 = rettv->vval.v_float;
4909 use_float = TRUE;
4910 n1 = 0;
4911 }
4912 else
4913#endif
4914 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004916 if (error)
4917 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 else
4920 n1 = 0;
4921
4922 /*
4923 * Get the second variable.
4924 */
4925 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004926 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 return FAIL;
4928
4929 if (evaluate)
4930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931#ifdef FEAT_FLOAT
4932 if (var2.v_type == VAR_FLOAT)
4933 {
4934 if (!use_float)
4935 {
4936 f1 = n1;
4937 use_float = TRUE;
4938 }
4939 f2 = var2.vval.v_float;
4940 n2 = 0;
4941 }
4942 else
4943#endif
4944 {
4945 n2 = get_tv_number_chk(&var2, &error);
4946 clear_tv(&var2);
4947 if (error)
4948 return FAIL;
4949#ifdef FEAT_FLOAT
4950 if (use_float)
4951 f2 = n2;
4952#endif
4953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
4955 /*
4956 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004959#ifdef FEAT_FLOAT
4960 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962 if (op == '*')
4963 f1 = f1 * f2;
4964 else if (op == '/')
4965 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966# ifdef VMS
4967 /* VMS crashes on divide by zero, work around it */
4968 if (f2 == 0.0)
4969 {
4970 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004973 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004974 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004975 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004976 }
4977 else
4978 f1 = f1 / f2;
4979# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 /* We rely on the floating point library to handle divide
4981 * by zero to result in "inf" and not a crash. */
4982 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004983# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004987 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 return FAIL;
4989 }
4990 rettv->v_type = VAR_FLOAT;
4991 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 if (op == '*')
4997 n1 = n1 * n2;
4998 else if (op == '/')
4999 {
5000 if (n2 == 0) /* give an error message? */
5001 {
5002 if (n1 == 0)
5003 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5004 else if (n1 < 0)
5005 n1 = -0x7fffffffL;
5006 else
5007 n1 = 0x7fffffffL;
5008 }
5009 else
5010 n1 = n1 / n2;
5011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005013 {
5014 if (n2 == 0) /* give an error message? */
5015 n1 = 0;
5016 else
5017 n1 = n1 % n2;
5018 }
5019 rettv->v_type = VAR_NUMBER;
5020 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 }
5024
5025 return OK;
5026}
5027
5028/*
5029 * Handle sixth level expression:
5030 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005031 * "string" string constant
5032 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 * &option-name option value
5034 * @r register contents
5035 * identifier variable value
5036 * function() function call
5037 * $VAR environment variable
5038 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005039 * [expr, expr] List
5040 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *
5042 * Also handle:
5043 * ! in front logical NOT
5044 * - in front unary minus
5045 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 * trailing [] subscript in String or List
5047 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 *
5049 * "arg" must point to the first non-white of the expression.
5050 * "arg" is advanced to the next non-white after the recognized expression.
5051 *
5052 * Return OK or FAIL.
5053 */
5054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005055eval7(
5056 char_u **arg,
5057 typval_T *rettv,
5058 int evaluate,
5059 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 long n;
5062 int len;
5063 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 char_u *start_leader, *end_leader;
5065 int ret = OK;
5066 char_u *alias;
5067
5068 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005070 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /*
5075 * Skip '!' and '-' characters. They are handled later.
5076 */
5077 start_leader = *arg;
5078 while (**arg == '!' || **arg == '-' || **arg == '+')
5079 *arg = skipwhite(*arg + 1);
5080 end_leader = *arg;
5081
5082 switch (**arg)
5083 {
5084 /*
5085 * Number constant.
5086 */
5087 case '0':
5088 case '1':
5089 case '2':
5090 case '3':
5091 case '4':
5092 case '5':
5093 case '6':
5094 case '7':
5095 case '8':
5096 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005097 {
5098#ifdef FEAT_FLOAT
5099 char_u *p = skipdigits(*arg + 1);
5100 int get_float = FALSE;
5101
5102 /* We accept a float when the format matches
5103 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005104 * strict to avoid backwards compatibility problems.
5105 * Don't look for a float after the "." operator, so that
5106 * ":let vers = 1.2.3" doesn't fail. */
5107 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 get_float = TRUE;
5110 p = skipdigits(p + 2);
5111 if (*p == 'e' || *p == 'E')
5112 {
5113 ++p;
5114 if (*p == '-' || *p == '+')
5115 ++p;
5116 if (!vim_isdigit(*p))
5117 get_float = FALSE;
5118 else
5119 p = skipdigits(p + 1);
5120 }
5121 if (ASCII_ISALPHA(*p) || *p == '.')
5122 get_float = FALSE;
5123 }
5124 if (get_float)
5125 {
5126 float_T f;
5127
5128 *arg += string2float(*arg, &f);
5129 if (evaluate)
5130 {
5131 rettv->v_type = VAR_FLOAT;
5132 rettv->vval.v_float = f;
5133 }
5134 }
5135 else
5136#endif
5137 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005138 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005139 *arg += len;
5140 if (evaluate)
5141 {
5142 rettv->v_type = VAR_NUMBER;
5143 rettv->vval.v_number = n;
5144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 /*
5150 * String constant: "string".
5151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 break;
5154
5155 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005158 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005159 break;
5160
5161 /*
5162 * List: [expr, expr]
5163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005164 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 break;
5166
5167 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 * Dictionary: {key: val, key: val}
5169 */
5170 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5171 break;
5172
5173 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005176 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178
5179 /*
5180 * Environment variable: $VAR.
5181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005182 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 break;
5184
5185 /*
5186 * Register contents: @r.
5187 */
5188 case '@': ++*arg;
5189 if (evaluate)
5190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005192 rettv->vval.v_string = get_reg_contents(**arg,
5193 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 if (**arg != NUL)
5196 ++*arg;
5197 break;
5198
5199 /*
5200 * nested expression: (expression).
5201 */
5202 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005203 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (**arg == ')')
5205 ++*arg;
5206 else if (ret == OK)
5207 {
5208 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005209 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 ret = FAIL;
5211 }
5212 break;
5213
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217
5218 if (ret == NOTDONE)
5219 {
5220 /*
5221 * Must be a variable or function name.
5222 * Can also be a curly-braces kind of name: {expr}.
5223 */
5224 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005225 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 if (alias != NULL)
5227 s = alias;
5228
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005229 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 ret = FAIL;
5231 else
5232 {
5233 if (**arg == '(') /* recursive! */
5234 {
5235 /* If "s" is the name of a variable of type VAR_FUNC
5236 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005237 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238
5239 /* Invoke the function. */
5240 ret = get_func_tv(s, len, rettv, arg,
5241 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005242 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005243
5244 /* If evaluate is FALSE rettv->v_type was not set in
5245 * get_func_tv, but it's needed in handle_subscript() to parse
5246 * what follows. So set it here. */
5247 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5248 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005249 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005250 rettv->v_type = VAR_FUNC;
5251 }
5252
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 /* Stop the expression evaluation when immediately
5254 * aborting on error, or when an interrupt occurred or
5255 * an exception was thrown but not caught. */
5256 if (aborting())
5257 {
5258 if (ret == OK)
5259 clear_tv(rettv);
5260 ret = FAIL;
5261 }
5262 }
5263 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005264 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005265 else
5266 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005268 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 }
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 *arg = skipwhite(*arg);
5272
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005273 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5274 * expr(expr). */
5275 if (ret == OK)
5276 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /*
5279 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5280 */
5281 if (ret == OK && evaluate && end_leader > start_leader)
5282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284 int val = 0;
5285#ifdef FEAT_FLOAT
5286 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005288 if (rettv->v_type == VAR_FLOAT)
5289 f = rettv->vval.v_float;
5290 else
5291#endif
5292 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 clear_tv(rettv);
5296 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 else
5299 {
5300 while (end_leader > start_leader)
5301 {
5302 --end_leader;
5303 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005304 {
5305#ifdef FEAT_FLOAT
5306 if (rettv->v_type == VAR_FLOAT)
5307 f = !f;
5308 else
5309#endif
5310 val = !val;
5311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005313 {
5314#ifdef FEAT_FLOAT
5315 if (rettv->v_type == VAR_FLOAT)
5316 f = -f;
5317 else
5318#endif
5319 val = -val;
5320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005322#ifdef FEAT_FLOAT
5323 if (rettv->v_type == VAR_FLOAT)
5324 {
5325 clear_tv(rettv);
5326 rettv->vval.v_float = f;
5327 }
5328 else
5329#endif
5330 {
5331 clear_tv(rettv);
5332 rettv->v_type = VAR_NUMBER;
5333 rettv->vval.v_number = val;
5334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337
5338 return ret;
5339}
5340
5341/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005342 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5343 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5345 */
5346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005347eval_index(
5348 char_u **arg,
5349 typval_T *rettv,
5350 int evaluate,
5351 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352{
5353 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 long len = -1;
5357 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 case VAR_FUNC:
5364 if (verbose)
5365 EMSG(_("E695: Cannot index a Funcref"));
5366 return FAIL;
5367 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005368#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005369 if (verbose)
5370 EMSG(_(e_float_as_string));
5371 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005372#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005373 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005374 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005375 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005376 if (verbose)
5377 EMSG(_("E909: Cannot index a special variable"));
5378 return FAIL;
5379 case VAR_UNKNOWN:
5380 if (evaluate)
5381 return FAIL;
5382 /* FALLTHROUGH */
5383
5384 case VAR_STRING:
5385 case VAR_NUMBER:
5386 case VAR_LIST:
5387 case VAR_DICT:
5388 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005389 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005391 init_tv(&var1);
5392 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 /*
5396 * dict.name
5397 */
5398 key = *arg + 1;
5399 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5400 ;
5401 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 }
5405 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 /*
5408 * something[idx]
5409 *
5410 * Get the (first) variable from inside the [].
5411 */
5412 *arg = skipwhite(*arg + 1);
5413 if (**arg == ':')
5414 empty1 = TRUE;
5415 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5416 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005417 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5418 {
5419 /* not a number or string */
5420 clear_tv(&var1);
5421 return FAIL;
5422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005423
5424 /*
5425 * Get the second variable from inside the [:].
5426 */
5427 if (**arg == ':')
5428 {
5429 range = TRUE;
5430 *arg = skipwhite(*arg + 1);
5431 if (**arg == ']')
5432 empty2 = TRUE;
5433 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005435 if (!empty1)
5436 clear_tv(&var1);
5437 return FAIL;
5438 }
5439 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5440 {
5441 /* not a number or string */
5442 if (!empty1)
5443 clear_tv(&var1);
5444 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 return FAIL;
5446 }
5447 }
5448
5449 /* Check for the ']'. */
5450 if (**arg != ']')
5451 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005452 if (verbose)
5453 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 clear_tv(&var1);
5455 if (range)
5456 clear_tv(&var2);
5457 return FAIL;
5458 }
5459 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461
5462 if (evaluate)
5463 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 n1 = 0;
5465 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 n1 = get_tv_number(&var1);
5468 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 }
5470 if (range)
5471 {
5472 if (empty2)
5473 n2 = -1;
5474 else
5475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 n2 = get_tv_number(&var2);
5477 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
5479 }
5480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005484 case VAR_FUNC:
5485 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 Moolenaar8b402a02006-10-17 13:16:39 +00006187 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006188 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006190 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 * recursiveness to a limit. We guess they are equal then.
6192 * A fixed limit has the problem of still taking an awful long time.
6193 * Reduce the limit every time running into it. That should work fine for
6194 * deeply linked structures that are not recursively linked and catch
6195 * recursiveness quickly. */
6196 if (!recursive)
6197 tv_equal_recurse_limit = 1000;
6198 if (recursive_cnt >= tv_equal_recurse_limit)
6199 {
6200 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203
6204 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006206 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 ++recursive_cnt;
6208 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6209 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211
6212 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 ++recursive_cnt;
6214 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6215 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006216 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217
6218 case VAR_FUNC:
6219 return (tv1->vval.v_string != NULL
6220 && tv2->vval.v_string != NULL
6221 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6222
6223 case VAR_NUMBER:
6224 return tv1->vval.v_number == tv2->vval.v_number;
6225
6226 case VAR_STRING:
6227 s1 = get_tv_string_buf(tv1, buf1);
6228 s2 = get_tv_string_buf(tv2, buf2);
6229 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006230
6231 case VAR_SPECIAL:
6232 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006233
6234 case VAR_FLOAT:
6235#ifdef FEAT_FLOAT
6236 return tv1->vval.v_float == tv2->vval.v_float;
6237#endif
6238 case VAR_JOB:
6239#ifdef FEAT_JOB
6240 return tv1->vval.v_job == tv2->vval.v_job;
6241#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006242 case VAR_CHANNEL:
6243#ifdef FEAT_CHANNEL
6244 return tv1->vval.v_channel == tv2->vval.v_channel;
6245#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006246 case VAR_UNKNOWN:
6247 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006248 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006249
Bram Moolenaara03f2332016-02-06 18:09:59 +01006250 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6251 * does not equal anything, not even itself. */
6252 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253}
6254
6255/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 * Locate item with index "n" in list "l" and return it.
6257 * A negative index is counted from the end; -1 is the last item.
6258 * Returns NULL when "n" is out of range.
6259 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006260 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006261list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262{
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 long idx;
6265
6266 if (l == NULL)
6267 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006268
6269 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006271 n = l->lv_len + n;
6272
6273 /* Check for index out of range. */
6274 if (n < 0 || n >= l->lv_len)
6275 return NULL;
6276
6277 /* When there is a cached index may start search from there. */
6278 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006280 if (n < l->lv_idx / 2)
6281 {
6282 /* closest to the start of the list */
6283 item = l->lv_first;
6284 idx = 0;
6285 }
6286 else if (n > (l->lv_idx + l->lv_len) / 2)
6287 {
6288 /* closest to the end of the list */
6289 item = l->lv_last;
6290 idx = l->lv_len - 1;
6291 }
6292 else
6293 {
6294 /* closest to the cached index */
6295 item = l->lv_idx_item;
6296 idx = l->lv_idx;
6297 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298 }
6299 else
6300 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 if (n < l->lv_len / 2)
6302 {
6303 /* closest to the start of the list */
6304 item = l->lv_first;
6305 idx = 0;
6306 }
6307 else
6308 {
6309 /* closest to the end of the list */
6310 item = l->lv_last;
6311 idx = l->lv_len - 1;
6312 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006314
6315 while (n > idx)
6316 {
6317 /* search forward */
6318 item = item->li_next;
6319 ++idx;
6320 }
6321 while (n < idx)
6322 {
6323 /* search backward */
6324 item = item->li_prev;
6325 --idx;
6326 }
6327
6328 /* cache the used index */
6329 l->lv_idx = idx;
6330 l->lv_idx_item = item;
6331
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006332 return item;
6333}
6334
6335/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006336 * Get list item "l[idx]" as a number.
6337 */
6338 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006339list_find_nr(
6340 list_T *l,
6341 long idx,
6342 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006343{
6344 listitem_T *li;
6345
6346 li = list_find(l, idx);
6347 if (li == NULL)
6348 {
6349 if (errorp != NULL)
6350 *errorp = TRUE;
6351 return -1L;
6352 }
6353 return get_tv_number_chk(&li->li_tv, errorp);
6354}
6355
6356/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006357 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6358 */
6359 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006360list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006361{
6362 listitem_T *li;
6363
6364 li = list_find(l, idx - 1);
6365 if (li == NULL)
6366 {
6367 EMSGN(_(e_listidx), idx);
6368 return NULL;
6369 }
6370 return get_tv_string(&li->li_tv);
6371}
6372
6373/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006374 * Locate "item" list "l" and return its index.
6375 * Returns -1 when "item" is not in the list.
6376 */
6377 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006378list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006379{
6380 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006382
6383 if (l == NULL)
6384 return -1;
6385 idx = 0;
6386 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6387 ++idx;
6388 if (li == NULL)
6389 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006390 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006391}
6392
6393/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 * Append item "item" to the end of list "l".
6395 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006397list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006398{
6399 if (l->lv_last == NULL)
6400 {
6401 /* empty list */
6402 l->lv_first = item;
6403 l->lv_last = item;
6404 item->li_prev = NULL;
6405 }
6406 else
6407 {
6408 l->lv_last->li_next = item;
6409 item->li_prev = l->lv_last;
6410 l->lv_last = item;
6411 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006412 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 item->li_next = NULL;
6414}
6415
6416/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006420 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006421list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424
Bram Moolenaar05159a02005-02-26 23:04:13 +00006425 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006427 copy_tv(tv, &li->li_tv);
6428 list_append(l, li);
6429 return OK;
6430}
6431
6432/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006433 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 * Return FAIL when out of memory.
6435 */
6436 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006437list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006438{
6439 listitem_T *li = listitem_alloc();
6440
6441 if (li == NULL)
6442 return FAIL;
6443 li->li_tv.v_type = VAR_DICT;
6444 li->li_tv.v_lock = 0;
6445 li->li_tv.vval.v_dict = dict;
6446 list_append(list, li);
6447 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006448 return OK;
6449}
6450
6451/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006452 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006453 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006454 * Returns FAIL when out of memory.
6455 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006457list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006458{
6459 listitem_T *li = listitem_alloc();
6460
6461 if (li == NULL)
6462 return FAIL;
6463 list_append(l, li);
6464 li->li_tv.v_type = VAR_STRING;
6465 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006466 if (str == NULL)
6467 li->li_tv.vval.v_string = NULL;
6468 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006469 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470 return FAIL;
6471 return OK;
6472}
6473
6474/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006475 * Append "n" to list "l".
6476 * Returns FAIL when out of memory.
6477 */
6478 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006479list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006480{
6481 listitem_T *li;
6482
6483 li = listitem_alloc();
6484 if (li == NULL)
6485 return FAIL;
6486 li->li_tv.v_type = VAR_NUMBER;
6487 li->li_tv.v_lock = 0;
6488 li->li_tv.vval.v_number = n;
6489 list_append(l, li);
6490 return OK;
6491}
6492
6493/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495 * If "item" is NULL append at the end.
6496 * Return FAIL when out of memory.
6497 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006499list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500{
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502
6503 if (ni == NULL)
6504 return FAIL;
6505 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006506 list_insert(l, ni, item);
6507 return OK;
6508}
6509
6510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006511list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006512{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513 if (item == NULL)
6514 /* Append new item at end of list. */
6515 list_append(l, ni);
6516 else
6517 {
6518 /* Insert new item before existing item. */
6519 ni->li_prev = item->li_prev;
6520 ni->li_next = item;
6521 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006522 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006524 ++l->lv_idx;
6525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006527 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006529 l->lv_idx_item = NULL;
6530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006532 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534}
6535
6536/*
6537 * Extend "l1" with "l2".
6538 * If "bef" is NULL append at the end, otherwise insert before this item.
6539 * Returns FAIL when out of memory.
6540 */
6541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006542list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543{
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006545 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006547 /* We also quit the loop when we have inserted the original item count of
6548 * the list, avoid a hang when we extend a list with itself. */
6549 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6551 return FAIL;
6552 return OK;
6553}
6554
6555/*
6556 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6557 * Return FAIL when out of memory.
6558 */
6559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006560list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561{
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006564 if (l1 == NULL || l2 == NULL)
6565 return FAIL;
6566
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006568 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569 if (l == NULL)
6570 return FAIL;
6571 tv->v_type = VAR_LIST;
6572 tv->vval.v_list = l;
6573
6574 /* append all items from the second list */
6575 return list_extend(l, l2, NULL);
6576}
6577
6578/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006579 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006581 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582 * Returns NULL when out of memory.
6583 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006585list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586{
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 list_T *copy;
6588 listitem_T *item;
6589 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590
6591 if (orig == NULL)
6592 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593
6594 copy = list_alloc();
6595 if (copy != NULL)
6596 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 if (copyID != 0)
6598 {
6599 /* Do this before adding the items, because one of the items may
6600 * refer back to this list. */
6601 orig->lv_copyID = copyID;
6602 orig->lv_copylist = copy;
6603 }
6604 for (item = orig->lv_first; item != NULL && !got_int;
6605 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 {
6607 ni = listitem_alloc();
6608 if (ni == NULL)
6609 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006610 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611 {
6612 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6613 {
6614 vim_free(ni);
6615 break;
6616 }
6617 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006619 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 list_append(copy, ni);
6621 }
6622 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006623 if (item != NULL)
6624 {
6625 list_unref(copy);
6626 copy = NULL;
6627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 }
6629
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 return copy;
6631}
6632
6633/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006635 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006636 * This used to be called list_remove, but that conflicts with a Sun header
6637 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006639 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006640vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641{
Bram Moolenaar33570922005-01-25 22:26:29 +00006642 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 /* notify watchers */
6645 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006647 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648 list_fix_watch(l, ip);
6649 if (ip == item2)
6650 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652
6653 if (item2->li_next == NULL)
6654 l->lv_last = item->li_prev;
6655 else
6656 item2->li_next->li_prev = item->li_prev;
6657 if (item->li_prev == NULL)
6658 l->lv_first = item2->li_next;
6659 else
6660 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006661 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662}
6663
6664/*
6665 * Return an allocated string with the string representation of a list.
6666 * May return NULL.
6667 */
6668 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670{
6671 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672
6673 if (tv->vval.v_list == NULL)
6674 return NULL;
6675 ga_init2(&ga, (int)sizeof(char), 80);
6676 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006678 {
6679 vim_free(ga.ga_data);
6680 return NULL;
6681 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682 ga_append(&ga, ']');
6683 ga_append(&ga, NUL);
6684 return (char_u *)ga.ga_data;
6685}
6686
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006687typedef struct join_S {
6688 char_u *s;
6689 char_u *tofree;
6690} join_T;
6691
6692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693list_join_inner(
6694 garray_T *gap, /* to store the result in */
6695 list_T *l,
6696 char_u *sep,
6697 int echo_style,
6698 int copyID,
6699 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700{
6701 int i;
6702 join_T *p;
6703 int len;
6704 int sumlen = 0;
6705 int first = TRUE;
6706 char_u *tofree;
6707 char_u numbuf[NUMBUFLEN];
6708 listitem_T *item;
6709 char_u *s;
6710
6711 /* Stringify each item in the list. */
6712 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6713 {
6714 if (echo_style)
6715 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6716 else
6717 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6718 if (s == NULL)
6719 return FAIL;
6720
6721 len = (int)STRLEN(s);
6722 sumlen += len;
6723
Bram Moolenaarcde88542015-08-11 19:14:00 +02006724 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006725 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6726 if (tofree != NULL || s != numbuf)
6727 {
6728 p->s = s;
6729 p->tofree = tofree;
6730 }
6731 else
6732 {
6733 p->s = vim_strnsave(s, len);
6734 p->tofree = p->s;
6735 }
6736
6737 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006738 if (did_echo_string_emsg) /* recursion error, bail out */
6739 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006740 }
6741
6742 /* Allocate result buffer with its total size, avoid re-allocation and
6743 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6744 if (join_gap->ga_len >= 2)
6745 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6746 if (ga_grow(gap, sumlen + 2) == FAIL)
6747 return FAIL;
6748
6749 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6750 {
6751 if (first)
6752 first = FALSE;
6753 else
6754 ga_concat(gap, sep);
6755 p = ((join_T *)join_gap->ga_data) + i;
6756
6757 if (p->s != NULL)
6758 ga_concat(gap, p->s);
6759 line_breakcheck();
6760 }
6761
6762 return OK;
6763}
6764
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006765/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006767 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006768 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771list_join(
6772 garray_T *gap,
6773 list_T *l,
6774 char_u *sep,
6775 int echo_style,
6776 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006778 garray_T join_ga;
6779 int retval;
6780 join_T *p;
6781 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782
Bram Moolenaard39a7512015-04-16 22:51:22 +02006783 if (l->lv_len < 1)
6784 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6786 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6787
6788 /* Dispose each item in join_ga. */
6789 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791 p = (join_T *)join_ga.ga_data;
6792 for (i = 0; i < join_ga.ga_len; ++i)
6793 {
6794 vim_free(p->tofree);
6795 ++p;
6796 }
6797 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006799
6800 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801}
6802
6803/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006804 * Return the next (unique) copy ID.
6805 * Used for serializing nested structures.
6806 */
6807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006808get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006809{
6810 current_copyID += COPYID_INC;
6811 return current_copyID;
6812}
6813
6814/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815 * Garbage collection for lists and dictionaries.
6816 *
6817 * We use reference counts to be able to free most items right away when they
6818 * are no longer used. But for composite items it's possible that it becomes
6819 * unused while the reference count is > 0: When there is a recursive
6820 * reference. Example:
6821 * :let l = [1, 2, 3]
6822 * :let d = {9: l}
6823 * :let l[1] = d
6824 *
6825 * Since this is quite unusual we handle this with garbage collection: every
6826 * once in a while find out which lists and dicts are not referenced from any
6827 * variable.
6828 *
6829 * Here is a good reference text about garbage collection (refers to Python
6830 * but it applies to all reference-counting mechanisms):
6831 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833
6834/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 * Do garbage collection for lists and dicts.
6836 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006837 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006839garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006840{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006841 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006842 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843 buf_T *buf;
6844 win_T *wp;
6845 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006846 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006847 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006849#ifdef FEAT_WINDOWS
6850 tabpage_T *tp;
6851#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006853 /* Only do this once. */
6854 want_garbage_collect = FALSE;
6855 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006856 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006857
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 /* We advance by two because we add one for items referenced through
6859 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006860 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 /*
6863 * 1. Go through all accessible variables and mark all lists and dicts
6864 * with copyID.
6865 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866
6867 /* Don't free variables in the previous_funccal list unless they are only
6868 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006869 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6871 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6873 NULL);
6874 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6875 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006876 }
6877
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878 /* script-local variables */
6879 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006880 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881
6882 /* buffer-local variables */
6883 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006884 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6885 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
6887 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006888 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006891#ifdef FEAT_AUTOCMD
6892 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6894 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006895#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006897#ifdef FEAT_WINDOWS
6898 /* tabpage-local variables */
6899 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6901 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006902#endif
6903
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906
6907 /* function-local variables */
6908 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6909 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6911 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006912 }
6913
Bram Moolenaard812df62008-11-09 12:46:09 +00006914 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006916
Bram Moolenaar1dced572012-04-05 16:54:08 +02006917#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006919#endif
6920
Bram Moolenaardb913952012-06-29 12:54:53 +02006921#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006923#endif
6924
6925#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006927#endif
6928
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006929#ifdef FEAT_CHANNEL
6930 abort = abort || set_ref_in_channel(copyID);
6931#endif
6932
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 /*
6936 * 2. Free lists and dictionaries that are not referenced.
6937 */
6938 did_free = free_unref_items(copyID);
6939
6940 /*
6941 * 3. Check if any funccal can be freed now.
6942 */
6943 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006944 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 if (can_free_funccal(*pfc, copyID))
6946 {
6947 fc = *pfc;
6948 *pfc = fc->caller;
6949 free_funccal(fc, TRUE);
6950 did_free = TRUE;
6951 did_free_funccal = TRUE;
6952 }
6953 else
6954 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 if (did_free_funccal)
6957 /* When a funccal was freed some more items might be garbage
6958 * collected, so run again. */
6959 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006960 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 else if (p_verbose > 0)
6962 {
6963 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6964 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965
6966 return did_free;
6967}
6968
6969/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006970 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 */
6972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006973free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006975 dict_T *dd, *dd_next;
6976 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 int did_free = FALSE;
6978
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 */
6982 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006983 {
6984 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006987 /* Free the Dictionary and ordinary items it contains, but don't
6988 * recurse into Lists and Dictionaries, they will be in the list
6989 * of dicts or list of lists. */
6990 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006993 dd = dd_next;
6994 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995
6996 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 * Go through the list of lists and free items without the copyID.
6998 * But don't free a list that has a watcher (used in a for loop), these
6999 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 */
7001 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007002 {
7003 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7005 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007007 /* Free the List and ordinary items it contains, but don't recurse
7008 * into Lists and Dictionaries, they will be in the list of dicts
7009 * or list of lists. */
7010 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007013 ll = ll_next;
7014 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007015
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 return did_free;
7017}
7018
7019/*
7020 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 * "list_stack" is used to add lists to be marked. Can be NULL.
7022 *
7023 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007026set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027{
7028 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 hashtab_T *cur_ht;
7032 ht_stack_T *ht_stack = NULL;
7033 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 cur_ht = ht;
7036 for (;;)
7037 {
7038 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 /* Mark each item in the hashtab. If the item contains a hashtab
7041 * it is added to ht_stack, if it contains a list it is added to
7042 * list_stack. */
7043 todo = (int)cur_ht->ht_used;
7044 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7045 if (!HASHITEM_EMPTY(hi))
7046 {
7047 --todo;
7048 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7049 &ht_stack, list_stack);
7050 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052
7053 if (ht_stack == NULL)
7054 break;
7055
7056 /* take an item from the stack */
7057 cur_ht = ht_stack->ht;
7058 tempitem = ht_stack;
7059 ht_stack = ht_stack->prev;
7060 free(tempitem);
7061 }
7062
7063 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064}
7065
7066/*
7067 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7069 *
7070 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007073set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 listitem_T *li;
7076 int abort = FALSE;
7077 list_T *cur_l;
7078 list_stack_T *list_stack = NULL;
7079 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 cur_l = l;
7082 for (;;)
7083 {
7084 if (!abort)
7085 /* Mark each item in the list. If the item contains a hashtab
7086 * it is added to ht_stack, if it contains a list it is added to
7087 * list_stack. */
7088 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7089 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7090 ht_stack, &list_stack);
7091 if (list_stack == NULL)
7092 break;
7093
7094 /* take an item from the stack */
7095 cur_l = list_stack->list;
7096 tempitem = list_stack;
7097 list_stack = list_stack->prev;
7098 free(tempitem);
7099 }
7100
7101 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102}
7103
7104/*
7105 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007106 * "list_stack" is used to add lists to be marked. Can be NULL.
7107 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7108 *
7109 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007110 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007112set_ref_in_item(
7113 typval_T *tv,
7114 int copyID,
7115 ht_stack_T **ht_stack,
7116 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117{
7118 dict_T *dd;
7119 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007120 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007121
Bram Moolenaara03f2332016-02-06 18:09:59 +01007122 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007123 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007124 dd = tv->vval.v_dict;
7125 if (dd != NULL && dd->dv_copyID != copyID)
7126 {
7127 /* Didn't see this dict yet. */
7128 dd->dv_copyID = copyID;
7129 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007130 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007131 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7132 }
7133 else
7134 {
7135 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7136 if (newitem == NULL)
7137 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007138 else
7139 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007140 newitem->ht = &dd->dv_hashtab;
7141 newitem->prev = *ht_stack;
7142 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007143 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007144 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007145 }
7146 }
7147 else if (tv->v_type == VAR_LIST)
7148 {
7149 ll = tv->vval.v_list;
7150 if (ll != NULL && ll->lv_copyID != copyID)
7151 {
7152 /* Didn't see this list yet. */
7153 ll->lv_copyID = copyID;
7154 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007155 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007156 abort = set_ref_in_list(ll, copyID, ht_stack);
7157 }
7158 else
7159 {
7160 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007162 if (newitem == NULL)
7163 abort = TRUE;
7164 else
7165 {
7166 newitem->list = ll;
7167 newitem->prev = *list_stack;
7168 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007169 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007170 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007171 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174}
7175
7176/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177 * Allocate an empty header for a dictionary.
7178 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007179 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007180dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181{
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007185 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007186 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007187 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188 if (first_dict != NULL)
7189 first_dict->dv_used_prev = d;
7190 d->dv_used_next = first_dict;
7191 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007192 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007195 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007196 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007197 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007199 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201}
7202
7203/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007204 * Allocate an empty dict for a return value.
7205 * Returns OK or FAIL.
7206 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007207 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007208rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007209{
7210 dict_T *d = dict_alloc();
7211
7212 if (d == NULL)
7213 return FAIL;
7214
7215 rettv->vval.v_dict = d;
7216 rettv->v_type = VAR_DICT;
7217 ++d->dv_refcount;
7218 return OK;
7219}
7220
7221
7222/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223 * Unreference a Dictionary: decrement the reference count and free it when it
7224 * becomes zero.
7225 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007227dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007229 if (d != NULL && --d->dv_refcount <= 0)
7230 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231}
7232
7233/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007234 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235 * Ignores the reference count.
7236 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007238dict_free(
7239 dict_T *d,
7240 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007242 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007244 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007246 /* Remove the dict from the list of dicts for garbage collection. */
7247 if (d->dv_used_prev == NULL)
7248 first_dict = d->dv_used_next;
7249 else
7250 d->dv_used_prev->dv_used_next = d->dv_used_next;
7251 if (d->dv_used_next != NULL)
7252 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7253
7254 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007255 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007256 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 if (!HASHITEM_EMPTY(hi))
7260 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007261 /* Remove the item before deleting it, just in case there is
7262 * something recursive causing trouble. */
7263 di = HI2DI(hi);
7264 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007265 if (recurse || (di->di_tv.v_type != VAR_LIST
7266 && di->di_tv.v_type != VAR_DICT))
7267 clear_tv(&di->di_tv);
7268 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 --todo;
7270 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 vim_free(d);
7274}
7275
7276/*
7277 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278 * The "key" is copied to the new item.
7279 * Note that the value of the item "di_tv" still needs to be initialized!
7280 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007282 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007283dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007284{
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007287 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007291 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007292 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294}
7295
7296/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297 * Make a copy of a Dictionary item.
7298 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301{
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007304 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7305 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 if (di != NULL)
7307 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007309 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007310 copy_tv(&org->di_tv, &di->di_tv);
7311 }
7312 return di;
7313}
7314
7315/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 * Remove item "item" from Dictionary "dict" and free it.
7317 */
7318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007319dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320{
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 if (HASHITEM_EMPTY(hi))
7325 EMSG2(_(e_intern2), "dictitem_remove()");
7326 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 dictitem_free(item);
7329}
7330
7331/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332 * Free a dict item. Also clears the value.
7333 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007335dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007338 if (item->di_flags & DI_FLAGS_ALLOC)
7339 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340}
7341
7342/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7344 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007345 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346 * Returns NULL when out of memory.
7347 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007349dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350{
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 dict_T *copy;
7352 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355
7356 if (orig == NULL)
7357 return NULL;
7358
7359 copy = dict_alloc();
7360 if (copy != NULL)
7361 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007362 if (copyID != 0)
7363 {
7364 orig->dv_copyID = copyID;
7365 orig->dv_copydict = copy;
7366 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007367 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007368 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372 --todo;
7373
7374 di = dictitem_alloc(hi->hi_key);
7375 if (di == NULL)
7376 break;
7377 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378 {
7379 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7380 copyID) == FAIL)
7381 {
7382 vim_free(di);
7383 break;
7384 }
7385 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007386 else
7387 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7388 if (dict_add(copy, di) == FAIL)
7389 {
7390 dictitem_free(di);
7391 break;
7392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007397 if (todo > 0)
7398 {
7399 dict_unref(copy);
7400 copy = NULL;
7401 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007402 }
7403
7404 return copy;
7405}
7406
7407/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007409 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007412dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413{
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415}
7416
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007418 * Add a number or string entry to dictionary "d".
7419 * When "str" is NULL use number "nr", otherwise use "str".
7420 * Returns FAIL when out of memory and when key already exists.
7421 */
7422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007423dict_add_nr_str(
7424 dict_T *d,
7425 char *key,
7426 long nr,
7427 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007428{
7429 dictitem_T *item;
7430
7431 item = dictitem_alloc((char_u *)key);
7432 if (item == NULL)
7433 return FAIL;
7434 item->di_tv.v_lock = 0;
7435 if (str == NULL)
7436 {
7437 item->di_tv.v_type = VAR_NUMBER;
7438 item->di_tv.vval.v_number = nr;
7439 }
7440 else
7441 {
7442 item->di_tv.v_type = VAR_STRING;
7443 item->di_tv.vval.v_string = vim_strsave(str);
7444 }
7445 if (dict_add(d, item) == FAIL)
7446 {
7447 dictitem_free(item);
7448 return FAIL;
7449 }
7450 return OK;
7451}
7452
7453/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007454 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007455 * Returns FAIL when out of memory and when key already exists.
7456 */
7457 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007458dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007459{
7460 dictitem_T *item;
7461
7462 item = dictitem_alloc((char_u *)key);
7463 if (item == NULL)
7464 return FAIL;
7465 item->di_tv.v_lock = 0;
7466 item->di_tv.v_type = VAR_LIST;
7467 item->di_tv.vval.v_list = list;
7468 if (dict_add(d, item) == FAIL)
7469 {
7470 dictitem_free(item);
7471 return FAIL;
7472 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007473 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007474 return OK;
7475}
7476
7477/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478 * Get the number of items in a Dictionary.
7479 */
7480 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007483 if (d == NULL)
7484 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007485 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007486}
7487
7488/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 * Find item "key[len]" in Dictionary "d".
7490 * If "len" is negative use strlen(key).
7491 * Returns NULL when not found.
7492 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007493 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007494dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496#define AKEYLEN 200
7497 char_u buf[AKEYLEN];
7498 char_u *akey;
7499 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 if (len < 0)
7503 akey = key;
7504 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007505 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 tofree = akey = vim_strnsave(key, len);
7507 if (akey == NULL)
7508 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007509 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 else
7511 {
7512 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007513 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 akey = buf;
7515 }
7516
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 vim_free(tofree);
7519 if (HASHITEM_EMPTY(hi))
7520 return NULL;
7521 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522}
7523
7524/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007525 * Get a string item from a dictionary.
7526 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007527 * Returns NULL if the entry doesn't exist or out of memory.
7528 */
7529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007530get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007531{
7532 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007533 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007534
7535 di = dict_find(d, key, -1);
7536 if (di == NULL)
7537 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007538 s = get_tv_string(&di->di_tv);
7539 if (save && s != NULL)
7540 s = vim_strsave(s);
7541 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007542}
7543
7544/*
7545 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007546 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007547 */
7548 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007550{
7551 dictitem_T *di;
7552
7553 di = dict_find(d, key, -1);
7554 if (di == NULL)
7555 return 0;
7556 return get_tv_number(&di->di_tv);
7557}
7558
7559/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 * Return an allocated string with the string representation of a Dictionary.
7561 * May return NULL.
7562 */
7563 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565{
7566 garray_T ga;
7567 int first = TRUE;
7568 char_u *tofree;
7569 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007573 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 return NULL;
7577 ga_init2(&ga, (int)sizeof(char), 80);
7578 ga_append(&ga, '{');
7579
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007580 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007581 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007583 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007585 --todo;
7586
7587 if (first)
7588 first = FALSE;
7589 else
7590 ga_concat(&ga, (char_u *)", ");
7591
7592 tofree = string_quote(hi->hi_key, FALSE);
7593 if (tofree != NULL)
7594 {
7595 ga_concat(&ga, tofree);
7596 vim_free(tofree);
7597 }
7598 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007600 if (s != NULL)
7601 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007603 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007604 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007605 line_breakcheck();
7606
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 if (todo > 0)
7610 {
7611 vim_free(ga.ga_data);
7612 return NULL;
7613 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614
7615 ga_append(&ga, '}');
7616 ga_append(&ga, NUL);
7617 return (char_u *)ga.ga_data;
7618}
7619
7620/*
7621 * Allocate a variable for a Dictionary and fill it from "*arg".
7622 * Return OK or FAIL. Returns NOTDONE for {expr}.
7623 */
7624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007625get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626{
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dict_T *d = NULL;
7628 typval_T tvkey;
7629 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007630 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634
7635 /*
7636 * First check if it's not a curly-braces thing: {expr}.
7637 * Must do this without evaluating, otherwise a function may be called
7638 * twice. Unfortunately this means we need to call eval1() twice for the
7639 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642 if (*start != '}')
7643 {
7644 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7645 return FAIL;
7646 if (*start == '}')
7647 return NOTDONE;
7648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649
7650 if (evaluate)
7651 {
7652 d = dict_alloc();
7653 if (d == NULL)
7654 return FAIL;
7655 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656 tvkey.v_type = VAR_UNKNOWN;
7657 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658
7659 *arg = skipwhite(*arg + 1);
7660 while (**arg != '}' && **arg != NUL)
7661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007662 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 goto failret;
7664 if (**arg != ':')
7665 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007666 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 goto failret;
7669 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007670 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007672 key = get_tv_string_buf_chk(&tvkey, buf);
7673 if (key == NULL || *key == NUL)
7674 {
7675 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7676 if (key != NULL)
7677 EMSG(_(e_emptykey));
7678 clear_tv(&tvkey);
7679 goto failret;
7680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682
7683 *arg = skipwhite(*arg + 1);
7684 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7685 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007686 if (evaluate)
7687 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 goto failret;
7689 }
7690 if (evaluate)
7691 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 if (item != NULL)
7694 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007695 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 clear_tv(&tv);
7698 goto failret;
7699 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007700 item = dictitem_alloc(key);
7701 clear_tv(&tvkey);
7702 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007705 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007706 if (dict_add(d, item) == FAIL)
7707 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 }
7709 }
7710
7711 if (**arg == '}')
7712 break;
7713 if (**arg != ',')
7714 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007715 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 goto failret;
7717 }
7718 *arg = skipwhite(*arg + 1);
7719 }
7720
7721 if (**arg != '}')
7722 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007723 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724failret:
7725 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007726 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 return FAIL;
7728 }
7729
7730 *arg = skipwhite(*arg + 1);
7731 if (evaluate)
7732 {
7733 rettv->v_type = VAR_DICT;
7734 rettv->vval.v_dict = d;
7735 ++d->dv_refcount;
7736 }
7737
7738 return OK;
7739}
7740
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007741#if defined(FEAT_CHANNEL) || defined(PROTO)
7742/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007743 * Decrement the reference count on "channel" and maybe free it when it goes
7744 * down to zero. Don't free it if there is a pending action.
Bram Moolenaard6051b52016-02-28 15:49:03 +01007745 * Returns TRUE when the channel is no longer referenced.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007746 */
7747 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007748channel_unref(channel_T *channel)
7749{
7750 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar70765942016-02-28 19:28:59 +01007751 return channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007752 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007753}
7754#endif
7755
Bram Moolenaar65edff82016-02-21 16:40:11 +01007756#if defined(FEAT_JOB) || defined(PROTO)
7757static job_T *first_job = NULL;
7758
Bram Moolenaar835dc632016-02-07 14:27:38 +01007759 static void
7760job_free(job_T *job)
7761{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007762# ifdef FEAT_CHANNEL
Bram Moolenaard6051b52016-02-28 15:49:03 +01007763 ch_log(job->jv_channel, "Freeing job");
Bram Moolenaar77073442016-02-13 23:23:53 +01007764 if (job->jv_channel != NULL)
7765 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007766 /* The link from the channel to the job doesn't count as a reference,
7767 * thus don't decrement the refcount of the job. The reference from
7768 * the job to the channel does count the refrence, decrement it and
7769 * NULL the reference. We don't set ch_job_killed, unreferencing the
7770 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007771 job->jv_channel->ch_job = NULL;
7772 channel_unref(job->jv_channel);
7773 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007774# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007775 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007776
7777 if (job->jv_next != NULL)
7778 job->jv_next->jv_prev = job->jv_prev;
7779 if (job->jv_prev == NULL)
7780 first_job = job->jv_next;
7781 else
7782 job->jv_prev->jv_next = job->jv_next;
7783
7784 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007785 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007786 vim_free(job);
7787}
7788
7789 static void
7790job_unref(job_T *job)
7791{
7792 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007793 {
7794 /* Do not free the job when it has not ended yet and there is a
7795 * "stoponexit" flag or an exit callback. */
7796 if (job->jv_status != JOB_STARTED
7797 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
Bram Moolenaard6051b52016-02-28 15:49:03 +01007798 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007799 job_free(job);
Bram Moolenaard6051b52016-02-28 15:49:03 +01007800 }
Bram Moolenaar8cc69772016-02-28 16:42:03 +01007801# ifdef FEAT_CHANNEL
Bram Moolenaard6051b52016-02-28 15:49:03 +01007802 else if (job->jv_channel != NULL)
7803 {
7804 /* Do remove the link to the channel, otherwise it hangs
7805 * around until Vim exits. See job_free() for refcount. */
7806 job->jv_channel->ch_job = NULL;
7807 channel_unref(job->jv_channel);
7808 job->jv_channel = NULL;
7809 }
Bram Moolenaar8cc69772016-02-28 16:42:03 +01007810# endif
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007811 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007812}
7813
7814/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007815 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007816 */
7817 static job_T *
7818job_alloc(void)
7819{
7820 job_T *job;
7821
7822 job = (job_T *)alloc_clear(sizeof(job_T));
7823 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007824 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007825 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007826 job->jv_stoponexit = vim_strsave((char_u *)"term");
7827
7828 if (first_job != NULL)
7829 {
7830 first_job->jv_prev = job;
7831 job->jv_next = first_job;
7832 }
7833 first_job = job;
7834 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007835 return job;
7836}
7837
Bram Moolenaar65edff82016-02-21 16:40:11 +01007838 static void
7839job_set_options(job_T *job, jobopt_T *opt)
7840{
7841 if (opt->jo_set & JO_STOPONEXIT)
7842 {
7843 vim_free(job->jv_stoponexit);
7844 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7845 job->jv_stoponexit = NULL;
7846 else
7847 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7848 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007849 if (opt->jo_set & JO_EXIT_CB)
7850 {
7851 vim_free(job->jv_exit_cb);
7852 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7853 job->jv_exit_cb = NULL;
7854 else
7855 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7856 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007857}
7858
7859/*
7860 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7861 */
7862 void
7863job_stop_on_exit()
7864{
7865 job_T *job;
7866
7867 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007868 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007869 mch_stop_job(job, job->jv_stoponexit);
7870}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007871#endif
7872
Bram Moolenaar17a13432016-01-24 14:22:10 +01007873 static char *
7874get_var_special_name(int nr)
7875{
7876 switch (nr)
7877 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007878 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007879 case VVAL_TRUE: return "v:true";
7880 case VVAL_NONE: return "v:none";
7881 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007882 }
7883 EMSG2(_(e_intern2), "get_var_special_name()");
7884 return "42";
7885}
7886
Bram Moolenaar8c711452005-01-14 21:53:12 +00007887/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888 * Return a string with the string representation of a variable.
7889 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007890 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007892 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007893 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007894 */
7895 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007896echo_string(
7897 typval_T *tv,
7898 char_u **tofree,
7899 char_u *numbuf,
7900 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007902 static int recurse = 0;
7903 char_u *r = NULL;
7904
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007907 if (!did_echo_string_emsg)
7908 {
7909 /* Only give this message once for a recursive call to avoid
7910 * flooding the user with errors. And stop iterating over lists
7911 * and dicts. */
7912 did_echo_string_emsg = TRUE;
7913 EMSG(_("E724: variable nested too deep for displaying"));
7914 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007915 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007916 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 }
7918 ++recurse;
7919
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007920 switch (tv->v_type)
7921 {
7922 case VAR_FUNC:
7923 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007924 r = tv->vval.v_string;
7925 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007926
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007927 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007928 if (tv->vval.v_list == NULL)
7929 {
7930 *tofree = NULL;
7931 r = NULL;
7932 }
7933 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7934 {
7935 *tofree = NULL;
7936 r = (char_u *)"[...]";
7937 }
7938 else
7939 {
7940 tv->vval.v_list->lv_copyID = copyID;
7941 *tofree = list2string(tv, copyID);
7942 r = *tofree;
7943 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007944 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007945
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007947 if (tv->vval.v_dict == NULL)
7948 {
7949 *tofree = NULL;
7950 r = NULL;
7951 }
7952 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7953 {
7954 *tofree = NULL;
7955 r = (char_u *)"{...}";
7956 }
7957 else
7958 {
7959 tv->vval.v_dict->dv_copyID = copyID;
7960 *tofree = dict2string(tv, copyID);
7961 r = *tofree;
7962 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007963 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007964
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 case VAR_STRING:
7966 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007967 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007968 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007969 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007970 *tofree = NULL;
7971 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007974 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007975#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007976 *tofree = NULL;
7977 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7978 r = numbuf;
7979 break;
7980#endif
7981
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007982 case VAR_SPECIAL:
7983 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007984 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007985 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987
Bram Moolenaar8502c702014-06-17 12:51:16 +02007988 if (--recurse == 0)
7989 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007991}
7992
7993/*
7994 * Return a string with the string representation of a variable.
7995 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7996 * "numbuf" is used for a number.
7997 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007998 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999 */
8000 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008001tv2string(
8002 typval_T *tv,
8003 char_u **tofree,
8004 char_u *numbuf,
8005 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006{
8007 switch (tv->v_type)
8008 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009 case VAR_FUNC:
8010 *tofree = string_quote(tv->vval.v_string, TRUE);
8011 return *tofree;
8012 case VAR_STRING:
8013 *tofree = string_quote(tv->vval.v_string, FALSE);
8014 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008015 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008016#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008017 *tofree = NULL;
8018 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8019 return numbuf;
8020#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008021 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008022 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008023 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008024 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008025 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008026 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008027 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008028 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008029 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008030 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008031}
8032
8033/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 * Return string "str" in ' quotes, doubling ' characters.
8035 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008036 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008037 */
8038 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008039string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008040{
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008042 char_u *p, *r, *s;
8043
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 len = (function ? 13 : 3);
8045 if (str != NULL)
8046 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008047 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 for (p = str; *p != NUL; mb_ptr_adv(p))
8049 if (*p == '\'')
8050 ++len;
8051 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008052 s = r = alloc(len);
8053 if (r != NULL)
8054 {
8055 if (function)
8056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008057 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008058 r += 10;
8059 }
8060 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 if (str != NULL)
8063 for (p = str; *p != NUL; )
8064 {
8065 if (*p == '\'')
8066 *r++ = '\'';
8067 MB_COPY_CHAR(p, r);
8068 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008070 if (function)
8071 *r++ = ')';
8072 *r++ = NUL;
8073 }
8074 return s;
8075}
8076
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008077#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078/*
8079 * Convert the string "text" to a floating point number.
8080 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8081 * this always uses a decimal point.
8082 * Returns the length of the text that was consumed.
8083 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008085string2float(
8086 char_u *text,
8087 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088{
8089 char *s = (char *)text;
8090 float_T f;
8091
8092 f = strtod(s, &s);
8093 *value = f;
8094 return (int)((char_u *)s - text);
8095}
8096#endif
8097
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 * Get the value of an environment variable.
8100 * "arg" is pointing to the '$'. It is advanced to after the name.
8101 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008102 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 */
8104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008105get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
8107 char_u *string = NULL;
8108 int len;
8109 int cc;
8110 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008111 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112
8113 ++*arg;
8114 name = *arg;
8115 len = get_env_len(arg);
8116 if (evaluate)
8117 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008118 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008119 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008120
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008121 cc = name[len];
8122 name[len] = NUL;
8123 /* first try vim_getenv(), fast for normal environment vars */
8124 string = vim_getenv(name, &mustfree);
8125 if (string != NULL && *string != NUL)
8126 {
8127 if (!mustfree)
8128 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008130 else
8131 {
8132 if (mustfree)
8133 vim_free(string);
8134
8135 /* next try expanding things like $VIM and ${HOME} */
8136 string = expand_env_save(name - 1);
8137 if (string != NULL && *string == '$')
8138 {
8139 vim_free(string);
8140 string = NULL;
8141 }
8142 }
8143 name[len] = cc;
8144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 rettv->v_type = VAR_STRING;
8146 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148
8149 return OK;
8150}
8151
8152/*
8153 * Array with names and number of arguments of all internal functions
8154 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8155 */
8156static struct fst
8157{
8158 char *f_name; /* function name */
8159 char f_min_argc; /* minimal number of arguments */
8160 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008161 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008162 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163} functions[] =
8164{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#ifdef FEAT_FLOAT
8166 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008167 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008169 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008170 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008171 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"append", 2, 2, f_append},
8173 {"argc", 0, 0, f_argc},
8174 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008175 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008176 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008177#ifdef FEAT_FLOAT
8178 {"asin", 1, 1, f_asin}, /* WJMc */
8179#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008180 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008181 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008182 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008183 {"assert_false", 1, 2, f_assert_false},
8184 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185#ifdef FEAT_FLOAT
8186 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008187 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008190 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"bufexists", 1, 1, f_bufexists},
8192 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8193 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8194 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8195 {"buflisted", 1, 1, f_buflisted},
8196 {"bufloaded", 1, 1, f_bufloaded},
8197 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008198 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"bufwinnr", 1, 1, f_bufwinnr},
8200 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008201 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008202 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008204#ifdef FEAT_FLOAT
8205 {"ceil", 1, 1, f_ceil},
8206#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008207#ifdef FEAT_CHANNEL
8208 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008209 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8210 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008211 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008212# ifdef FEAT_JOB
8213 {"ch_getjob", 1, 1, f_ch_getjob},
8214# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008215 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008216 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008217 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008218 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008219 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008220 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8221 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008222 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008223 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008224#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008225 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008226 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008228 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008230#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008231 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008232 {"complete_add", 1, 1, f_complete_add},
8233 {"complete_check", 0, 0, f_complete_check},
8234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008236 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237#ifdef FEAT_FLOAT
8238 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008239 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008243 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008244 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008245 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008247 {"diff_filler", 1, 1, f_diff_filler},
8248 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008249 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008250 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"eventhandler", 0, 0, f_eventhandler},
8254 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008255 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008257#ifdef FEAT_FLOAT
8258 {"exp", 1, 1, f_exp},
8259#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008260 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008261 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008262 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8264 {"filereadable", 1, 1, f_filereadable},
8265 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008266 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008267 {"finddir", 1, 3, f_finddir},
8268 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269#ifdef FEAT_FLOAT
8270 {"float2nr", 1, 1, f_float2nr},
8271 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008272 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008274 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"fnamemodify", 2, 2, f_fnamemodify},
8276 {"foldclosed", 1, 1, f_foldclosed},
8277 {"foldclosedend", 1, 1, f_foldclosedend},
8278 {"foldlevel", 1, 1, f_foldlevel},
8279 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008280 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008283 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008284 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008285 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008286 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"getchar", 0, 1, f_getchar},
8288 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008289 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"getcmdline", 0, 0, f_getcmdline},
8291 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008292 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008293 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008294 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008295 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008296 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008297 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"getfsize", 1, 1, f_getfsize},
8299 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008300 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008301 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008302 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008303 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008304 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008305 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008306 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008307 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008309 {"gettabvar", 2, 3, f_gettabvar},
8310 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"getwinposx", 0, 0, f_getwinposx},
8312 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008313 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008314 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008315 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008316 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008318 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008319 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008320 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8322 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8323 {"histadd", 2, 2, f_histadd},
8324 {"histdel", 1, 2, f_histdel},
8325 {"histget", 1, 2, f_histget},
8326 {"histnr", 1, 1, f_histnr},
8327 {"hlID", 1, 1, f_hlID},
8328 {"hlexists", 1, 1, f_hlexists},
8329 {"hostname", 0, 0, f_hostname},
8330 {"iconv", 3, 3, f_iconv},
8331 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008332 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008333 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008335 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"inputrestore", 0, 0, f_inputrestore},
8337 {"inputsave", 0, 0, f_inputsave},
8338 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008339 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008340 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008342 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008343#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8344 {"isnan", 1, 1, f_isnan},
8345#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008347#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008348# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008349 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008350# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008351 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008352 {"job_start", 1, 2, f_job_start},
8353 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008354 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008355#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008356 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008357 {"js_decode", 1, 1, f_js_decode},
8358 {"js_encode", 1, 1, f_js_encode},
8359 {"json_decode", 1, 1, f_json_decode},
8360 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008361 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008363 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"libcall", 3, 3, f_libcall},
8365 {"libcallnr", 3, 3, f_libcallnr},
8366 {"line", 1, 1, f_line},
8367 {"line2byte", 1, 1, f_line2byte},
8368 {"lispindent", 1, 1, f_lispindent},
8369 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008371 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008372 {"log10", 1, 1, f_log10},
8373#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008374#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008375 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008376#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008377 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008378 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008379 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008380 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008381 {"matchadd", 2, 5, f_matchadd},
8382 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008383 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008384 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008385 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008386 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008387 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008388 {"max", 1, 1, f_max},
8389 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008390#ifdef vim_mkdir
8391 {"mkdir", 1, 3, f_mkdir},
8392#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008394#ifdef FEAT_MZSCHEME
8395 {"mzeval", 1, 1, f_mzeval},
8396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008398 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008399 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008400 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008401#ifdef FEAT_PERL
8402 {"perleval", 1, 1, f_perleval},
8403#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008404#ifdef FEAT_FLOAT
8405 {"pow", 2, 2, f_pow},
8406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008408 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008409 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008410#ifdef FEAT_PYTHON3
8411 {"py3eval", 1, 1, f_py3eval},
8412#endif
8413#ifdef FEAT_PYTHON
8414 {"pyeval", 1, 1, f_pyeval},
8415#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008416 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008417 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008418 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008419#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008420 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008421#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008422 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"remote_expr", 2, 3, f_remote_expr},
8424 {"remote_foreground", 1, 1, f_remote_foreground},
8425 {"remote_peek", 1, 2, f_remote_peek},
8426 {"remote_read", 1, 1, f_remote_read},
8427 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008428 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008430 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008432 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008433#ifdef FEAT_FLOAT
8434 {"round", 1, 1, f_round},
8435#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008436 {"screenattr", 2, 2, f_screenattr},
8437 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008438 {"screencol", 0, 0, f_screencol},
8439 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008440 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008441 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008442 {"searchpair", 3, 7, f_searchpair},
8443 {"searchpairpos", 3, 7, f_searchpairpos},
8444 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"server2client", 2, 2, f_server2client},
8446 {"serverlist", 0, 0, f_serverlist},
8447 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008448 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008450 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008452 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008453 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008454 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008455 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008457 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008458 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008460#ifdef FEAT_CRYPT
8461 {"sha256", 1, 1, f_sha256},
8462#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008463 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008464 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008466#ifdef FEAT_FLOAT
8467 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008468 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008469#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008470 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008471 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008472 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008473 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008474 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008475#ifdef FEAT_FLOAT
8476 {"sqrt", 1, 1, f_sqrt},
8477 {"str2float", 1, 1, f_str2float},
8478#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008479 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008480 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008481 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482#ifdef HAVE_STRFTIME
8483 {"strftime", 1, 2, f_strftime},
8484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008485 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {"strlen", 1, 1, f_strlen},
8488 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008489 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008491 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008492 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {"substitute", 4, 4, f_substitute},
8494 {"synID", 3, 3, f_synID},
8495 {"synIDattr", 2, 3, f_synIDattr},
8496 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008497 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008498 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008499 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008500 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008501 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008502 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008503 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008504 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008505 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008506#ifdef FEAT_FLOAT
8507 {"tan", 1, 1, f_tan},
8508 {"tanh", 1, 1, f_tanh},
8509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008511 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {"tolower", 1, 1, f_tolower},
8513 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008514 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008515#ifdef FEAT_FLOAT
8516 {"trunc", 1, 1, f_trunc},
8517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008519 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008520 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008521 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008522 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {"virtcol", 1, 1, f_virtcol},
8524 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008525 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 {"winbufnr", 1, 1, f_winbufnr},
8527 {"wincol", 0, 0, f_wincol},
8528 {"winheight", 1, 1, f_winheight},
8529 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008530 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008532 {"winrestview", 1, 1, f_winrestview},
8533 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008535 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008536 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008537 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538};
8539
8540#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8541
8542/*
8543 * Function given to ExpandGeneric() to obtain the list of internal
8544 * or user defined function names.
8545 */
8546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008547get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 static int intidx = -1;
8550 char_u *name;
8551
8552 if (idx == 0)
8553 intidx = -1;
8554 if (intidx < 0)
8555 {
8556 name = get_user_func_name(xp, idx);
8557 if (name != NULL)
8558 return name;
8559 }
8560 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8561 {
8562 STRCPY(IObuff, functions[intidx].f_name);
8563 STRCAT(IObuff, "(");
8564 if (functions[intidx].f_max_argc == 0)
8565 STRCAT(IObuff, ")");
8566 return IObuff;
8567 }
8568
8569 return NULL;
8570}
8571
8572/*
8573 * Function given to ExpandGeneric() to obtain the list of internal or
8574 * user defined variable or function names.
8575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008577get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 static int intidx = -1;
8580 char_u *name;
8581
8582 if (idx == 0)
8583 intidx = -1;
8584 if (intidx < 0)
8585 {
8586 name = get_function_name(xp, idx);
8587 if (name != NULL)
8588 return name;
8589 }
8590 return get_user_var_name(xp, ++intidx);
8591}
8592
8593#endif /* FEAT_CMDL_COMPL */
8594
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008595#if defined(EBCDIC) || defined(PROTO)
8596/*
8597 * Compare struct fst by function name.
8598 */
8599 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008600compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008601{
8602 struct fst *p1 = (struct fst *)s1;
8603 struct fst *p2 = (struct fst *)s2;
8604
8605 return STRCMP(p1->f_name, p2->f_name);
8606}
8607
8608/*
8609 * Sort the function table by function name.
8610 * The sorting of the table above is ASCII dependant.
8611 * On machines using EBCDIC we have to sort it.
8612 */
8613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008614sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008615{
8616 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8617
8618 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8619}
8620#endif
8621
8622
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623/*
8624 * Find internal function in table above.
8625 * Return index, or -1 if not found
8626 */
8627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008628find_internal_func(
8629 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631 int first = 0;
8632 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8633 int cmp;
8634 int x;
8635
8636 /*
8637 * Find the function name in the table. Binary search.
8638 */
8639 while (first <= last)
8640 {
8641 x = first + ((unsigned)(last - first) >> 1);
8642 cmp = STRCMP(name, functions[x].f_name);
8643 if (cmp < 0)
8644 last = x - 1;
8645 else if (cmp > 0)
8646 first = x + 1;
8647 else
8648 return x;
8649 }
8650 return -1;
8651}
8652
8653/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8655 * name it contains, otherwise return "name".
8656 */
8657 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008658deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659{
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 int cc;
8662
8663 cc = name[*lenp];
8664 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008665 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008666 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008667 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008668 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008669 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008670 {
8671 *lenp = 0;
8672 return (char_u *)""; /* just in case */
8673 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008674 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008675 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008676 }
8677
8678 return name;
8679}
8680
8681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 * Allocate a variable for the result of a function.
8683 * Return OK or FAIL.
8684 */
8685 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008686get_func_tv(
8687 char_u *name, /* name of the function */
8688 int len, /* length of "name" */
8689 typval_T *rettv,
8690 char_u **arg, /* argument, pointing to the '(' */
8691 linenr_T firstline, /* first line of range */
8692 linenr_T lastline, /* last line of range */
8693 int *doesrange, /* return: function handled range */
8694 int evaluate,
8695 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 char_u *argp;
8698 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008699 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 int argcount = 0; /* number of arguments found */
8701
8702 /*
8703 * Get the arguments.
8704 */
8705 argp = *arg;
8706 while (argcount < MAX_FUNC_ARGS)
8707 {
8708 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8709 if (*argp == ')' || *argp == ',' || *argp == NUL)
8710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8712 {
8713 ret = FAIL;
8714 break;
8715 }
8716 ++argcount;
8717 if (*argp != ',')
8718 break;
8719 }
8720 if (*argp == ')')
8721 ++argp;
8722 else
8723 ret = FAIL;
8724
8725 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008726 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008727 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 {
8730 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008731 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008732 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008733 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
8739 *arg = skipwhite(argp);
8740 return ret;
8741}
8742
8743
8744/*
8745 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008746 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008747 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750call_func(
8751 char_u *funcname, /* name of the function */
8752 int len, /* length of "name" */
8753 typval_T *rettv, /* return value goes here */
8754 int argcount, /* number of "argvars" */
8755 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008756 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008757 linenr_T firstline, /* first line of range */
8758 linenr_T lastline, /* last line of range */
8759 int *doesrange, /* return: function handled range */
8760 int evaluate,
8761 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764#define ERROR_UNKNOWN 0
8765#define ERROR_TOOMANY 1
8766#define ERROR_TOOFEW 2
8767#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008768#define ERROR_DICT 4
8769#define ERROR_NONE 5
8770#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 int error = ERROR_NONE;
8772 int i;
8773 int llen;
8774 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775#define FLEN_FIXED 40
8776 char_u fname_buf[FLEN_FIXED + 1];
8777 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008778 char_u *name;
8779
8780 /* Make a copy of the name, if it comes from a funcref variable it could
8781 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008782 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008783 if (name == NULL)
8784 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 /*
8787 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8788 * Change <SNR>123_name() to K_SNR 123_name().
8789 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 llen = eval_fname_script(name);
8792 if (llen > 0)
8793 {
8794 fname_buf[0] = K_SPECIAL;
8795 fname_buf[1] = KS_EXTRA;
8796 fname_buf[2] = (int)KE_SNR;
8797 i = 3;
8798 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8799 {
8800 if (current_SID <= 0)
8801 error = ERROR_SCRIPT;
8802 else
8803 {
8804 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8805 i = (int)STRLEN(fname_buf);
8806 }
8807 }
8808 if (i + STRLEN(name + llen) < FLEN_FIXED)
8809 {
8810 STRCPY(fname_buf + i, name + llen);
8811 fname = fname_buf;
8812 }
8813 else
8814 {
8815 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8816 if (fname == NULL)
8817 error = ERROR_OTHER;
8818 else
8819 {
8820 mch_memmove(fname, fname_buf, (size_t)i);
8821 STRCPY(fname + i, name + llen);
8822 }
8823 }
8824 }
8825 else
8826 fname = name;
8827
8828 *doesrange = FALSE;
8829
8830
8831 /* execute the function if no errors detected and executing */
8832 if (evaluate && error == ERROR_NONE)
8833 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008834 char_u *rfname = fname;
8835
8836 /* Ignore "g:" before a function name. */
8837 if (fname[0] == 'g' && fname[1] == ':')
8838 rfname = fname + 2;
8839
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008840 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8841 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 error = ERROR_UNKNOWN;
8843
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008844 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
8846 /*
8847 * User defined function.
8848 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008849 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008852 /* Trigger FuncUndefined event, may load the function. */
8853 if (fp == NULL
8854 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008855 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008856 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008858 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008859 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 }
8861#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008862 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008863 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008864 {
8865 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008866 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008867 }
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (fp != NULL)
8870 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008871 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008873 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008875 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008877 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008878 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 else
8880 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008881 int did_save_redo = FALSE;
8882
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 /*
8884 * Call the user function.
8885 * Save and restore search patterns, script variables and
8886 * redo buffer.
8887 */
8888 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008889#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008890 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008891#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008892 {
8893 saveRedobuff();
8894 did_save_redo = TRUE;
8895 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008896 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008897 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008898 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008899 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8900 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8901 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008902 /* Function was unreferenced while being used, free it
8903 * now. */
8904 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008905 if (did_save_redo)
8906 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 restore_search_patterns();
8908 error = ERROR_NONE;
8909 }
8910 }
8911 }
8912 else
8913 {
8914 /*
8915 * Find the function name in the table, call its implementation.
8916 */
8917 i = find_internal_func(fname);
8918 if (i >= 0)
8919 {
8920 if (argcount < functions[i].f_min_argc)
8921 error = ERROR_TOOFEW;
8922 else if (argcount > functions[i].f_max_argc)
8923 error = ERROR_TOOMANY;
8924 else
8925 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008926 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 error = ERROR_NONE;
8929 }
8930 }
8931 }
8932 /*
8933 * The function call (or "FuncUndefined" autocommand sequence) might
8934 * have been aborted by an error, an interrupt, or an explicitly thrown
8935 * exception that has not been caught so far. This situation can be
8936 * tested for by calling aborting(). For an error in an internal
8937 * function or for the "E132" error in call_user_func(), however, the
8938 * throw point at which the "force_abort" flag (temporarily reset by
8939 * emsg()) is normally updated has not been reached yet. We need to
8940 * update that flag first to make aborting() reliable.
8941 */
8942 update_force_abort();
8943 }
8944 if (error == ERROR_NONE)
8945 ret = OK;
8946
8947 /*
8948 * Report an error unless the argument evaluation or function call has been
8949 * cancelled due to an aborting error, an interrupt, or an exception.
8950 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008951 if (!aborting())
8952 {
8953 switch (error)
8954 {
8955 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008956 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008957 break;
8958 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008959 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008960 break;
8961 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008962 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008963 name);
8964 break;
8965 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008966 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008967 name);
8968 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008969 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008970 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 name);
8972 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008973 }
8974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 if (fname != name && fname != fname_buf)
8977 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008978 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
8980 return ret;
8981}
8982
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008983/*
8984 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008985 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008986 */
8987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008988emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008989{
8990 char_u *p;
8991
8992 if (*name == K_SPECIAL)
8993 p = concat_str((char_u *)"<SNR>", name + 3);
8994 else
8995 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008996 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008997 if (p != name)
8998 vim_free(p);
8999}
9000
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009001/*
9002 * Return TRUE for a non-zero Number and a non-empty String.
9003 */
9004 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009005non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009006{
9007 return ((argvars[0].v_type == VAR_NUMBER
9008 && argvars[0].vval.v_number != 0)
9009 || (argvars[0].v_type == VAR_STRING
9010 && argvars[0].vval.v_string != NULL
9011 && *argvars[0].vval.v_string != NUL));
9012}
9013
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014/*********************************************
9015 * Implementation of the built-in functions
9016 */
9017
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009018#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009019static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009020
9021/*
9022 * Get the float value of "argvars[0]" into "f".
9023 * Returns FAIL when the argument is not a Number or Float.
9024 */
9025 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009026get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009027{
9028 if (argvars[0].v_type == VAR_FLOAT)
9029 {
9030 *f = argvars[0].vval.v_float;
9031 return OK;
9032 }
9033 if (argvars[0].v_type == VAR_NUMBER)
9034 {
9035 *f = (float_T)argvars[0].vval.v_number;
9036 return OK;
9037 }
9038 EMSG(_("E808: Number or Float required"));
9039 return FAIL;
9040}
9041
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009042/*
9043 * "abs(expr)" function
9044 */
9045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009046f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009047{
9048 if (argvars[0].v_type == VAR_FLOAT)
9049 {
9050 rettv->v_type = VAR_FLOAT;
9051 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9052 }
9053 else
9054 {
9055 varnumber_T n;
9056 int error = FALSE;
9057
9058 n = get_tv_number_chk(&argvars[0], &error);
9059 if (error)
9060 rettv->vval.v_number = -1;
9061 else if (n > 0)
9062 rettv->vval.v_number = n;
9063 else
9064 rettv->vval.v_number = -n;
9065 }
9066}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009067
9068/*
9069 * "acos()" function
9070 */
9071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009072f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009073{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009074 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009075
9076 rettv->v_type = VAR_FLOAT;
9077 if (get_float_arg(argvars, &f) == OK)
9078 rettv->vval.v_float = acos(f);
9079 else
9080 rettv->vval.v_float = 0.0;
9081}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009082#endif
9083
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009085 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 */
9087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009088f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089{
Bram Moolenaar33570922005-01-25 22:26:29 +00009090 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009093 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009095 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009096 && !tv_check_lock(l->lv_lock,
9097 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009098 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009100 }
9101 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009102 EMSG(_(e_listreq));
9103}
9104
9105/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009106 * "alloc_fail(id, countdown, repeat)" function
9107 */
9108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009109f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009110{
9111 if (argvars[0].v_type != VAR_NUMBER
9112 || argvars[0].vval.v_number <= 0
9113 || argvars[1].v_type != VAR_NUMBER
9114 || argvars[1].vval.v_number < 0
9115 || argvars[2].v_type != VAR_NUMBER)
9116 EMSG(_(e_invarg));
9117 else
9118 {
9119 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009120 if (alloc_fail_id >= aid_last)
9121 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009122 alloc_fail_countdown = argvars[1].vval.v_number;
9123 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009124 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009125 }
9126}
9127
9128/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009129 * "and(expr, expr)" function
9130 */
9131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009132f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009133{
9134 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9135 & get_tv_number_chk(&argvars[1], NULL);
9136}
9137
9138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009139 * "append(lnum, string/list)" function
9140 */
9141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009142f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009143{
9144 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009145 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009146 list_T *l = NULL;
9147 listitem_T *li = NULL;
9148 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009149 long added = 0;
9150
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009151 /* When coming here from Insert mode, sync undo, so that this can be
9152 * undone separately from what was previously inserted. */
9153 if (u_sync_once == 2)
9154 {
9155 u_sync_once = 1; /* notify that u_sync() was called */
9156 u_sync(TRUE);
9157 }
9158
Bram Moolenaar0d660222005-01-07 21:51:51 +00009159 lnum = get_tv_lnum(argvars);
9160 if (lnum >= 0
9161 && lnum <= curbuf->b_ml.ml_line_count
9162 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009163 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009164 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009165 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009166 l = argvars[1].vval.v_list;
9167 if (l == NULL)
9168 return;
9169 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009170 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009171 for (;;)
9172 {
9173 if (l == NULL)
9174 tv = &argvars[1]; /* append a string */
9175 else if (li == NULL)
9176 break; /* end of list */
9177 else
9178 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 line = get_tv_string_chk(tv);
9180 if (line == NULL) /* type error */
9181 {
9182 rettv->vval.v_number = 1; /* Failed */
9183 break;
9184 }
9185 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009186 ++added;
9187 if (l == NULL)
9188 break;
9189 li = li->li_next;
9190 }
9191
9192 appended_lines_mark(lnum, added);
9193 if (curwin->w_cursor.lnum > lnum)
9194 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 else
9197 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198}
9199
9200/*
9201 * "argc()" function
9202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009204f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207}
9208
9209/*
9210 * "argidx()" function
9211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009213f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216}
9217
9218/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009219 * "arglistid()" function
9220 */
9221 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009222f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009223{
9224 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009225
9226 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009227 wp = find_tabwin(&argvars[0], &argvars[1]);
9228 if (wp != NULL)
9229 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009230}
9231
9232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 * "argv(nr)" function
9234 */
9235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009236f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
9238 int idx;
9239
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009240 if (argvars[0].v_type != VAR_UNKNOWN)
9241 {
9242 idx = get_tv_number_chk(&argvars[0], NULL);
9243 if (idx >= 0 && idx < ARGCOUNT)
9244 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9245 else
9246 rettv->vval.v_string = NULL;
9247 rettv->v_type = VAR_STRING;
9248 }
9249 else if (rettv_list_alloc(rettv) == OK)
9250 for (idx = 0; idx < ARGCOUNT; ++idx)
9251 list_append_string(rettv->vval.v_list,
9252 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253}
9254
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009255static void prepare_assert_error(garray_T*gap);
9256static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9257static void assert_error(garray_T *gap);
9258static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009259
9260/*
9261 * Prepare "gap" for an assert error and add the sourcing position.
9262 */
9263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009264prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009265{
9266 char buf[NUMBUFLEN];
9267
9268 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009269 if (sourcing_name != NULL)
9270 {
9271 ga_concat(gap, sourcing_name);
9272 if (sourcing_lnum > 0)
9273 ga_concat(gap, (char_u *)" ");
9274 }
9275 if (sourcing_lnum > 0)
9276 {
9277 sprintf(buf, "line %ld", (long)sourcing_lnum);
9278 ga_concat(gap, (char_u *)buf);
9279 }
9280 if (sourcing_name != NULL || sourcing_lnum > 0)
9281 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009282}
9283
9284/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009285 * Append "str" to "gap", escaping unprintable characters.
9286 * Changes NL to \n, CR to \r, etc.
9287 */
9288 static void
9289ga_concat_esc(garray_T *gap, char_u *str)
9290{
9291 char_u *p;
9292 char_u buf[NUMBUFLEN];
9293
9294 for (p = str; *p != NUL; ++p)
9295 switch (*p)
9296 {
9297 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9298 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9299 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9300 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9301 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9302 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9303 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9304 default:
9305 if (*p < ' ')
9306 {
9307 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9308 ga_concat(gap, buf);
9309 }
9310 else
9311 ga_append(gap, *p);
9312 break;
9313 }
9314}
9315
9316/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009317 * Fill "gap" with information about an assert error.
9318 */
9319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009320fill_assert_error(
9321 garray_T *gap,
9322 typval_T *opt_msg_tv,
9323 char_u *exp_str,
9324 typval_T *exp_tv,
9325 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009326{
9327 char_u numbuf[NUMBUFLEN];
9328 char_u *tofree;
9329
9330 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9331 {
9332 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9333 vim_free(tofree);
9334 }
9335 else
9336 {
9337 ga_concat(gap, (char_u *)"Expected ");
9338 if (exp_str == NULL)
9339 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009340 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009341 vim_free(tofree);
9342 }
9343 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009344 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009345 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009346 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009347 vim_free(tofree);
9348 }
9349}
Bram Moolenaar43345542015-11-29 17:35:35 +01009350
9351/*
9352 * Add an assert error to v:errors.
9353 */
9354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009355assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009356{
9357 struct vimvar *vp = &vimvars[VV_ERRORS];
9358
9359 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9360 /* Make sure v:errors is a list. */
9361 set_vim_var_list(VV_ERRORS, list_alloc());
9362 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9363}
9364
Bram Moolenaar43345542015-11-29 17:35:35 +01009365/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009366 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009367 */
9368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009369f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009370{
9371 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009372
9373 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9374 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009375 prepare_assert_error(&ga);
9376 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9377 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009378 ga_clear(&ga);
9379 }
9380}
9381
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009382/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009383 * "assert_exception(string[, msg])" function
9384 */
9385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009386f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009387{
9388 garray_T ga;
9389 char *error;
9390
9391 error = (char *)get_tv_string_chk(&argvars[0]);
9392 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9393 {
9394 prepare_assert_error(&ga);
9395 ga_concat(&ga, (char_u *)"v:exception is not set");
9396 assert_error(&ga);
9397 ga_clear(&ga);
9398 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009399 else if (error != NULL
9400 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009401 {
9402 prepare_assert_error(&ga);
9403 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9404 &vimvars[VV_EXCEPTION].vv_tv);
9405 assert_error(&ga);
9406 ga_clear(&ga);
9407 }
9408}
9409
9410/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009411 * "assert_fails(cmd [, error])" function
9412 */
9413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009414f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009415{
9416 char_u *cmd = get_tv_string_chk(&argvars[0]);
9417 garray_T ga;
9418
9419 called_emsg = FALSE;
9420 suppress_errthrow = TRUE;
9421 emsg_silent = TRUE;
9422 do_cmdline_cmd(cmd);
9423 if (!called_emsg)
9424 {
9425 prepare_assert_error(&ga);
9426 ga_concat(&ga, (char_u *)"command did not fail: ");
9427 ga_concat(&ga, cmd);
9428 assert_error(&ga);
9429 ga_clear(&ga);
9430 }
9431 else if (argvars[1].v_type != VAR_UNKNOWN)
9432 {
9433 char_u buf[NUMBUFLEN];
9434 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9435
9436 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9437 {
9438 prepare_assert_error(&ga);
9439 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9440 &vimvars[VV_ERRMSG].vv_tv);
9441 assert_error(&ga);
9442 ga_clear(&ga);
9443 }
9444 }
9445
9446 called_emsg = FALSE;
9447 suppress_errthrow = FALSE;
9448 emsg_silent = FALSE;
9449 emsg_on_display = FALSE;
9450 set_vim_var_string(VV_ERRMSG, NULL, 0);
9451}
9452
9453/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009454 * Common for assert_true() and assert_false().
9455 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009457assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009458{
9459 int error = FALSE;
9460 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009461
Bram Moolenaar37127922016-02-06 20:29:28 +01009462 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009463 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009464 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009465 if (argvars[0].v_type != VAR_NUMBER
9466 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9467 || error)
9468 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009469 prepare_assert_error(&ga);
9470 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009471 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009472 NULL, &argvars[0]);
9473 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009474 ga_clear(&ga);
9475 }
9476}
9477
9478/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009479 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009480 */
9481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009482f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009483{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009484 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009485}
9486
9487/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009488 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009489 */
9490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009492{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009493 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009494}
9495
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009497/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009498 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009499 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009502{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009503 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009504
9505 rettv->v_type = VAR_FLOAT;
9506 if (get_float_arg(argvars, &f) == OK)
9507 rettv->vval.v_float = asin(f);
9508 else
9509 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009510}
9511
9512/*
9513 * "atan()" function
9514 */
9515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009516f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009517{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009518 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009519
9520 rettv->v_type = VAR_FLOAT;
9521 if (get_float_arg(argvars, &f) == OK)
9522 rettv->vval.v_float = atan(f);
9523 else
9524 rettv->vval.v_float = 0.0;
9525}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009526
9527/*
9528 * "atan2()" function
9529 */
9530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009531f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009532{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009533 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009534
9535 rettv->v_type = VAR_FLOAT;
9536 if (get_float_arg(argvars, &fx) == OK
9537 && get_float_arg(&argvars[1], &fy) == OK)
9538 rettv->vval.v_float = atan2(fx, fy);
9539 else
9540 rettv->vval.v_float = 0.0;
9541}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009542#endif
9543
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544/*
9545 * "browse(save, title, initdir, default)" function
9546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009548f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550#ifdef FEAT_BROWSE
9551 int save;
9552 char_u *title;
9553 char_u *initdir;
9554 char_u *defname;
9555 char_u buf[NUMBUFLEN];
9556 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009559 save = get_tv_number_chk(&argvars[0], &error);
9560 title = get_tv_string_chk(&argvars[1]);
9561 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9562 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 if (error || title == NULL || initdir == NULL || defname == NULL)
9565 rettv->vval.v_string = NULL;
9566 else
9567 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009568 do_browse(save ? BROWSE_SAVE : 0,
9569 title, defname, NULL, initdir, NULL, curbuf);
9570#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009572#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574}
9575
9576/*
9577 * "browsedir(title, initdir)" function
9578 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009580f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009581{
9582#ifdef FEAT_BROWSE
9583 char_u *title;
9584 char_u *initdir;
9585 char_u buf[NUMBUFLEN];
9586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 title = get_tv_string_chk(&argvars[0]);
9588 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590 if (title == NULL || initdir == NULL)
9591 rettv->vval.v_string = NULL;
9592 else
9593 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009594 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599}
9600
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009601static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009602
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603/*
9604 * Find a buffer by number or exact name.
9605 */
9606 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009607find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009611 if (avar->v_type == VAR_NUMBER)
9612 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009613 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009615 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009616 if (buf == NULL)
9617 {
9618 /* No full path name match, try a match with a URL or a "nofile"
9619 * buffer, these don't use the full path. */
9620 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9621 if (buf->b_fname != NULL
9622 && (path_with_url(buf->b_fname)
9623#ifdef FEAT_QUICKFIX
9624 || bt_nofile(buf)
9625#endif
9626 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009627 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009628 break;
9629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 }
9631 return buf;
9632}
9633
9634/*
9635 * "bufexists(expr)" function
9636 */
9637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009638f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641}
9642
9643/*
9644 * "buflisted(expr)" function
9645 */
9646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009647f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
9649 buf_T *buf;
9650
9651 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009652 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653}
9654
9655/*
9656 * "bufloaded(expr)" function
9657 */
9658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009659f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
9661 buf_T *buf;
9662
9663 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665}
9666
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 static buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009668buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 int save_magic;
9671 char_u *save_cpo;
9672 buf_T *buf;
9673
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9675 save_magic = p_magic;
9676 p_magic = TRUE;
9677 save_cpo = p_cpo;
9678 p_cpo = (char_u *)"";
9679
9680 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009681 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682
9683 p_magic = save_magic;
9684 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009685 return buf;
9686}
9687
9688/*
9689 * Get buffer by number or pattern.
9690 */
9691 static buf_T *
9692get_buf_tv(typval_T *tv, int curtab_only)
9693{
9694 char_u *name = tv->vval.v_string;
9695 buf_T *buf;
9696
9697 if (tv->v_type == VAR_NUMBER)
9698 return buflist_findnr((int)tv->vval.v_number);
9699 if (tv->v_type != VAR_STRING)
9700 return NULL;
9701 if (name == NULL || *name == NUL)
9702 return curbuf;
9703 if (name[0] == '$' && name[1] == NUL)
9704 return lastbuf;
9705
9706 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707
9708 /* If not found, try expanding the name, like done for bufexists(). */
9709 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
9712 return buf;
9713}
9714
9715/*
9716 * "bufname(expr)" function
9717 */
9718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009719f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721 buf_T *buf;
9722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009723 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009725 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 --emsg_off;
9732}
9733
9734/*
9735 * "bufnr(expr)" function
9736 */
9737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009738f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739{
9740 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009741 int error = FALSE;
9742 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009746 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009747 --emsg_off;
9748
9749 /* If the buffer isn't found and the second argument is not zero create a
9750 * new buffer. */
9751 if (buf == NULL
9752 && argvars[1].v_type != VAR_UNKNOWN
9753 && get_tv_number_chk(&argvars[1], &error) != 0
9754 && !error
9755 && (name = get_tv_string_chk(&argvars[0])) != NULL
9756 && !error)
9757 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9758
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763}
9764
9765/*
9766 * "bufwinnr(nr)" function
9767 */
9768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009769f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771#ifdef FEAT_WINDOWS
9772 win_T *wp;
9773 int winnr = 0;
9774#endif
9775 buf_T *buf;
9776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009779 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780#ifdef FEAT_WINDOWS
9781 for (wp = firstwin; wp; wp = wp->w_next)
9782 {
9783 ++winnr;
9784 if (wp->w_buffer == buf)
9785 break;
9786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790#endif
9791 --emsg_off;
9792}
9793
9794/*
9795 * "byte2line(byte)" function
9796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009798f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
9800#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802#else
9803 long boff = 0;
9804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009805 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 (linenr_T)0, &boff);
9811#endif
9812}
9813
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009815byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009816{
9817#ifdef FEAT_MBYTE
9818 char_u *t;
9819#endif
9820 char_u *str;
9821 long idx;
9822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009823 str = get_tv_string_chk(&argvars[0]);
9824 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009827 return;
9828
9829#ifdef FEAT_MBYTE
9830 t = str;
9831 for ( ; idx > 0; idx--)
9832 {
9833 if (*t == NUL) /* EOL reached */
9834 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009835 if (enc_utf8 && comp)
9836 t += utf_ptr2len(t);
9837 else
9838 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009839 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009840 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009841#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009842 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009844#endif
9845}
9846
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009847/*
9848 * "byteidx()" function
9849 */
9850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009851f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009852{
9853 byteidx(argvars, rettv, FALSE);
9854}
9855
9856/*
9857 * "byteidxcomp()" function
9858 */
9859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009860f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009861{
9862 byteidx(argvars, rettv, TRUE);
9863}
9864
Bram Moolenaardb913952012-06-29 12:54:53 +02009865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009866func_call(
9867 char_u *name,
9868 typval_T *args,
9869 dict_T *selfdict,
9870 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009871{
9872 listitem_T *item;
9873 typval_T argv[MAX_FUNC_ARGS + 1];
9874 int argc = 0;
9875 int dummy;
9876 int r = 0;
9877
9878 for (item = args->vval.v_list->lv_first; item != NULL;
9879 item = item->li_next)
9880 {
9881 if (argc == MAX_FUNC_ARGS)
9882 {
9883 EMSG(_("E699: Too many arguments"));
9884 break;
9885 }
9886 /* Make a copy of each argument. This is needed to be able to set
9887 * v_lock to VAR_FIXED in the copy without changing the original list.
9888 */
9889 copy_tv(&item->li_tv, &argv[argc++]);
9890 }
9891
9892 if (item == NULL)
9893 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9894 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9895 &dummy, TRUE, selfdict);
9896
9897 /* Free the arguments. */
9898 while (argc > 0)
9899 clear_tv(&argv[--argc]);
9900
9901 return r;
9902}
9903
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009904/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009905 * "call(func, arglist)" function
9906 */
9907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009908f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009909{
9910 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009911 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009912
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009913 if (argvars[1].v_type != VAR_LIST)
9914 {
9915 EMSG(_(e_listreq));
9916 return;
9917 }
9918 if (argvars[1].vval.v_list == NULL)
9919 return;
9920
9921 if (argvars[0].v_type == VAR_FUNC)
9922 func = argvars[0].vval.v_string;
9923 else
9924 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 if (*func == NUL)
9926 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009927
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 if (argvars[2].v_type != VAR_UNKNOWN)
9929 {
9930 if (argvars[2].v_type != VAR_DICT)
9931 {
9932 EMSG(_(e_dictreq));
9933 return;
9934 }
9935 selfdict = argvars[2].vval.v_dict;
9936 }
9937
Bram Moolenaardb913952012-06-29 12:54:53 +02009938 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009939}
9940
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009941#ifdef FEAT_FLOAT
9942/*
9943 * "ceil({float})" function
9944 */
9945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009946f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009947{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009948 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009949
9950 rettv->v_type = VAR_FLOAT;
9951 if (get_float_arg(argvars, &f) == OK)
9952 rettv->vval.v_float = ceil(f);
9953 else
9954 rettv->vval.v_float = 0.0;
9955}
9956#endif
9957
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009958#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009959/*
9960 * Get a callback from "arg". It can be a Funcref or a function name.
9961 * When "arg" is zero return an empty string.
9962 * Return NULL for an invalid argument.
9963 */
9964 static char_u *
9965get_callback(typval_T *arg)
9966{
9967 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9968 return arg->vval.v_string;
9969 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9970 return (char_u *)"";
9971 EMSG(_("E999: Invalid callback argument"));
9972 return NULL;
9973}
9974
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009975 static int
9976handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9977{
9978 char_u *val = get_tv_string(item);
9979
9980 opt->jo_set |= jo;
9981 if (STRCMP(val, "nl") == 0)
9982 *modep = MODE_NL;
9983 else if (STRCMP(val, "raw") == 0)
9984 *modep = MODE_RAW;
9985 else if (STRCMP(val, "js") == 0)
9986 *modep = MODE_JS;
9987 else if (STRCMP(val, "json") == 0)
9988 *modep = MODE_JSON;
9989 else
9990 {
9991 EMSG2(_(e_invarg2), val);
9992 return FAIL;
9993 }
9994 return OK;
9995}
9996
Bram Moolenaar187db502016-02-27 14:44:26 +01009997 static int
9998handle_io(typval_T *item, int part, jobopt_T *opt)
9999{
10000 char_u *val = get_tv_string(item);
10001
10002 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
10003 if (STRCMP(val, "null") == 0)
10004 opt->jo_io[part] = JIO_NULL;
10005 else if (STRCMP(val, "pipe") == 0)
10006 opt->jo_io[part] = JIO_PIPE;
10007 else if (STRCMP(val, "file") == 0)
10008 opt->jo_io[part] = JIO_FILE;
10009 else if (STRCMP(val, "buffer") == 0)
10010 opt->jo_io[part] = JIO_BUFFER;
10011 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
10012 opt->jo_io[part] = JIO_OUT;
10013 else
10014 {
10015 EMSG2(_(e_invarg2), val);
10016 return FAIL;
10017 }
10018 return OK;
10019}
10020
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010021 static void
10022clear_job_options(jobopt_T *opt)
10023{
10024 vim_memset(opt, 0, sizeof(jobopt_T));
10025}
10026
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010027/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010028 * Get the PART_ number from the first character of an option name.
10029 */
10030 static int
10031part_from_char(int c)
10032{
10033 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10034}
10035
10036/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010037 * Get the option entries from the dict in "tv", parse them and put the result
10038 * in "opt".
10039 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010040 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010041 */
10042 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010043get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010044{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010045 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010046 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010047 dict_T *dict;
10048 int todo;
10049 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010050 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010051
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010052 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010053 if (tv->v_type == VAR_UNKNOWN)
10054 return OK;
10055 if (tv->v_type != VAR_DICT)
10056 {
10057 EMSG(_(e_invarg));
10058 return FAIL;
10059 }
10060 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010061 if (dict == NULL)
10062 return OK;
10063
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010064 todo = (int)dict->dv_hashtab.ht_used;
10065 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10066 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010067 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010068 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010069
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010070 if (STRCMP(hi->hi_key, "mode") == 0)
10071 {
10072 if (!(supported & JO_MODE))
10073 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010074 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010075 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010076 }
10077 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10078 {
10079 if (!(supported & JO_IN_MODE))
10080 break;
10081 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10082 == FAIL)
10083 return FAIL;
10084 }
10085 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10086 {
10087 if (!(supported & JO_OUT_MODE))
10088 break;
10089 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10090 == FAIL)
10091 return FAIL;
10092 }
10093 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10094 {
10095 if (!(supported & JO_ERR_MODE))
10096 break;
10097 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10098 == FAIL)
10099 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010100 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010101 else if (STRCMP(hi->hi_key, "in-io") == 0
10102 || STRCMP(hi->hi_key, "out-io") == 0
10103 || STRCMP(hi->hi_key, "err-io") == 0)
10104 {
10105 if (!(supported & JO_OUT_IO))
10106 break;
10107 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10108 return FAIL;
10109 }
10110 else if (STRCMP(hi->hi_key, "in-name") == 0
10111 || STRCMP(hi->hi_key, "out-name") == 0
10112 || STRCMP(hi->hi_key, "err-name") == 0)
10113 {
10114 part = part_from_char(*hi->hi_key);
10115
10116 if (!(supported & JO_OUT_IO))
10117 break;
10118 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10119 opt->jo_io_name[part] =
10120 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10121 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +010010122 else if (STRCMP(hi->hi_key, "in-buf") == 0
10123 || STRCMP(hi->hi_key, "out-buf") == 0
10124 || STRCMP(hi->hi_key, "err-buf") == 0)
10125 {
10126 part = part_from_char(*hi->hi_key);
10127
10128 if (!(supported & JO_OUT_IO))
10129 break;
10130 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
10131 opt->jo_io_buf[part] = get_tv_number(item);
10132 if (opt->jo_io_buf[part] <= 0)
10133 {
10134 EMSG2(_(e_invarg2), get_tv_string(item));
10135 return FAIL;
10136 }
10137 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
10138 {
10139 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
10140 return FAIL;
10141 }
10142 }
Bram Moolenaar014069a2016-03-03 22:51:40 +010010143 else if (STRCMP(hi->hi_key, "in-top") == 0
10144 || STRCMP(hi->hi_key, "in-bot") == 0)
10145 {
10146 linenr_T *lp;
10147
10148 if (!(supported & JO_OUT_IO))
10149 break;
10150 if (hi->hi_key[3] == 't')
10151 {
10152 lp = &opt->jo_in_top;
10153 opt->jo_set |= JO_IN_TOP;
10154 }
10155 else
10156 {
10157 lp = &opt->jo_in_bot;
10158 opt->jo_set |= JO_IN_BOT;
10159 }
10160 *lp = get_tv_number(item);
10161 if (*lp < 0)
10162 {
10163 EMSG2(_(e_invarg2), get_tv_string(item));
10164 return FAIL;
10165 }
10166 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010167 else if (STRCMP(hi->hi_key, "callback") == 0)
10168 {
10169 if (!(supported & JO_CALLBACK))
10170 break;
10171 opt->jo_set |= JO_CALLBACK;
10172 opt->jo_callback = get_callback(item);
10173 if (opt->jo_callback == NULL)
10174 {
10175 EMSG2(_(e_invarg2), "callback");
10176 return FAIL;
10177 }
10178 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010179 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10180 {
10181 if (!(supported & JO_OUT_CALLBACK))
10182 break;
10183 opt->jo_set |= JO_OUT_CALLBACK;
10184 opt->jo_out_cb = get_callback(item);
10185 if (opt->jo_out_cb == NULL)
10186 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010187 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010188 return FAIL;
10189 }
10190 }
10191 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10192 {
10193 if (!(supported & JO_ERR_CALLBACK))
10194 break;
10195 opt->jo_set |= JO_ERR_CALLBACK;
10196 opt->jo_err_cb = get_callback(item);
10197 if (opt->jo_err_cb == NULL)
10198 {
10199 EMSG2(_(e_invarg2), "err-cb");
10200 return FAIL;
10201 }
10202 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010203 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10204 {
10205 if (!(supported & JO_CLOSE_CALLBACK))
10206 break;
10207 opt->jo_set |= JO_CLOSE_CALLBACK;
10208 opt->jo_close_cb = get_callback(item);
10209 if (opt->jo_close_cb == NULL)
10210 {
10211 EMSG2(_(e_invarg2), "close-cb");
10212 return FAIL;
10213 }
10214 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010215 else if (STRCMP(hi->hi_key, "waittime") == 0)
10216 {
10217 if (!(supported & JO_WAITTIME))
10218 break;
10219 opt->jo_set |= JO_WAITTIME;
10220 opt->jo_waittime = get_tv_number(item);
10221 }
10222 else if (STRCMP(hi->hi_key, "timeout") == 0)
10223 {
10224 if (!(supported & JO_TIMEOUT))
10225 break;
10226 opt->jo_set |= JO_TIMEOUT;
10227 opt->jo_timeout = get_tv_number(item);
10228 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010229 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10230 {
10231 if (!(supported & JO_OUT_TIMEOUT))
10232 break;
10233 opt->jo_set |= JO_OUT_TIMEOUT;
10234 opt->jo_out_timeout = get_tv_number(item);
10235 }
10236 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10237 {
10238 if (!(supported & JO_ERR_TIMEOUT))
10239 break;
10240 opt->jo_set |= JO_ERR_TIMEOUT;
10241 opt->jo_err_timeout = get_tv_number(item);
10242 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010243 else if (STRCMP(hi->hi_key, "part") == 0)
10244 {
10245 if (!(supported & JO_PART))
10246 break;
10247 opt->jo_set |= JO_PART;
10248 val = get_tv_string(item);
10249 if (STRCMP(val, "err") == 0)
10250 opt->jo_part = PART_ERR;
10251 else
10252 {
10253 EMSG2(_(e_invarg2), val);
10254 return FAIL;
10255 }
10256 }
10257 else if (STRCMP(hi->hi_key, "id") == 0)
10258 {
10259 if (!(supported & JO_ID))
10260 break;
10261 opt->jo_set |= JO_ID;
10262 opt->jo_id = get_tv_number(item);
10263 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010264 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10265 {
10266 if (!(supported & JO_STOPONEXIT))
10267 break;
10268 opt->jo_set |= JO_STOPONEXIT;
10269 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10270 opt->jo_soe_buf);
10271 if (opt->jo_stoponexit == NULL)
10272 {
10273 EMSG2(_(e_invarg2), "stoponexit");
10274 return FAIL;
10275 }
10276 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010277 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10278 {
10279 if (!(supported & JO_EXIT_CB))
10280 break;
10281 opt->jo_set |= JO_EXIT_CB;
10282 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010283 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010284 {
10285 EMSG2(_(e_invarg2), "exit-cb");
10286 return FAIL;
10287 }
10288 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010289 else
10290 break;
10291 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010292 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010293 if (todo > 0)
10294 {
10295 EMSG2(_(e_invarg2), hi->hi_key);
10296 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010297 }
10298
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010299 return OK;
10300}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010301#endif
10302
10303#ifdef FEAT_CHANNEL
10304/*
10305 * Get the channel from the argument.
10306 * Returns NULL if the handle is invalid.
10307 */
10308 static channel_T *
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010309get_channel_arg(typval_T *tv, int check_open)
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010310{
Bram Moolenaar151f6562016-03-07 21:19:38 +010010311 channel_T *channel = NULL;
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010312
Bram Moolenaar151f6562016-03-07 21:19:38 +010010313 if (tv->v_type == VAR_JOB)
10314 {
10315 if (tv->vval.v_job != NULL)
10316 channel = tv->vval.v_job->jv_channel;
10317 }
10318 else if (tv->v_type == VAR_CHANNEL)
10319 {
10320 channel = tv->vval.v_channel;
10321 }
10322 else
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010323 {
10324 EMSG2(_(e_invarg2), get_tv_string(tv));
10325 return NULL;
10326 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010327
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010328 if (check_open && (channel == NULL || !channel_is_open(channel)))
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010329 {
10330 EMSG(_("E906: not an open channel"));
10331 return NULL;
10332 }
10333 return channel;
10334}
10335
10336/*
10337 * "ch_close()" function
10338 */
10339 static void
10340f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10341{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010342 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010343
10344 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010345 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010346 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010347 channel_clear(channel);
10348 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010349}
10350
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010351/*
10352 * "ch_getbufnr()" function
10353 */
10354 static void
10355f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10356{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010357 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010358
10359 rettv->vval.v_number = -1;
10360 if (channel != NULL)
10361 {
10362 char_u *what = get_tv_string(&argvars[1]);
10363 int part;
10364
10365 if (STRCMP(what, "err") == 0)
10366 part = PART_ERR;
10367 else if (STRCMP(what, "out") == 0)
10368 part = PART_OUT;
10369 else if (STRCMP(what, "in") == 0)
10370 part = PART_IN;
10371 else
10372 part = PART_SOCK;
10373 if (channel->ch_part[part].ch_buffer != NULL)
10374 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10375 }
10376}
10377
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010378# ifdef FEAT_JOB
10379/*
10380 * "ch_getjob()" function
10381 */
10382 static void
10383f_ch_getjob(typval_T *argvars, typval_T *rettv)
10384{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010385 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010386
10387 if (channel != NULL)
10388 {
10389 rettv->v_type = VAR_JOB;
10390 rettv->vval.v_job = channel->ch_job;
10391 if (channel->ch_job != NULL)
10392 ++channel->ch_job->jv_refcount;
10393 }
10394}
10395# endif
10396
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010397/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010398 * "ch_log()" function
10399 */
10400 static void
10401f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10402{
10403 char_u *msg = get_tv_string(&argvars[0]);
10404 channel_T *channel = NULL;
10405
10406 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010407 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010408
10409 ch_log(channel, (char *)msg);
10410}
10411
10412/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010413 * "ch_logfile()" function
10414 */
10415 static void
10416f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10417{
10418 char_u *fname;
10419 char_u *opt = (char_u *)"";
10420 char_u buf[NUMBUFLEN];
10421 FILE *file = NULL;
10422
10423 fname = get_tv_string(&argvars[0]);
10424 if (argvars[1].v_type == VAR_STRING)
10425 opt = get_tv_string_buf(&argvars[1], buf);
10426 if (*fname != NUL)
10427 {
10428 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10429 if (file == NULL)
10430 {
10431 EMSG2(_(e_notopen), fname);
10432 return;
10433 }
10434 }
10435 ch_logfile(file);
10436}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010437
10438/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010439 * "ch_open()" function
10440 */
10441 static void
10442f_ch_open(typval_T *argvars, typval_T *rettv)
10443{
10444 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010445 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010446 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010447 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010448 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010449 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010450
10451 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010452 rettv->v_type = VAR_CHANNEL;
10453 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010454
10455 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010456 if (argvars[1].v_type != VAR_UNKNOWN
10457 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010458 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010459 EMSG(_(e_invarg));
10460 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010461 }
10462
10463 /* parse address */
10464 p = vim_strchr(address, ':');
10465 if (p == NULL)
10466 {
10467 EMSG2(_(e_invarg2), address);
10468 return;
10469 }
10470 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010471 port = strtol((char *)p, &rest, 10);
10472 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010473 {
10474 p[-1] = ':';
10475 EMSG2(_(e_invarg2), address);
10476 return;
10477 }
10478
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010479 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010480 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010481 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010482 opt.jo_timeout = 2000;
10483 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010484 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010485 return;
10486 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010487 {
10488 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010489 return;
10490 }
10491
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010492 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010493 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010494 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010495 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010496 opt.jo_set = JO_ALL;
10497 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010498 }
10499}
10500
10501/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010502 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010503 */
10504 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010505common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010506{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010507 channel_T *channel;
10508 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010509 jobopt_T opt;
10510 int mode;
10511 int timeout;
10512 int id = -1;
10513 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010514
10515 /* return an empty string by default */
10516 rettv->v_type = VAR_STRING;
10517 rettv->vval.v_string = NULL;
10518
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010519 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010520 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10521 == FAIL)
10522 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010523
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010524 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010525 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010526 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010527 if (opt.jo_set & JO_PART)
10528 part = opt.jo_part;
10529 else
10530 part = channel_part_read(channel);
10531 mode = channel_get_mode(channel, part);
10532 timeout = channel_get_timeout(channel, part);
10533 if (opt.jo_set & JO_TIMEOUT)
10534 timeout = opt.jo_timeout;
10535
10536 if (raw || mode == MODE_RAW || mode == MODE_NL)
10537 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10538 else
10539 {
10540 if (opt.jo_set & JO_ID)
10541 id = opt.jo_id;
10542 channel_read_json_block(channel, part, timeout, id, &listtv);
10543 if (listtv != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +010010544 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010545 *rettv = *listtv;
Bram Moolenaard6051b52016-02-28 15:49:03 +010010546 vim_free(listtv);
10547 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010548 else
10549 {
10550 rettv->v_type = VAR_SPECIAL;
10551 rettv->vval.v_number = VVAL_NONE;
10552 }
10553 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010554 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010555}
10556
10557/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010558 * "ch_read()" function
10559 */
10560 static void
10561f_ch_read(typval_T *argvars, typval_T *rettv)
10562{
10563 common_channel_read(argvars, rettv, FALSE);
10564}
10565
10566/*
10567 * "ch_readraw()" function
10568 */
10569 static void
10570f_ch_readraw(typval_T *argvars, typval_T *rettv)
10571{
10572 common_channel_read(argvars, rettv, TRUE);
10573}
10574
10575/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010576 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010577 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010578 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010579 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010580 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010581 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010582send_common(
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010583 typval_T *argvars,
10584 char_u *text,
10585 int id,
10586 int eval,
10587 jobopt_T *opt,
10588 char *fun,
10589 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010590{
Bram Moolenaar77073442016-02-13 23:23:53 +010010591 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010592 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010593
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010594 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010595 if (channel == NULL)
10596 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010597 part_send = channel_part_send(channel);
10598 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010599
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010600 clear_job_options(opt);
10601 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010602 return NULL;
10603
Bram Moolenaara07fec92016-02-05 21:04:08 +010010604 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010605 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10606 * allowed. */
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010607 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010608 {
10609 if (eval)
10610 {
10611 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10612 return NULL;
10613 }
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010614 channel_set_req_callback(channel, part_send, opt->jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010615 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010616
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010617 if (channel_send(channel, part_send, text, fun) == OK
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010618 && opt->jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010619 return channel;
10620 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010621}
10622
10623/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010624 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010625 */
10626 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010627ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010628{
10629 char_u *text;
10630 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010631 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010632 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010633 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010634 int part_send;
10635 int part_read;
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010636 jobopt_T opt;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010637 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010638
10639 /* return an empty string by default */
10640 rettv->v_type = VAR_STRING;
10641 rettv->vval.v_string = NULL;
10642
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010643 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010644 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010645 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010646 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010647
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010648 ch_mode = channel_get_mode(channel, part_send);
10649 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010650 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010651 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010652 return;
10653 }
10654
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010655 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010656 text = json_encode_nr_expr(id, &argvars[1],
10657 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010658 if (text == NULL)
10659 return;
10660
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010661 channel = send_common(argvars, text, id, eval, &opt,
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010662 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010663 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010664 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010665 {
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010666 if (opt.jo_set & JO_TIMEOUT)
10667 timeout = opt.jo_timeout;
10668 else
10669 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010670 timeout = channel_get_timeout(channel, part_read);
10671 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10672 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010673 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010674 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010675
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010676 /* Move the item from the list and then change the type to
10677 * avoid the value being freed. */
10678 *rettv = list->lv_last->li_tv;
10679 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010680 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010681 }
10682 }
10683}
10684
10685/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010686 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010687 */
10688 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010689f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10690{
10691 ch_expr_common(argvars, rettv, TRUE);
10692}
10693
10694/*
10695 * "ch_sendexpr()" function
10696 */
10697 static void
10698f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10699{
10700 ch_expr_common(argvars, rettv, FALSE);
10701}
10702
10703/*
10704 * common for "ch_evalraw()" and "ch_sendraw()"
10705 */
10706 static void
10707ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010708{
10709 char_u buf[NUMBUFLEN];
10710 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010711 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010712 int part_read;
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010713 jobopt_T opt;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010714 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010715
10716 /* return an empty string by default */
10717 rettv->v_type = VAR_STRING;
10718 rettv->vval.v_string = NULL;
10719
10720 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010721 channel = send_common(argvars, text, 0, eval, &opt,
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010722 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10723 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010724 {
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010725 if (opt.jo_set & JO_TIMEOUT)
10726 timeout = opt.jo_timeout;
10727 else
10728 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010729 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10730 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010731}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010732
10733/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010734 * "ch_evalraw()" function
10735 */
10736 static void
10737f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10738{
10739 ch_raw_common(argvars, rettv, TRUE);
10740}
10741
10742/*
10743 * "ch_sendraw()" function
10744 */
10745 static void
10746f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10747{
10748 ch_raw_common(argvars, rettv, FALSE);
10749}
10750
10751/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010752 * "ch_setoptions()" function
10753 */
10754 static void
10755f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10756{
10757 channel_T *channel;
10758 jobopt_T opt;
10759
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010760 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010761 if (channel == NULL)
10762 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010763 clear_job_options(&opt);
10764 if (get_job_options(&argvars[1], &opt,
10765 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010766 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010767 channel_set_options(channel, &opt);
10768}
10769
10770/*
10771 * "ch_status()" function
10772 */
10773 static void
10774f_ch_status(typval_T *argvars, typval_T *rettv)
10775{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010776 channel_T *channel;
10777
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010778 /* return an empty string by default */
10779 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010780 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010781
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010782 channel = get_channel_arg(&argvars[0], FALSE);
10783 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010784}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010785#endif
10786
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010787/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010788 * "changenr()" function
10789 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010791f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010792{
10793 rettv->vval.v_number = curbuf->b_u_seq_cur;
10794}
10795
10796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 * "char2nr(string)" function
10798 */
10799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010800f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801{
10802#ifdef FEAT_MBYTE
10803 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010804 {
10805 int utf8 = 0;
10806
10807 if (argvars[1].v_type != VAR_UNKNOWN)
10808 utf8 = get_tv_number_chk(&argvars[1], NULL);
10809
10810 if (utf8)
10811 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10812 else
10813 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 else
10816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818}
10819
10820/*
10821 * "cindent(lnum)" function
10822 */
10823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010824f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825{
10826#ifdef FEAT_CINDENT
10827 pos_T pos;
10828 linenr_T lnum;
10829
10830 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10833 {
10834 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 curwin->w_cursor = pos;
10837 }
10838 else
10839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841}
10842
10843/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010844 * "clearmatches()" function
10845 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010847f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010848{
10849#ifdef FEAT_SEARCH_EXTRA
10850 clear_matches(curwin);
10851#endif
10852}
10853
10854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 * "col(string)" function
10856 */
10857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010858f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859{
10860 colnr_T col = 0;
10861 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010862 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010864 fp = var2fpos(&argvars[0], FALSE, &fnum);
10865 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 {
10867 if (fp->col == MAXCOL)
10868 {
10869 /* '> can be MAXCOL, get the length of the line then */
10870 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010871 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 else
10873 col = MAXCOL;
10874 }
10875 else
10876 {
10877 col = fp->col + 1;
10878#ifdef FEAT_VIRTUALEDIT
10879 /* col(".") when the cursor is on the NUL at the end of the line
10880 * because of "coladd" can be seen as an extra column. */
10881 if (virtual_active() && fp == &curwin->w_cursor)
10882 {
10883 char_u *p = ml_get_cursor();
10884
10885 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10886 curwin->w_virtcol - curwin->w_cursor.coladd))
10887 {
10888# ifdef FEAT_MBYTE
10889 int l;
10890
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010891 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 col += l;
10893# else
10894 if (*p != NUL && p[1] == NUL)
10895 ++col;
10896# endif
10897 }
10898 }
10899#endif
10900 }
10901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010902 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903}
10904
Bram Moolenaar572cb562005-08-05 21:35:02 +000010905#if defined(FEAT_INS_EXPAND)
10906/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010907 * "complete()" function
10908 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010910f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010911{
10912 int startcol;
10913
10914 if ((State & INSERT) == 0)
10915 {
10916 EMSG(_("E785: complete() can only be used in Insert mode"));
10917 return;
10918 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010919
10920 /* Check for undo allowed here, because if something was already inserted
10921 * the line was already saved for undo and this check isn't done. */
10922 if (!undo_allowed())
10923 return;
10924
Bram Moolenaarade00832006-03-10 21:46:58 +000010925 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10926 {
10927 EMSG(_(e_invarg));
10928 return;
10929 }
10930
10931 startcol = get_tv_number_chk(&argvars[0], NULL);
10932 if (startcol <= 0)
10933 return;
10934
10935 set_completion(startcol - 1, argvars[1].vval.v_list);
10936}
10937
10938/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010939 * "complete_add()" function
10940 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010942f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010943{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010944 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010945}
10946
10947/*
10948 * "complete_check()" function
10949 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010951f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010952{
10953 int saved = RedrawingDisabled;
10954
10955 RedrawingDisabled = 0;
10956 ins_compl_check_keys(0);
10957 rettv->vval.v_number = compl_interrupted;
10958 RedrawingDisabled = saved;
10959}
10960#endif
10961
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962/*
10963 * "confirm(message, buttons[, default [, type]])" function
10964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010966f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967{
10968#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10969 char_u *message;
10970 char_u *buttons = NULL;
10971 char_u buf[NUMBUFLEN];
10972 char_u buf2[NUMBUFLEN];
10973 int def = 1;
10974 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010975 char_u *typestr;
10976 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010978 message = get_tv_string_chk(&argvars[0]);
10979 if (message == NULL)
10980 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010981 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010983 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10984 if (buttons == NULL)
10985 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010986 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010989 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10992 if (typestr == NULL)
10993 error = TRUE;
10994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996 switch (TOUPPER_ASC(*typestr))
10997 {
10998 case 'E': type = VIM_ERROR; break;
10999 case 'Q': type = VIM_QUESTION; break;
11000 case 'I': type = VIM_INFO; break;
11001 case 'W': type = VIM_WARNING; break;
11002 case 'G': type = VIM_GENERIC; break;
11003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 }
11005 }
11006 }
11007 }
11008
11009 if (buttons == NULL || *buttons == NUL)
11010 buttons = (char_u *)_("&Ok");
11011
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011012 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011013 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010011014 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015#endif
11016}
11017
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011018/*
11019 * "copy()" function
11020 */
11021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011022f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011023{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011024 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011025}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011027#ifdef FEAT_FLOAT
11028/*
11029 * "cos()" function
11030 */
11031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011032f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011033{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011034 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011035
11036 rettv->v_type = VAR_FLOAT;
11037 if (get_float_arg(argvars, &f) == OK)
11038 rettv->vval.v_float = cos(f);
11039 else
11040 rettv->vval.v_float = 0.0;
11041}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011042
11043/*
11044 * "cosh()" function
11045 */
11046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011047f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011048{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011049 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011050
11051 rettv->v_type = VAR_FLOAT;
11052 if (get_float_arg(argvars, &f) == OK)
11053 rettv->vval.v_float = cosh(f);
11054 else
11055 rettv->vval.v_float = 0.0;
11056}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011057#endif
11058
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011060 * "count()" function
11061 */
11062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011063f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011064{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011065 long n = 0;
11066 int ic = FALSE;
11067
Bram Moolenaare9a41262005-01-15 22:18:47 +000011068 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011069 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011070 listitem_T *li;
11071 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011072 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011073
Bram Moolenaare9a41262005-01-15 22:18:47 +000011074 if ((l = argvars[0].vval.v_list) != NULL)
11075 {
11076 li = l->lv_first;
11077 if (argvars[2].v_type != VAR_UNKNOWN)
11078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 int error = FALSE;
11080
11081 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011082 if (argvars[3].v_type != VAR_UNKNOWN)
11083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 idx = get_tv_number_chk(&argvars[3], &error);
11085 if (!error)
11086 {
11087 li = list_find(l, idx);
11088 if (li == NULL)
11089 EMSGN(_(e_listidx), idx);
11090 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 if (error)
11093 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011094 }
11095
11096 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011097 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011098 ++n;
11099 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011100 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 else if (argvars[0].v_type == VAR_DICT)
11102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 int todo;
11104 dict_T *d;
11105 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011106
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011107 if ((d = argvars[0].vval.v_dict) != NULL)
11108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 int error = FALSE;
11110
Bram Moolenaare9a41262005-01-15 22:18:47 +000011111 if (argvars[2].v_type != VAR_UNKNOWN)
11112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011114 if (argvars[3].v_type != VAR_UNKNOWN)
11115 EMSG(_(e_invarg));
11116 }
11117
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011118 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011120 {
11121 if (!HASHITEM_EMPTY(hi))
11122 {
11123 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011124 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011125 ++n;
11126 }
11127 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011128 }
11129 }
11130 else
11131 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011132 rettv->vval.v_number = n;
11133}
11134
11135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11137 *
11138 * Checks the existence of a cscope connection.
11139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011141f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142{
11143#ifdef FEAT_CSCOPE
11144 int num = 0;
11145 char_u *dbpath = NULL;
11146 char_u *prepend = NULL;
11147 char_u buf[NUMBUFLEN];
11148
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011149 if (argvars[0].v_type != VAR_UNKNOWN
11150 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 num = (int)get_tv_number(&argvars[0]);
11153 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011154 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 }
11157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159#endif
11160}
11161
11162/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011163 * "cursor(lnum, col)" function, or
11164 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011166 * Moves the cursor to the specified line and column.
11167 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011170f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171{
11172 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011173#ifdef FEAT_VIRTUALEDIT
11174 long coladd = 0;
11175#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011176 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011178 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011179 if (argvars[1].v_type == VAR_UNKNOWN)
11180 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011181 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011182 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011183
Bram Moolenaar493c1782014-05-28 14:34:46 +020011184 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011185 {
11186 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011187 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011188 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011189 line = pos.lnum;
11190 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011191#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011192 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011193#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011194 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011195 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011196 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011197 set_curswant = FALSE;
11198 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011199 }
11200 else
11201 {
11202 line = get_tv_lnum(argvars);
11203 col = get_tv_number_chk(&argvars[1], NULL);
11204#ifdef FEAT_VIRTUALEDIT
11205 if (argvars[2].v_type != VAR_UNKNOWN)
11206 coladd = get_tv_number_chk(&argvars[2], NULL);
11207#endif
11208 }
11209 if (line < 0 || col < 0
11210#ifdef FEAT_VIRTUALEDIT
11211 || coladd < 0
11212#endif
11213 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 if (line > 0)
11216 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 if (col > 0)
11218 curwin->w_cursor.col = col - 1;
11219#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011220 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221#endif
11222
11223 /* Make sure the cursor is in a valid position. */
11224 check_cursor();
11225#ifdef FEAT_MBYTE
11226 /* Correct cursor for multi-byte character. */
11227 if (has_mbyte)
11228 mb_adjust_cursor();
11229#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011230
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011231 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011232 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233}
11234
11235/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011236 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 */
11238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011239f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011241 int noref = 0;
11242
11243 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011244 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011245 if (noref < 0 || noref > 1)
11246 EMSG(_(e_invarg));
11247 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011248 {
11249 current_copyID += COPYID_INC;
11250 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253
11254/*
11255 * "delete()" function
11256 */
11257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011258f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011260 char_u nbuf[NUMBUFLEN];
11261 char_u *name;
11262 char_u *flags;
11263
11264 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011266 return;
11267
11268 name = get_tv_string(&argvars[0]);
11269 if (name == NULL || *name == NUL)
11270 {
11271 EMSG(_(e_invarg));
11272 return;
11273 }
11274
11275 if (argvars[1].v_type != VAR_UNKNOWN)
11276 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011278 flags = (char_u *)"";
11279
11280 if (*flags == NUL)
11281 /* delete a file */
11282 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11283 else if (STRCMP(flags, "d") == 0)
11284 /* delete an empty directory */
11285 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11286 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011287 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011288 rettv->vval.v_number = delete_recursive(name);
11289 else
11290 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291}
11292
11293/*
11294 * "did_filetype()" function
11295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011297f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298{
11299#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301#endif
11302}
11303
11304/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011305 * "diff_filler()" function
11306 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011308f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011309{
11310#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011312#endif
11313}
11314
11315/*
11316 * "diff_hlID()" function
11317 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011319f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011320{
11321#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011323 static linenr_T prev_lnum = 0;
11324 static int changedtick = 0;
11325 static int fnum = 0;
11326 static int change_start = 0;
11327 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011328 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011329 int filler_lines;
11330 int col;
11331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 if (lnum < 0) /* ignore type error in {lnum} arg */
11333 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011334 if (lnum != prev_lnum
11335 || changedtick != curbuf->b_changedtick
11336 || fnum != curbuf->b_fnum)
11337 {
11338 /* New line, buffer, change: need to get the values. */
11339 filler_lines = diff_check(curwin, lnum);
11340 if (filler_lines < 0)
11341 {
11342 if (filler_lines == -1)
11343 {
11344 change_start = MAXCOL;
11345 change_end = -1;
11346 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11347 hlID = HLF_ADD; /* added line */
11348 else
11349 hlID = HLF_CHD; /* changed line */
11350 }
11351 else
11352 hlID = HLF_ADD; /* added line */
11353 }
11354 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011355 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011356 prev_lnum = lnum;
11357 changedtick = curbuf->b_changedtick;
11358 fnum = curbuf->b_fnum;
11359 }
11360
11361 if (hlID == HLF_CHD || hlID == HLF_TXD)
11362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011363 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011364 if (col >= change_start && col <= change_end)
11365 hlID = HLF_TXD; /* changed text */
11366 else
11367 hlID = HLF_CHD; /* changed line */
11368 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011369 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011370#endif
11371}
11372
11373/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011374 * "disable_char_avail_for_testing({expr})" function
11375 */
11376 static void
11377f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11378{
11379 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11380}
11381
11382/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011383 * "empty({expr})" function
11384 */
11385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011386f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011387{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011388 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011389
11390 switch (argvars[0].v_type)
11391 {
11392 case VAR_STRING:
11393 case VAR_FUNC:
11394 n = argvars[0].vval.v_string == NULL
11395 || *argvars[0].vval.v_string == NUL;
11396 break;
11397 case VAR_NUMBER:
11398 n = argvars[0].vval.v_number == 0;
11399 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011400 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011401#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011402 n = argvars[0].vval.v_float == 0.0;
11403 break;
11404#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011405 case VAR_LIST:
11406 n = argvars[0].vval.v_list == NULL
11407 || argvars[0].vval.v_list->lv_first == NULL;
11408 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011409 case VAR_DICT:
11410 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011412 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011413 case VAR_SPECIAL:
11414 n = argvars[0].vval.v_number != VVAL_TRUE;
11415 break;
11416
Bram Moolenaar835dc632016-02-07 14:27:38 +010011417 case VAR_JOB:
11418#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011419 n = argvars[0].vval.v_job == NULL
11420 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11421 break;
11422#endif
11423 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011424#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011425 n = argvars[0].vval.v_channel == NULL
11426 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011427 break;
11428#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011429 case VAR_UNKNOWN:
11430 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11431 n = TRUE;
11432 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011433 }
11434
11435 rettv->vval.v_number = n;
11436}
11437
11438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 * "escape({string}, {chars})" function
11440 */
11441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011442f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
11444 char_u buf[NUMBUFLEN];
11445
Bram Moolenaar758711c2005-02-02 23:11:38 +000011446 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11447 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449}
11450
11451/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011452 * "eval()" function
11453 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011455f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011456{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011457 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011459 s = get_tv_string_chk(&argvars[0]);
11460 if (s != NULL)
11461 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011462
Bram Moolenaar615b9972015-01-14 17:15:05 +010011463 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11465 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011466 if (p != NULL && !aborting())
11467 EMSG2(_(e_invexpr2), p);
11468 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011469 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011470 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011471 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011472 else if (*s != NUL)
11473 EMSG(_(e_trailing));
11474}
11475
11476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 * "eventhandler()" function
11478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011480f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483}
11484
11485/*
11486 * "executable()" function
11487 */
11488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011489f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011491 char_u *name = get_tv_string(&argvars[0]);
11492
11493 /* Check in $PATH and also check directly if there is a directory name. */
11494 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11495 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011496}
11497
11498/*
11499 * "exepath()" function
11500 */
11501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011502f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011503{
11504 char_u *p = NULL;
11505
Bram Moolenaarb5971142015-03-21 17:32:19 +010011506 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011507 rettv->v_type = VAR_STRING;
11508 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509}
11510
11511/*
11512 * "exists()" function
11513 */
11514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011515f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516{
11517 char_u *p;
11518 char_u *name;
11519 int n = FALSE;
11520 int len = 0;
11521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 if (*p == '$') /* environment variable */
11524 {
11525 /* first try "normal" environment variables (fast) */
11526 if (mch_getenv(p + 1) != NULL)
11527 n = TRUE;
11528 else
11529 {
11530 /* try expanding things like $VIM and ${HOME} */
11531 p = expand_env_save(p);
11532 if (p != NULL && *p != '$')
11533 n = TRUE;
11534 vim_free(p);
11535 }
11536 }
11537 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011540 if (*skipwhite(p) != NUL)
11541 n = FALSE; /* trailing garbage */
11542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 else if (*p == '*') /* internal or user defined function */
11544 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011545 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 }
11547 else if (*p == ':')
11548 {
11549 n = cmd_exists(p + 1);
11550 }
11551 else if (*p == '#')
11552 {
11553#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011554 if (p[1] == '#')
11555 n = autocmd_supported(p + 2);
11556 else
11557 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558#endif
11559 }
11560 else /* internal variable */
11561 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011562 char_u *tofree;
11563 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011565 /* get_name_len() takes care of expanding curly braces */
11566 name = p;
11567 len = get_name_len(&p, &tofree, TRUE, FALSE);
11568 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011570 if (tofree != NULL)
11571 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011572 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011573 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011575 /* handle d.key, l[idx], f(expr) */
11576 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11577 if (n)
11578 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579 }
11580 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011581 if (*p != NUL)
11582 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011584 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 }
11586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588}
11589
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011590#ifdef FEAT_FLOAT
11591/*
11592 * "exp()" function
11593 */
11594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011595f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011596{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011597 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011598
11599 rettv->v_type = VAR_FLOAT;
11600 if (get_float_arg(argvars, &f) == OK)
11601 rettv->vval.v_float = exp(f);
11602 else
11603 rettv->vval.v_float = 0.0;
11604}
11605#endif
11606
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607/*
11608 * "expand()" function
11609 */
11610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011611f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612{
11613 char_u *s;
11614 int len;
11615 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011616 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011618 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011619 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011622 if (argvars[1].v_type != VAR_UNKNOWN
11623 && argvars[2].v_type != VAR_UNKNOWN
11624 && get_tv_number_chk(&argvars[2], &error)
11625 && !error)
11626 {
11627 rettv->v_type = VAR_LIST;
11628 rettv->vval.v_list = NULL;
11629 }
11630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 if (*s == '%' || *s == '#' || *s == '<')
11633 {
11634 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011635 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011637 if (rettv->v_type == VAR_LIST)
11638 {
11639 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11640 list_append_string(rettv->vval.v_list, result, -1);
11641 else
11642 vim_free(result);
11643 }
11644 else
11645 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 }
11647 else
11648 {
11649 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011650 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 if (argvars[1].v_type != VAR_UNKNOWN
11652 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011653 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011654 if (!error)
11655 {
11656 ExpandInit(&xpc);
11657 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011658 if (p_wic)
11659 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011660 if (rettv->v_type == VAR_STRING)
11661 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11662 options, WILD_ALL);
11663 else if (rettv_list_alloc(rettv) != FAIL)
11664 {
11665 int i;
11666
11667 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11668 for (i = 0; i < xpc.xp_numfiles; i++)
11669 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11670 ExpandCleanup(&xpc);
11671 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 }
11673 else
11674 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 }
11676}
11677
11678/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011679 * Go over all entries in "d2" and add them to "d1".
11680 * When "action" is "error" then a duplicate key is an error.
11681 * When "action" is "force" then a duplicate key is overwritten.
11682 * Otherwise duplicate keys are ignored ("action" is "keep").
11683 */
11684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011685dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011686{
11687 dictitem_T *di1;
11688 hashitem_T *hi2;
11689 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011690 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011691
11692 todo = (int)d2->dv_hashtab.ht_used;
11693 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11694 {
11695 if (!HASHITEM_EMPTY(hi2))
11696 {
11697 --todo;
11698 di1 = dict_find(d1, hi2->hi_key, -1);
11699 if (d1->dv_scope != 0)
11700 {
11701 /* Disallow replacing a builtin function in l: and g:.
11702 * Check the key to be valid when adding to any
11703 * scope. */
11704 if (d1->dv_scope == VAR_DEF_SCOPE
11705 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11706 && var_check_func_name(hi2->hi_key,
11707 di1 == NULL))
11708 break;
11709 if (!valid_varname(hi2->hi_key))
11710 break;
11711 }
11712 if (di1 == NULL)
11713 {
11714 di1 = dictitem_copy(HI2DI(hi2));
11715 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11716 dictitem_free(di1);
11717 }
11718 else if (*action == 'e')
11719 {
11720 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11721 break;
11722 }
11723 else if (*action == 'f' && HI2DI(hi2) != di1)
11724 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011725 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11726 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011727 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011728 clear_tv(&di1->di_tv);
11729 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11730 }
11731 }
11732 }
11733}
11734
11735/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011736 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011737 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011738 */
11739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011740f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011741{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011742 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011743
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011745 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011746 list_T *l1, *l2;
11747 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011749 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011750
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 l1 = argvars[0].vval.v_list;
11752 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011753 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011754 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011755 {
11756 if (argvars[2].v_type != VAR_UNKNOWN)
11757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011758 before = get_tv_number_chk(&argvars[2], &error);
11759 if (error)
11760 return; /* type error; errmsg already given */
11761
Bram Moolenaar758711c2005-02-02 23:11:38 +000011762 if (before == l1->lv_len)
11763 item = NULL;
11764 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011765 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011766 item = list_find(l1, before);
11767 if (item == NULL)
11768 {
11769 EMSGN(_(e_listidx), before);
11770 return;
11771 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011772 }
11773 }
11774 else
11775 item = NULL;
11776 list_extend(l1, l2, item);
11777
Bram Moolenaare9a41262005-01-15 22:18:47 +000011778 copy_tv(&argvars[0], rettv);
11779 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011780 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011781 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11782 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011783 dict_T *d1, *d2;
11784 char_u *action;
11785 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011786
11787 d1 = argvars[0].vval.v_dict;
11788 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011789 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011790 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011791 {
11792 /* Check the third argument. */
11793 if (argvars[2].v_type != VAR_UNKNOWN)
11794 {
11795 static char *(av[]) = {"keep", "force", "error"};
11796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011797 action = get_tv_string_chk(&argvars[2]);
11798 if (action == NULL)
11799 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011800 for (i = 0; i < 3; ++i)
11801 if (STRCMP(action, av[i]) == 0)
11802 break;
11803 if (i == 3)
11804 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011805 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011806 return;
11807 }
11808 }
11809 else
11810 action = (char_u *)"force";
11811
Bram Moolenaara9922d62013-05-30 13:01:18 +020011812 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011813
Bram Moolenaare9a41262005-01-15 22:18:47 +000011814 copy_tv(&argvars[0], rettv);
11815 }
11816 }
11817 else
11818 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011819}
11820
11821/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011822 * "feedkeys()" function
11823 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011825f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011826{
11827 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011828 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011829 char_u *keys, *flags;
11830 char_u nbuf[NUMBUFLEN];
11831 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011832 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011833 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011834
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011835 /* This is not allowed in the sandbox. If the commands would still be
11836 * executed in the sandbox it would be OK, but it probably happens later,
11837 * when "sandbox" is no longer set. */
11838 if (check_secure())
11839 return;
11840
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011841 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011842
11843 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011844 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011845 flags = get_tv_string_buf(&argvars[1], nbuf);
11846 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011847 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011848 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011849 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011850 case 'n': remap = FALSE; break;
11851 case 'm': remap = TRUE; break;
11852 case 't': typed = TRUE; break;
11853 case 'i': insert = TRUE; break;
11854 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011855 }
11856 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011857 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011858
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011859 if (*keys != NUL || execute)
11860 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011861 /* Need to escape K_SPECIAL and CSI before putting the string in the
11862 * typeahead buffer. */
11863 keys_esc = vim_strsave_escape_csi(keys);
11864 if (keys_esc != NULL)
11865 {
11866 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011867 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011868 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011869 if (vgetc_busy)
11870 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011871 if (execute)
11872 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011873 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011874 }
11875}
11876
11877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878 * "filereadable()" function
11879 */
11880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011881f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011883 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 char_u *p;
11885 int n;
11886
Bram Moolenaarc236c162008-07-13 17:41:49 +000011887#ifndef O_NONBLOCK
11888# define O_NONBLOCK 0
11889#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011891 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11892 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 {
11894 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011895 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 }
11897 else
11898 n = FALSE;
11899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901}
11902
11903/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011904 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 * rights to write into.
11906 */
11907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011908f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011910 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911}
11912
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011913 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011914findfilendir(
11915 typval_T *argvars UNUSED,
11916 typval_T *rettv,
11917 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011918{
11919#ifdef FEAT_SEARCHPATH
11920 char_u *fname;
11921 char_u *fresult = NULL;
11922 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11923 char_u *p;
11924 char_u pathbuf[NUMBUFLEN];
11925 int count = 1;
11926 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011927 int error = FALSE;
11928#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011929
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011930 rettv->vval.v_string = NULL;
11931 rettv->v_type = VAR_STRING;
11932
11933#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011934 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011935
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011936 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011938 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11939 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011940 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011941 else
11942 {
11943 if (*p != NUL)
11944 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011946 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011947 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011948 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011949 }
11950
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011951 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11952 error = TRUE;
11953
11954 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011956 do
11957 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011958 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011959 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011960 fresult = find_file_in_path_option(first ? fname : NULL,
11961 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011962 0, first, path,
11963 find_what,
11964 curbuf->b_ffname,
11965 find_what == FINDFILE_DIR
11966 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011967 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011968
11969 if (fresult != NULL && rettv->v_type == VAR_LIST)
11970 list_append_string(rettv->vval.v_list, fresult, -1);
11971
11972 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011973 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011974
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011975 if (rettv->v_type == VAR_STRING)
11976 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011977#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011978}
11979
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011980static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11981static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011982
11983/*
11984 * Implementation of map() and filter().
11985 */
11986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011987filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011988{
11989 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011990 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011991 listitem_T *li, *nli;
11992 list_T *l = NULL;
11993 dictitem_T *di;
11994 hashtab_T *ht;
11995 hashitem_T *hi;
11996 dict_T *d = NULL;
11997 typval_T save_val;
11998 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011999 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012000 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012001 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020012002 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012003 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012004 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012005 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012006
Bram Moolenaare9a41262005-01-15 22:18:47 +000012007 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012008 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012009 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012010 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012011 return;
12012 }
12013 else if (argvars[0].v_type == VAR_DICT)
12014 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012015 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012016 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012017 return;
12018 }
12019 else
12020 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000012021 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012022 return;
12023 }
12024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012025 expr = get_tv_string_buf_chk(&argvars[1], buf);
12026 /* On type errors, the preceding call has already displayed an error
12027 * message. Avoid a misleading error message for an empty string that
12028 * was not passed as argument. */
12029 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012030 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 prepare_vimvar(VV_VAL, &save_val);
12032 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012033
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012034 /* We reset "did_emsg" to be able to detect whether an error
12035 * occurred during evaluation of the expression. */
12036 save_did_emsg = did_emsg;
12037 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012038
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012039 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012040 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012042 vimvars[VV_KEY].vv_type = VAR_STRING;
12043
12044 ht = &d->dv_hashtab;
12045 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012046 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012047 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012049 if (!HASHITEM_EMPTY(hi))
12050 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012051 int r;
12052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012053 --todo;
12054 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012055 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020012056 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12057 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 break;
12059 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012060 r = filter_map_one(&di->di_tv, expr, map, &rem);
12061 clear_tv(&vimvars[VV_KEY].vv_tv);
12062 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012063 break;
12064 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012065 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012066 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12067 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012068 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012069 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012070 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012071 }
12072 }
12073 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012074 }
12075 else
12076 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012077 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012079 for (li = l->lv_first; li != NULL; li = nli)
12080 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012081 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012082 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012083 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012084 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012085 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012086 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012087 break;
12088 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012089 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012090 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012091 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012092 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012093
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012094 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012095 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012096
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012097 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012098 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012099
12100 copy_tv(&argvars[0], rettv);
12101}
12102
12103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010012104filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012105{
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012107 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012108 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012109
Bram Moolenaar33570922005-01-25 22:26:29 +000012110 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012111 s = expr;
12112 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012113 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012114 if (*s != NUL) /* check for trailing chars after expr */
12115 {
12116 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012117 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012118 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012119 }
12120 if (map)
12121 {
12122 /* map(): replace the list item value */
12123 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012124 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012125 *tv = rettv;
12126 }
12127 else
12128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012129 int error = FALSE;
12130
Bram Moolenaare9a41262005-01-15 22:18:47 +000012131 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012132 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012133 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012134 /* On type error, nothing has been removed; return FAIL to stop the
12135 * loop. The error message was given by get_tv_number_chk(). */
12136 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012137 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012138 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012139 retval = OK;
12140theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012141 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012142 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012143}
12144
12145/*
12146 * "filter()" function
12147 */
12148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012149f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012150{
12151 filter_map(argvars, rettv, FALSE);
12152}
12153
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012154/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012155 * "finddir({fname}[, {path}[, {count}]])" function
12156 */
12157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012158f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012159{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012160 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161}
12162
12163/*
12164 * "findfile({fname}[, {path}[, {count}]])" function
12165 */
12166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012167f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012168{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012169 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012170}
12171
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012172#ifdef FEAT_FLOAT
12173/*
12174 * "float2nr({float})" function
12175 */
12176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012177f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012178{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012179 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012180
12181 if (get_float_arg(argvars, &f) == OK)
12182 {
12183 if (f < -0x7fffffff)
12184 rettv->vval.v_number = -0x7fffffff;
12185 else if (f > 0x7fffffff)
12186 rettv->vval.v_number = 0x7fffffff;
12187 else
12188 rettv->vval.v_number = (varnumber_T)f;
12189 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012190}
12191
12192/*
12193 * "floor({float})" function
12194 */
12195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012196f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012197{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012198 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012199
12200 rettv->v_type = VAR_FLOAT;
12201 if (get_float_arg(argvars, &f) == OK)
12202 rettv->vval.v_float = floor(f);
12203 else
12204 rettv->vval.v_float = 0.0;
12205}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012206
12207/*
12208 * "fmod()" function
12209 */
12210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012211f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012212{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012213 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012214
12215 rettv->v_type = VAR_FLOAT;
12216 if (get_float_arg(argvars, &fx) == OK
12217 && get_float_arg(&argvars[1], &fy) == OK)
12218 rettv->vval.v_float = fmod(fx, fy);
12219 else
12220 rettv->vval.v_float = 0.0;
12221}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012222#endif
12223
Bram Moolenaar0d660222005-01-07 21:51:51 +000012224/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012225 * "fnameescape({string})" function
12226 */
12227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012228f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012229{
12230 rettv->vval.v_string = vim_strsave_fnameescape(
12231 get_tv_string(&argvars[0]), FALSE);
12232 rettv->v_type = VAR_STRING;
12233}
12234
12235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 * "fnamemodify({fname}, {mods})" function
12237 */
12238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012239f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240{
12241 char_u *fname;
12242 char_u *mods;
12243 int usedlen = 0;
12244 int len;
12245 char_u *fbuf = NULL;
12246 char_u buf[NUMBUFLEN];
12247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012248 fname = get_tv_string_chk(&argvars[0]);
12249 mods = get_tv_string_buf_chk(&argvars[1], buf);
12250 if (fname == NULL || mods == NULL)
12251 fname = NULL;
12252 else
12253 {
12254 len = (int)STRLEN(fname);
12255 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012262 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 vim_free(fbuf);
12264}
12265
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012266static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267
12268/*
12269 * "foldclosed()" function
12270 */
12271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012272foldclosed_both(
12273 typval_T *argvars UNUSED,
12274 typval_T *rettv,
12275 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276{
12277#ifdef FEAT_FOLDING
12278 linenr_T lnum;
12279 linenr_T first, last;
12280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012281 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12283 {
12284 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12285 {
12286 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012287 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012289 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 return;
12291 }
12292 }
12293#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295}
12296
12297/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012298 * "foldclosed()" function
12299 */
12300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012301f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012302{
12303 foldclosed_both(argvars, rettv, FALSE);
12304}
12305
12306/*
12307 * "foldclosedend()" function
12308 */
12309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012310f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012311{
12312 foldclosed_both(argvars, rettv, TRUE);
12313}
12314
12315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 * "foldlevel()" function
12317 */
12318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012319f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320{
12321#ifdef FEAT_FOLDING
12322 linenr_T lnum;
12323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328}
12329
12330/*
12331 * "foldtext()" function
12332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012334f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335{
12336#ifdef FEAT_FOLDING
12337 linenr_T lnum;
12338 char_u *s;
12339 char_u *r;
12340 int len;
12341 char *txt;
12342#endif
12343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344 rettv->v_type = VAR_STRING;
12345 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012347 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12348 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12349 <= curbuf->b_ml.ml_line_count
12350 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 {
12352 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012353 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12354 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 {
12356 if (!linewhite(lnum))
12357 break;
12358 ++lnum;
12359 }
12360
12361 /* Find interesting text in this line. */
12362 s = skipwhite(ml_get(lnum));
12363 /* skip C comment-start */
12364 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012367 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012368 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012369 {
12370 s = skipwhite(ml_get(lnum + 1));
12371 if (*s == '*')
12372 s = skipwhite(s + 1);
12373 }
12374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 txt = _("+-%s%3ld lines: ");
12376 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012377 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 + 20 /* for %3ld */
12379 + STRLEN(s))); /* concatenated */
12380 if (r != NULL)
12381 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012382 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12383 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12384 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 len = (int)STRLEN(r);
12386 STRCAT(r, s);
12387 /* remove 'foldmarker' and 'commentstring' */
12388 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 }
12391 }
12392#endif
12393}
12394
12395/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012396 * "foldtextresult(lnum)" function
12397 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012399f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012400{
12401#ifdef FEAT_FOLDING
12402 linenr_T lnum;
12403 char_u *text;
12404 char_u buf[51];
12405 foldinfo_T foldinfo;
12406 int fold_count;
12407#endif
12408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409 rettv->v_type = VAR_STRING;
12410 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012411#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012412 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012413 /* treat illegal types and illegal string values for {lnum} the same */
12414 if (lnum < 0)
12415 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012416 fold_count = foldedCount(curwin, lnum, &foldinfo);
12417 if (fold_count > 0)
12418 {
12419 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12420 &foldinfo, buf);
12421 if (text == buf)
12422 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012424 }
12425#endif
12426}
12427
12428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 * "foreground()" function
12430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012432f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434#ifdef FEAT_GUI
12435 if (gui.in_use)
12436 gui_mch_set_foreground();
12437#else
12438# ifdef WIN32
12439 win32_set_foreground();
12440# endif
12441#endif
12442}
12443
12444/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012445 * "function()" function
12446 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012448f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012449{
12450 char_u *s;
12451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012452 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012453 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012454 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012455 /* Don't check an autoload name for existence here. */
12456 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012457 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012458 else
12459 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012460 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012461 {
12462 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012463 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012464
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012465 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12466 * also be called from another script. Using trans_function_name()
12467 * would also work, but some plugins depend on the name being
12468 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012469 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012470 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012471 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012472 if (rettv->vval.v_string != NULL)
12473 {
12474 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012475 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012476 }
12477 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012478 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012479 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012481 }
12482}
12483
12484/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012485 * "garbagecollect()" function
12486 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012488f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012489{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012490 /* This is postponed until we are back at the toplevel, because we may be
12491 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12492 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012493
12494 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12495 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012496}
12497
12498/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499 * "get()" function
12500 */
12501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012502f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012503{
Bram Moolenaar33570922005-01-25 22:26:29 +000012504 listitem_T *li;
12505 list_T *l;
12506 dictitem_T *di;
12507 dict_T *d;
12508 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012509
Bram Moolenaare9a41262005-01-15 22:18:47 +000012510 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012512 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012514 int error = FALSE;
12515
12516 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12517 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012518 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012519 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012520 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012521 else if (argvars[0].v_type == VAR_DICT)
12522 {
12523 if ((d = argvars[0].vval.v_dict) != NULL)
12524 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012525 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012526 if (di != NULL)
12527 tv = &di->di_tv;
12528 }
12529 }
12530 else
12531 EMSG2(_(e_listdictarg), "get()");
12532
12533 if (tv == NULL)
12534 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012535 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012536 copy_tv(&argvars[2], rettv);
12537 }
12538 else
12539 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540}
12541
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012542static 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 +000012543
12544/*
12545 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012546 * Return a range (from start to end) of lines in rettv from the specified
12547 * buffer.
12548 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012549 */
12550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012551get_buffer_lines(
12552 buf_T *buf,
12553 linenr_T start,
12554 linenr_T end,
12555 int retlist,
12556 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012557{
12558 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012559
Bram Moolenaar959a1432013-12-14 12:17:38 +010012560 rettv->v_type = VAR_STRING;
12561 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012562 if (retlist && rettv_list_alloc(rettv) == FAIL)
12563 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012564
12565 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12566 return;
12567
12568 if (!retlist)
12569 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012570 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12571 p = ml_get_buf(buf, start, FALSE);
12572 else
12573 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012574 rettv->vval.v_string = vim_strsave(p);
12575 }
12576 else
12577 {
12578 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012579 return;
12580
12581 if (start < 1)
12582 start = 1;
12583 if (end > buf->b_ml.ml_line_count)
12584 end = buf->b_ml.ml_line_count;
12585 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012586 if (list_append_string(rettv->vval.v_list,
12587 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012588 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012589 }
12590}
12591
12592/*
12593 * "getbufline()" function
12594 */
12595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012596f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012597{
12598 linenr_T lnum;
12599 linenr_T end;
12600 buf_T *buf;
12601
12602 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12603 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012604 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012605 --emsg_off;
12606
Bram Moolenaar661b1822005-07-28 22:36:45 +000012607 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012608 if (argvars[2].v_type == VAR_UNKNOWN)
12609 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012610 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012611 end = get_tv_lnum_buf(&argvars[2], buf);
12612
Bram Moolenaar342337a2005-07-21 21:11:17 +000012613 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012614}
12615
Bram Moolenaar0d660222005-01-07 21:51:51 +000012616/*
12617 * "getbufvar()" function
12618 */
12619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012620f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012621{
12622 buf_T *buf;
12623 buf_T *save_curbuf;
12624 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012625 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012626 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012627
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012628 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12629 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012630 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012631 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012632
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012633 rettv->v_type = VAR_STRING;
12634 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012635
12636 if (buf != NULL && varname != NULL)
12637 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012638 /* set curbuf to be our buf, temporarily */
12639 save_curbuf = curbuf;
12640 curbuf = buf;
12641
Bram Moolenaar0d660222005-01-07 21:51:51 +000012642 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012643 {
12644 if (get_option_tv(&varname, rettv, TRUE) == OK)
12645 done = TRUE;
12646 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012647 else if (STRCMP(varname, "changedtick") == 0)
12648 {
12649 rettv->v_type = VAR_NUMBER;
12650 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012651 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012652 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012653 else
12654 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012655 /* Look up the variable. */
12656 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12657 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12658 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012659 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012660 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012661 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012662 done = TRUE;
12663 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012664 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012665
12666 /* restore previous notion of curbuf */
12667 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012668 }
12669
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012670 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12671 /* use the default value */
12672 copy_tv(&argvars[2], rettv);
12673
Bram Moolenaar0d660222005-01-07 21:51:51 +000012674 --emsg_off;
12675}
12676
12677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 * "getchar()" function
12679 */
12680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012681f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682{
12683 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012684 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012686 /* Position the cursor. Needed after a message that ends in a space. */
12687 windgoto(msg_row, msg_col);
12688
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 ++no_mapping;
12690 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012691 for (;;)
12692 {
12693 if (argvars[0].v_type == VAR_UNKNOWN)
12694 /* getchar(): blocking wait. */
12695 n = safe_vgetc();
12696 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12697 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012698 n = vpeekc_any();
12699 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012700 /* illegal argument or getchar(0) and no char avail: return zero */
12701 n = 0;
12702 else
12703 /* getchar(0) and char avail: return char */
12704 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012705
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012706 if (n == K_IGNORE)
12707 continue;
12708 break;
12709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710 --no_mapping;
12711 --allow_keys;
12712
Bram Moolenaar219b8702006-11-01 14:32:36 +000012713 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12714 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12715 vimvars[VV_MOUSE_COL].vv_nr = 0;
12716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 if (IS_SPECIAL(n) || mod_mask != 0)
12719 {
12720 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12721 int i = 0;
12722
12723 /* Turn a special key into three bytes, plus modifier. */
12724 if (mod_mask != 0)
12725 {
12726 temp[i++] = K_SPECIAL;
12727 temp[i++] = KS_MODIFIER;
12728 temp[i++] = mod_mask;
12729 }
12730 if (IS_SPECIAL(n))
12731 {
12732 temp[i++] = K_SPECIAL;
12733 temp[i++] = K_SECOND(n);
12734 temp[i++] = K_THIRD(n);
12735 }
12736#ifdef FEAT_MBYTE
12737 else if (has_mbyte)
12738 i += (*mb_char2bytes)(n, temp + i);
12739#endif
12740 else
12741 temp[i++] = n;
12742 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 rettv->v_type = VAR_STRING;
12744 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012745
12746#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012747 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012748 {
12749 int row = mouse_row;
12750 int col = mouse_col;
12751 win_T *win;
12752 linenr_T lnum;
12753# ifdef FEAT_WINDOWS
12754 win_T *wp;
12755# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012756 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012757
12758 if (row >= 0 && col >= 0)
12759 {
12760 /* Find the window at the mouse coordinates and compute the
12761 * text position. */
12762 win = mouse_find_win(&row, &col);
12763 (void)mouse_comp_pos(win, &row, &col, &lnum);
12764# ifdef FEAT_WINDOWS
12765 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012766 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012767# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012768 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012769 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12770 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12771 }
12772 }
12773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 }
12775}
12776
12777/*
12778 * "getcharmod()" function
12779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012781f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012783 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784}
12785
12786/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012787 * "getcharsearch()" function
12788 */
12789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012790f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012791{
12792 if (rettv_dict_alloc(rettv) != FAIL)
12793 {
12794 dict_T *dict = rettv->vval.v_dict;
12795
12796 dict_add_nr_str(dict, "char", 0L, last_csearch());
12797 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12798 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12799 }
12800}
12801
12802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 * "getcmdline()" function
12804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012806f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 rettv->v_type = VAR_STRING;
12809 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810}
12811
12812/*
12813 * "getcmdpos()" function
12814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012816f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819}
12820
12821/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012822 * "getcmdtype()" function
12823 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012825f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012826{
12827 rettv->v_type = VAR_STRING;
12828 rettv->vval.v_string = alloc(2);
12829 if (rettv->vval.v_string != NULL)
12830 {
12831 rettv->vval.v_string[0] = get_cmdline_type();
12832 rettv->vval.v_string[1] = NUL;
12833 }
12834}
12835
12836/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012837 * "getcmdwintype()" function
12838 */
12839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012840f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012841{
12842 rettv->v_type = VAR_STRING;
12843 rettv->vval.v_string = NULL;
12844#ifdef FEAT_CMDWIN
12845 rettv->vval.v_string = alloc(2);
12846 if (rettv->vval.v_string != NULL)
12847 {
12848 rettv->vval.v_string[0] = cmdwin_type;
12849 rettv->vval.v_string[1] = NUL;
12850 }
12851#endif
12852}
12853
12854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 * "getcwd()" function
12856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012858f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012860 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012861 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012864 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012865
12866 wp = find_tabwin(&argvars[0], &argvars[1]);
12867 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012869 if (wp->w_localdir != NULL)
12870 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12871 else if(globaldir != NULL)
12872 rettv->vval.v_string = vim_strsave(globaldir);
12873 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012874 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012875 cwd = alloc(MAXPATHL);
12876 if (cwd != NULL)
12877 {
12878 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12879 rettv->vval.v_string = vim_strsave(cwd);
12880 vim_free(cwd);
12881 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012882 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012883#ifdef BACKSLASH_IN_FILENAME
12884 if (rettv->vval.v_string != NULL)
12885 slash_adjust(rettv->vval.v_string);
12886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 }
12888}
12889
12890/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012891 * "getfontname()" function
12892 */
12893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012894f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012895{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896 rettv->v_type = VAR_STRING;
12897 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012898#ifdef FEAT_GUI
12899 if (gui.in_use)
12900 {
12901 GuiFont font;
12902 char_u *name = NULL;
12903
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012904 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012905 {
12906 /* Get the "Normal" font. Either the name saved by
12907 * hl_set_font_name() or from the font ID. */
12908 font = gui.norm_font;
12909 name = hl_get_font_name();
12910 }
12911 else
12912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012914 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12915 return;
12916 font = gui_mch_get_font(name, FALSE);
12917 if (font == NOFONT)
12918 return; /* Invalid font name, return empty string. */
12919 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012921 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012922 gui_mch_free_font(font);
12923 }
12924#endif
12925}
12926
12927/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012928 * "getfperm({fname})" function
12929 */
12930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012931f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012932{
12933 char_u *fname;
12934 struct stat st;
12935 char_u *perm = NULL;
12936 char_u flags[] = "rwx";
12937 int i;
12938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012942 if (mch_stat((char *)fname, &st) >= 0)
12943 {
12944 perm = vim_strsave((char_u *)"---------");
12945 if (perm != NULL)
12946 {
12947 for (i = 0; i < 9; i++)
12948 {
12949 if (st.st_mode & (1 << (8 - i)))
12950 perm[i] = flags[i % 3];
12951 }
12952 }
12953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012955}
12956
12957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 * "getfsize({fname})" function
12959 */
12960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012961f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962{
12963 char_u *fname;
12964 struct stat st;
12965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012966 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012968 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969
12970 if (mch_stat((char *)fname, &st) >= 0)
12971 {
12972 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012973 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012975 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012976 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012977
12978 /* non-perfect check for overflow */
12979 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12980 rettv->vval.v_number = -2;
12981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982 }
12983 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985}
12986
12987/*
12988 * "getftime({fname})" function
12989 */
12990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012991f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992{
12993 char_u *fname;
12994 struct stat st;
12995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012996 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997
12998 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002}
13003
13004/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013005 * "getftype({fname})" function
13006 */
13007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013008f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013009{
13010 char_u *fname;
13011 struct stat st;
13012 char_u *type = NULL;
13013 char *t;
13014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013015 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013017 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013018 if (mch_lstat((char *)fname, &st) >= 0)
13019 {
13020#ifdef S_ISREG
13021 if (S_ISREG(st.st_mode))
13022 t = "file";
13023 else if (S_ISDIR(st.st_mode))
13024 t = "dir";
13025# ifdef S_ISLNK
13026 else if (S_ISLNK(st.st_mode))
13027 t = "link";
13028# endif
13029# ifdef S_ISBLK
13030 else if (S_ISBLK(st.st_mode))
13031 t = "bdev";
13032# endif
13033# ifdef S_ISCHR
13034 else if (S_ISCHR(st.st_mode))
13035 t = "cdev";
13036# endif
13037# ifdef S_ISFIFO
13038 else if (S_ISFIFO(st.st_mode))
13039 t = "fifo";
13040# endif
13041# ifdef S_ISSOCK
13042 else if (S_ISSOCK(st.st_mode))
13043 t = "fifo";
13044# endif
13045 else
13046 t = "other";
13047#else
13048# ifdef S_IFMT
13049 switch (st.st_mode & S_IFMT)
13050 {
13051 case S_IFREG: t = "file"; break;
13052 case S_IFDIR: t = "dir"; break;
13053# ifdef S_IFLNK
13054 case S_IFLNK: t = "link"; break;
13055# endif
13056# ifdef S_IFBLK
13057 case S_IFBLK: t = "bdev"; break;
13058# endif
13059# ifdef S_IFCHR
13060 case S_IFCHR: t = "cdev"; break;
13061# endif
13062# ifdef S_IFIFO
13063 case S_IFIFO: t = "fifo"; break;
13064# endif
13065# ifdef S_IFSOCK
13066 case S_IFSOCK: t = "socket"; break;
13067# endif
13068 default: t = "other";
13069 }
13070# else
13071 if (mch_isdir(fname))
13072 t = "dir";
13073 else
13074 t = "file";
13075# endif
13076#endif
13077 type = vim_strsave((char_u *)t);
13078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013079 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013080}
13081
13082/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013083 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013084 */
13085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013086f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013087{
13088 linenr_T lnum;
13089 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013090 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013091
13092 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013093 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013094 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013095 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013096 retlist = FALSE;
13097 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013098 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013099 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013100 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013101 retlist = TRUE;
13102 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013103
Bram Moolenaar342337a2005-07-21 21:11:17 +000013104 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013105}
13106
13107/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013108 * "getmatches()" function
13109 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013111f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013112{
13113#ifdef FEAT_SEARCH_EXTRA
13114 dict_T *dict;
13115 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013116 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013117
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013118 if (rettv_list_alloc(rettv) == OK)
13119 {
13120 while (cur != NULL)
13121 {
13122 dict = dict_alloc();
13123 if (dict == NULL)
13124 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013125 if (cur->match.regprog == NULL)
13126 {
13127 /* match added with matchaddpos() */
13128 for (i = 0; i < MAXPOSMATCH; ++i)
13129 {
13130 llpos_T *llpos;
13131 char buf[6];
13132 list_T *l;
13133
13134 llpos = &cur->pos.pos[i];
13135 if (llpos->lnum == 0)
13136 break;
13137 l = list_alloc();
13138 if (l == NULL)
13139 break;
13140 list_append_number(l, (varnumber_T)llpos->lnum);
13141 if (llpos->col > 0)
13142 {
13143 list_append_number(l, (varnumber_T)llpos->col);
13144 list_append_number(l, (varnumber_T)llpos->len);
13145 }
13146 sprintf(buf, "pos%d", i + 1);
13147 dict_add_list(dict, buf, l);
13148 }
13149 }
13150 else
13151 {
13152 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13153 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013154 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013155 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13156 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013157# ifdef FEAT_CONCEAL
13158 if (cur->conceal_char)
13159 {
13160 char_u buf[MB_MAXBYTES + 1];
13161
13162 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13163 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13164 }
13165# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013166 list_append_dict(rettv->vval.v_list, dict);
13167 cur = cur->next;
13168 }
13169 }
13170#endif
13171}
13172
13173/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013174 * "getpid()" function
13175 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013177f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013178{
13179 rettv->vval.v_number = mch_get_pid();
13180}
13181
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013182static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013183
13184/*
13185 * "getcurpos()" function
13186 */
13187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013188f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013189{
13190 getpos_both(argvars, rettv, TRUE);
13191}
13192
Bram Moolenaar18081e32008-02-20 19:11:07 +000013193/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013194 * "getpos(string)" function
13195 */
13196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013197f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013198{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013199 getpos_both(argvars, rettv, FALSE);
13200}
13201
13202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013203getpos_both(
13204 typval_T *argvars,
13205 typval_T *rettv,
13206 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013207{
Bram Moolenaara5525202006-03-02 22:52:09 +000013208 pos_T *fp;
13209 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013210 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013211
13212 if (rettv_list_alloc(rettv) == OK)
13213 {
13214 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013215 if (getcurpos)
13216 fp = &curwin->w_cursor;
13217 else
13218 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013219 if (fnum != -1)
13220 list_append_number(l, (varnumber_T)fnum);
13221 else
13222 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013223 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13224 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013225 list_append_number(l, (fp != NULL)
13226 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013227 : (varnumber_T)0);
13228 list_append_number(l,
13229#ifdef FEAT_VIRTUALEDIT
13230 (fp != NULL) ? (varnumber_T)fp->coladd :
13231#endif
13232 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013233 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013234 {
13235 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013236 list_append_number(l, curwin->w_curswant == MAXCOL ?
13237 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013238 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013239 }
13240 else
13241 rettv->vval.v_number = FALSE;
13242}
13243
13244/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013245 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013246 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013248f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013249{
13250#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013251 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013252#endif
13253
Bram Moolenaar2641f772005-03-25 21:58:17 +000013254#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013255 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013256 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013257 wp = NULL;
13258 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13259 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013260 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013261 if (wp == NULL)
13262 return;
13263 }
13264
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013265 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013266 }
13267#endif
13268}
13269
13270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 * "getreg()" function
13272 */
13273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013274f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275{
13276 char_u *strregname;
13277 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013278 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013279 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013280 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013282 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013284 strregname = get_tv_string_chk(&argvars[0]);
13285 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013286 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013288 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013289 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13290 return_list = get_tv_number_chk(&argvars[2], &error);
13291 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013294 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013295
13296 if (error)
13297 return;
13298
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299 regname = (strregname == NULL ? '"' : *strregname);
13300 if (regname == 0)
13301 regname = '"';
13302
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013303 if (return_list)
13304 {
13305 rettv->v_type = VAR_LIST;
13306 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13307 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013308 if (rettv->vval.v_list != NULL)
13309 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013310 }
13311 else
13312 {
13313 rettv->v_type = VAR_STRING;
13314 rettv->vval.v_string = get_reg_contents(regname,
13315 arg2 ? GREG_EXPR_SRC : 0);
13316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317}
13318
13319/*
13320 * "getregtype()" function
13321 */
13322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013323f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324{
13325 char_u *strregname;
13326 int regname;
13327 char_u buf[NUMBUFLEN + 2];
13328 long reglen = 0;
13329
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013330 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013331 {
13332 strregname = get_tv_string_chk(&argvars[0]);
13333 if (strregname == NULL) /* type error; errmsg already given */
13334 {
13335 rettv->v_type = VAR_STRING;
13336 rettv->vval.v_string = NULL;
13337 return;
13338 }
13339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 else
13341 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013342 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343
13344 regname = (strregname == NULL ? '"' : *strregname);
13345 if (regname == 0)
13346 regname = '"';
13347
13348 buf[0] = NUL;
13349 buf[1] = NUL;
13350 switch (get_reg_type(regname, &reglen))
13351 {
13352 case MLINE: buf[0] = 'V'; break;
13353 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354 case MBLOCK:
13355 buf[0] = Ctrl_V;
13356 sprintf((char *)buf + 1, "%ld", reglen + 1);
13357 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359 rettv->v_type = VAR_STRING;
13360 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361}
13362
13363/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013364 * "gettabvar()" function
13365 */
13366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013367f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013368{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013369 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013370 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013371 dictitem_T *v;
13372 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013373 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013374
13375 rettv->v_type = VAR_STRING;
13376 rettv->vval.v_string = NULL;
13377
13378 varname = get_tv_string_chk(&argvars[1]);
13379 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13380 if (tp != NULL && varname != NULL)
13381 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013382 /* Set tp to be our tabpage, temporarily. Also set the window to the
13383 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013384 if (switch_win(&oldcurwin, &oldtabpage,
13385 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013386 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013387 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013388 /* look up the variable */
13389 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13390 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13391 if (v != NULL)
13392 {
13393 copy_tv(&v->di_tv, rettv);
13394 done = TRUE;
13395 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013396 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013397
13398 /* restore previous notion of curwin */
13399 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013400 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013401
13402 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013403 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013404}
13405
13406/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013407 * "gettabwinvar()" function
13408 */
13409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013410f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013411{
13412 getwinvar(argvars, rettv, 1);
13413}
13414
13415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 * "getwinposx()" function
13417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013419f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013421 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422#ifdef FEAT_GUI
13423 if (gui.in_use)
13424 {
13425 int x, y;
13426
13427 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013428 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429 }
13430#endif
13431}
13432
13433/*
13434 * "getwinposy()" function
13435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013437f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440#ifdef FEAT_GUI
13441 if (gui.in_use)
13442 {
13443 int x, y;
13444
13445 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013446 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447 }
13448#endif
13449}
13450
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013451/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013452 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013453 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013454 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013455find_win_by_nr(
13456 typval_T *vp,
13457 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013458{
13459#ifdef FEAT_WINDOWS
13460 win_T *wp;
13461#endif
13462 int nr;
13463
13464 nr = get_tv_number_chk(vp, NULL);
13465
13466#ifdef FEAT_WINDOWS
13467 if (nr < 0)
13468 return NULL;
13469 if (nr == 0)
13470 return curwin;
13471
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013472 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13473 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013474 if (--nr <= 0)
13475 break;
13476 return wp;
13477#else
13478 if (nr == 0 || nr == 1)
13479 return curwin;
13480 return NULL;
13481#endif
13482}
13483
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013485 * Find window specified by "wvp" in tabpage "tvp".
13486 */
13487 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013488find_tabwin(
13489 typval_T *wvp, /* VAR_UNKNOWN for current window */
13490 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013491{
13492 win_T *wp = NULL;
13493 tabpage_T *tp = NULL;
13494 long n;
13495
13496 if (wvp->v_type != VAR_UNKNOWN)
13497 {
13498 if (tvp->v_type != VAR_UNKNOWN)
13499 {
13500 n = get_tv_number(tvp);
13501 if (n >= 0)
13502 tp = find_tabpage(n);
13503 }
13504 else
13505 tp = curtab;
13506
13507 if (tp != NULL)
13508 wp = find_win_by_nr(wvp, tp);
13509 }
13510 else
13511 wp = curwin;
13512
13513 return wp;
13514}
13515
13516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 * "getwinvar()" function
13518 */
13519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013520f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013522 getwinvar(argvars, rettv, 0);
13523}
13524
13525/*
13526 * getwinvar() and gettabwinvar()
13527 */
13528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013529getwinvar(
13530 typval_T *argvars,
13531 typval_T *rettv,
13532 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013533{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013534 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013536 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013537 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013538 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013539#ifdef FEAT_WINDOWS
13540 win_T *oldcurwin;
13541 tabpage_T *oldtabpage;
13542 int need_switch_win;
13543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013545#ifdef FEAT_WINDOWS
13546 if (off == 1)
13547 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13548 else
13549 tp = curtab;
13550#endif
13551 win = find_win_by_nr(&argvars[off], tp);
13552 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013553 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013555 rettv->v_type = VAR_STRING;
13556 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557
13558 if (win != NULL && varname != NULL)
13559 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013560#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013561 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013562 * otherwise the window is not valid. Only do this when needed,
13563 * autocommands get blocked. */
13564 need_switch_win = !(tp == curtab && win == curwin);
13565 if (!need_switch_win
13566 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13567#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013568 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013569 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013570 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013571 if (get_option_tv(&varname, rettv, 1) == OK)
13572 done = TRUE;
13573 }
13574 else
13575 {
13576 /* Look up the variable. */
13577 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13578 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13579 varname, FALSE);
13580 if (v != NULL)
13581 {
13582 copy_tv(&v->di_tv, rettv);
13583 done = TRUE;
13584 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013587
Bram Moolenaarba117c22015-09-29 16:53:22 +020013588#ifdef FEAT_WINDOWS
13589 if (need_switch_win)
13590 /* restore previous notion of curwin */
13591 restore_win(oldcurwin, oldtabpage, TRUE);
13592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 }
13594
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013595 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13596 /* use the default return value */
13597 copy_tv(&argvars[off + 2], rettv);
13598
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 --emsg_off;
13600}
13601
13602/*
13603 * "glob()" function
13604 */
13605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013606f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013608 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013610 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013612 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013613 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013615 if (argvars[1].v_type != VAR_UNKNOWN)
13616 {
13617 if (get_tv_number_chk(&argvars[1], &error))
13618 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013619 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013620 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013621 if (get_tv_number_chk(&argvars[2], &error))
13622 {
13623 rettv->v_type = VAR_LIST;
13624 rettv->vval.v_list = NULL;
13625 }
13626 if (argvars[3].v_type != VAR_UNKNOWN
13627 && get_tv_number_chk(&argvars[3], &error))
13628 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013629 }
13630 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013631 if (!error)
13632 {
13633 ExpandInit(&xpc);
13634 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013635 if (p_wic)
13636 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013637 if (rettv->v_type == VAR_STRING)
13638 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013639 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013640 else if (rettv_list_alloc(rettv) != FAIL)
13641 {
13642 int i;
13643
13644 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13645 NULL, options, WILD_ALL_KEEP);
13646 for (i = 0; i < xpc.xp_numfiles; i++)
13647 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13648
13649 ExpandCleanup(&xpc);
13650 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013651 }
13652 else
13653 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654}
13655
13656/*
13657 * "globpath()" function
13658 */
13659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013660f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013662 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013664 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013665 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013666 garray_T ga;
13667 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013669 /* When the optional second argument is non-zero, don't remove matches
13670 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013672 if (argvars[2].v_type != VAR_UNKNOWN)
13673 {
13674 if (get_tv_number_chk(&argvars[2], &error))
13675 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013676 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013677 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013678 if (get_tv_number_chk(&argvars[3], &error))
13679 {
13680 rettv->v_type = VAR_LIST;
13681 rettv->vval.v_list = NULL;
13682 }
13683 if (argvars[4].v_type != VAR_UNKNOWN
13684 && get_tv_number_chk(&argvars[4], &error))
13685 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013686 }
13687 }
13688 if (file != NULL && !error)
13689 {
13690 ga_init2(&ga, (int)sizeof(char_u *), 10);
13691 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13692 if (rettv->v_type == VAR_STRING)
13693 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13694 else if (rettv_list_alloc(rettv) != FAIL)
13695 for (i = 0; i < ga.ga_len; ++i)
13696 list_append_string(rettv->vval.v_list,
13697 ((char_u **)(ga.ga_data))[i], -1);
13698 ga_clear_strings(&ga);
13699 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013700 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013701 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702}
13703
13704/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013705 * "glob2regpat()" function
13706 */
13707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013708f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013709{
13710 char_u *pat = get_tv_string_chk(&argvars[0]);
13711
13712 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013713 rettv->vval.v_string = (pat == NULL)
13714 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013715}
13716
13717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 * "has()" function
13719 */
13720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013721f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722{
13723 int i;
13724 char_u *name;
13725 int n = FALSE;
13726 static char *(has_list[]) =
13727 {
13728#ifdef AMIGA
13729 "amiga",
13730# ifdef FEAT_ARP
13731 "arp",
13732# endif
13733#endif
13734#ifdef __BEOS__
13735 "beos",
13736#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013737#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738 "mac",
13739#endif
13740#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013741 "macunix", /* built with 'darwin' enabled */
13742#endif
13743#if defined(__APPLE__) && __APPLE__ == 1
13744 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746#ifdef __QNX__
13747 "qnx",
13748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#ifdef UNIX
13750 "unix",
13751#endif
13752#ifdef VMS
13753 "vms",
13754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#ifdef WIN32
13756 "win32",
13757#endif
13758#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13759 "win32unix",
13760#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013761#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 "win64",
13763#endif
13764#ifdef EBCDIC
13765 "ebcdic",
13766#endif
13767#ifndef CASE_INSENSITIVE_FILENAME
13768 "fname_case",
13769#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013770#ifdef HAVE_ACL
13771 "acl",
13772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773#ifdef FEAT_ARABIC
13774 "arabic",
13775#endif
13776#ifdef FEAT_AUTOCMD
13777 "autocmd",
13778#endif
13779#ifdef FEAT_BEVAL
13780 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013781# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13782 "balloon_multiline",
13783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784#endif
13785#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13786 "builtin_terms",
13787# ifdef ALL_BUILTIN_TCAPS
13788 "all_builtin_terms",
13789# endif
13790#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013791#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13792 || defined(FEAT_GUI_W32) \
13793 || defined(FEAT_GUI_MOTIF))
13794 "browsefilter",
13795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796#ifdef FEAT_BYTEOFF
13797 "byte_offset",
13798#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013799#ifdef FEAT_CHANNEL
13800 "channel",
13801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802#ifdef FEAT_CINDENT
13803 "cindent",
13804#endif
13805#ifdef FEAT_CLIENTSERVER
13806 "clientserver",
13807#endif
13808#ifdef FEAT_CLIPBOARD
13809 "clipboard",
13810#endif
13811#ifdef FEAT_CMDL_COMPL
13812 "cmdline_compl",
13813#endif
13814#ifdef FEAT_CMDHIST
13815 "cmdline_hist",
13816#endif
13817#ifdef FEAT_COMMENTS
13818 "comments",
13819#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013820#ifdef FEAT_CONCEAL
13821 "conceal",
13822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823#ifdef FEAT_CRYPT
13824 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013825 "crypt-blowfish",
13826 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827#endif
13828#ifdef FEAT_CSCOPE
13829 "cscope",
13830#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013831#ifdef FEAT_CURSORBIND
13832 "cursorbind",
13833#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013834#ifdef CURSOR_SHAPE
13835 "cursorshape",
13836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837#ifdef DEBUG
13838 "debug",
13839#endif
13840#ifdef FEAT_CON_DIALOG
13841 "dialog_con",
13842#endif
13843#ifdef FEAT_GUI_DIALOG
13844 "dialog_gui",
13845#endif
13846#ifdef FEAT_DIFF
13847 "diff",
13848#endif
13849#ifdef FEAT_DIGRAPHS
13850 "digraphs",
13851#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013852#ifdef FEAT_DIRECTX
13853 "directx",
13854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855#ifdef FEAT_DND
13856 "dnd",
13857#endif
13858#ifdef FEAT_EMACS_TAGS
13859 "emacs_tags",
13860#endif
13861 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013862 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863#ifdef FEAT_SEARCH_EXTRA
13864 "extra_search",
13865#endif
13866#ifdef FEAT_FKMAP
13867 "farsi",
13868#endif
13869#ifdef FEAT_SEARCHPATH
13870 "file_in_path",
13871#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013872#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013873 "filterpipe",
13874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875#ifdef FEAT_FIND_ID
13876 "find_in_path",
13877#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013878#ifdef FEAT_FLOAT
13879 "float",
13880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881#ifdef FEAT_FOLDING
13882 "folding",
13883#endif
13884#ifdef FEAT_FOOTER
13885 "footer",
13886#endif
13887#if !defined(USE_SYSTEM) && defined(UNIX)
13888 "fork",
13889#endif
13890#ifdef FEAT_GETTEXT
13891 "gettext",
13892#endif
13893#ifdef FEAT_GUI
13894 "gui",
13895#endif
13896#ifdef FEAT_GUI_ATHENA
13897# ifdef FEAT_GUI_NEXTAW
13898 "gui_neXtaw",
13899# else
13900 "gui_athena",
13901# endif
13902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903#ifdef FEAT_GUI_GTK
13904 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013905# ifdef USE_GTK3
13906 "gui_gtk3",
13907# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013911#ifdef FEAT_GUI_GNOME
13912 "gui_gnome",
13913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914#ifdef FEAT_GUI_MAC
13915 "gui_mac",
13916#endif
13917#ifdef FEAT_GUI_MOTIF
13918 "gui_motif",
13919#endif
13920#ifdef FEAT_GUI_PHOTON
13921 "gui_photon",
13922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923#ifdef FEAT_GUI_W32
13924 "gui_win32",
13925#endif
13926#ifdef FEAT_HANGULIN
13927 "hangul_input",
13928#endif
13929#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13930 "iconv",
13931#endif
13932#ifdef FEAT_INS_EXPAND
13933 "insert_expand",
13934#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013935#ifdef FEAT_JOB
13936 "job",
13937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938#ifdef FEAT_JUMPLIST
13939 "jumplist",
13940#endif
13941#ifdef FEAT_KEYMAP
13942 "keymap",
13943#endif
13944#ifdef FEAT_LANGMAP
13945 "langmap",
13946#endif
13947#ifdef FEAT_LIBCALL
13948 "libcall",
13949#endif
13950#ifdef FEAT_LINEBREAK
13951 "linebreak",
13952#endif
13953#ifdef FEAT_LISP
13954 "lispindent",
13955#endif
13956#ifdef FEAT_LISTCMDS
13957 "listcmds",
13958#endif
13959#ifdef FEAT_LOCALMAP
13960 "localmap",
13961#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013962#ifdef FEAT_LUA
13963# ifndef DYNAMIC_LUA
13964 "lua",
13965# endif
13966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967#ifdef FEAT_MENU
13968 "menu",
13969#endif
13970#ifdef FEAT_SESSION
13971 "mksession",
13972#endif
13973#ifdef FEAT_MODIFY_FNAME
13974 "modify_fname",
13975#endif
13976#ifdef FEAT_MOUSE
13977 "mouse",
13978#endif
13979#ifdef FEAT_MOUSESHAPE
13980 "mouseshape",
13981#endif
13982#if defined(UNIX) || defined(VMS)
13983# ifdef FEAT_MOUSE_DEC
13984 "mouse_dec",
13985# endif
13986# ifdef FEAT_MOUSE_GPM
13987 "mouse_gpm",
13988# endif
13989# ifdef FEAT_MOUSE_JSB
13990 "mouse_jsbterm",
13991# endif
13992# ifdef FEAT_MOUSE_NET
13993 "mouse_netterm",
13994# endif
13995# ifdef FEAT_MOUSE_PTERM
13996 "mouse_pterm",
13997# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013998# ifdef FEAT_MOUSE_SGR
13999 "mouse_sgr",
14000# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014001# ifdef FEAT_SYSMOUSE
14002 "mouse_sysmouse",
14003# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014004# ifdef FEAT_MOUSE_URXVT
14005 "mouse_urxvt",
14006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007# ifdef FEAT_MOUSE_XTERM
14008 "mouse_xterm",
14009# endif
14010#endif
14011#ifdef FEAT_MBYTE
14012 "multi_byte",
14013#endif
14014#ifdef FEAT_MBYTE_IME
14015 "multi_byte_ime",
14016#endif
14017#ifdef FEAT_MULTI_LANG
14018 "multi_lang",
14019#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014020#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014021#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014022 "mzscheme",
14023#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025#ifdef FEAT_OLE
14026 "ole",
14027#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014028 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029#ifdef FEAT_PATH_EXTRA
14030 "path_extra",
14031#endif
14032#ifdef FEAT_PERL
14033#ifndef DYNAMIC_PERL
14034 "perl",
14035#endif
14036#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014037#ifdef FEAT_PERSISTENT_UNDO
14038 "persistent_undo",
14039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040#ifdef FEAT_PYTHON
14041#ifndef DYNAMIC_PYTHON
14042 "python",
14043#endif
14044#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014045#ifdef FEAT_PYTHON3
14046#ifndef DYNAMIC_PYTHON3
14047 "python3",
14048#endif
14049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050#ifdef FEAT_POSTSCRIPT
14051 "postscript",
14052#endif
14053#ifdef FEAT_PRINTER
14054 "printer",
14055#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014056#ifdef FEAT_PROFILE
14057 "profile",
14058#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014059#ifdef FEAT_RELTIME
14060 "reltime",
14061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062#ifdef FEAT_QUICKFIX
14063 "quickfix",
14064#endif
14065#ifdef FEAT_RIGHTLEFT
14066 "rightleft",
14067#endif
14068#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14069 "ruby",
14070#endif
14071#ifdef FEAT_SCROLLBIND
14072 "scrollbind",
14073#endif
14074#ifdef FEAT_CMDL_INFO
14075 "showcmd",
14076 "cmdline_info",
14077#endif
14078#ifdef FEAT_SIGNS
14079 "signs",
14080#endif
14081#ifdef FEAT_SMARTINDENT
14082 "smartindent",
14083#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014084#ifdef STARTUPTIME
14085 "startuptime",
14086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087#ifdef FEAT_STL_OPT
14088 "statusline",
14089#endif
14090#ifdef FEAT_SUN_WORKSHOP
14091 "sun_workshop",
14092#endif
14093#ifdef FEAT_NETBEANS_INTG
14094 "netbeans_intg",
14095#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014096#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014097 "spell",
14098#endif
14099#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100 "syntax",
14101#endif
14102#if defined(USE_SYSTEM) || !defined(UNIX)
14103 "system",
14104#endif
14105#ifdef FEAT_TAG_BINS
14106 "tag_binary",
14107#endif
14108#ifdef FEAT_TAG_OLDSTATIC
14109 "tag_old_static",
14110#endif
14111#ifdef FEAT_TAG_ANYWHITE
14112 "tag_any_white",
14113#endif
14114#ifdef FEAT_TCL
14115# ifndef DYNAMIC_TCL
14116 "tcl",
14117# endif
14118#endif
14119#ifdef TERMINFO
14120 "terminfo",
14121#endif
14122#ifdef FEAT_TERMRESPONSE
14123 "termresponse",
14124#endif
14125#ifdef FEAT_TEXTOBJ
14126 "textobjects",
14127#endif
14128#ifdef HAVE_TGETENT
14129 "tgetent",
14130#endif
14131#ifdef FEAT_TITLE
14132 "title",
14133#endif
14134#ifdef FEAT_TOOLBAR
14135 "toolbar",
14136#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014137#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14138 "unnamedplus",
14139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140#ifdef FEAT_USR_CMDS
14141 "user-commands", /* was accidentally included in 5.4 */
14142 "user_commands",
14143#endif
14144#ifdef FEAT_VIMINFO
14145 "viminfo",
14146#endif
14147#ifdef FEAT_VERTSPLIT
14148 "vertsplit",
14149#endif
14150#ifdef FEAT_VIRTUALEDIT
14151 "virtualedit",
14152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154#ifdef FEAT_VISUALEXTRA
14155 "visualextra",
14156#endif
14157#ifdef FEAT_VREPLACE
14158 "vreplace",
14159#endif
14160#ifdef FEAT_WILDIGN
14161 "wildignore",
14162#endif
14163#ifdef FEAT_WILDMENU
14164 "wildmenu",
14165#endif
14166#ifdef FEAT_WINDOWS
14167 "windows",
14168#endif
14169#ifdef FEAT_WAK
14170 "winaltkeys",
14171#endif
14172#ifdef FEAT_WRITEBACKUP
14173 "writebackup",
14174#endif
14175#ifdef FEAT_XIM
14176 "xim",
14177#endif
14178#ifdef FEAT_XFONTSET
14179 "xfontset",
14180#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014181#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014182 "xpm",
14183 "xpm_w32", /* for backward compatibility */
14184#else
14185# if defined(HAVE_XPM)
14186 "xpm",
14187# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189#ifdef USE_XSMP
14190 "xsmp",
14191#endif
14192#ifdef USE_XSMP_INTERACT
14193 "xsmp_interact",
14194#endif
14195#ifdef FEAT_XCLIPBOARD
14196 "xterm_clipboard",
14197#endif
14198#ifdef FEAT_XTERM_SAVE
14199 "xterm_save",
14200#endif
14201#if defined(UNIX) && defined(FEAT_X11)
14202 "X11",
14203#endif
14204 NULL
14205 };
14206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014207 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 for (i = 0; has_list[i] != NULL; ++i)
14209 if (STRICMP(name, has_list[i]) == 0)
14210 {
14211 n = TRUE;
14212 break;
14213 }
14214
14215 if (n == FALSE)
14216 {
14217 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014218 {
14219 if (name[5] == '-'
14220 && STRLEN(name) > 11
14221 && vim_isdigit(name[6])
14222 && vim_isdigit(name[8])
14223 && vim_isdigit(name[10]))
14224 {
14225 int major = atoi((char *)name + 6);
14226 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014227
14228 /* Expect "patch-9.9.01234". */
14229 n = (major < VIM_VERSION_MAJOR
14230 || (major == VIM_VERSION_MAJOR
14231 && (minor < VIM_VERSION_MINOR
14232 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014233 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014234 }
14235 else
14236 n = has_patch(atoi((char *)name + 5));
14237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 else if (STRICMP(name, "vim_starting") == 0)
14239 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014240#ifdef FEAT_MBYTE
14241 else if (STRICMP(name, "multi_byte_encoding") == 0)
14242 n = has_mbyte;
14243#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014244#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14245 else if (STRICMP(name, "balloon_multiline") == 0)
14246 n = multiline_balloon_available();
14247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248#ifdef DYNAMIC_TCL
14249 else if (STRICMP(name, "tcl") == 0)
14250 n = tcl_enabled(FALSE);
14251#endif
14252#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14253 else if (STRICMP(name, "iconv") == 0)
14254 n = iconv_enabled(FALSE);
14255#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014256#ifdef DYNAMIC_LUA
14257 else if (STRICMP(name, "lua") == 0)
14258 n = lua_enabled(FALSE);
14259#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014260#ifdef DYNAMIC_MZSCHEME
14261 else if (STRICMP(name, "mzscheme") == 0)
14262 n = mzscheme_enabled(FALSE);
14263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264#ifdef DYNAMIC_RUBY
14265 else if (STRICMP(name, "ruby") == 0)
14266 n = ruby_enabled(FALSE);
14267#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014268#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269#ifdef DYNAMIC_PYTHON
14270 else if (STRICMP(name, "python") == 0)
14271 n = python_enabled(FALSE);
14272#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014273#endif
14274#ifdef FEAT_PYTHON3
14275#ifdef DYNAMIC_PYTHON3
14276 else if (STRICMP(name, "python3") == 0)
14277 n = python3_enabled(FALSE);
14278#endif
14279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280#ifdef DYNAMIC_PERL
14281 else if (STRICMP(name, "perl") == 0)
14282 n = perl_enabled(FALSE);
14283#endif
14284#ifdef FEAT_GUI
14285 else if (STRICMP(name, "gui_running") == 0)
14286 n = (gui.in_use || gui.starting);
14287# ifdef FEAT_GUI_W32
14288 else if (STRICMP(name, "gui_win32s") == 0)
14289 n = gui_is_win32s();
14290# endif
14291# ifdef FEAT_BROWSE
14292 else if (STRICMP(name, "browse") == 0)
14293 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14294# endif
14295#endif
14296#ifdef FEAT_SYN_HL
14297 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014298 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299#endif
14300#if defined(WIN3264)
14301 else if (STRICMP(name, "win95") == 0)
14302 n = mch_windows95();
14303#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014304#ifdef FEAT_NETBEANS_INTG
14305 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014306 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 }
14309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014310 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311}
14312
14313/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014314 * "has_key()" function
14315 */
14316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014317f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014318{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014319 if (argvars[0].v_type != VAR_DICT)
14320 {
14321 EMSG(_(e_dictreq));
14322 return;
14323 }
14324 if (argvars[0].vval.v_dict == NULL)
14325 return;
14326
14327 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014328 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014329}
14330
14331/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014332 * "haslocaldir()" function
14333 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014335f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014336{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014337 win_T *wp = NULL;
14338
14339 wp = find_tabwin(&argvars[0], &argvars[1]);
14340 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014341}
14342
14343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 * "hasmapto()" function
14345 */
14346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014347f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348{
14349 char_u *name;
14350 char_u *mode;
14351 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014352 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014354 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014355 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356 mode = (char_u *)"nvo";
14357 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014359 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014360 if (argvars[2].v_type != VAR_UNKNOWN)
14361 abbr = get_tv_number(&argvars[2]);
14362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363
Bram Moolenaar2c932302006-03-18 21:42:09 +000014364 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014367 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368}
14369
14370/*
14371 * "histadd()" function
14372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014374f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375{
14376#ifdef FEAT_CMDHIST
14377 int histype;
14378 char_u *str;
14379 char_u buf[NUMBUFLEN];
14380#endif
14381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014382 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 if (check_restricted() || check_secure())
14384 return;
14385#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14387 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 if (histype >= 0)
14389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 if (*str != NUL)
14392 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014393 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014395 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 return;
14397 }
14398 }
14399#endif
14400}
14401
14402/*
14403 * "histdel()" function
14404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014406f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407{
14408#ifdef FEAT_CMDHIST
14409 int n;
14410 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14414 if (str == NULL)
14415 n = 0;
14416 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014418 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014419 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014422 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 else
14424 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014425 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014426 get_tv_string_buf(&argvars[1], buf));
14427 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428#endif
14429}
14430
14431/*
14432 * "histget()" function
14433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014435f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436{
14437#ifdef FEAT_CMDHIST
14438 int type;
14439 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014440 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014442 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14443 if (str == NULL)
14444 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446 {
14447 type = get_histtype(str);
14448 if (argvars[1].v_type == VAR_UNKNOWN)
14449 idx = get_history_idx(type);
14450 else
14451 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14452 /* -1 on type error */
14453 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459}
14460
14461/*
14462 * "histnr()" function
14463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014465f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466{
14467 int i;
14468
14469#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014470 char_u *history = get_tv_string_chk(&argvars[0]);
14471
14472 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 if (i >= HIST_CMD && i < HIST_COUNT)
14474 i = get_history_idx(i);
14475 else
14476#endif
14477 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014478 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479}
14480
14481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 * "highlightID(name)" function
14483 */
14484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014485f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014487 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488}
14489
14490/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491 * "highlight_exists()" function
14492 */
14493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014494f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495{
14496 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14497}
14498
14499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 * "hostname()" function
14501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014503f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504{
14505 char_u hostname[256];
14506
14507 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->v_type = VAR_STRING;
14509 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510}
14511
14512/*
14513 * iconv() function
14514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014516f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517{
14518#ifdef FEAT_MBYTE
14519 char_u buf1[NUMBUFLEN];
14520 char_u buf2[NUMBUFLEN];
14521 char_u *from, *to, *str;
14522 vimconv_T vimconv;
14523#endif
14524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014525 rettv->v_type = VAR_STRING;
14526 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527
14528#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529 str = get_tv_string(&argvars[0]);
14530 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14531 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 vimconv.vc_type = CONV_NONE;
14533 convert_setup(&vimconv, from, to);
14534
14535 /* If the encodings are equal, no conversion needed. */
14536 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540
14541 convert_setup(&vimconv, NULL, NULL);
14542 vim_free(from);
14543 vim_free(to);
14544#endif
14545}
14546
14547/*
14548 * "indent()" function
14549 */
14550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014551f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552{
14553 linenr_T lnum;
14554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014555 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560}
14561
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014562/*
14563 * "index()" function
14564 */
14565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014566f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014567{
Bram Moolenaar33570922005-01-25 22:26:29 +000014568 list_T *l;
14569 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014570 long idx = 0;
14571 int ic = FALSE;
14572
14573 rettv->vval.v_number = -1;
14574 if (argvars[0].v_type != VAR_LIST)
14575 {
14576 EMSG(_(e_listreq));
14577 return;
14578 }
14579 l = argvars[0].vval.v_list;
14580 if (l != NULL)
14581 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014582 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014585 int error = FALSE;
14586
Bram Moolenaar758711c2005-02-02 23:11:38 +000014587 /* Start at specified item. Use the cached index that list_find()
14588 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014589 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014590 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014591 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014592 ic = get_tv_number_chk(&argvars[3], &error);
14593 if (error)
14594 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014595 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014596
Bram Moolenaar758711c2005-02-02 23:11:38 +000014597 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014598 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014599 {
14600 rettv->vval.v_number = idx;
14601 break;
14602 }
14603 }
14604}
14605
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606static int inputsecret_flag = 0;
14607
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014608static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014609
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014611 * This function is used by f_input() and f_inputdialog() functions. The third
14612 * argument to f_input() specifies the type of completion to use at the
14613 * prompt. The third argument to f_inputdialog() specifies the value to return
14614 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 */
14616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014617get_user_input(
14618 typval_T *argvars,
14619 typval_T *rettv,
14620 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014622 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623 char_u *p = NULL;
14624 int c;
14625 char_u buf[NUMBUFLEN];
14626 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014628 int xp_type = EXPAND_NOTHING;
14629 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014631 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014632 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633
14634#ifdef NO_CONSOLE_INPUT
14635 /* While starting up, there is no place to enter text. */
14636 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638#endif
14639
14640 cmd_silent = FALSE; /* Want to see the prompt. */
14641 if (prompt != NULL)
14642 {
14643 /* Only the part of the message after the last NL is considered as
14644 * prompt for the command line */
14645 p = vim_strrchr(prompt, '\n');
14646 if (p == NULL)
14647 p = prompt;
14648 else
14649 {
14650 ++p;
14651 c = *p;
14652 *p = NUL;
14653 msg_start();
14654 msg_clr_eos();
14655 msg_puts_attr(prompt, echo_attr);
14656 msg_didout = FALSE;
14657 msg_starthere();
14658 *p = c;
14659 }
14660 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 if (argvars[1].v_type != VAR_UNKNOWN)
14663 {
14664 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14665 if (defstr != NULL)
14666 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014668 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014669 {
14670 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014671 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014672 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014673
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014674 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014675 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014676
Bram Moolenaar4463f292005-09-25 22:20:24 +000014677 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14678 if (xp_name == NULL)
14679 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014680
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014681 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014682
Bram Moolenaar4463f292005-09-25 22:20:24 +000014683 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14684 &xp_arg) == FAIL)
14685 return;
14686 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014687 }
14688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014689 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014690 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014691 int save_ex_normal_busy = ex_normal_busy;
14692 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014693 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014694 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14695 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014696 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014697 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014698 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014699 && argvars[1].v_type != VAR_UNKNOWN
14700 && argvars[2].v_type != VAR_UNKNOWN)
14701 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14702 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014703
14704 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014706 /* since the user typed this, no need to wait for return */
14707 need_wait_return = FALSE;
14708 msg_didout = FALSE;
14709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710 cmd_silent = cmd_silent_save;
14711}
14712
14713/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014714 * "input()" function
14715 * Also handles inputsecret() when inputsecret is set.
14716 */
14717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014718f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014719{
14720 get_user_input(argvars, rettv, FALSE);
14721}
14722
14723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 * "inputdialog()" function
14725 */
14726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014727f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728{
14729#if defined(FEAT_GUI_TEXTDIALOG)
14730 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14731 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14732 {
14733 char_u *message;
14734 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014735 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014737 message = get_tv_string_chk(&argvars[0]);
14738 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014739 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014740 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 else
14742 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014743 if (message != NULL && defstr != NULL
14744 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014745 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 else
14748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014749 if (message != NULL && defstr != NULL
14750 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014751 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014752 rettv->vval.v_string = vim_strsave(
14753 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014757 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 }
14759 else
14760#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014761 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762}
14763
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014764/*
14765 * "inputlist()" function
14766 */
14767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014768f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014769{
14770 listitem_T *li;
14771 int selected;
14772 int mouse_used;
14773
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014774#ifdef NO_CONSOLE_INPUT
14775 /* While starting up, there is no place to enter text. */
14776 if (no_console_input())
14777 return;
14778#endif
14779 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14780 {
14781 EMSG2(_(e_listarg), "inputlist()");
14782 return;
14783 }
14784
14785 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014786 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014787 lines_left = Rows; /* avoid more prompt */
14788 msg_scroll = TRUE;
14789 msg_clr_eos();
14790
14791 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14792 {
14793 msg_puts(get_tv_string(&li->li_tv));
14794 msg_putchar('\n');
14795 }
14796
14797 /* Ask for choice. */
14798 selected = prompt_for_number(&mouse_used);
14799 if (mouse_used)
14800 selected -= lines_left;
14801
14802 rettv->vval.v_number = selected;
14803}
14804
14805
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14807
14808/*
14809 * "inputrestore()" function
14810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014812f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813{
14814 if (ga_userinput.ga_len > 0)
14815 {
14816 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14818 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014819 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 }
14821 else if (p_verbose > 1)
14822 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014823 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014824 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 }
14826}
14827
14828/*
14829 * "inputsave()" function
14830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014832f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014834 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 if (ga_grow(&ga_userinput, 1) == OK)
14836 {
14837 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14838 + ga_userinput.ga_len);
14839 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014840 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 }
14842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014843 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844}
14845
14846/*
14847 * "inputsecret()" function
14848 */
14849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014850f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851{
14852 ++cmdline_star;
14853 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014854 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855 --cmdline_star;
14856 --inputsecret_flag;
14857}
14858
14859/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014860 * "insert()" function
14861 */
14862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014863f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014864{
14865 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014866 listitem_T *item;
14867 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014868 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014869
14870 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014871 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014872 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014873 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014874 {
14875 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014876 before = get_tv_number_chk(&argvars[2], &error);
14877 if (error)
14878 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014879
Bram Moolenaar758711c2005-02-02 23:11:38 +000014880 if (before == l->lv_len)
14881 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014882 else
14883 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014884 item = list_find(l, before);
14885 if (item == NULL)
14886 {
14887 EMSGN(_(e_listidx), before);
14888 l = NULL;
14889 }
14890 }
14891 if (l != NULL)
14892 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014893 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014894 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014895 }
14896 }
14897}
14898
14899/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014900 * "invert(expr)" function
14901 */
14902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014903f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014904{
14905 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14906}
14907
14908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 * "isdirectory()" function
14910 */
14911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014912f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014914 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915}
14916
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014917/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014918 * "islocked()" function
14919 */
14920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014921f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014922{
14923 lval_T lv;
14924 char_u *end;
14925 dictitem_T *di;
14926
14927 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014928 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14929 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014930 if (end != NULL && lv.ll_name != NULL)
14931 {
14932 if (*end != NUL)
14933 EMSG(_(e_trailing));
14934 else
14935 {
14936 if (lv.ll_tv == NULL)
14937 {
14938 if (check_changedtick(lv.ll_name))
14939 rettv->vval.v_number = 1; /* always locked */
14940 else
14941 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014942 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014943 if (di != NULL)
14944 {
14945 /* Consider a variable locked when:
14946 * 1. the variable itself is locked
14947 * 2. the value of the variable is locked.
14948 * 3. the List or Dict value is locked.
14949 */
14950 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14951 || tv_islocked(&di->di_tv));
14952 }
14953 }
14954 }
14955 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014956 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014957 else if (lv.ll_newkey != NULL)
14958 EMSG2(_(e_dictkey), lv.ll_newkey);
14959 else if (lv.ll_list != NULL)
14960 /* List item. */
14961 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14962 else
14963 /* Dictionary item. */
14964 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14965 }
14966 }
14967
14968 clear_lval(&lv);
14969}
14970
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014971#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14972/*
14973 * "isnan()" function
14974 */
14975 static void
14976f_isnan(typval_T *argvars, typval_T *rettv)
14977{
14978 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14979 && isnan(argvars[0].vval.v_float);
14980}
14981#endif
14982
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014983static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014984
14985/*
14986 * Turn a dict into a list:
14987 * "what" == 0: list of keys
14988 * "what" == 1: list of values
14989 * "what" == 2: list of items
14990 */
14991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014992dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014993{
Bram Moolenaar33570922005-01-25 22:26:29 +000014994 list_T *l2;
14995 dictitem_T *di;
14996 hashitem_T *hi;
14997 listitem_T *li;
14998 listitem_T *li2;
14999 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015000 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015001
Bram Moolenaar8c711452005-01-14 21:53:12 +000015002 if (argvars[0].v_type != VAR_DICT)
15003 {
15004 EMSG(_(e_dictreq));
15005 return;
15006 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015007 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015008 return;
15009
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015010 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015011 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015012
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015013 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015014 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015016 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015017 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015018 --todo;
15019 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015020
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015021 li = listitem_alloc();
15022 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015023 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015024 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015025
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015026 if (what == 0)
15027 {
15028 /* keys() */
15029 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015030 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015031 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15032 }
15033 else if (what == 1)
15034 {
15035 /* values() */
15036 copy_tv(&di->di_tv, &li->li_tv);
15037 }
15038 else
15039 {
15040 /* items() */
15041 l2 = list_alloc();
15042 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015043 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015044 li->li_tv.vval.v_list = l2;
15045 if (l2 == NULL)
15046 break;
15047 ++l2->lv_refcount;
15048
15049 li2 = listitem_alloc();
15050 if (li2 == NULL)
15051 break;
15052 list_append(l2, li2);
15053 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015054 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015055 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15056
15057 li2 = listitem_alloc();
15058 if (li2 == NULL)
15059 break;
15060 list_append(l2, li2);
15061 copy_tv(&di->di_tv, &li2->li_tv);
15062 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015063 }
15064 }
15065}
15066
15067/*
15068 * "items(dict)" function
15069 */
15070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015071f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015072{
15073 dict_list(argvars, rettv, 2);
15074}
15075
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015076#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015077/*
15078 * Get the job from the argument.
15079 * Returns NULL if the job is invalid.
15080 */
15081 static job_T *
15082get_job_arg(typval_T *tv)
15083{
15084 job_T *job;
15085
15086 if (tv->v_type != VAR_JOB)
15087 {
15088 EMSG2(_(e_invarg2), get_tv_string(tv));
15089 return NULL;
15090 }
15091 job = tv->vval.v_job;
15092
15093 if (job == NULL)
15094 EMSG(_("E916: not a valid job"));
15095 return job;
15096}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015097
15098# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010015099/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015100 * "job_getchannel()" function
15101 */
15102 static void
15103f_job_getchannel(typval_T *argvars, typval_T *rettv)
15104{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015105 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015106
Bram Moolenaar65edff82016-02-21 16:40:11 +010015107 if (job != NULL)
15108 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015109 rettv->v_type = VAR_CHANNEL;
15110 rettv->vval.v_channel = job->jv_channel;
15111 if (job->jv_channel != NULL)
15112 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015113 }
15114}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015115# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015116
15117/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015118 * "job_setoptions()" function
15119 */
15120 static void
15121f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15122{
15123 job_T *job = get_job_arg(&argvars[0]);
15124 jobopt_T opt;
15125
15126 if (job == NULL)
15127 return;
15128 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015129 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015130 return;
15131 job_set_options(job, &opt);
15132}
15133
15134/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015135 * "job_start()" function
15136 */
15137 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015138f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015139{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015140 job_T *job;
15141 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015142#if defined(UNIX)
15143# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015144 char **argv = NULL;
15145 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015146#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015147 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015148#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015149 jobopt_T opt;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +010015150 int part;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015151
15152 rettv->v_type = VAR_JOB;
15153 job = job_alloc();
15154 rettv->vval.v_job = job;
15155 if (job == NULL)
15156 return;
15157
15158 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015159
15160 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015161 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015162 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015163 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015164 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015165 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015166 return;
Bram Moolenaar014069a2016-03-03 22:51:40 +010015167
Bram Moolenaarb69fccf2016-03-06 23:06:25 +010015168 /* Check that when io is "file" that there is a file name. */
15169 for (part = PART_OUT; part <= PART_IN; ++part)
15170 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
15171 && opt.jo_io[part] == JIO_FILE
15172 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
15173 || *opt.jo_io_name[part] == NUL))
15174 {
15175 EMSG(_("E920: -io file requires -name to be set"));
15176 return;
15177 }
15178
Bram Moolenaar014069a2016-03-03 22:51:40 +010015179 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
15180 {
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015181 buf_T *buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +010015182
15183 /* check that we can find the buffer before starting the job */
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015184 if (opt.jo_set & JO_IN_BUF)
Bram Moolenaar014069a2016-03-03 22:51:40 +010015185 {
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015186 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
15187 if (buf == NULL)
15188 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015189 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015190 else if (!(opt.jo_set & JO_IN_NAME))
15191 {
15192 EMSG(_("E915: in-io buffer requires in-buf or in-name to be set"));
15193 }
15194 else
15195 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015196 if (buf == NULL)
15197 return;
15198 if (buf->b_ml.ml_mfp == NULL)
15199 {
Bram Moolenaar846cdb22016-03-11 18:52:22 +010015200 char_u numbuf[NUMBUFLEN];
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015201 char_u *s;
15202
15203 if (opt.jo_set & JO_IN_BUF)
15204 {
Bram Moolenaar846cdb22016-03-11 18:52:22 +010015205 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
15206 s = numbuf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015207 }
15208 else
15209 s = opt.jo_io_name[PART_IN];
15210 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015211 return;
15212 }
15213 job->jv_in_buf = buf;
15214 }
15215
Bram Moolenaar65edff82016-02-21 16:40:11 +010015216 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015217
Bram Moolenaar835dc632016-02-07 14:27:38 +010015218#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015219 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015220#endif
15221
15222 if (argvars[0].v_type == VAR_STRING)
15223 {
15224 /* Command is a string. */
15225 cmd = argvars[0].vval.v_string;
15226#ifdef USE_ARGV
15227 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15228 return;
15229 argv[argc] = NULL;
15230#endif
15231 }
15232 else if (argvars[0].v_type != VAR_LIST
15233 || argvars[0].vval.v_list == NULL
15234 || argvars[0].vval.v_list->lv_len < 1)
15235 {
15236 EMSG(_(e_invarg));
15237 return;
15238 }
15239 else
15240 {
15241 list_T *l = argvars[0].vval.v_list;
15242 listitem_T *li;
15243 char_u *s;
15244
15245#ifdef USE_ARGV
15246 /* Pass argv[] to mch_call_shell(). */
15247 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15248 if (argv == NULL)
15249 return;
15250#endif
15251 for (li = l->lv_first; li != NULL; li = li->li_next)
15252 {
15253 s = get_tv_string_chk(&li->li_tv);
15254 if (s == NULL)
15255 goto theend;
15256#ifdef USE_ARGV
15257 argv[argc++] = (char *)s;
15258#else
Bram Moolenaara86f14a2016-02-29 21:05:48 +010015259 /* Only escape when needed, double quotes are not always allowed. */
15260 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015261 {
15262 s = vim_strsave_shellescape(s, FALSE, TRUE);
15263 if (s == NULL)
15264 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015265 ga_concat(&ga, s);
15266 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015267 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015268 else
15269 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015270 if (li->li_next != NULL)
15271 ga_append(&ga, ' ');
15272#endif
15273 }
15274#ifdef USE_ARGV
15275 argv[argc] = NULL;
15276#else
15277 cmd = ga.ga_data;
15278#endif
15279 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015280
Bram Moolenaar835dc632016-02-07 14:27:38 +010015281#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015282# ifdef FEAT_CHANNEL
15283 if (ch_log_active())
15284 {
15285 garray_T ga;
15286 int i;
15287
15288 ga_init2(&ga, (int)sizeof(char), 200);
15289 for (i = 0; i < argc; ++i)
15290 {
15291 if (i > 0)
15292 ga_concat(&ga, (char_u *)" ");
15293 ga_concat(&ga, (char_u *)argv[i]);
15294 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015295 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015296 ga_clear(&ga);
15297 }
15298# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015299 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015300#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015301# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015302 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015303# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015304 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015305#endif
15306
Bram Moolenaar014069a2016-03-03 22:51:40 +010015307#ifdef FEAT_CHANNEL
Bram Moolenaar839fd112016-03-06 21:34:03 +010015308 /* If the channel is reading from a buffer, write lines now. */
Bram Moolenaar4e329fc2016-03-07 15:24:03 +010015309 if (job->jv_channel != NULL)
15310 channel_write_in(job->jv_channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015311#endif
15312
Bram Moolenaar835dc632016-02-07 14:27:38 +010015313theend:
15314#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015315 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015316#else
15317 vim_free(ga.ga_data);
15318#endif
15319}
15320
15321/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015322 * Get the status of "job" and invoke the exit callback when needed.
15323 * The returned string is not allocated.
15324 */
15325 static char *
15326job_status(job_T *job)
15327{
15328 char *result;
15329
15330 if (job->jv_status == JOB_ENDED)
15331 /* No need to check, dead is dead. */
15332 result = "dead";
15333 else if (job->jv_status == JOB_FAILED)
15334 result = "fail";
15335 else
15336 {
15337 result = mch_job_status(job);
15338# ifdef FEAT_CHANNEL
15339 if (job->jv_status == JOB_ENDED)
15340 ch_log(job->jv_channel, "Job ended");
15341# endif
15342 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15343 {
15344 typval_T argv[3];
15345 typval_T rettv;
15346 int dummy;
15347
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015348 /* invoke the exit callback; make sure the refcount is > 0 */
15349 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015350 argv[0].v_type = VAR_JOB;
15351 argv[0].vval.v_job = job;
15352 argv[1].v_type = VAR_NUMBER;
15353 argv[1].vval.v_number = job->jv_exitval;
15354 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15355 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15356 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015357 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015358 }
15359 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15360 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015361 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015362 * freed. Careful: caller must not use "job" after this! */
15363 job_free(job);
15364 }
15365 }
15366 return result;
15367}
15368
15369/*
15370 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15371 */
15372 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015373job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015374{
15375 static time_t last_check = 0;
15376 time_t now;
15377 job_T *job;
15378 job_T *next;
15379
15380 /* Only do this once in 10 seconds. */
15381 now = time(NULL);
15382 if (last_check + 10 < now)
15383 {
15384 last_check = now;
15385 for (job = first_job; job != NULL; job = next)
15386 {
15387 next = job->jv_next;
15388 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15389 job_status(job); /* may free "job" */
15390 }
15391 }
15392}
15393
15394/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015395 * "job_status()" function
15396 */
15397 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015398f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015399{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015400 job_T *job = get_job_arg(&argvars[0]);
15401 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015402
Bram Moolenaar65edff82016-02-21 16:40:11 +010015403 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015404 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015405 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015406 rettv->v_type = VAR_STRING;
15407 rettv->vval.v_string = vim_strsave((char_u *)result);
15408 }
15409}
15410
15411/*
15412 * "job_stop()" function
15413 */
15414 static void
15415f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15416{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015417 job_T *job = get_job_arg(&argvars[0]);
15418
15419 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015420 {
15421 char_u *arg;
15422
15423 if (argvars[1].v_type == VAR_UNKNOWN)
15424 arg = (char_u *)"";
15425 else
15426 {
15427 arg = get_tv_string_chk(&argvars[1]);
15428 if (arg == NULL)
15429 {
15430 EMSG(_(e_invarg));
15431 return;
15432 }
15433 }
Bram Moolenaard6051b52016-02-28 15:49:03 +010015434# ifdef FEAT_CHANNEL
15435 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
15436# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +010015437 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015438 rettv->vval.v_number = 0;
15439 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015440 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015441 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015442 /* Assume that "hup" does not kill the job. */
15443 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15444 job->jv_channel->ch_job_killed = TRUE;
15445 }
15446 /* We don't try freeing the job, obviously the caller still has a
15447 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015448 }
15449}
15450#endif
15451
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015453 * "join()" function
15454 */
15455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015456f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015457{
15458 garray_T ga;
15459 char_u *sep;
15460
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015461 if (argvars[0].v_type != VAR_LIST)
15462 {
15463 EMSG(_(e_listreq));
15464 return;
15465 }
15466 if (argvars[0].vval.v_list == NULL)
15467 return;
15468 if (argvars[1].v_type == VAR_UNKNOWN)
15469 sep = (char_u *)" ";
15470 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015471 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015472
15473 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015474
15475 if (sep != NULL)
15476 {
15477 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015478 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015479 ga_append(&ga, NUL);
15480 rettv->vval.v_string = (char_u *)ga.ga_data;
15481 }
15482 else
15483 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015484}
15485
15486/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015487 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015488 */
15489 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015490f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015491{
15492 js_read_T reader;
15493
15494 reader.js_buf = get_tv_string(&argvars[0]);
15495 reader.js_fill = NULL;
15496 reader.js_used = 0;
15497 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15498 EMSG(_(e_invarg));
15499}
15500
15501/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015502 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015503 */
15504 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015505f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015506{
15507 rettv->v_type = VAR_STRING;
15508 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15509}
15510
15511/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015512 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015513 */
15514 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015515f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015516{
15517 js_read_T reader;
15518
15519 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015520 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015521 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015522 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015523 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015524}
15525
15526/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015527 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015528 */
15529 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015530f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015531{
15532 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015533 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015534}
15535
15536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015537 * "keys()" function
15538 */
15539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015540f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015541{
15542 dict_list(argvars, rettv, 0);
15543}
15544
15545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 * "last_buffer_nr()" function.
15547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015549f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550{
15551 int n = 0;
15552 buf_T *buf;
15553
15554 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15555 if (n < buf->b_fnum)
15556 n = buf->b_fnum;
15557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015559}
15560
15561/*
15562 * "len()" function
15563 */
15564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015565f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015566{
15567 switch (argvars[0].v_type)
15568 {
15569 case VAR_STRING:
15570 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015571 rettv->vval.v_number = (varnumber_T)STRLEN(
15572 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015573 break;
15574 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015575 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015576 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015577 case VAR_DICT:
15578 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15579 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015580 case VAR_UNKNOWN:
15581 case VAR_SPECIAL:
15582 case VAR_FLOAT:
15583 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015584 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015585 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015586 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015587 break;
15588 }
15589}
15590
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015591static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015592
15593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015594libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015595{
15596#ifdef FEAT_LIBCALL
15597 char_u *string_in;
15598 char_u **string_result;
15599 int nr_result;
15600#endif
15601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015602 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015603 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015604 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015605
15606 if (check_restricted() || check_secure())
15607 return;
15608
15609#ifdef FEAT_LIBCALL
15610 /* The first two args must be strings, otherwise its meaningless */
15611 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15612 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015613 string_in = NULL;
15614 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015615 string_in = argvars[2].vval.v_string;
15616 if (type == VAR_NUMBER)
15617 string_result = NULL;
15618 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015619 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015620 if (mch_libcall(argvars[0].vval.v_string,
15621 argvars[1].vval.v_string,
15622 string_in,
15623 argvars[2].vval.v_number,
15624 string_result,
15625 &nr_result) == OK
15626 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015627 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015628 }
15629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630}
15631
15632/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015633 * "libcall()" function
15634 */
15635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015636f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015637{
15638 libcall_common(argvars, rettv, VAR_STRING);
15639}
15640
15641/*
15642 * "libcallnr()" function
15643 */
15644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015645f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015646{
15647 libcall_common(argvars, rettv, VAR_NUMBER);
15648}
15649
15650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 * "line(string)" function
15652 */
15653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015654f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655{
15656 linenr_T lnum = 0;
15657 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015658 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015660 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 if (fp != NULL)
15662 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015663 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664}
15665
15666/*
15667 * "line2byte(lnum)" function
15668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015670f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671{
15672#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674#else
15675 linenr_T lnum;
15676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015677 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015681 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15682 if (rettv->vval.v_number >= 0)
15683 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684#endif
15685}
15686
15687/*
15688 * "lispindent(lnum)" function
15689 */
15690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015691f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692{
15693#ifdef FEAT_LISP
15694 pos_T pos;
15695 linenr_T lnum;
15696
15697 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015698 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15700 {
15701 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015702 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703 curwin->w_cursor = pos;
15704 }
15705 else
15706#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015707 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708}
15709
15710/*
15711 * "localtime()" function
15712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015714f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015716 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717}
15718
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015719static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720
15721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015722get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723{
15724 char_u *keys;
15725 char_u *which;
15726 char_u buf[NUMBUFLEN];
15727 char_u *keys_buf = NULL;
15728 char_u *rhs;
15729 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015730 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015731 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015732 mapblock_T *mp;
15733 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734
15735 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015736 rettv->v_type = VAR_STRING;
15737 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015739 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 if (*keys == NUL)
15741 return;
15742
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015743 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015745 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015746 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015747 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015748 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015749 if (argvars[3].v_type != VAR_UNKNOWN)
15750 get_dict = get_tv_number(&argvars[3]);
15751 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 else
15754 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015755 if (which == NULL)
15756 return;
15757
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 mode = get_map_mode(&which, 0);
15759
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015760 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015761 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015763
15764 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015766 /* Return a string. */
15767 if (rhs != NULL)
15768 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769
Bram Moolenaarbd743252010-10-20 21:23:33 +020015770 }
15771 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15772 {
15773 /* Return a dictionary. */
15774 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15775 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15776 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777
Bram Moolenaarbd743252010-10-20 21:23:33 +020015778 dict_add_nr_str(dict, "lhs", 0L, lhs);
15779 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15780 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15781 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15782 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15783 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15784 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015785 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015786 dict_add_nr_str(dict, "mode", 0L, mapmode);
15787
15788 vim_free(lhs);
15789 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 }
15791}
15792
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015793#ifdef FEAT_FLOAT
15794/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015795 * "log()" function
15796 */
15797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015798f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015799{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015800 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015801
15802 rettv->v_type = VAR_FLOAT;
15803 if (get_float_arg(argvars, &f) == OK)
15804 rettv->vval.v_float = log(f);
15805 else
15806 rettv->vval.v_float = 0.0;
15807}
15808
15809/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015810 * "log10()" function
15811 */
15812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015813f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015814{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015815 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015816
15817 rettv->v_type = VAR_FLOAT;
15818 if (get_float_arg(argvars, &f) == OK)
15819 rettv->vval.v_float = log10(f);
15820 else
15821 rettv->vval.v_float = 0.0;
15822}
15823#endif
15824
Bram Moolenaar1dced572012-04-05 16:54:08 +020015825#ifdef FEAT_LUA
15826/*
15827 * "luaeval()" function
15828 */
15829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015830f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015831{
15832 char_u *str;
15833 char_u buf[NUMBUFLEN];
15834
15835 str = get_tv_string_buf(&argvars[0], buf);
15836 do_luaeval(str, argvars + 1, rettv);
15837}
15838#endif
15839
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015841 * "map()" function
15842 */
15843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015844f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015845{
15846 filter_map(argvars, rettv, TRUE);
15847}
15848
15849/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015850 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 */
15852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015853f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015855 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856}
15857
15858/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015859 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860 */
15861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015862f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015864 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865}
15866
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015867static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868
15869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015870find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015872 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015873 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015874 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875 char_u *pat;
15876 regmatch_T regmatch;
15877 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015878 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879 char_u *save_cpo;
15880 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015881 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015882 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015883 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015884 list_T *l = NULL;
15885 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015886 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015887 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888
15889 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15890 save_cpo = p_cpo;
15891 p_cpo = (char_u *)"";
15892
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015893 rettv->vval.v_number = -1;
15894 if (type == 3)
15895 {
15896 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015897 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015898 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015899 }
15900 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015902 rettv->v_type = VAR_STRING;
15903 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015906 if (argvars[0].v_type == VAR_LIST)
15907 {
15908 if ((l = argvars[0].vval.v_list) == NULL)
15909 goto theend;
15910 li = l->lv_first;
15911 }
15912 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015913 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015914 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015915 len = (long)STRLEN(str);
15916 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015918 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15919 if (pat == NULL)
15920 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015921
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015922 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015924 int error = FALSE;
15925
15926 start = get_tv_number_chk(&argvars[2], &error);
15927 if (error)
15928 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015929 if (l != NULL)
15930 {
15931 li = list_find(l, start);
15932 if (li == NULL)
15933 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015934 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015935 }
15936 else
15937 {
15938 if (start < 0)
15939 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015940 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015941 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015942 /* When "count" argument is there ignore matches before "start",
15943 * otherwise skip part of the string. Differs when pattern is "^"
15944 * or "\<". */
15945 if (argvars[3].v_type != VAR_UNKNOWN)
15946 startcol = start;
15947 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015948 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015949 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015950 len -= start;
15951 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015952 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015953
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015954 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015955 nth = get_tv_number_chk(&argvars[3], &error);
15956 if (error)
15957 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958 }
15959
15960 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15961 if (regmatch.regprog != NULL)
15962 {
15963 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015964
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015965 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015966 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015967 if (l != NULL)
15968 {
15969 if (li == NULL)
15970 {
15971 match = FALSE;
15972 break;
15973 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015974 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015975 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015976 if (str == NULL)
15977 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015978 }
15979
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015980 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015982 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015983 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015984 if (l == NULL && !match)
15985 break;
15986
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015987 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015988 if (l != NULL)
15989 {
15990 li = li->li_next;
15991 ++idx;
15992 }
15993 else
15994 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015995#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015996 startcol = (colnr_T)(regmatch.startp[0]
15997 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015998#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015999 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016000#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016001 if (startcol > (colnr_T)len
16002 || str + startcol <= regmatch.startp[0])
16003 {
16004 match = FALSE;
16005 break;
16006 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016007 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016008 }
16009
16010 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016012 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016013 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016014 int i;
16015
16016 /* return list with matched string and submatches */
16017 for (i = 0; i < NSUBEXP; ++i)
16018 {
16019 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016020 {
16021 if (list_append_string(rettv->vval.v_list,
16022 (char_u *)"", 0) == FAIL)
16023 break;
16024 }
16025 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016026 regmatch.startp[i],
16027 (int)(regmatch.endp[i] - regmatch.startp[i]))
16028 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016029 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016030 }
16031 }
16032 else if (type == 2)
16033 {
16034 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016035 if (l != NULL)
16036 copy_tv(&li->li_tv, rettv);
16037 else
16038 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016040 }
16041 else if (l != NULL)
16042 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043 else
16044 {
16045 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016046 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 (varnumber_T)(regmatch.startp[0] - str);
16048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016049 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016051 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 }
16053 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016054 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 }
16056
16057theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016058 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 p_cpo = save_cpo;
16060}
16061
16062/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016063 * "match()" function
16064 */
16065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016066f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016067{
16068 find_some_match(argvars, rettv, 1);
16069}
16070
16071/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016072 * "matchadd()" function
16073 */
16074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016075f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016076{
16077#ifdef FEAT_SEARCH_EXTRA
16078 char_u buf[NUMBUFLEN];
16079 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16080 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16081 int prio = 10; /* default priority */
16082 int id = -1;
16083 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016084 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016085
16086 rettv->vval.v_number = -1;
16087
16088 if (grp == NULL || pat == NULL)
16089 return;
16090 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016091 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016092 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016093 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016094 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016095 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016096 if (argvars[4].v_type != VAR_UNKNOWN)
16097 {
16098 if (argvars[4].v_type != VAR_DICT)
16099 {
16100 EMSG(_(e_dictreq));
16101 return;
16102 }
16103 if (dict_find(argvars[4].vval.v_dict,
16104 (char_u *)"conceal", -1) != NULL)
16105 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16106 (char_u *)"conceal", FALSE);
16107 }
16108 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016109 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016110 if (error == TRUE)
16111 return;
16112 if (id >= 1 && id <= 3)
16113 {
16114 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16115 return;
16116 }
16117
Bram Moolenaar6561d522015-07-21 15:48:27 +020016118 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16119 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016120#endif
16121}
16122
16123/*
16124 * "matchaddpos()" function
16125 */
16126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016127f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016128{
16129#ifdef FEAT_SEARCH_EXTRA
16130 char_u buf[NUMBUFLEN];
16131 char_u *group;
16132 int prio = 10;
16133 int id = -1;
16134 int error = FALSE;
16135 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016136 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016137
16138 rettv->vval.v_number = -1;
16139
16140 group = get_tv_string_buf_chk(&argvars[0], buf);
16141 if (group == NULL)
16142 return;
16143
16144 if (argvars[1].v_type != VAR_LIST)
16145 {
16146 EMSG2(_(e_listarg), "matchaddpos()");
16147 return;
16148 }
16149 l = argvars[1].vval.v_list;
16150 if (l == NULL)
16151 return;
16152
16153 if (argvars[2].v_type != VAR_UNKNOWN)
16154 {
16155 prio = get_tv_number_chk(&argvars[2], &error);
16156 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016157 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016158 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016159 if (argvars[4].v_type != VAR_UNKNOWN)
16160 {
16161 if (argvars[4].v_type != VAR_DICT)
16162 {
16163 EMSG(_(e_dictreq));
16164 return;
16165 }
16166 if (dict_find(argvars[4].vval.v_dict,
16167 (char_u *)"conceal", -1) != NULL)
16168 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16169 (char_u *)"conceal", FALSE);
16170 }
16171 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016172 }
16173 if (error == TRUE)
16174 return;
16175
16176 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16177 if (id == 1 || id == 2)
16178 {
16179 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16180 return;
16181 }
16182
Bram Moolenaar6561d522015-07-21 15:48:27 +020016183 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16184 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016185#endif
16186}
16187
16188/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016189 * "matcharg()" function
16190 */
16191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016192f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016193{
16194 if (rettv_list_alloc(rettv) == OK)
16195 {
16196#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016197 int id = get_tv_number(&argvars[0]);
16198 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016199
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016200 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016201 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016202 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16203 {
16204 list_append_string(rettv->vval.v_list,
16205 syn_id2name(m->hlg_id), -1);
16206 list_append_string(rettv->vval.v_list, m->pattern, -1);
16207 }
16208 else
16209 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016210 list_append_string(rettv->vval.v_list, NULL, -1);
16211 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016212 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016213 }
16214#endif
16215 }
16216}
16217
16218/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016219 * "matchdelete()" function
16220 */
16221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016222f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016223{
16224#ifdef FEAT_SEARCH_EXTRA
16225 rettv->vval.v_number = match_delete(curwin,
16226 (int)get_tv_number(&argvars[0]), TRUE);
16227#endif
16228}
16229
16230/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016231 * "matchend()" function
16232 */
16233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016234f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016235{
16236 find_some_match(argvars, rettv, 0);
16237}
16238
16239/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016240 * "matchlist()" function
16241 */
16242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016243f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016244{
16245 find_some_match(argvars, rettv, 3);
16246}
16247
16248/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249 * "matchstr()" function
16250 */
16251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016252f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016253{
16254 find_some_match(argvars, rettv, 2);
16255}
16256
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016257static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016258
16259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016260max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016261{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016262 long n = 0;
16263 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016264 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016265
16266 if (argvars[0].v_type == VAR_LIST)
16267 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016268 list_T *l;
16269 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016270
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016271 l = argvars[0].vval.v_list;
16272 if (l != NULL)
16273 {
16274 li = l->lv_first;
16275 if (li != NULL)
16276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016277 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016278 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016279 {
16280 li = li->li_next;
16281 if (li == NULL)
16282 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016283 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016284 if (domax ? i > n : i < n)
16285 n = i;
16286 }
16287 }
16288 }
16289 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016290 else if (argvars[0].v_type == VAR_DICT)
16291 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016292 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016293 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016294 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016295 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016296
16297 d = argvars[0].vval.v_dict;
16298 if (d != NULL)
16299 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016300 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016301 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016303 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016305 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016306 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016307 if (first)
16308 {
16309 n = i;
16310 first = FALSE;
16311 }
16312 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016313 n = i;
16314 }
16315 }
16316 }
16317 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016318 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016319 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016320 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016321}
16322
16323/*
16324 * "max()" function
16325 */
16326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016327f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016328{
16329 max_min(argvars, rettv, TRUE);
16330}
16331
16332/*
16333 * "min()" function
16334 */
16335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016336f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016337{
16338 max_min(argvars, rettv, FALSE);
16339}
16340
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016341static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016342
16343/*
16344 * Create the directory in which "dir" is located, and higher levels when
16345 * needed.
16346 */
16347 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016348mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016349{
16350 char_u *p;
16351 char_u *updir;
16352 int r = FAIL;
16353
16354 /* Get end of directory name in "dir".
16355 * We're done when it's "/" or "c:/". */
16356 p = gettail_sep(dir);
16357 if (p <= get_past_head(dir))
16358 return OK;
16359
16360 /* If the directory exists we're done. Otherwise: create it.*/
16361 updir = vim_strnsave(dir, (int)(p - dir));
16362 if (updir == NULL)
16363 return FAIL;
16364 if (mch_isdir(updir))
16365 r = OK;
16366 else if (mkdir_recurse(updir, prot) == OK)
16367 r = vim_mkdir_emsg(updir, prot);
16368 vim_free(updir);
16369 return r;
16370}
16371
16372#ifdef vim_mkdir
16373/*
16374 * "mkdir()" function
16375 */
16376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016377f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016378{
16379 char_u *dir;
16380 char_u buf[NUMBUFLEN];
16381 int prot = 0755;
16382
16383 rettv->vval.v_number = FAIL;
16384 if (check_restricted() || check_secure())
16385 return;
16386
16387 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016388 if (*dir == NUL)
16389 rettv->vval.v_number = FAIL;
16390 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016391 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016392 if (*gettail(dir) == NUL)
16393 /* remove trailing slashes */
16394 *gettail_sep(dir) = NUL;
16395
16396 if (argvars[1].v_type != VAR_UNKNOWN)
16397 {
16398 if (argvars[2].v_type != VAR_UNKNOWN)
16399 prot = get_tv_number_chk(&argvars[2], NULL);
16400 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16401 mkdir_recurse(dir, prot);
16402 }
16403 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016404 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016405}
16406#endif
16407
Bram Moolenaar0d660222005-01-07 21:51:51 +000016408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 * "mode()" function
16410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016412f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016414 char_u buf[3];
16415
16416 buf[1] = NUL;
16417 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 if (VIsual_active)
16420 {
16421 if (VIsual_select)
16422 buf[0] = VIsual_mode + 's' - 'v';
16423 else
16424 buf[0] = VIsual_mode;
16425 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016426 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016427 || State == CONFIRM)
16428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016430 if (State == ASKMORE)
16431 buf[1] = 'm';
16432 else if (State == CONFIRM)
16433 buf[1] = '?';
16434 }
16435 else if (State == EXTERNCMD)
16436 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437 else if (State & INSERT)
16438 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016439#ifdef FEAT_VREPLACE
16440 if (State & VREPLACE_FLAG)
16441 {
16442 buf[0] = 'R';
16443 buf[1] = 'v';
16444 }
16445 else
16446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 if (State & REPLACE_FLAG)
16448 buf[0] = 'R';
16449 else
16450 buf[0] = 'i';
16451 }
16452 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016455 if (exmode_active)
16456 buf[1] = 'v';
16457 }
16458 else if (exmode_active)
16459 {
16460 buf[0] = 'c';
16461 buf[1] = 'e';
16462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016466 if (finish_op)
16467 buf[1] = 'o';
16468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016470 /* Clear out the minor mode when the argument is not a non-zero number or
16471 * non-empty string. */
16472 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016473 buf[1] = NUL;
16474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016475 rettv->vval.v_string = vim_strsave(buf);
16476 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477}
16478
Bram Moolenaar429fa852013-04-15 12:27:36 +020016479#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016480/*
16481 * "mzeval()" function
16482 */
16483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016484f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016485{
16486 char_u *str;
16487 char_u buf[NUMBUFLEN];
16488
16489 str = get_tv_string_buf(&argvars[0], buf);
16490 do_mzeval(str, rettv);
16491}
Bram Moolenaar75676462013-01-30 14:55:42 +010016492
16493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016494mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016495{
16496 typval_T argvars[3];
16497
16498 argvars[0].v_type = VAR_STRING;
16499 argvars[0].vval.v_string = name;
16500 copy_tv(args, &argvars[1]);
16501 argvars[2].v_type = VAR_UNKNOWN;
16502 f_call(argvars, rettv);
16503 clear_tv(&argvars[1]);
16504}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016505#endif
16506
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016508 * "nextnonblank()" function
16509 */
16510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016511f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016512{
16513 linenr_T lnum;
16514
16515 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016517 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016518 {
16519 lnum = 0;
16520 break;
16521 }
16522 if (*skipwhite(ml_get(lnum)) != NUL)
16523 break;
16524 }
16525 rettv->vval.v_number = lnum;
16526}
16527
16528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 * "nr2char()" function
16530 */
16531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016532f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533{
16534 char_u buf[NUMBUFLEN];
16535
16536#ifdef FEAT_MBYTE
16537 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016538 {
16539 int utf8 = 0;
16540
16541 if (argvars[1].v_type != VAR_UNKNOWN)
16542 utf8 = get_tv_number_chk(&argvars[1], NULL);
16543 if (utf8)
16544 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16545 else
16546 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548 else
16549#endif
16550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016551 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 buf[1] = NUL;
16553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016554 rettv->v_type = VAR_STRING;
16555 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016556}
16557
16558/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016559 * "or(expr, expr)" function
16560 */
16561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016562f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016563{
16564 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16565 | get_tv_number_chk(&argvars[1], NULL);
16566}
16567
16568/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016569 * "pathshorten()" function
16570 */
16571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016572f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016573{
16574 char_u *p;
16575
16576 rettv->v_type = VAR_STRING;
16577 p = get_tv_string_chk(&argvars[0]);
16578 if (p == NULL)
16579 rettv->vval.v_string = NULL;
16580 else
16581 {
16582 p = vim_strsave(p);
16583 rettv->vval.v_string = p;
16584 if (p != NULL)
16585 shorten_dir(p);
16586 }
16587}
16588
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016589#ifdef FEAT_PERL
16590/*
16591 * "perleval()" function
16592 */
16593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016594f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016595{
16596 char_u *str;
16597 char_u buf[NUMBUFLEN];
16598
16599 str = get_tv_string_buf(&argvars[0], buf);
16600 do_perleval(str, rettv);
16601}
16602#endif
16603
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016604#ifdef FEAT_FLOAT
16605/*
16606 * "pow()" function
16607 */
16608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016609f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016610{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016611 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016612
16613 rettv->v_type = VAR_FLOAT;
16614 if (get_float_arg(argvars, &fx) == OK
16615 && get_float_arg(&argvars[1], &fy) == OK)
16616 rettv->vval.v_float = pow(fx, fy);
16617 else
16618 rettv->vval.v_float = 0.0;
16619}
16620#endif
16621
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016622/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016623 * "prevnonblank()" function
16624 */
16625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016626f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016627{
16628 linenr_T lnum;
16629
16630 lnum = get_tv_lnum(argvars);
16631 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16632 lnum = 0;
16633 else
16634 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16635 --lnum;
16636 rettv->vval.v_number = lnum;
16637}
16638
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016639/* This dummy va_list is here because:
16640 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16641 * - locally in the function results in a "used before set" warning
16642 * - using va_start() to initialize it gives "function with fixed args" error */
16643static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016644
Bram Moolenaar8c711452005-01-14 21:53:12 +000016645/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016646 * "printf()" function
16647 */
16648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016649f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016650{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016651 char_u buf[NUMBUFLEN];
16652 int len;
16653 char_u *s;
16654 int saved_did_emsg = did_emsg;
16655 char *fmt;
16656
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016657 rettv->v_type = VAR_STRING;
16658 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016659
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016660 /* Get the required length, allocate the buffer and do it for real. */
16661 did_emsg = FALSE;
16662 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16663 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16664 if (!did_emsg)
16665 {
16666 s = alloc(len + 1);
16667 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016668 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016669 rettv->vval.v_string = s;
16670 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016671 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016672 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016673 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016674}
16675
16676/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016677 * "pumvisible()" function
16678 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016680f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016681{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016682#ifdef FEAT_INS_EXPAND
16683 if (pum_visible())
16684 rettv->vval.v_number = 1;
16685#endif
16686}
16687
Bram Moolenaardb913952012-06-29 12:54:53 +020016688#ifdef FEAT_PYTHON3
16689/*
16690 * "py3eval()" function
16691 */
16692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016693f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016694{
16695 char_u *str;
16696 char_u buf[NUMBUFLEN];
16697
16698 str = get_tv_string_buf(&argvars[0], buf);
16699 do_py3eval(str, rettv);
16700}
16701#endif
16702
16703#ifdef FEAT_PYTHON
16704/*
16705 * "pyeval()" function
16706 */
16707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016708f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016709{
16710 char_u *str;
16711 char_u buf[NUMBUFLEN];
16712
16713 str = get_tv_string_buf(&argvars[0], buf);
16714 do_pyeval(str, rettv);
16715}
16716#endif
16717
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016718/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016719 * "range()" function
16720 */
16721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016722f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016723{
16724 long start;
16725 long end;
16726 long stride = 1;
16727 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016728 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016730 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016731 if (argvars[1].v_type == VAR_UNKNOWN)
16732 {
16733 end = start - 1;
16734 start = 0;
16735 }
16736 else
16737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016738 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016739 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016740 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016741 }
16742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016743 if (error)
16744 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016745 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016746 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016747 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016748 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016749 else
16750 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016751 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016752 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016753 if (list_append_number(rettv->vval.v_list,
16754 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016755 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016756 }
16757}
16758
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016759/*
16760 * "readfile()" function
16761 */
16762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016763f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016764{
16765 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016766 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016767 char_u *fname;
16768 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016769 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16770 int io_size = sizeof(buf);
16771 int readlen; /* size of last fread() */
16772 char_u *prev = NULL; /* previously read bytes, if any */
16773 long prevlen = 0; /* length of data in prev */
16774 long prevsize = 0; /* size of prev buffer */
16775 long maxline = MAXLNUM;
16776 long cnt = 0;
16777 char_u *p; /* position in buf */
16778 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016779
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016780 if (argvars[1].v_type != VAR_UNKNOWN)
16781 {
16782 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16783 binary = TRUE;
16784 if (argvars[2].v_type != VAR_UNKNOWN)
16785 maxline = get_tv_number(&argvars[2]);
16786 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016787
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016788 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016789 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016790
16791 /* Always open the file in binary mode, library functions have a mind of
16792 * their own about CR-LF conversion. */
16793 fname = get_tv_string(&argvars[0]);
16794 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16795 {
16796 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16797 return;
16798 }
16799
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016800 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016801 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016802 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016803
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016804 /* This for loop processes what was read, but is also entered at end
16805 * of file so that either:
16806 * - an incomplete line gets written
16807 * - a "binary" file gets an empty line at the end if it ends in a
16808 * newline. */
16809 for (p = buf, start = buf;
16810 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16811 ++p)
16812 {
16813 if (*p == '\n' || readlen <= 0)
16814 {
16815 listitem_T *li;
16816 char_u *s = NULL;
16817 long_u len = p - start;
16818
16819 /* Finished a line. Remove CRs before NL. */
16820 if (readlen > 0 && !binary)
16821 {
16822 while (len > 0 && start[len - 1] == '\r')
16823 --len;
16824 /* removal may cross back to the "prev" string */
16825 if (len == 0)
16826 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16827 --prevlen;
16828 }
16829 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016830 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016831 else
16832 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016833 /* Change "prev" buffer to be the right size. This way
16834 * the bytes are only copied once, and very long lines are
16835 * allocated only once. */
16836 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016837 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016838 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016839 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016840 prev = NULL; /* the list will own the string */
16841 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016842 }
16843 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016844 if (s == NULL)
16845 {
16846 do_outofmem_msg((long_u) prevlen + len + 1);
16847 failed = TRUE;
16848 break;
16849 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016850
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016851 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016852 {
16853 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016854 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016855 break;
16856 }
16857 li->li_tv.v_type = VAR_STRING;
16858 li->li_tv.v_lock = 0;
16859 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016860 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016861
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016862 start = p + 1; /* step over newline */
16863 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016864 break;
16865 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016866 else if (*p == NUL)
16867 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016868#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016869 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16870 * when finding the BF and check the previous two bytes. */
16871 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016872 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016873 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16874 * + 1, these may be in the "prev" string. */
16875 char_u back1 = p >= buf + 1 ? p[-1]
16876 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16877 char_u back2 = p >= buf + 2 ? p[-2]
16878 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16879 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016880
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016881 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016882 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016883 char_u *dest = p - 2;
16884
16885 /* Usually a BOM is at the beginning of a file, and so at
16886 * the beginning of a line; then we can just step over it.
16887 */
16888 if (start == dest)
16889 start = p + 1;
16890 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016891 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016892 /* have to shuffle buf to close gap */
16893 int adjust_prevlen = 0;
16894
16895 if (dest < buf)
16896 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016897 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016898 dest = buf;
16899 }
16900 if (readlen > p - buf + 1)
16901 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16902 readlen -= 3 - adjust_prevlen;
16903 prevlen -= adjust_prevlen;
16904 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016905 }
16906 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016907 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016908#endif
16909 } /* for */
16910
16911 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16912 break;
16913 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016914 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016915 /* There's part of a line in buf, store it in "prev". */
16916 if (p - start + prevlen >= prevsize)
16917 {
16918 /* need bigger "prev" buffer */
16919 char_u *newprev;
16920
16921 /* A common use case is ordinary text files and "prev" gets a
16922 * fragment of a line, so the first allocation is made
16923 * small, to avoid repeatedly 'allocing' large and
16924 * 'reallocing' small. */
16925 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016926 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016927 else
16928 {
16929 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016930 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016931 prevsize = grow50pc > growmin ? grow50pc : growmin;
16932 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016933 newprev = prev == NULL ? alloc(prevsize)
16934 : vim_realloc(prev, prevsize);
16935 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016936 {
16937 do_outofmem_msg((long_u)prevsize);
16938 failed = TRUE;
16939 break;
16940 }
16941 prev = newprev;
16942 }
16943 /* Add the line part to end of "prev". */
16944 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016945 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016946 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016947 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016948
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016949 /*
16950 * For a negative line count use only the lines at the end of the file,
16951 * free the rest.
16952 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016953 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016954 while (cnt > -maxline)
16955 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016956 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016957 --cnt;
16958 }
16959
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016960 if (failed)
16961 {
16962 list_free(rettv->vval.v_list, TRUE);
16963 /* readfile doc says an empty list is returned on error */
16964 rettv->vval.v_list = list_alloc();
16965 }
16966
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016967 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016968 fclose(fd);
16969}
16970
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016971#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016972static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016973
16974/*
16975 * Convert a List to proftime_T.
16976 * Return FAIL when there is something wrong.
16977 */
16978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016979list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016980{
16981 long n1, n2;
16982 int error = FALSE;
16983
16984 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16985 || arg->vval.v_list->lv_len != 2)
16986 return FAIL;
16987 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16988 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16989# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016990 tm->HighPart = n1;
16991 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016992# else
16993 tm->tv_sec = n1;
16994 tm->tv_usec = n2;
16995# endif
16996 return error ? FAIL : OK;
16997}
16998#endif /* FEAT_RELTIME */
16999
17000/*
17001 * "reltime()" function
17002 */
17003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017004f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017005{
17006#ifdef FEAT_RELTIME
17007 proftime_T res;
17008 proftime_T start;
17009
17010 if (argvars[0].v_type == VAR_UNKNOWN)
17011 {
17012 /* No arguments: get current time. */
17013 profile_start(&res);
17014 }
17015 else if (argvars[1].v_type == VAR_UNKNOWN)
17016 {
17017 if (list2proftime(&argvars[0], &res) == FAIL)
17018 return;
17019 profile_end(&res);
17020 }
17021 else
17022 {
17023 /* Two arguments: compute the difference. */
17024 if (list2proftime(&argvars[0], &start) == FAIL
17025 || list2proftime(&argvars[1], &res) == FAIL)
17026 return;
17027 profile_sub(&res, &start);
17028 }
17029
17030 if (rettv_list_alloc(rettv) == OK)
17031 {
17032 long n1, n2;
17033
17034# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017035 n1 = res.HighPart;
17036 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017037# else
17038 n1 = res.tv_sec;
17039 n2 = res.tv_usec;
17040# endif
17041 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17042 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17043 }
17044#endif
17045}
17046
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017047#ifdef FEAT_FLOAT
17048/*
17049 * "reltimefloat()" function
17050 */
17051 static void
17052f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17053{
17054# ifdef FEAT_RELTIME
17055 proftime_T tm;
17056# endif
17057
17058 rettv->v_type = VAR_FLOAT;
17059 rettv->vval.v_float = 0;
17060# ifdef FEAT_RELTIME
17061 if (list2proftime(&argvars[0], &tm) == OK)
17062 rettv->vval.v_float = profile_float(&tm);
17063# endif
17064}
17065#endif
17066
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017067/*
17068 * "reltimestr()" function
17069 */
17070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017071f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017072{
17073#ifdef FEAT_RELTIME
17074 proftime_T tm;
17075#endif
17076
17077 rettv->v_type = VAR_STRING;
17078 rettv->vval.v_string = NULL;
17079#ifdef FEAT_RELTIME
17080 if (list2proftime(&argvars[0], &tm) == OK)
17081 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17082#endif
17083}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017084
Bram Moolenaar0d660222005-01-07 21:51:51 +000017085#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017086static void make_connection(void);
17087static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017088
17089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017090make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091{
17092 if (X_DISPLAY == NULL
17093# ifdef FEAT_GUI
17094 && !gui.in_use
17095# endif
17096 )
17097 {
17098 x_force_connect = TRUE;
17099 setup_term_clip();
17100 x_force_connect = FALSE;
17101 }
17102}
17103
17104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017105check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106{
17107 make_connection();
17108 if (X_DISPLAY == NULL)
17109 {
17110 EMSG(_("E240: No connection to Vim server"));
17111 return FAIL;
17112 }
17113 return OK;
17114}
17115#endif
17116
17117#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017118static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017119
17120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017121remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017122{
17123 char_u *server_name;
17124 char_u *keys;
17125 char_u *r = NULL;
17126 char_u buf[NUMBUFLEN];
17127# ifdef WIN32
17128 HWND w;
17129# else
17130 Window w;
17131# endif
17132
17133 if (check_restricted() || check_secure())
17134 return;
17135
17136# ifdef FEAT_X11
17137 if (check_connection() == FAIL)
17138 return;
17139# endif
17140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017141 server_name = get_tv_string_chk(&argvars[0]);
17142 if (server_name == NULL)
17143 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 keys = get_tv_string_buf(&argvars[1], buf);
17145# ifdef WIN32
17146 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17147# else
17148 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17149 < 0)
17150# endif
17151 {
17152 if (r != NULL)
17153 EMSG(r); /* sending worked but evaluation failed */
17154 else
17155 EMSG2(_("E241: Unable to send to %s"), server_name);
17156 return;
17157 }
17158
17159 rettv->vval.v_string = r;
17160
17161 if (argvars[2].v_type != VAR_UNKNOWN)
17162 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017163 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017164 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017165 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017167 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017168 v.di_tv.v_type = VAR_STRING;
17169 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017170 idvar = get_tv_string_chk(&argvars[2]);
17171 if (idvar != NULL)
17172 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017173 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017174 }
17175}
17176#endif
17177
17178/*
17179 * "remote_expr()" function
17180 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017182f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017183{
17184 rettv->v_type = VAR_STRING;
17185 rettv->vval.v_string = NULL;
17186#ifdef FEAT_CLIENTSERVER
17187 remote_common(argvars, rettv, TRUE);
17188#endif
17189}
17190
17191/*
17192 * "remote_foreground()" function
17193 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017195f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017196{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017197#ifdef FEAT_CLIENTSERVER
17198# ifdef WIN32
17199 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017200 {
17201 char_u *server_name = get_tv_string_chk(&argvars[0]);
17202
17203 if (server_name != NULL)
17204 serverForeground(server_name);
17205 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206# else
17207 /* Send a foreground() expression to the server. */
17208 argvars[1].v_type = VAR_STRING;
17209 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17210 argvars[2].v_type = VAR_UNKNOWN;
17211 remote_common(argvars, rettv, TRUE);
17212 vim_free(argvars[1].vval.v_string);
17213# endif
17214#endif
17215}
17216
Bram Moolenaar0d660222005-01-07 21:51:51 +000017217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017218f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017219{
17220#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017221 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017222 char_u *s = NULL;
17223# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017224 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017225# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017226 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227
17228 if (check_restricted() || check_secure())
17229 {
17230 rettv->vval.v_number = -1;
17231 return;
17232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017233 serverid = get_tv_string_chk(&argvars[0]);
17234 if (serverid == NULL)
17235 {
17236 rettv->vval.v_number = -1;
17237 return; /* type error; errmsg already given */
17238 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017239# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017240 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017241 if (n == 0)
17242 rettv->vval.v_number = -1;
17243 else
17244 {
17245 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17246 rettv->vval.v_number = (s != NULL);
17247 }
17248# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017249 if (check_connection() == FAIL)
17250 return;
17251
17252 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017253 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017254# endif
17255
17256 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017258 char_u *retvar;
17259
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 v.di_tv.v_type = VAR_STRING;
17261 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017262 retvar = get_tv_string_chk(&argvars[1]);
17263 if (retvar != NULL)
17264 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017265 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017266 }
17267#else
17268 rettv->vval.v_number = -1;
17269#endif
17270}
17271
Bram Moolenaar0d660222005-01-07 21:51:51 +000017272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017273f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017274{
17275 char_u *r = NULL;
17276
17277#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017278 char_u *serverid = get_tv_string_chk(&argvars[0]);
17279
17280 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017281 {
17282# ifdef WIN32
17283 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017284 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017285
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017286 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017287 if (n != 0)
17288 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17289 if (r == NULL)
17290# else
17291 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017292 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017293# endif
17294 EMSG(_("E277: Unable to read a server reply"));
17295 }
17296#endif
17297 rettv->v_type = VAR_STRING;
17298 rettv->vval.v_string = r;
17299}
17300
17301/*
17302 * "remote_send()" function
17303 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017305f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017306{
17307 rettv->v_type = VAR_STRING;
17308 rettv->vval.v_string = NULL;
17309#ifdef FEAT_CLIENTSERVER
17310 remote_common(argvars, rettv, FALSE);
17311#endif
17312}
17313
17314/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017315 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017316 */
17317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017318f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017319{
Bram Moolenaar33570922005-01-25 22:26:29 +000017320 list_T *l;
17321 listitem_T *item, *item2;
17322 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017323 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017324 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017325 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017326 dict_T *d;
17327 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017328 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017329
Bram Moolenaar8c711452005-01-14 21:53:12 +000017330 if (argvars[0].v_type == VAR_DICT)
17331 {
17332 if (argvars[2].v_type != VAR_UNKNOWN)
17333 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017334 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017335 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017337 key = get_tv_string_chk(&argvars[1]);
17338 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017340 di = dict_find(d, key, -1);
17341 if (di == NULL)
17342 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017343 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17344 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017345 {
17346 *rettv = di->di_tv;
17347 init_tv(&di->di_tv);
17348 dictitem_remove(d, di);
17349 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017350 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017351 }
17352 }
17353 else if (argvars[0].v_type != VAR_LIST)
17354 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017355 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017356 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017358 int error = FALSE;
17359
17360 idx = get_tv_number_chk(&argvars[1], &error);
17361 if (error)
17362 ; /* type error: do nothing, errmsg already given */
17363 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017364 EMSGN(_(e_listidx), idx);
17365 else
17366 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017367 if (argvars[2].v_type == VAR_UNKNOWN)
17368 {
17369 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017370 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017371 *rettv = item->li_tv;
17372 vim_free(item);
17373 }
17374 else
17375 {
17376 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017377 end = get_tv_number_chk(&argvars[2], &error);
17378 if (error)
17379 ; /* type error: do nothing */
17380 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017381 EMSGN(_(e_listidx), end);
17382 else
17383 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017384 int cnt = 0;
17385
17386 for (li = item; li != NULL; li = li->li_next)
17387 {
17388 ++cnt;
17389 if (li == item2)
17390 break;
17391 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017392 if (li == NULL) /* didn't find "item2" after "item" */
17393 EMSG(_(e_invrange));
17394 else
17395 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017396 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017397 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017398 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017399 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017400 l->lv_first = item;
17401 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017402 item->li_prev = NULL;
17403 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017404 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017405 }
17406 }
17407 }
17408 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017409 }
17410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411}
17412
17413/*
17414 * "rename({from}, {to})" function
17415 */
17416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017417f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418{
17419 char_u buf[NUMBUFLEN];
17420
17421 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017422 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17425 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426}
17427
17428/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017429 * "repeat()" function
17430 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017432f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017433{
17434 char_u *p;
17435 int n;
17436 int slen;
17437 int len;
17438 char_u *r;
17439 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017440
17441 n = get_tv_number(&argvars[1]);
17442 if (argvars[0].v_type == VAR_LIST)
17443 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017444 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017445 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017446 if (list_extend(rettv->vval.v_list,
17447 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017448 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017449 }
17450 else
17451 {
17452 p = get_tv_string(&argvars[0]);
17453 rettv->v_type = VAR_STRING;
17454 rettv->vval.v_string = NULL;
17455
17456 slen = (int)STRLEN(p);
17457 len = slen * n;
17458 if (len <= 0)
17459 return;
17460
17461 r = alloc(len + 1);
17462 if (r != NULL)
17463 {
17464 for (i = 0; i < n; i++)
17465 mch_memmove(r + i * slen, p, (size_t)slen);
17466 r[len] = NUL;
17467 }
17468
17469 rettv->vval.v_string = r;
17470 }
17471}
17472
17473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 * "resolve()" function
17475 */
17476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017477f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478{
17479 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017480#ifdef HAVE_READLINK
17481 char_u *buf = NULL;
17482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017484 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485#ifdef FEAT_SHORTCUT
17486 {
17487 char_u *v = NULL;
17488
17489 v = mch_resolve_shortcut(p);
17490 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017491 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017493 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 }
17495#else
17496# ifdef HAVE_READLINK
17497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 char_u *cpy;
17499 int len;
17500 char_u *remain = NULL;
17501 char_u *q;
17502 int is_relative_to_current = FALSE;
17503 int has_trailing_pathsep = FALSE;
17504 int limit = 100;
17505
17506 p = vim_strsave(p);
17507
17508 if (p[0] == '.' && (vim_ispathsep(p[1])
17509 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17510 is_relative_to_current = TRUE;
17511
17512 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017513 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017516 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518
17519 q = getnextcomp(p);
17520 if (*q != NUL)
17521 {
17522 /* Separate the first path component in "p", and keep the
17523 * remainder (beginning with the path separator). */
17524 remain = vim_strsave(q - 1);
17525 q[-1] = NUL;
17526 }
17527
Bram Moolenaard9462e32011-04-11 21:35:11 +020017528 buf = alloc(MAXPATHL + 1);
17529 if (buf == NULL)
17530 goto fail;
17531
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 for (;;)
17533 {
17534 for (;;)
17535 {
17536 len = readlink((char *)p, (char *)buf, MAXPATHL);
17537 if (len <= 0)
17538 break;
17539 buf[len] = NUL;
17540
17541 if (limit-- == 0)
17542 {
17543 vim_free(p);
17544 vim_free(remain);
17545 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017546 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 goto fail;
17548 }
17549
17550 /* Ensure that the result will have a trailing path separator
17551 * if the argument has one. */
17552 if (remain == NULL && has_trailing_pathsep)
17553 add_pathsep(buf);
17554
17555 /* Separate the first path component in the link value and
17556 * concatenate the remainders. */
17557 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17558 if (*q != NUL)
17559 {
17560 if (remain == NULL)
17561 remain = vim_strsave(q - 1);
17562 else
17563 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017564 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565 if (cpy != NULL)
17566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 vim_free(remain);
17568 remain = cpy;
17569 }
17570 }
17571 q[-1] = NUL;
17572 }
17573
17574 q = gettail(p);
17575 if (q > p && *q == NUL)
17576 {
17577 /* Ignore trailing path separator. */
17578 q[-1] = NUL;
17579 q = gettail(p);
17580 }
17581 if (q > p && !mch_isFullName(buf))
17582 {
17583 /* symlink is relative to directory of argument */
17584 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17585 if (cpy != NULL)
17586 {
17587 STRCPY(cpy, p);
17588 STRCPY(gettail(cpy), buf);
17589 vim_free(p);
17590 p = cpy;
17591 }
17592 }
17593 else
17594 {
17595 vim_free(p);
17596 p = vim_strsave(buf);
17597 }
17598 }
17599
17600 if (remain == NULL)
17601 break;
17602
17603 /* Append the first path component of "remain" to "p". */
17604 q = getnextcomp(remain + 1);
17605 len = q - remain - (*q != NUL);
17606 cpy = vim_strnsave(p, STRLEN(p) + len);
17607 if (cpy != NULL)
17608 {
17609 STRNCAT(cpy, remain, len);
17610 vim_free(p);
17611 p = cpy;
17612 }
17613 /* Shorten "remain". */
17614 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017615 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 else
17617 {
17618 vim_free(remain);
17619 remain = NULL;
17620 }
17621 }
17622
17623 /* If the result is a relative path name, make it explicitly relative to
17624 * the current directory if and only if the argument had this form. */
17625 if (!vim_ispathsep(*p))
17626 {
17627 if (is_relative_to_current
17628 && *p != NUL
17629 && !(p[0] == '.'
17630 && (p[1] == NUL
17631 || vim_ispathsep(p[1])
17632 || (p[1] == '.'
17633 && (p[2] == NUL
17634 || vim_ispathsep(p[2]))))))
17635 {
17636 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017637 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 if (cpy != NULL)
17639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 vim_free(p);
17641 p = cpy;
17642 }
17643 }
17644 else if (!is_relative_to_current)
17645 {
17646 /* Strip leading "./". */
17647 q = p;
17648 while (q[0] == '.' && vim_ispathsep(q[1]))
17649 q += 2;
17650 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017651 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 }
17653 }
17654
17655 /* Ensure that the result will have no trailing path separator
17656 * if the argument had none. But keep "/" or "//". */
17657 if (!has_trailing_pathsep)
17658 {
17659 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017660 if (after_pathsep(p, q))
17661 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 }
17663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 }
17666# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017667 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668# endif
17669#endif
17670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672
17673#ifdef HAVE_READLINK
17674fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017675 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017677 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678}
17679
17680/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017681 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 */
17683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017684f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685{
Bram Moolenaar33570922005-01-25 22:26:29 +000017686 list_T *l;
17687 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688
Bram Moolenaar0d660222005-01-07 21:51:51 +000017689 if (argvars[0].v_type != VAR_LIST)
17690 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017691 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017692 && !tv_check_lock(l->lv_lock,
17693 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017694 {
17695 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017696 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017697 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017698 while (li != NULL)
17699 {
17700 ni = li->li_prev;
17701 list_append(l, li);
17702 li = ni;
17703 }
17704 rettv->vval.v_list = l;
17705 rettv->v_type = VAR_LIST;
17706 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017707 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709}
17710
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017711#define SP_NOMOVE 0x01 /* don't move cursor */
17712#define SP_REPEAT 0x02 /* repeat to find outer pair */
17713#define SP_RETCOUNT 0x04 /* return matchcount */
17714#define SP_SETPCMARK 0x08 /* set previous context mark */
17715#define SP_START 0x10 /* accept match at start position */
17716#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17717#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017718#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017719
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017720static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017721
17722/*
17723 * Get flags for a search function.
17724 * Possibly sets "p_ws".
17725 * Returns BACKWARD, FORWARD or zero (for an error).
17726 */
17727 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017728get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017729{
17730 int dir = FORWARD;
17731 char_u *flags;
17732 char_u nbuf[NUMBUFLEN];
17733 int mask;
17734
17735 if (varp->v_type != VAR_UNKNOWN)
17736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017737 flags = get_tv_string_buf_chk(varp, nbuf);
17738 if (flags == NULL)
17739 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017740 while (*flags != NUL)
17741 {
17742 switch (*flags)
17743 {
17744 case 'b': dir = BACKWARD; break;
17745 case 'w': p_ws = TRUE; break;
17746 case 'W': p_ws = FALSE; break;
17747 default: mask = 0;
17748 if (flagsp != NULL)
17749 switch (*flags)
17750 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017751 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017752 case 'e': mask = SP_END; break;
17753 case 'm': mask = SP_RETCOUNT; break;
17754 case 'n': mask = SP_NOMOVE; break;
17755 case 'p': mask = SP_SUBPAT; break;
17756 case 'r': mask = SP_REPEAT; break;
17757 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017758 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017759 }
17760 if (mask == 0)
17761 {
17762 EMSG2(_(e_invarg2), flags);
17763 dir = 0;
17764 }
17765 else
17766 *flagsp |= mask;
17767 }
17768 if (dir == 0)
17769 break;
17770 ++flags;
17771 }
17772 }
17773 return dir;
17774}
17775
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017777 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017779 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017780search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017782 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 char_u *pat;
17784 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017785 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 int save_p_ws = p_ws;
17787 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017788 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017789 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017790 proftime_T tm;
17791#ifdef FEAT_RELTIME
17792 long time_limit = 0;
17793#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017794 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017795 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017797 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017798 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017799 if (dir == 0)
17800 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017801 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017802 if (flags & SP_START)
17803 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017804 if (flags & SP_END)
17805 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017806 if (flags & SP_COLUMN)
17807 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017808
Bram Moolenaar76929292008-01-06 19:07:36 +000017809 /* Optional arguments: line number to stop searching and timeout. */
17810 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017811 {
17812 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17813 if (lnum_stop < 0)
17814 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017815#ifdef FEAT_RELTIME
17816 if (argvars[3].v_type != VAR_UNKNOWN)
17817 {
17818 time_limit = get_tv_number_chk(&argvars[3], NULL);
17819 if (time_limit < 0)
17820 goto theend;
17821 }
17822#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017823 }
17824
Bram Moolenaar76929292008-01-06 19:07:36 +000017825#ifdef FEAT_RELTIME
17826 /* Set the time limit, if there is one. */
17827 profile_setlimit(time_limit, &tm);
17828#endif
17829
Bram Moolenaar231334e2005-07-25 20:46:57 +000017830 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017831 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017832 * Check to make sure only those flags are set.
17833 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17834 * flags cannot be set. Check for that condition also.
17835 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017836 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017837 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017840 goto theend;
17841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017843 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017844 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017845 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017846 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017848 if (flags & SP_SUBPAT)
17849 retval = subpatnum;
17850 else
17851 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017852 if (flags & SP_SETPCMARK)
17853 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017855 if (match_pos != NULL)
17856 {
17857 /* Store the match cursor position */
17858 match_pos->lnum = pos.lnum;
17859 match_pos->col = pos.col + 1;
17860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 /* "/$" will put the cursor after the end of the line, may need to
17862 * correct that here */
17863 check_cursor();
17864 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017865
17866 /* If 'n' flag is used: restore cursor position. */
17867 if (flags & SP_NOMOVE)
17868 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017869 else
17870 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017871theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017873
17874 return retval;
17875}
17876
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017877#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017878
17879/*
17880 * round() is not in C90, use ceil() or floor() instead.
17881 */
17882 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017883vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017884{
17885 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17886}
17887
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017888/*
17889 * "round({float})" function
17890 */
17891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017892f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017893{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017894 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017895
17896 rettv->v_type = VAR_FLOAT;
17897 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017898 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017899 else
17900 rettv->vval.v_float = 0.0;
17901}
17902#endif
17903
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017904/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017905 * "screenattr()" function
17906 */
17907 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017908f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017909{
17910 int row;
17911 int col;
17912 int c;
17913
17914 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17915 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17916 if (row < 0 || row >= screen_Rows
17917 || col < 0 || col >= screen_Columns)
17918 c = -1;
17919 else
17920 c = ScreenAttrs[LineOffset[row] + col];
17921 rettv->vval.v_number = c;
17922}
17923
17924/*
17925 * "screenchar()" function
17926 */
17927 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017928f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017929{
17930 int row;
17931 int col;
17932 int off;
17933 int c;
17934
17935 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17936 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17937 if (row < 0 || row >= screen_Rows
17938 || col < 0 || col >= screen_Columns)
17939 c = -1;
17940 else
17941 {
17942 off = LineOffset[row] + col;
17943#ifdef FEAT_MBYTE
17944 if (enc_utf8 && ScreenLinesUC[off] != 0)
17945 c = ScreenLinesUC[off];
17946 else
17947#endif
17948 c = ScreenLines[off];
17949 }
17950 rettv->vval.v_number = c;
17951}
17952
17953/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017954 * "screencol()" function
17955 *
17956 * First column is 1 to be consistent with virtcol().
17957 */
17958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017959f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017960{
17961 rettv->vval.v_number = screen_screencol() + 1;
17962}
17963
17964/*
17965 * "screenrow()" function
17966 */
17967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017968f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017969{
17970 rettv->vval.v_number = screen_screenrow() + 1;
17971}
17972
17973/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017974 * "search()" function
17975 */
17976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017977f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017978{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017979 int flags = 0;
17980
17981 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982}
17983
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017985 * "searchdecl()" function
17986 */
17987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017988f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017989{
17990 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017991 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017992 int error = FALSE;
17993 char_u *name;
17994
17995 rettv->vval.v_number = 1; /* default: FAIL */
17996
17997 name = get_tv_string_chk(&argvars[0]);
17998 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017999 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018000 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018001 if (!error && argvars[2].v_type != VAR_UNKNOWN)
18002 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
18003 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018004 if (!error && name != NULL)
18005 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000018006 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018007}
18008
18009/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018010 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018012 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018013searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014{
18015 char_u *spat, *mpat, *epat;
18016 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 int dir;
18019 int flags = 0;
18020 char_u nbuf1[NUMBUFLEN];
18021 char_u nbuf2[NUMBUFLEN];
18022 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018023 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018024 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018025 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018028 spat = get_tv_string_chk(&argvars[0]);
18029 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18030 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18031 if (spat == NULL || mpat == NULL || epat == NULL)
18032 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 /* Handle the optional fourth argument: flags */
18035 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018036 if (dir == 0)
18037 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018038
18039 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018040 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18041 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018042 if ((flags & (SP_END | SP_SUBPAT)) != 0
18043 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018044 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018045 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018046 goto theend;
18047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018049 /* Using 'r' implies 'W', otherwise it doesn't work. */
18050 if (flags & SP_REPEAT)
18051 p_ws = FALSE;
18052
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018053 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018054 if (argvars[3].v_type == VAR_UNKNOWN
18055 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 skip = (char_u *)"";
18057 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018059 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018060 if (argvars[5].v_type != VAR_UNKNOWN)
18061 {
18062 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
18063 if (lnum_stop < 0)
18064 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018065#ifdef FEAT_RELTIME
18066 if (argvars[6].v_type != VAR_UNKNOWN)
18067 {
18068 time_limit = get_tv_number_chk(&argvars[6], NULL);
18069 if (time_limit < 0)
18070 goto theend;
18071 }
18072#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018073 }
18074 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018075 if (skip == NULL)
18076 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018078 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018079 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018080
18081theend:
18082 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018083
18084 return retval;
18085}
18086
18087/*
18088 * "searchpair()" function
18089 */
18090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018091f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018092{
18093 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18094}
18095
18096/*
18097 * "searchpairpos()" function
18098 */
18099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018100f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018101{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018102 pos_T match_pos;
18103 int lnum = 0;
18104 int col = 0;
18105
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018106 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018107 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018108
18109 if (searchpair_cmn(argvars, &match_pos) > 0)
18110 {
18111 lnum = match_pos.lnum;
18112 col = match_pos.col;
18113 }
18114
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018115 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18116 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018117}
18118
18119/*
18120 * Search for a start/middle/end thing.
18121 * Used by searchpair(), see its documentation for the details.
18122 * Returns 0 or -1 for no match,
18123 */
18124 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018125do_searchpair(
18126 char_u *spat, /* start pattern */
18127 char_u *mpat, /* middle pattern */
18128 char_u *epat, /* end pattern */
18129 int dir, /* BACKWARD or FORWARD */
18130 char_u *skip, /* skip expression */
18131 int flags, /* SP_SETPCMARK and other SP_ values */
18132 pos_T *match_pos,
18133 linenr_T lnum_stop, /* stop at this line if not zero */
18134 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018135{
18136 char_u *save_cpo;
18137 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18138 long retval = 0;
18139 pos_T pos;
18140 pos_T firstpos;
18141 pos_T foundpos;
18142 pos_T save_cursor;
18143 pos_T save_pos;
18144 int n;
18145 int r;
18146 int nest = 1;
18147 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018148 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018149 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018150
18151 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18152 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018153 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018154
Bram Moolenaar76929292008-01-06 19:07:36 +000018155#ifdef FEAT_RELTIME
18156 /* Set the time limit, if there is one. */
18157 profile_setlimit(time_limit, &tm);
18158#endif
18159
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018160 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18161 * start/middle/end (pat3, for the top pair). */
18162 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18163 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18164 if (pat2 == NULL || pat3 == NULL)
18165 goto theend;
18166 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18167 if (*mpat == NUL)
18168 STRCPY(pat3, pat2);
18169 else
18170 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18171 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018172 if (flags & SP_START)
18173 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018174
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 save_cursor = curwin->w_cursor;
18176 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018177 clearpos(&firstpos);
18178 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 pat = pat3;
18180 for (;;)
18181 {
18182 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018183 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18185 /* didn't find it or found the first match again: FAIL */
18186 break;
18187
18188 if (firstpos.lnum == 0)
18189 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018190 if (equalpos(pos, foundpos))
18191 {
18192 /* Found the same position again. Can happen with a pattern that
18193 * has "\zs" at the end and searching backwards. Advance one
18194 * character and try again. */
18195 if (dir == BACKWARD)
18196 decl(&pos);
18197 else
18198 incl(&pos);
18199 }
18200 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018202 /* clear the start flag to avoid getting stuck here */
18203 options &= ~SEARCH_START;
18204
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 /* If the skip pattern matches, ignore this match. */
18206 if (*skip != NUL)
18207 {
18208 save_pos = curwin->w_cursor;
18209 curwin->w_cursor = pos;
18210 r = eval_to_bool(skip, &err, NULL, FALSE);
18211 curwin->w_cursor = save_pos;
18212 if (err)
18213 {
18214 /* Evaluating {skip} caused an error, break here. */
18215 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018216 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 break;
18218 }
18219 if (r)
18220 continue;
18221 }
18222
18223 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18224 {
18225 /* Found end when searching backwards or start when searching
18226 * forward: nested pair. */
18227 ++nest;
18228 pat = pat2; /* nested, don't search for middle */
18229 }
18230 else
18231 {
18232 /* Found end when searching forward or start when searching
18233 * backward: end of (nested) pair; or found middle in outer pair. */
18234 if (--nest == 1)
18235 pat = pat3; /* outer level, search for middle */
18236 }
18237
18238 if (nest == 0)
18239 {
18240 /* Found the match: return matchcount or line number. */
18241 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018242 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018244 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018245 if (flags & SP_SETPCMARK)
18246 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 curwin->w_cursor = pos;
18248 if (!(flags & SP_REPEAT))
18249 break;
18250 nest = 1; /* search for next unmatched */
18251 }
18252 }
18253
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018254 if (match_pos != NULL)
18255 {
18256 /* Store the match cursor position */
18257 match_pos->lnum = curwin->w_cursor.lnum;
18258 match_pos->col = curwin->w_cursor.col + 1;
18259 }
18260
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018262 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 curwin->w_cursor = save_cursor;
18264
18265theend:
18266 vim_free(pat2);
18267 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018268 if (p_cpo == empty_option)
18269 p_cpo = save_cpo;
18270 else
18271 /* Darn, evaluating the {skip} expression changed the value. */
18272 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018273
18274 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275}
18276
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018277/*
18278 * "searchpos()" function
18279 */
18280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018281f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018282{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018283 pos_T match_pos;
18284 int lnum = 0;
18285 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018286 int n;
18287 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018288
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018289 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018290 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018291
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018292 n = search_cmn(argvars, &match_pos, &flags);
18293 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018294 {
18295 lnum = match_pos.lnum;
18296 col = match_pos.col;
18297 }
18298
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018299 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18300 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018301 if (flags & SP_SUBPAT)
18302 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018303}
18304
Bram Moolenaar0d660222005-01-07 21:51:51 +000018305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018306f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018308#ifdef FEAT_CLIENTSERVER
18309 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018310 char_u *server = get_tv_string_chk(&argvars[0]);
18311 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312
Bram Moolenaar0d660222005-01-07 21:51:51 +000018313 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018314 if (server == NULL || reply == NULL)
18315 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018316 if (check_restricted() || check_secure())
18317 return;
18318# ifdef FEAT_X11
18319 if (check_connection() == FAIL)
18320 return;
18321# endif
18322
18323 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325 EMSG(_("E258: Unable to send to client"));
18326 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018328 rettv->vval.v_number = 0;
18329#else
18330 rettv->vval.v_number = -1;
18331#endif
18332}
18333
Bram Moolenaar0d660222005-01-07 21:51:51 +000018334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018335f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018336{
18337 char_u *r = NULL;
18338
18339#ifdef FEAT_CLIENTSERVER
18340# ifdef WIN32
18341 r = serverGetVimNames();
18342# else
18343 make_connection();
18344 if (X_DISPLAY != NULL)
18345 r = serverGetVimNames(X_DISPLAY);
18346# endif
18347#endif
18348 rettv->v_type = VAR_STRING;
18349 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350}
18351
18352/*
18353 * "setbufvar()" function
18354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018356f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357{
18358 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018361 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 char_u nbuf[NUMBUFLEN];
18363
18364 if (check_restricted() || check_secure())
18365 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018366 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18367 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018368 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369 varp = &argvars[2];
18370
18371 if (buf != NULL && varname != NULL && varp != NULL)
18372 {
18373 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375
18376 if (*varname == '&')
18377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018378 long numval;
18379 char_u *strval;
18380 int error = FALSE;
18381
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018383 numval = get_tv_number_chk(varp, &error);
18384 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018385 if (!error && strval != NULL)
18386 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 }
18388 else
18389 {
18390 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18391 if (bufvarname != NULL)
18392 {
18393 STRCPY(bufvarname, "b:");
18394 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018395 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 vim_free(bufvarname);
18397 }
18398 }
18399
18400 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403}
18404
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018406f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018407{
18408 dict_T *d;
18409 dictitem_T *di;
18410 char_u *csearch;
18411
18412 if (argvars[0].v_type != VAR_DICT)
18413 {
18414 EMSG(_(e_dictreq));
18415 return;
18416 }
18417
18418 if ((d = argvars[0].vval.v_dict) != NULL)
18419 {
18420 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18421 if (csearch != NULL)
18422 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018423#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018424 if (enc_utf8)
18425 {
18426 int pcc[MAX_MCO];
18427 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018428
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018429 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18430 }
18431 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018432#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018433 set_last_csearch(PTR2CHAR(csearch),
18434 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018435 }
18436
18437 di = dict_find(d, (char_u *)"forward", -1);
18438 if (di != NULL)
18439 set_csearch_direction(get_tv_number(&di->di_tv)
18440 ? FORWARD : BACKWARD);
18441
18442 di = dict_find(d, (char_u *)"until", -1);
18443 if (di != NULL)
18444 set_csearch_until(!!get_tv_number(&di->di_tv));
18445 }
18446}
18447
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448/*
18449 * "setcmdpos()" function
18450 */
18451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018452f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018454 int pos = (int)get_tv_number(&argvars[0]) - 1;
18455
18456 if (pos >= 0)
18457 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458}
18459
18460/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018461 * "setfperm({fname}, {mode})" function
18462 */
18463 static void
18464f_setfperm(typval_T *argvars, typval_T *rettv)
18465{
18466 char_u *fname;
18467 char_u modebuf[NUMBUFLEN];
18468 char_u *mode_str;
18469 int i;
18470 int mask;
18471 int mode = 0;
18472
18473 rettv->vval.v_number = 0;
18474 fname = get_tv_string_chk(&argvars[0]);
18475 if (fname == NULL)
18476 return;
18477 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18478 if (mode_str == NULL)
18479 return;
18480 if (STRLEN(mode_str) != 9)
18481 {
18482 EMSG2(_(e_invarg2), mode_str);
18483 return;
18484 }
18485
18486 mask = 1;
18487 for (i = 8; i >= 0; --i)
18488 {
18489 if (mode_str[i] != '-')
18490 mode |= mask;
18491 mask = mask << 1;
18492 }
18493 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18494}
18495
18496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 * "setline()" function
18498 */
18499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018500f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501{
18502 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018503 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018504 list_T *l = NULL;
18505 listitem_T *li = NULL;
18506 long added = 0;
18507 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018509 lnum = get_tv_lnum(&argvars[0]);
18510 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018512 l = argvars[1].vval.v_list;
18513 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018515 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018516 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018517
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018518 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018519 for (;;)
18520 {
18521 if (l != NULL)
18522 {
18523 /* list argument, get next string */
18524 if (li == NULL)
18525 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018526 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018527 li = li->li_next;
18528 }
18529
18530 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018531 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018532 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018533
18534 /* When coming here from Insert mode, sync undo, so that this can be
18535 * undone separately from what was previously inserted. */
18536 if (u_sync_once == 2)
18537 {
18538 u_sync_once = 1; /* notify that u_sync() was called */
18539 u_sync(TRUE);
18540 }
18541
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018542 if (lnum <= curbuf->b_ml.ml_line_count)
18543 {
18544 /* existing line, replace it */
18545 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18546 {
18547 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018548 if (lnum == curwin->w_cursor.lnum)
18549 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018550 rettv->vval.v_number = 0; /* OK */
18551 }
18552 }
18553 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18554 {
18555 /* lnum is one past the last line, append the line */
18556 ++added;
18557 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18558 rettv->vval.v_number = 0; /* OK */
18559 }
18560
18561 if (l == NULL) /* only one string argument */
18562 break;
18563 ++lnum;
18564 }
18565
18566 if (added > 0)
18567 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568}
18569
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018570static 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 +000018571
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018573 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018574 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018576set_qf_ll_list(
18577 win_T *wp UNUSED,
18578 typval_T *list_arg UNUSED,
18579 typval_T *action_arg UNUSED,
18580 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018581{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018582#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018583 char_u *act;
18584 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018585#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018586
Bram Moolenaar2641f772005-03-25 21:58:17 +000018587 rettv->vval.v_number = -1;
18588
18589#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018590 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018591 EMSG(_(e_listreq));
18592 else
18593 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018594 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018595
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018596 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018597 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018598 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018599 if (act == NULL)
18600 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018601 if (*act == 'a' || *act == 'r')
18602 action = *act;
18603 }
18604
Bram Moolenaar81484f42012-12-05 15:16:47 +010018605 if (l != NULL && set_errorlist(wp, l, action,
18606 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018607 rettv->vval.v_number = 0;
18608 }
18609#endif
18610}
18611
18612/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018613 * "setloclist()" function
18614 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018616f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018617{
18618 win_T *win;
18619
18620 rettv->vval.v_number = -1;
18621
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018622 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018623 if (win != NULL)
18624 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18625}
18626
18627/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018628 * "setmatches()" function
18629 */
18630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018631f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018632{
18633#ifdef FEAT_SEARCH_EXTRA
18634 list_T *l;
18635 listitem_T *li;
18636 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018637 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018638
18639 rettv->vval.v_number = -1;
18640 if (argvars[0].v_type != VAR_LIST)
18641 {
18642 EMSG(_(e_listreq));
18643 return;
18644 }
18645 if ((l = argvars[0].vval.v_list) != NULL)
18646 {
18647
18648 /* To some extent make sure that we are dealing with a list from
18649 * "getmatches()". */
18650 li = l->lv_first;
18651 while (li != NULL)
18652 {
18653 if (li->li_tv.v_type != VAR_DICT
18654 || (d = li->li_tv.vval.v_dict) == NULL)
18655 {
18656 EMSG(_(e_invarg));
18657 return;
18658 }
18659 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018660 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18661 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018662 && dict_find(d, (char_u *)"priority", -1) != NULL
18663 && dict_find(d, (char_u *)"id", -1) != NULL))
18664 {
18665 EMSG(_(e_invarg));
18666 return;
18667 }
18668 li = li->li_next;
18669 }
18670
18671 clear_matches(curwin);
18672 li = l->lv_first;
18673 while (li != NULL)
18674 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018675 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018676 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018677 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018678 char_u *group;
18679 int priority;
18680 int id;
18681 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018682
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018683 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018684 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18685 {
18686 if (s == NULL)
18687 {
18688 s = list_alloc();
18689 if (s == NULL)
18690 return;
18691 }
18692
18693 /* match from matchaddpos() */
18694 for (i = 1; i < 9; i++)
18695 {
18696 sprintf((char *)buf, (char *)"pos%d", i);
18697 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18698 {
18699 if (di->di_tv.v_type != VAR_LIST)
18700 return;
18701
18702 list_append_tv(s, &di->di_tv);
18703 s->lv_refcount++;
18704 }
18705 else
18706 break;
18707 }
18708 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018709
18710 group = get_dict_string(d, (char_u *)"group", FALSE);
18711 priority = (int)get_dict_number(d, (char_u *)"priority");
18712 id = (int)get_dict_number(d, (char_u *)"id");
18713 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18714 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18715 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018716 if (i == 0)
18717 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018718 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018719 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018720 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018721 }
18722 else
18723 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018724 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018725 list_unref(s);
18726 s = NULL;
18727 }
18728
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018729 li = li->li_next;
18730 }
18731 rettv->vval.v_number = 0;
18732 }
18733#endif
18734}
18735
18736/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018737 * "setpos()" function
18738 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018740f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018741{
18742 pos_T pos;
18743 int fnum;
18744 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018745 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018746
Bram Moolenaar08250432008-02-13 11:42:46 +000018747 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018748 name = get_tv_string_chk(argvars);
18749 if (name != NULL)
18750 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018751 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018752 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018753 if (--pos.col < 0)
18754 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018755 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018756 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018757 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018758 if (fnum == curbuf->b_fnum)
18759 {
18760 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018761 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018762 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018763 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018764 curwin->w_set_curswant = FALSE;
18765 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018766 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018767 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018768 }
18769 else
18770 EMSG(_(e_invarg));
18771 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018772 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18773 {
18774 /* set mark */
18775 if (setmark_pos(name[1], &pos, fnum) == OK)
18776 rettv->vval.v_number = 0;
18777 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018778 else
18779 EMSG(_(e_invarg));
18780 }
18781 }
18782}
18783
18784/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018785 * "setqflist()" function
18786 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018788f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018789{
18790 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18791}
18792
18793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 * "setreg()" function
18795 */
18796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018797f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798{
18799 int regname;
18800 char_u *strregname;
18801 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018802 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 int append;
18804 char_u yank_type;
18805 long block_len;
18806
18807 block_len = -1;
18808 yank_type = MAUTO;
18809 append = FALSE;
18810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018811 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018812 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018814 if (strregname == NULL)
18815 return; /* type error; errmsg already given */
18816 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 if (regname == 0 || regname == '@')
18818 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018820 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018822 stropt = get_tv_string_chk(&argvars[2]);
18823 if (stropt == NULL)
18824 return; /* type error */
18825 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826 switch (*stropt)
18827 {
18828 case 'a': case 'A': /* append */
18829 append = TRUE;
18830 break;
18831 case 'v': case 'c': /* character-wise selection */
18832 yank_type = MCHAR;
18833 break;
18834 case 'V': case 'l': /* line-wise selection */
18835 yank_type = MLINE;
18836 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 case 'b': case Ctrl_V: /* block-wise selection */
18838 yank_type = MBLOCK;
18839 if (VIM_ISDIGIT(stropt[1]))
18840 {
18841 ++stropt;
18842 block_len = getdigits(&stropt) - 1;
18843 --stropt;
18844 }
18845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846 }
18847 }
18848
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018849 if (argvars[1].v_type == VAR_LIST)
18850 {
18851 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018852 char_u **allocval;
18853 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018854 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018855 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018856 int len = argvars[1].vval.v_list->lv_len;
18857 listitem_T *li;
18858
Bram Moolenaar7d647822014-04-05 21:28:56 +020018859 /* First half: use for pointers to result lines; second half: use for
18860 * pointers to allocated copies. */
18861 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018862 if (lstval == NULL)
18863 return;
18864 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018865 allocval = lstval + len + 2;
18866 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018867
18868 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18869 li = li->li_next)
18870 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018871 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018872 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018873 goto free_lstval;
18874 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018875 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018876 /* Need to make a copy, next get_tv_string_buf_chk() will
18877 * overwrite the string. */
18878 strval = vim_strsave(buf);
18879 if (strval == NULL)
18880 goto free_lstval;
18881 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018882 }
18883 *curval++ = strval;
18884 }
18885 *curval++ = NULL;
18886
18887 write_reg_contents_lst(regname, lstval, -1,
18888 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018889free_lstval:
18890 while (curallocval > allocval)
18891 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018892 vim_free(lstval);
18893 }
18894 else
18895 {
18896 strval = get_tv_string_chk(&argvars[1]);
18897 if (strval == NULL)
18898 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018899 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018902 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903}
18904
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018905/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018906 * "settabvar()" function
18907 */
18908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018909f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018910{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018911#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018912 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018913 tabpage_T *tp;
18914#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018915 char_u *varname, *tabvarname;
18916 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018917
18918 rettv->vval.v_number = 0;
18919
18920 if (check_restricted() || check_secure())
18921 return;
18922
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018923#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018924 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018925#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018926 varname = get_tv_string_chk(&argvars[1]);
18927 varp = &argvars[2];
18928
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018929 if (varname != NULL && varp != NULL
18930#ifdef FEAT_WINDOWS
18931 && tp != NULL
18932#endif
18933 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018934 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018935#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018936 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018937 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018938#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018939
18940 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18941 if (tabvarname != NULL)
18942 {
18943 STRCPY(tabvarname, "t:");
18944 STRCPY(tabvarname + 2, varname);
18945 set_var(tabvarname, varp, TRUE);
18946 vim_free(tabvarname);
18947 }
18948
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018949#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018950 /* Restore current tabpage */
18951 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018952 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018953#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018954 }
18955}
18956
18957/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018958 * "settabwinvar()" function
18959 */
18960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018961f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018962{
18963 setwinvar(argvars, rettv, 1);
18964}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965
18966/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018967 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018970f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018972 setwinvar(argvars, rettv, 0);
18973}
18974
18975/*
18976 * "setwinvar()" and "settabwinvar()" functions
18977 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018978
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018980setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018981{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 win_T *win;
18983#ifdef FEAT_WINDOWS
18984 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018985 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018986 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987#endif
18988 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018989 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018991 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992
18993 if (check_restricted() || check_secure())
18994 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018995
18996#ifdef FEAT_WINDOWS
18997 if (off == 1)
18998 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18999 else
19000 tp = curtab;
19001#endif
19002 win = find_win_by_nr(&argvars[off], tp);
19003 varname = get_tv_string_chk(&argvars[off + 1]);
19004 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005
19006 if (win != NULL && varname != NULL && varp != NULL)
19007 {
19008#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019009 need_switch_win = !(tp == curtab && win == curwin);
19010 if (!need_switch_win
19011 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019014 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019016 long numval;
19017 char_u *strval;
19018 int error = FALSE;
19019
19020 ++varname;
19021 numval = get_tv_number_chk(varp, &error);
19022 strval = get_tv_string_buf_chk(varp, nbuf);
19023 if (!error && strval != NULL)
19024 set_option_value(varname, numval, strval, OPT_LOCAL);
19025 }
19026 else
19027 {
19028 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19029 if (winvarname != NULL)
19030 {
19031 STRCPY(winvarname, "w:");
19032 STRCPY(winvarname + 2, varname);
19033 set_var(winvarname, varp, TRUE);
19034 vim_free(winvarname);
19035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036 }
19037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019039 if (need_switch_win)
19040 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041#endif
19042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043}
19044
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019045#ifdef FEAT_CRYPT
19046/*
19047 * "sha256({string})" function
19048 */
19049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019050f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019051{
19052 char_u *p;
19053
19054 p = get_tv_string(&argvars[0]);
19055 rettv->vval.v_string = vim_strsave(
19056 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19057 rettv->v_type = VAR_STRING;
19058}
19059#endif /* FEAT_CRYPT */
19060
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019062 * "shellescape({string})" function
19063 */
19064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019065f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019066{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019067 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019068 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019069 rettv->v_type = VAR_STRING;
19070}
19071
19072/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019073 * shiftwidth() function
19074 */
19075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019076f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019077{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019078 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019079}
19080
19081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019082 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 */
19084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019085f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019087 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088
Bram Moolenaar0d660222005-01-07 21:51:51 +000019089 p = get_tv_string(&argvars[0]);
19090 rettv->vval.v_string = vim_strsave(p);
19091 simplify_filename(rettv->vval.v_string); /* simplify in place */
19092 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093}
19094
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019095#ifdef FEAT_FLOAT
19096/*
19097 * "sin()" function
19098 */
19099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019100f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019101{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019102 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019103
19104 rettv->v_type = VAR_FLOAT;
19105 if (get_float_arg(argvars, &f) == OK)
19106 rettv->vval.v_float = sin(f);
19107 else
19108 rettv->vval.v_float = 0.0;
19109}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019110
19111/*
19112 * "sinh()" function
19113 */
19114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019115f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019116{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019117 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019118
19119 rettv->v_type = VAR_FLOAT;
19120 if (get_float_arg(argvars, &f) == OK)
19121 rettv->vval.v_float = sinh(f);
19122 else
19123 rettv->vval.v_float = 0.0;
19124}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019125#endif
19126
Bram Moolenaar0d660222005-01-07 21:51:51 +000019127static int
19128#ifdef __BORLANDC__
19129 _RTLENTRYF
19130#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019131 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019132static int
19133#ifdef __BORLANDC__
19134 _RTLENTRYF
19135#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019136 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019137
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019138/* struct used in the array that's given to qsort() */
19139typedef struct
19140{
19141 listitem_T *item;
19142 int idx;
19143} sortItem_T;
19144
Bram Moolenaar0b962472016-02-22 22:51:33 +010019145/* struct storing information about current sort */
19146typedef struct
19147{
19148 int item_compare_ic;
19149 int item_compare_numeric;
19150 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019151#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019152 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019153#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019154 char_u *item_compare_func;
19155 dict_T *item_compare_selfdict;
19156 int item_compare_func_err;
19157 int item_compare_keep_zero;
19158} sortinfo_T;
19159static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019160static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019161#define ITEM_COMPARE_FAIL 999
19162
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019164 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019166 static int
19167#ifdef __BORLANDC__
19168_RTLENTRYF
19169#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019170item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019172 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019173 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019174 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019175 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019176 int res;
19177 char_u numbuf1[NUMBUFLEN];
19178 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019180 si1 = (sortItem_T *)s1;
19181 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019182 tv1 = &si1->item->li_tv;
19183 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019184
Bram Moolenaar0b962472016-02-22 22:51:33 +010019185 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019186 {
19187 long v1 = get_tv_number(tv1);
19188 long v2 = get_tv_number(tv2);
19189
19190 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19191 }
19192
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019193#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019194 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019195 {
19196 float_T v1 = get_tv_float(tv1);
19197 float_T v2 = get_tv_float(tv2);
19198
19199 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19200 }
19201#endif
19202
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019203 /* tv2string() puts quotes around a string and allocates memory. Don't do
19204 * that for string variables. Use a single quote when comparing with a
19205 * non-string to do what the docs promise. */
19206 if (tv1->v_type == VAR_STRING)
19207 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019208 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019209 p1 = (char_u *)"'";
19210 else
19211 p1 = tv1->vval.v_string;
19212 }
19213 else
19214 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19215 if (tv2->v_type == VAR_STRING)
19216 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019217 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019218 p2 = (char_u *)"'";
19219 else
19220 p2 = tv2->vval.v_string;
19221 }
19222 else
19223 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019224 if (p1 == NULL)
19225 p1 = (char_u *)"";
19226 if (p2 == NULL)
19227 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019228 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019229 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019230 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019231 res = STRICMP(p1, p2);
19232 else
19233 res = STRCMP(p1, p2);
19234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019236 {
19237 double n1, n2;
19238 n1 = strtod((char *)p1, (char **)&p1);
19239 n2 = strtod((char *)p2, (char **)&p2);
19240 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19241 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019242
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019243 /* When the result would be zero, compare the item indexes. Makes the
19244 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019245 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019246 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019247
Bram Moolenaar0d660222005-01-07 21:51:51 +000019248 vim_free(tofree1);
19249 vim_free(tofree2);
19250 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251}
19252
19253 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019254#ifdef __BORLANDC__
19255_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019257item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019258{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019259 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019260 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019261 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019262 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019263 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019265 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019266 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019267 return 0;
19268
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019269 si1 = (sortItem_T *)s1;
19270 si2 = (sortItem_T *)s2;
19271
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019272 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019273 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019274 copy_tv(&si1->item->li_tv, &argv[0]);
19275 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019276
19277 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019278 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019279 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019280 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019281 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019282 clear_tv(&argv[0]);
19283 clear_tv(&argv[1]);
19284
19285 if (res == FAIL)
19286 res = ITEM_COMPARE_FAIL;
19287 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019288 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19289 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019290 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019291 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019292
19293 /* When the result would be zero, compare the pointers themselves. Makes
19294 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019295 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019296 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019297
Bram Moolenaar0d660222005-01-07 21:51:51 +000019298 return res;
19299}
19300
19301/*
19302 * "sort({list})" function
19303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019305do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306{
Bram Moolenaar33570922005-01-25 22:26:29 +000019307 list_T *l;
19308 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019309 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019310 sortinfo_T *old_sortinfo;
19311 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019312 long len;
19313 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314
Bram Moolenaar0b962472016-02-22 22:51:33 +010019315 /* Pointer to current info struct used in compare function. Save and
19316 * restore the current one for nested calls. */
19317 old_sortinfo = sortinfo;
19318 sortinfo = &info;
19319
Bram Moolenaar0d660222005-01-07 21:51:51 +000019320 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019321 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322 else
19323 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019324 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019325 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019326 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19327 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019328 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019329 rettv->vval.v_list = l;
19330 rettv->v_type = VAR_LIST;
19331 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332
Bram Moolenaar0d660222005-01-07 21:51:51 +000019333 len = list_len(l);
19334 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019335 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336
Bram Moolenaar0b962472016-02-22 22:51:33 +010019337 info.item_compare_ic = FALSE;
19338 info.item_compare_numeric = FALSE;
19339 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019340#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019341 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019342#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019343 info.item_compare_func = NULL;
19344 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019345 if (argvars[1].v_type != VAR_UNKNOWN)
19346 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019347 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019348 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019349 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019350 else
19351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019352 int error = FALSE;
19353
19354 i = get_tv_number_chk(&argvars[1], &error);
19355 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019356 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019357 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019358 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019359 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019360 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019361 else if (i != 0)
19362 {
19363 EMSG(_(e_invarg));
19364 goto theend;
19365 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019366 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019367 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019368 if (*info.item_compare_func == NUL)
19369 {
19370 /* empty string means default sort */
19371 info.item_compare_func = NULL;
19372 }
19373 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019374 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019375 info.item_compare_func = NULL;
19376 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019377 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019378 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019379 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019380 info.item_compare_func = NULL;
19381 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019382 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019383#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019384 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019385 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019386 info.item_compare_func = NULL;
19387 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019388 }
19389#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019390 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019391 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019392 info.item_compare_func = NULL;
19393 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019394 }
19395 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019396 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019397
19398 if (argvars[2].v_type != VAR_UNKNOWN)
19399 {
19400 /* optional third argument: {dict} */
19401 if (argvars[2].v_type != VAR_DICT)
19402 {
19403 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019404 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019405 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019406 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019407 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409
Bram Moolenaar0d660222005-01-07 21:51:51 +000019410 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019411 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019412 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019413 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414
Bram Moolenaar327aa022014-03-25 18:24:23 +010019415 i = 0;
19416 if (sort)
19417 {
19418 /* sort(): ptrs will be the list to sort */
19419 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019420 {
19421 ptrs[i].item = li;
19422 ptrs[i].idx = i;
19423 ++i;
19424 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019425
Bram Moolenaar0b962472016-02-22 22:51:33 +010019426 info.item_compare_func_err = FALSE;
19427 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019428 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019429 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019430 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019431 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019432 EMSG(_("E702: Sort compare function failed"));
19433 else
19434 {
19435 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019436 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019437 info.item_compare_func == NULL
19438 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019439
Bram Moolenaar0b962472016-02-22 22:51:33 +010019440 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019441 {
19442 /* Clear the List and append the items in sorted order. */
19443 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19444 l->lv_len = 0;
19445 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019446 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019447 }
19448 }
19449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019451 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019452 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019453
19454 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019455 info.item_compare_func_err = FALSE;
19456 info.item_compare_keep_zero = TRUE;
19457 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019458 ? item_compare2 : item_compare;
19459
19460 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19461 li = li->li_next)
19462 {
19463 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19464 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019465 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019466 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019467 {
19468 EMSG(_("E882: Uniq compare function failed"));
19469 break;
19470 }
19471 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019472
Bram Moolenaar0b962472016-02-22 22:51:33 +010019473 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019474 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019475 while (--i >= 0)
19476 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019477 li = ptrs[i].item->li_next;
19478 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019479 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019480 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019481 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019482 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019483 list_fix_watch(l, li);
19484 listitem_free(li);
19485 l->lv_len--;
19486 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019487 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019488 }
19489
19490 vim_free(ptrs);
19491 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019492theend:
19493 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019494}
19495
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019496/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019497 * "sort({list})" function
19498 */
19499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019500f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019501{
19502 do_sort_uniq(argvars, rettv, TRUE);
19503}
19504
19505/*
19506 * "uniq({list})" function
19507 */
19508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019509f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019510{
19511 do_sort_uniq(argvars, rettv, FALSE);
19512}
19513
19514/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019515 * "soundfold({word})" function
19516 */
19517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019518f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019519{
19520 char_u *s;
19521
19522 rettv->v_type = VAR_STRING;
19523 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019524#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019525 rettv->vval.v_string = eval_soundfold(s);
19526#else
19527 rettv->vval.v_string = vim_strsave(s);
19528#endif
19529}
19530
19531/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019532 * "spellbadword()" function
19533 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019535f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019536{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019537 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019538 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019539 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019540
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019541 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019542 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019543
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019544#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019545 if (argvars[0].v_type == VAR_UNKNOWN)
19546 {
19547 /* Find the start and length of the badly spelled word. */
19548 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19549 if (len != 0)
19550 word = ml_get_cursor();
19551 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019552 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019553 {
19554 char_u *str = get_tv_string_chk(&argvars[0]);
19555 int capcol = -1;
19556
19557 if (str != NULL)
19558 {
19559 /* Check the argument for spelling. */
19560 while (*str != NUL)
19561 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019562 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019563 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019564 {
19565 word = str;
19566 break;
19567 }
19568 str += len;
19569 }
19570 }
19571 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019572#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019573
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019574 list_append_string(rettv->vval.v_list, word, len);
19575 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019576 attr == HLF_SPB ? "bad" :
19577 attr == HLF_SPR ? "rare" :
19578 attr == HLF_SPL ? "local" :
19579 attr == HLF_SPC ? "caps" :
19580 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019581}
19582
19583/*
19584 * "spellsuggest()" function
19585 */
19586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019587f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019588{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019589#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019590 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019591 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019592 int maxcount;
19593 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019594 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019595 listitem_T *li;
19596 int need_capital = FALSE;
19597#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019598
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019599 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019600 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019601
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019602#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019603 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019604 {
19605 str = get_tv_string(&argvars[0]);
19606 if (argvars[1].v_type != VAR_UNKNOWN)
19607 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019608 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019609 if (maxcount <= 0)
19610 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019611 if (argvars[2].v_type != VAR_UNKNOWN)
19612 {
19613 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19614 if (typeerr)
19615 return;
19616 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019617 }
19618 else
19619 maxcount = 25;
19620
Bram Moolenaar4770d092006-01-12 23:22:24 +000019621 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019622
19623 for (i = 0; i < ga.ga_len; ++i)
19624 {
19625 str = ((char_u **)ga.ga_data)[i];
19626
19627 li = listitem_alloc();
19628 if (li == NULL)
19629 vim_free(str);
19630 else
19631 {
19632 li->li_tv.v_type = VAR_STRING;
19633 li->li_tv.v_lock = 0;
19634 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019635 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019636 }
19637 }
19638 ga_clear(&ga);
19639 }
19640#endif
19641}
19642
Bram Moolenaar0d660222005-01-07 21:51:51 +000019643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019644f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019645{
19646 char_u *str;
19647 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019648 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019649 regmatch_T regmatch;
19650 char_u patbuf[NUMBUFLEN];
19651 char_u *save_cpo;
19652 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019653 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019654 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019655 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019656
19657 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19658 save_cpo = p_cpo;
19659 p_cpo = (char_u *)"";
19660
19661 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019662 if (argvars[1].v_type != VAR_UNKNOWN)
19663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019664 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19665 if (pat == NULL)
19666 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019667 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019668 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019669 }
19670 if (pat == NULL || *pat == NUL)
19671 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019672
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019673 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019675 if (typeerr)
19676 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677
Bram Moolenaar0d660222005-01-07 21:51:51 +000019678 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19679 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019681 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019682 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019683 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019684 if (*str == NUL)
19685 match = FALSE; /* empty item at the end */
19686 else
19687 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019688 if (match)
19689 end = regmatch.startp[0];
19690 else
19691 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019692 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19693 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019694 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019695 if (list_append_string(rettv->vval.v_list, str,
19696 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019697 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019698 }
19699 if (!match)
19700 break;
19701 /* Advance to just after the match. */
19702 if (regmatch.endp[0] > str)
19703 col = 0;
19704 else
19705 {
19706 /* Don't get stuck at the same match. */
19707#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019708 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019709#else
19710 col = 1;
19711#endif
19712 }
19713 str = regmatch.endp[0];
19714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715
Bram Moolenaar473de612013-06-08 18:19:48 +020019716 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718
Bram Moolenaar0d660222005-01-07 21:51:51 +000019719 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720}
19721
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019722#ifdef FEAT_FLOAT
19723/*
19724 * "sqrt()" function
19725 */
19726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019727f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019728{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019729 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019730
19731 rettv->v_type = VAR_FLOAT;
19732 if (get_float_arg(argvars, &f) == OK)
19733 rettv->vval.v_float = sqrt(f);
19734 else
19735 rettv->vval.v_float = 0.0;
19736}
19737
19738/*
19739 * "str2float()" function
19740 */
19741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019742f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019743{
19744 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19745
19746 if (*p == '+')
19747 p = skipwhite(p + 1);
19748 (void)string2float(p, &rettv->vval.v_float);
19749 rettv->v_type = VAR_FLOAT;
19750}
19751#endif
19752
Bram Moolenaar2c932302006-03-18 21:42:09 +000019753/*
19754 * "str2nr()" function
19755 */
19756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019757f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019758{
19759 int base = 10;
19760 char_u *p;
19761 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019762 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019763
19764 if (argvars[1].v_type != VAR_UNKNOWN)
19765 {
19766 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019767 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019768 {
19769 EMSG(_(e_invarg));
19770 return;
19771 }
19772 }
19773
19774 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019775 if (*p == '+')
19776 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019777 switch (base)
19778 {
19779 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19780 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19781 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19782 default: what = 0;
19783 }
19784 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019785 rettv->vval.v_number = n;
19786}
19787
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788#ifdef HAVE_STRFTIME
19789/*
19790 * "strftime({format}[, {time}])" function
19791 */
19792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019793f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794{
19795 char_u result_buf[256];
19796 struct tm *curtime;
19797 time_t seconds;
19798 char_u *p;
19799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019800 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019802 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019803 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 seconds = time(NULL);
19805 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019806 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 curtime = localtime(&seconds);
19808 /* MSVC returns NULL for an invalid value of seconds. */
19809 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019810 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 else
19812 {
19813# ifdef FEAT_MBYTE
19814 vimconv_T conv;
19815 char_u *enc;
19816
19817 conv.vc_type = CONV_NONE;
19818 enc = enc_locale();
19819 convert_setup(&conv, p_enc, enc);
19820 if (conv.vc_type != CONV_NONE)
19821 p = string_convert(&conv, p, NULL);
19822# endif
19823 if (p != NULL)
19824 (void)strftime((char *)result_buf, sizeof(result_buf),
19825 (char *)p, curtime);
19826 else
19827 result_buf[0] = NUL;
19828
19829# ifdef FEAT_MBYTE
19830 if (conv.vc_type != CONV_NONE)
19831 vim_free(p);
19832 convert_setup(&conv, enc, p_enc);
19833 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 else
19836# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019837 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838
19839# ifdef FEAT_MBYTE
19840 /* Release conversion descriptors */
19841 convert_setup(&conv, NULL, NULL);
19842 vim_free(enc);
19843# endif
19844 }
19845}
19846#endif
19847
19848/*
19849 * "stridx()" function
19850 */
19851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019852f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853{
19854 char_u buf[NUMBUFLEN];
19855 char_u *needle;
19856 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019857 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019859 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019861 needle = get_tv_string_chk(&argvars[1]);
19862 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019863 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019864 if (needle == NULL || haystack == NULL)
19865 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866
Bram Moolenaar33570922005-01-25 22:26:29 +000019867 if (argvars[2].v_type != VAR_UNKNOWN)
19868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019869 int error = FALSE;
19870
19871 start_idx = get_tv_number_chk(&argvars[2], &error);
19872 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019873 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019874 if (start_idx >= 0)
19875 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019876 }
19877
19878 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19879 if (pos != NULL)
19880 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881}
19882
19883/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019884 * "string()" function
19885 */
19886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019887f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019888{
19889 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019890 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019892 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019893 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019894 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019895 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019896 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897}
19898
19899/*
19900 * "strlen()" function
19901 */
19902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019903f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019905 rettv->vval.v_number = (varnumber_T)(STRLEN(
19906 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907}
19908
19909/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019910 * "strchars()" function
19911 */
19912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019913f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019914{
19915 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019916 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019917#ifdef FEAT_MBYTE
19918 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019919 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019920#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019921
19922 if (argvars[1].v_type != VAR_UNKNOWN)
19923 skipcc = get_tv_number_chk(&argvars[1], NULL);
19924 if (skipcc < 0 || skipcc > 1)
19925 EMSG(_(e_invarg));
19926 else
19927 {
19928#ifdef FEAT_MBYTE
19929 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19930 while (*s != NUL)
19931 {
19932 func_mb_ptr2char_adv(&s);
19933 ++len;
19934 }
19935 rettv->vval.v_number = len;
19936#else
19937 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19938#endif
19939 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019940}
19941
19942/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019943 * "strdisplaywidth()" function
19944 */
19945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019946f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019947{
19948 char_u *s = get_tv_string(&argvars[0]);
19949 int col = 0;
19950
19951 if (argvars[1].v_type != VAR_UNKNOWN)
19952 col = get_tv_number(&argvars[1]);
19953
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019954 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019955}
19956
19957/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019958 * "strwidth()" function
19959 */
19960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019961f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019962{
19963 char_u *s = get_tv_string(&argvars[0]);
19964
19965 rettv->vval.v_number = (varnumber_T)(
19966#ifdef FEAT_MBYTE
19967 mb_string2cells(s, -1)
19968#else
19969 STRLEN(s)
19970#endif
19971 );
19972}
19973
19974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 * "strpart()" function
19976 */
19977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019978f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979{
19980 char_u *p;
19981 int n;
19982 int len;
19983 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019984 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019986 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987 slen = (int)STRLEN(p);
19988
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019989 n = get_tv_number_chk(&argvars[1], &error);
19990 if (error)
19991 len = 0;
19992 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019993 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 else
19995 len = slen - n; /* default len: all bytes that are available. */
19996
19997 /*
19998 * Only return the overlap between the specified part and the actual
19999 * string.
20000 */
20001 if (n < 0)
20002 {
20003 len += n;
20004 n = 0;
20005 }
20006 else if (n > slen)
20007 n = slen;
20008 if (len < 0)
20009 len = 0;
20010 else if (n + len > slen)
20011 len = slen - n;
20012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020013 rettv->v_type = VAR_STRING;
20014 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020015}
20016
20017/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020018 * "strridx()" function
20019 */
20020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020021f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020022{
20023 char_u buf[NUMBUFLEN];
20024 char_u *needle;
20025 char_u *haystack;
20026 char_u *rest;
20027 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020028 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020030 needle = get_tv_string_chk(&argvars[1]);
20031 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020032
20033 rettv->vval.v_number = -1;
20034 if (needle == NULL || haystack == NULL)
20035 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020036
20037 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020038 if (argvars[2].v_type != VAR_UNKNOWN)
20039 {
20040 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020041 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020042 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020043 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020044 }
20045 else
20046 end_idx = haystack_len;
20047
Bram Moolenaar0d660222005-01-07 21:51:51 +000020048 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020049 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020050 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020051 lastmatch = haystack + end_idx;
20052 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020053 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020054 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020055 for (rest = haystack; *rest != '\0'; ++rest)
20056 {
20057 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020058 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020059 break;
20060 lastmatch = rest;
20061 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020062 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020063
20064 if (lastmatch == NULL)
20065 rettv->vval.v_number = -1;
20066 else
20067 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20068}
20069
20070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071 * "strtrans()" function
20072 */
20073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020074f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020076 rettv->v_type = VAR_STRING;
20077 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078}
20079
20080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020081 * "submatch()" function
20082 */
20083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020084f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020085{
Bram Moolenaar41571762014-04-02 19:00:58 +020020086 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020087 int no;
20088 int retList = 0;
20089
20090 no = (int)get_tv_number_chk(&argvars[0], &error);
20091 if (error)
20092 return;
20093 error = FALSE;
20094 if (argvars[1].v_type != VAR_UNKNOWN)
20095 retList = get_tv_number_chk(&argvars[1], &error);
20096 if (error)
20097 return;
20098
20099 if (retList == 0)
20100 {
20101 rettv->v_type = VAR_STRING;
20102 rettv->vval.v_string = reg_submatch(no);
20103 }
20104 else
20105 {
20106 rettv->v_type = VAR_LIST;
20107 rettv->vval.v_list = reg_submatch_list(no);
20108 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020109}
20110
20111/*
20112 * "substitute()" function
20113 */
20114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020115f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020116{
20117 char_u patbuf[NUMBUFLEN];
20118 char_u subbuf[NUMBUFLEN];
20119 char_u flagsbuf[NUMBUFLEN];
20120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020121 char_u *str = get_tv_string_chk(&argvars[0]);
20122 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20123 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20124 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20125
Bram Moolenaar0d660222005-01-07 21:51:51 +000020126 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020127 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20128 rettv->vval.v_string = NULL;
20129 else
20130 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020131}
20132
20133/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020134 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020137f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138{
20139 int id = 0;
20140#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020141 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 long col;
20143 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020144 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020146 lnum = get_tv_lnum(argvars); /* -1 on type error */
20147 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20148 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020150 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020151 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020152 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153#endif
20154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020155 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156}
20157
20158/*
20159 * "synIDattr(id, what [, mode])" function
20160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020162f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163{
20164 char_u *p = NULL;
20165#ifdef FEAT_SYN_HL
20166 int id;
20167 char_u *what;
20168 char_u *mode;
20169 char_u modebuf[NUMBUFLEN];
20170 int modec;
20171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020172 id = get_tv_number(&argvars[0]);
20173 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020174 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020176 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020178 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 modec = 0; /* replace invalid with current */
20180 }
20181 else
20182 {
20183#ifdef FEAT_GUI
20184 if (gui.in_use)
20185 modec = 'g';
20186 else
20187#endif
20188 if (t_colors > 1)
20189 modec = 'c';
20190 else
20191 modec = 't';
20192 }
20193
20194
20195 switch (TOLOWER_ASC(what[0]))
20196 {
20197 case 'b':
20198 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20199 p = highlight_color(id, what, modec);
20200 else /* bold */
20201 p = highlight_has_attr(id, HL_BOLD, modec);
20202 break;
20203
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020204 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 p = highlight_color(id, what, modec);
20206 break;
20207
20208 case 'i':
20209 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20210 p = highlight_has_attr(id, HL_INVERSE, modec);
20211 else /* italic */
20212 p = highlight_has_attr(id, HL_ITALIC, modec);
20213 break;
20214
20215 case 'n': /* name */
20216 p = get_highlight_name(NULL, id - 1);
20217 break;
20218
20219 case 'r': /* reverse */
20220 p = highlight_has_attr(id, HL_INVERSE, modec);
20221 break;
20222
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020223 case 's':
20224 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20225 p = highlight_color(id, what, modec);
20226 else /* standout */
20227 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 break;
20229
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020230 case 'u':
20231 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20232 /* underline */
20233 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20234 else
20235 /* undercurl */
20236 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 break;
20238 }
20239
20240 if (p != NULL)
20241 p = vim_strsave(p);
20242#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020243 rettv->v_type = VAR_STRING;
20244 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245}
20246
20247/*
20248 * "synIDtrans(id)" function
20249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020251f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252{
20253 int id;
20254
20255#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020256 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257
20258 if (id > 0)
20259 id = syn_get_final_id(id);
20260 else
20261#endif
20262 id = 0;
20263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020264 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265}
20266
20267/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020268 * "synconcealed(lnum, col)" function
20269 */
20270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020271f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020272{
20273#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20274 long lnum;
20275 long col;
20276 int syntax_flags = 0;
20277 int cchar;
20278 int matchid = 0;
20279 char_u str[NUMBUFLEN];
20280#endif
20281
20282 rettv->v_type = VAR_LIST;
20283 rettv->vval.v_list = NULL;
20284
20285#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20286 lnum = get_tv_lnum(argvars); /* -1 on type error */
20287 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20288
20289 vim_memset(str, NUL, sizeof(str));
20290
20291 if (rettv_list_alloc(rettv) != FAIL)
20292 {
20293 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20294 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20295 && curwin->w_p_cole > 0)
20296 {
20297 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20298 syntax_flags = get_syntax_info(&matchid);
20299
20300 /* get the conceal character */
20301 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20302 {
20303 cchar = syn_get_sub_char();
20304 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20305 cchar = lcs_conceal;
20306 if (cchar != NUL)
20307 {
20308# ifdef FEAT_MBYTE
20309 if (has_mbyte)
20310 (*mb_char2bytes)(cchar, str);
20311 else
20312# endif
20313 str[0] = cchar;
20314 }
20315 }
20316 }
20317
20318 list_append_number(rettv->vval.v_list,
20319 (syntax_flags & HL_CONCEAL) != 0);
20320 /* -1 to auto-determine strlen */
20321 list_append_string(rettv->vval.v_list, str, -1);
20322 list_append_number(rettv->vval.v_list, matchid);
20323 }
20324#endif
20325}
20326
20327/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020328 * "synstack(lnum, col)" function
20329 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020331f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020332{
20333#ifdef FEAT_SYN_HL
20334 long lnum;
20335 long col;
20336 int i;
20337 int id;
20338#endif
20339
20340 rettv->v_type = VAR_LIST;
20341 rettv->vval.v_list = NULL;
20342
20343#ifdef FEAT_SYN_HL
20344 lnum = get_tv_lnum(argvars); /* -1 on type error */
20345 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20346
20347 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020348 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020349 && rettv_list_alloc(rettv) != FAIL)
20350 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020351 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020352 for (i = 0; ; ++i)
20353 {
20354 id = syn_get_stack_item(i);
20355 if (id < 0)
20356 break;
20357 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20358 break;
20359 }
20360 }
20361#endif
20362}
20363
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020365get_cmd_output_as_rettv(
20366 typval_T *argvars,
20367 typval_T *rettv,
20368 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020370 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020372 char_u *infile = NULL;
20373 char_u buf[NUMBUFLEN];
20374 int err = FALSE;
20375 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020376 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020377 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020379 rettv->v_type = VAR_STRING;
20380 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020381 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020382 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020383
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020384 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020385 {
20386 /*
20387 * Write the string to a temp file, to be used for input of the shell
20388 * command.
20389 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020390 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020391 {
20392 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020393 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020394 }
20395
20396 fd = mch_fopen((char *)infile, WRITEBIN);
20397 if (fd == NULL)
20398 {
20399 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020400 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020401 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020402 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020403 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020404 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20405 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020406 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020407 else
20408 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020409 size_t len;
20410
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020411 p = get_tv_string_buf_chk(&argvars[1], buf);
20412 if (p == NULL)
20413 {
20414 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020415 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020416 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020417 len = STRLEN(p);
20418 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020419 err = TRUE;
20420 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020421 if (fclose(fd) != 0)
20422 err = TRUE;
20423 if (err)
20424 {
20425 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020426 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020427 }
20428 }
20429
Bram Moolenaar52a72462014-08-29 15:53:52 +020020430 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20431 * echoes typeahead, that messes up the display. */
20432 if (!msg_silent)
20433 flags += SHELL_COOKED;
20434
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020435 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020437 int len;
20438 listitem_T *li;
20439 char_u *s = NULL;
20440 char_u *start;
20441 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020442 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443
Bram Moolenaar52a72462014-08-29 15:53:52 +020020444 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020445 if (res == NULL)
20446 goto errret;
20447
20448 list = list_alloc();
20449 if (list == NULL)
20450 goto errret;
20451
20452 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020454 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020455 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020456 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020457 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020458
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020459 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020460 if (s == NULL)
20461 goto errret;
20462
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020463 for (p = s; start < end; ++p, ++start)
20464 *p = *start == NUL ? NL : *start;
20465 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020466
20467 li = listitem_alloc();
20468 if (li == NULL)
20469 {
20470 vim_free(s);
20471 goto errret;
20472 }
20473 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020474 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020475 li->li_tv.vval.v_string = s;
20476 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020478
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020479 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020480 rettv->v_type = VAR_LIST;
20481 rettv->vval.v_list = list;
20482 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020484 else
20485 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020486 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020487#ifdef USE_CR
20488 /* translate <CR> into <NL> */
20489 if (res != NULL)
20490 {
20491 char_u *s;
20492
20493 for (s = res; *s; ++s)
20494 {
20495 if (*s == CAR)
20496 *s = NL;
20497 }
20498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499#else
20500# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020501 /* translate <CR><NL> into <NL> */
20502 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020504 char_u *s, *d;
20505
20506 d = res;
20507 for (s = res; *s; ++s)
20508 {
20509 if (s[0] == CAR && s[1] == NL)
20510 ++s;
20511 *d++ = *s;
20512 }
20513 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515# endif
20516#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020517 rettv->vval.v_string = res;
20518 res = NULL;
20519 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020520
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020521errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020522 if (infile != NULL)
20523 {
20524 mch_remove(infile);
20525 vim_free(infile);
20526 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020527 if (res != NULL)
20528 vim_free(res);
20529 if (list != NULL)
20530 list_free(list, TRUE);
20531}
20532
20533/*
20534 * "system()" function
20535 */
20536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020537f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020538{
20539 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20540}
20541
20542/*
20543 * "systemlist()" function
20544 */
20545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020546f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020547{
20548 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549}
20550
20551/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020552 * "tabpagebuflist()" function
20553 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020555f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020556{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020557#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020558 tabpage_T *tp;
20559 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020560
20561 if (argvars[0].v_type == VAR_UNKNOWN)
20562 wp = firstwin;
20563 else
20564 {
20565 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20566 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020567 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020568 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020569 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020570 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020571 for (; wp != NULL; wp = wp->w_next)
20572 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020573 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020574 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020575 }
20576#endif
20577}
20578
20579
20580/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020581 * "tabpagenr()" function
20582 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020584f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020585{
20586 int nr = 1;
20587#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020588 char_u *arg;
20589
20590 if (argvars[0].v_type != VAR_UNKNOWN)
20591 {
20592 arg = get_tv_string_chk(&argvars[0]);
20593 nr = 0;
20594 if (arg != NULL)
20595 {
20596 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020597 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020598 else
20599 EMSG2(_(e_invexpr2), arg);
20600 }
20601 }
20602 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020603 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020604#endif
20605 rettv->vval.v_number = nr;
20606}
20607
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020608
20609#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020610static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020611
20612/*
20613 * Common code for tabpagewinnr() and winnr().
20614 */
20615 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020616get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020617{
20618 win_T *twin;
20619 int nr = 1;
20620 win_T *wp;
20621 char_u *arg;
20622
20623 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20624 if (argvar->v_type != VAR_UNKNOWN)
20625 {
20626 arg = get_tv_string_chk(argvar);
20627 if (arg == NULL)
20628 nr = 0; /* type error; errmsg already given */
20629 else if (STRCMP(arg, "$") == 0)
20630 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20631 else if (STRCMP(arg, "#") == 0)
20632 {
20633 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20634 if (twin == NULL)
20635 nr = 0;
20636 }
20637 else
20638 {
20639 EMSG2(_(e_invexpr2), arg);
20640 nr = 0;
20641 }
20642 }
20643
20644 if (nr > 0)
20645 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20646 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020647 {
20648 if (wp == NULL)
20649 {
20650 /* didn't find it in this tabpage */
20651 nr = 0;
20652 break;
20653 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020654 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020655 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020656 return nr;
20657}
20658#endif
20659
20660/*
20661 * "tabpagewinnr()" function
20662 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020664f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020665{
20666 int nr = 1;
20667#ifdef FEAT_WINDOWS
20668 tabpage_T *tp;
20669
20670 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20671 if (tp == NULL)
20672 nr = 0;
20673 else
20674 nr = get_winnr(tp, &argvars[1]);
20675#endif
20676 rettv->vval.v_number = nr;
20677}
20678
20679
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020680/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020681 * "tagfiles()" function
20682 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020684f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020685{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020686 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020687 tagname_T tn;
20688 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020689
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020690 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020691 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020692 fname = alloc(MAXPATHL);
20693 if (fname == NULL)
20694 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020695
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020696 for (first = TRUE; ; first = FALSE)
20697 if (get_tagfname(&tn, first, fname) == FAIL
20698 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020699 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020700 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020701 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020702}
20703
20704/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020705 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020706 */
20707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020708f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020709{
20710 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020711
20712 tag_pattern = get_tv_string(&argvars[0]);
20713
20714 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020715 if (*tag_pattern == NUL)
20716 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020717
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020718 if (rettv_list_alloc(rettv) == OK)
20719 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020720}
20721
20722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 * "tempname()" function
20724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020726f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727{
20728 static int x = 'A';
20729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020730 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020731 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732
20733 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20734 * names. Skip 'I' and 'O', they are used for shell redirection. */
20735 do
20736 {
20737 if (x == 'Z')
20738 x = '0';
20739 else if (x == '9')
20740 x = 'A';
20741 else
20742 {
20743#ifdef EBCDIC
20744 if (x == 'I')
20745 x = 'J';
20746 else if (x == 'R')
20747 x = 'S';
20748 else
20749#endif
20750 ++x;
20751 }
20752 } while (x == 'I' || x == 'O');
20753}
20754
20755/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020756 * "test(list)" function: Just checking the walls...
20757 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020759f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020760{
20761 /* Used for unit testing. Change the code below to your liking. */
20762#if 0
20763 listitem_T *li;
20764 list_T *l;
20765 char_u *bad, *good;
20766
20767 if (argvars[0].v_type != VAR_LIST)
20768 return;
20769 l = argvars[0].vval.v_list;
20770 if (l == NULL)
20771 return;
20772 li = l->lv_first;
20773 if (li == NULL)
20774 return;
20775 bad = get_tv_string(&li->li_tv);
20776 li = li->li_next;
20777 if (li == NULL)
20778 return;
20779 good = get_tv_string(&li->li_tv);
20780 rettv->vval.v_number = test_edit_score(bad, good);
20781#endif
20782}
20783
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020784#ifdef FEAT_FLOAT
20785/*
20786 * "tan()" function
20787 */
20788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020789f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020790{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020791 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020792
20793 rettv->v_type = VAR_FLOAT;
20794 if (get_float_arg(argvars, &f) == OK)
20795 rettv->vval.v_float = tan(f);
20796 else
20797 rettv->vval.v_float = 0.0;
20798}
20799
20800/*
20801 * "tanh()" function
20802 */
20803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020804f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020805{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020806 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020807
20808 rettv->v_type = VAR_FLOAT;
20809 if (get_float_arg(argvars, &f) == OK)
20810 rettv->vval.v_float = tanh(f);
20811 else
20812 rettv->vval.v_float = 0.0;
20813}
20814#endif
20815
Bram Moolenaard52d9742005-08-21 22:20:28 +000020816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 * "tolower(string)" function
20818 */
20819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020820f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821{
20822 char_u *p;
20823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020824 p = vim_strsave(get_tv_string(&argvars[0]));
20825 rettv->v_type = VAR_STRING;
20826 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827
20828 if (p != NULL)
20829 while (*p != NUL)
20830 {
20831#ifdef FEAT_MBYTE
20832 int l;
20833
20834 if (enc_utf8)
20835 {
20836 int c, lc;
20837
20838 c = utf_ptr2char(p);
20839 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020840 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 /* TODO: reallocate string when byte count changes. */
20842 if (utf_char2len(lc) == l)
20843 utf_char2bytes(lc, p);
20844 p += l;
20845 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020846 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 p += l; /* skip multi-byte character */
20848 else
20849#endif
20850 {
20851 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20852 ++p;
20853 }
20854 }
20855}
20856
20857/*
20858 * "toupper(string)" function
20859 */
20860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020861f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020863 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020864 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865}
20866
20867/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020868 * "tr(string, fromstr, tostr)" function
20869 */
20870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020871f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020872{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020873 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020874 char_u *fromstr;
20875 char_u *tostr;
20876 char_u *p;
20877#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020878 int inlen;
20879 int fromlen;
20880 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020881 int idx;
20882 char_u *cpstr;
20883 int cplen;
20884 int first = TRUE;
20885#endif
20886 char_u buf[NUMBUFLEN];
20887 char_u buf2[NUMBUFLEN];
20888 garray_T ga;
20889
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020890 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020891 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20892 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020893
20894 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020895 rettv->v_type = VAR_STRING;
20896 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020897 if (fromstr == NULL || tostr == NULL)
20898 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020899 ga_init2(&ga, (int)sizeof(char), 80);
20900
20901#ifdef FEAT_MBYTE
20902 if (!has_mbyte)
20903#endif
20904 /* not multi-byte: fromstr and tostr must be the same length */
20905 if (STRLEN(fromstr) != STRLEN(tostr))
20906 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020907#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020908error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020909#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020910 EMSG2(_(e_invarg2), fromstr);
20911 ga_clear(&ga);
20912 return;
20913 }
20914
20915 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020916 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020917 {
20918#ifdef FEAT_MBYTE
20919 if (has_mbyte)
20920 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020921 inlen = (*mb_ptr2len)(in_str);
20922 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020923 cplen = inlen;
20924 idx = 0;
20925 for (p = fromstr; *p != NUL; p += fromlen)
20926 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020927 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020928 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020929 {
20930 for (p = tostr; *p != NUL; p += tolen)
20931 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020932 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020933 if (idx-- == 0)
20934 {
20935 cplen = tolen;
20936 cpstr = p;
20937 break;
20938 }
20939 }
20940 if (*p == NUL) /* tostr is shorter than fromstr */
20941 goto error;
20942 break;
20943 }
20944 ++idx;
20945 }
20946
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020947 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020948 {
20949 /* Check that fromstr and tostr have the same number of
20950 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020951 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020952 first = FALSE;
20953 for (p = tostr; *p != NUL; p += tolen)
20954 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020955 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020956 --idx;
20957 }
20958 if (idx != 0)
20959 goto error;
20960 }
20961
Bram Moolenaarcde88542015-08-11 19:14:00 +020020962 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020963 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020964 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020965
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020966 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020967 }
20968 else
20969#endif
20970 {
20971 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020972 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020973 if (p != NULL)
20974 ga_append(&ga, tostr[p - fromstr]);
20975 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020976 ga_append(&ga, *in_str);
20977 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020978 }
20979 }
20980
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020981 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020982 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020983 ga_append(&ga, NUL);
20984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020985 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020986}
20987
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020988#ifdef FEAT_FLOAT
20989/*
20990 * "trunc({float})" function
20991 */
20992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020993f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020994{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020995 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020996
20997 rettv->v_type = VAR_FLOAT;
20998 if (get_float_arg(argvars, &f) == OK)
20999 /* trunc() is not in C90, use floor() or ceil() instead. */
21000 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21001 else
21002 rettv->vval.v_float = 0.0;
21003}
21004#endif
21005
Bram Moolenaar8299df92004-07-10 09:47:34 +000021006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 * "type(expr)" function
21008 */
21009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021010f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021012 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021013
21014 switch (argvars[0].v_type)
21015 {
21016 case VAR_NUMBER: n = 0; break;
21017 case VAR_STRING: n = 1; break;
21018 case VAR_FUNC: n = 2; break;
21019 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021020 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021021 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021022 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021023 if (argvars[0].vval.v_number == VVAL_FALSE
21024 || argvars[0].vval.v_number == VVAL_TRUE)
21025 n = 6;
21026 else
21027 n = 7;
21028 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021029 case VAR_JOB: n = 8; break;
21030 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021031 case VAR_UNKNOWN:
21032 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21033 n = -1;
21034 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021035 }
21036 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037}
21038
21039/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021040 * "undofile(name)" function
21041 */
21042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021043f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021044{
21045 rettv->v_type = VAR_STRING;
21046#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021047 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021048 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021049
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021050 if (*fname == NUL)
21051 {
21052 /* If there is no file name there will be no undo file. */
21053 rettv->vval.v_string = NULL;
21054 }
21055 else
21056 {
21057 char_u *ffname = FullName_save(fname, FALSE);
21058
21059 if (ffname != NULL)
21060 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21061 vim_free(ffname);
21062 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021063 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021064#else
21065 rettv->vval.v_string = NULL;
21066#endif
21067}
21068
21069/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021070 * "undotree()" function
21071 */
21072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021073f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021074{
21075 if (rettv_dict_alloc(rettv) == OK)
21076 {
21077 dict_T *dict = rettv->vval.v_dict;
21078 list_T *list;
21079
Bram Moolenaar730cde92010-06-27 05:18:54 +020021080 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021081 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021082 dict_add_nr_str(dict, "save_last",
21083 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021084 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21085 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021086 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021087
21088 list = list_alloc();
21089 if (list != NULL)
21090 {
21091 u_eval_tree(curbuf->b_u_oldhead, list);
21092 dict_add_list(dict, "entries", list);
21093 }
21094 }
21095}
21096
21097/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021098 * "values(dict)" function
21099 */
21100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021101f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021102{
21103 dict_list(argvars, rettv, 1);
21104}
21105
21106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 * "virtcol(string)" function
21108 */
21109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021110f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111{
21112 colnr_T vcol = 0;
21113 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021114 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021116 fp = var2fpos(&argvars[0], FALSE, &fnum);
21117 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21118 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 {
21120 getvvcol(curwin, fp, NULL, NULL, &vcol);
21121 ++vcol;
21122 }
21123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021124 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125}
21126
21127/*
21128 * "visualmode()" function
21129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021131f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 char_u str[2];
21134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021135 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 str[0] = curbuf->b_visual_mode_eval;
21137 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021138 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139
21140 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021141 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143}
21144
21145/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021146 * "wildmenumode()" function
21147 */
21148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021149f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021150{
21151#ifdef FEAT_WILDMENU
21152 if (wild_menu_showing)
21153 rettv->vval.v_number = 1;
21154#endif
21155}
21156
21157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 * "winbufnr(nr)" function
21159 */
21160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021161f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162{
21163 win_T *wp;
21164
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021165 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021167 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021169 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170}
21171
21172/*
21173 * "wincol()" function
21174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021176f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177{
21178 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021179 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180}
21181
21182/*
21183 * "winheight(nr)" function
21184 */
21185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021186f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187{
21188 win_T *wp;
21189
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021190 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021192 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021194 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195}
21196
21197/*
21198 * "winline()" function
21199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021201f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202{
21203 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021204 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205}
21206
21207/*
21208 * "winnr()" function
21209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021211f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212{
21213 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021214
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021216 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021218 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219}
21220
21221/*
21222 * "winrestcmd()" function
21223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021225f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226{
21227#ifdef FEAT_WINDOWS
21228 win_T *wp;
21229 int winnr = 1;
21230 garray_T ga;
21231 char_u buf[50];
21232
21233 ga_init2(&ga, (int)sizeof(char), 70);
21234 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21235 {
21236 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21237 ga_concat(&ga, buf);
21238# ifdef FEAT_VERTSPLIT
21239 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21240 ga_concat(&ga, buf);
21241# endif
21242 ++winnr;
21243 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021244 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021246 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021248 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021250 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251}
21252
21253/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021254 * "winrestview()" function
21255 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021257f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021258{
21259 dict_T *dict;
21260
21261 if (argvars[0].v_type != VAR_DICT
21262 || (dict = argvars[0].vval.v_dict) == NULL)
21263 EMSG(_(e_invarg));
21264 else
21265 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021266 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21267 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21268 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21269 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021270#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021271 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21272 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021273#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021274 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21275 {
21276 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21277 curwin->w_set_curswant = FALSE;
21278 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021279
Bram Moolenaar82c25852014-05-28 16:47:16 +020021280 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21281 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021282#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021283 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21284 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021285#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021286 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21287 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21288 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21289 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021290
21291 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021292 win_new_height(curwin, curwin->w_height);
21293# ifdef FEAT_VERTSPLIT
21294 win_new_width(curwin, W_WIDTH(curwin));
21295# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021296 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021297
Bram Moolenaarb851a962014-10-31 15:45:52 +010021298 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021299 curwin->w_topline = 1;
21300 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21301 curwin->w_topline = curbuf->b_ml.ml_line_count;
21302#ifdef FEAT_DIFF
21303 check_topfill(curwin, TRUE);
21304#endif
21305 }
21306}
21307
21308/*
21309 * "winsaveview()" function
21310 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021312f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021313{
21314 dict_T *dict;
21315
Bram Moolenaara800b422010-06-27 01:15:55 +020021316 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021317 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021318 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021319
21320 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21321 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21322#ifdef FEAT_VIRTUALEDIT
21323 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21324#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021325 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021326 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21327
21328 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21329#ifdef FEAT_DIFF
21330 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21331#endif
21332 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21333 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21334}
21335
21336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 * "winwidth(nr)" function
21338 */
21339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021340f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341{
21342 win_T *wp;
21343
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021344 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021346 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 else
21348#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021349 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021351 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352#endif
21353}
21354
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021356 * "wordcount()" function
21357 */
21358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021359f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021360{
21361 if (rettv_dict_alloc(rettv) == FAIL)
21362 return;
21363 cursor_pos_info(rettv->vval.v_dict);
21364}
21365
21366/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021367 * Write list of strings to file
21368 */
21369 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021370write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021371{
21372 listitem_T *li;
21373 int c;
21374 int ret = OK;
21375 char_u *s;
21376
21377 for (li = list->lv_first; li != NULL; li = li->li_next)
21378 {
21379 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21380 {
21381 if (*s == '\n')
21382 c = putc(NUL, fd);
21383 else
21384 c = putc(*s, fd);
21385 if (c == EOF)
21386 {
21387 ret = FAIL;
21388 break;
21389 }
21390 }
21391 if (!binary || li->li_next != NULL)
21392 if (putc('\n', fd) == EOF)
21393 {
21394 ret = FAIL;
21395 break;
21396 }
21397 if (ret == FAIL)
21398 {
21399 EMSG(_(e_write));
21400 break;
21401 }
21402 }
21403 return ret;
21404}
21405
21406/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021407 * "writefile()" function
21408 */
21409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021410f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021411{
21412 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021413 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021414 char_u *fname;
21415 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021416 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021417
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021418 if (check_restricted() || check_secure())
21419 return;
21420
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021421 if (argvars[0].v_type != VAR_LIST)
21422 {
21423 EMSG2(_(e_listarg), "writefile()");
21424 return;
21425 }
21426 if (argvars[0].vval.v_list == NULL)
21427 return;
21428
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021429 if (argvars[2].v_type != VAR_UNKNOWN)
21430 {
21431 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21432 binary = TRUE;
21433 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21434 append = TRUE;
21435 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021436
21437 /* Always open the file in binary mode, library functions have a mind of
21438 * their own about CR-LF conversion. */
21439 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021440 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21441 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021442 {
21443 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21444 ret = -1;
21445 }
21446 else
21447 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021448 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21449 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021450 fclose(fd);
21451 }
21452
21453 rettv->vval.v_number = ret;
21454}
21455
21456/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021457 * "xor(expr, expr)" function
21458 */
21459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021460f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021461{
21462 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21463 ^ get_tv_number_chk(&argvars[1], NULL);
21464}
21465
21466
21467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021469 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470 */
21471 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021472var2fpos(
21473 typval_T *varp,
21474 int dollar_lnum, /* TRUE when $ is last line */
21475 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021477 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021479 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480
Bram Moolenaara5525202006-03-02 22:52:09 +000021481 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021482 if (varp->v_type == VAR_LIST)
21483 {
21484 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021485 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021486 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021487 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021488
21489 l = varp->vval.v_list;
21490 if (l == NULL)
21491 return NULL;
21492
21493 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021494 pos.lnum = list_find_nr(l, 0L, &error);
21495 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021496 return NULL; /* invalid line number */
21497
21498 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021499 pos.col = list_find_nr(l, 1L, &error);
21500 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021501 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021502 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021503
21504 /* We accept "$" for the column number: last column. */
21505 li = list_find(l, 1L);
21506 if (li != NULL && li->li_tv.v_type == VAR_STRING
21507 && li->li_tv.vval.v_string != NULL
21508 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21509 pos.col = len + 1;
21510
Bram Moolenaara5525202006-03-02 22:52:09 +000021511 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021512 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021513 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021514 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021515
Bram Moolenaara5525202006-03-02 22:52:09 +000021516#ifdef FEAT_VIRTUALEDIT
21517 /* Get the virtual offset. Defaults to zero. */
21518 pos.coladd = list_find_nr(l, 2L, &error);
21519 if (error)
21520 pos.coladd = 0;
21521#endif
21522
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021523 return &pos;
21524 }
21525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021526 name = get_tv_string_chk(varp);
21527 if (name == NULL)
21528 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021529 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021531 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21532 {
21533 if (VIsual_active)
21534 return &VIsual;
21535 return &curwin->w_cursor;
21536 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021537 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021539 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21541 return NULL;
21542 return pp;
21543 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021544
21545#ifdef FEAT_VIRTUALEDIT
21546 pos.coladd = 0;
21547#endif
21548
Bram Moolenaar477933c2007-07-17 14:32:23 +000021549 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021550 {
21551 pos.col = 0;
21552 if (name[1] == '0') /* "w0": first visible line */
21553 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021554 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021555 pos.lnum = curwin->w_topline;
21556 return &pos;
21557 }
21558 else if (name[1] == '$') /* "w$": last visible line */
21559 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021560 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021561 pos.lnum = curwin->w_botline - 1;
21562 return &pos;
21563 }
21564 }
21565 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021567 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 {
21569 pos.lnum = curbuf->b_ml.ml_line_count;
21570 pos.col = 0;
21571 }
21572 else
21573 {
21574 pos.lnum = curwin->w_cursor.lnum;
21575 pos.col = (colnr_T)STRLEN(ml_get_curline());
21576 }
21577 return &pos;
21578 }
21579 return NULL;
21580}
21581
21582/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021583 * Convert list in "arg" into a position and optional file number.
21584 * When "fnump" is NULL there is no file number, only 3 items.
21585 * Note that the column is passed on as-is, the caller may want to decrement
21586 * it to use 1 for the first column.
21587 * Return FAIL when conversion is not possible, doesn't check the position for
21588 * validity.
21589 */
21590 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021591list2fpos(
21592 typval_T *arg,
21593 pos_T *posp,
21594 int *fnump,
21595 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021596{
21597 list_T *l = arg->vval.v_list;
21598 long i = 0;
21599 long n;
21600
Bram Moolenaar493c1782014-05-28 14:34:46 +020021601 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21602 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021603 if (arg->v_type != VAR_LIST
21604 || l == NULL
21605 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021606 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021607 return FAIL;
21608
21609 if (fnump != NULL)
21610 {
21611 n = list_find_nr(l, i++, NULL); /* fnum */
21612 if (n < 0)
21613 return FAIL;
21614 if (n == 0)
21615 n = curbuf->b_fnum; /* current buffer */
21616 *fnump = n;
21617 }
21618
21619 n = list_find_nr(l, i++, NULL); /* lnum */
21620 if (n < 0)
21621 return FAIL;
21622 posp->lnum = n;
21623
21624 n = list_find_nr(l, i++, NULL); /* col */
21625 if (n < 0)
21626 return FAIL;
21627 posp->col = n;
21628
21629#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021630 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021631 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021632 posp->coladd = 0;
21633 else
21634 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021635#endif
21636
Bram Moolenaar493c1782014-05-28 14:34:46 +020021637 if (curswantp != NULL)
21638 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21639
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021640 return OK;
21641}
21642
21643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 * Get the length of an environment variable name.
21645 * Advance "arg" to the first character after the name.
21646 * Return 0 for error.
21647 */
21648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021649get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650{
21651 char_u *p;
21652 int len;
21653
21654 for (p = *arg; vim_isIDc(*p); ++p)
21655 ;
21656 if (p == *arg) /* no name found */
21657 return 0;
21658
21659 len = (int)(p - *arg);
21660 *arg = p;
21661 return len;
21662}
21663
21664/*
21665 * Get the length of the name of a function or internal variable.
21666 * "arg" is advanced to the first non-white character after the name.
21667 * Return 0 if something is wrong.
21668 */
21669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021670get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671{
21672 char_u *p;
21673 int len;
21674
21675 /* Find the end of the name. */
21676 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021677 {
21678 if (*p == ':')
21679 {
21680 /* "s:" is start of "s:var", but "n:" is not and can be used in
21681 * slice "[n:]". Also "xx:" is not a namespace. */
21682 len = (int)(p - *arg);
21683 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21684 || len > 1)
21685 break;
21686 }
21687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688 if (p == *arg) /* no name found */
21689 return 0;
21690
21691 len = (int)(p - *arg);
21692 *arg = skipwhite(p);
21693
21694 return len;
21695}
21696
21697/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021698 * Get the length of the name of a variable or function.
21699 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021701 * Return -1 if curly braces expansion failed.
21702 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 * If the name contains 'magic' {}'s, expand them and return the
21704 * expanded name in an allocated string via 'alias' - caller must free.
21705 */
21706 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021707get_name_len(
21708 char_u **arg,
21709 char_u **alias,
21710 int evaluate,
21711 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712{
21713 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 char_u *p;
21715 char_u *expr_start;
21716 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717
21718 *alias = NULL; /* default to no alias */
21719
21720 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21721 && (*arg)[2] == (int)KE_SNR)
21722 {
21723 /* hard coded <SNR>, already translated */
21724 *arg += 3;
21725 return get_id_len(arg) + 3;
21726 }
21727 len = eval_fname_script(*arg);
21728 if (len > 0)
21729 {
21730 /* literal "<SID>", "s:" or "<SNR>" */
21731 *arg += len;
21732 }
21733
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021735 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021737 p = find_name_end(*arg, &expr_start, &expr_end,
21738 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 if (expr_start != NULL)
21740 {
21741 char_u *temp_string;
21742
21743 if (!evaluate)
21744 {
21745 len += (int)(p - *arg);
21746 *arg = skipwhite(p);
21747 return len;
21748 }
21749
21750 /*
21751 * Include any <SID> etc in the expanded string:
21752 * Thus the -len here.
21753 */
21754 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21755 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021756 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 *alias = temp_string;
21758 *arg = skipwhite(p);
21759 return (int)STRLEN(temp_string);
21760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761
21762 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021763 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 EMSG2(_(e_invexpr2), *arg);
21765
21766 return len;
21767}
21768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021769/*
21770 * Find the end of a variable or function name, taking care of magic braces.
21771 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21772 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021773 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021774 * Return a pointer to just after the name. Equal to "arg" if there is no
21775 * valid name.
21776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021778find_name_end(
21779 char_u *arg,
21780 char_u **expr_start,
21781 char_u **expr_end,
21782 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021784 int mb_nest = 0;
21785 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021787 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021789 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021791 *expr_start = NULL;
21792 *expr_end = NULL;
21793 }
21794
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021795 /* Quick check for valid starting character. */
21796 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21797 return arg;
21798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021799 for (p = arg; *p != NUL
21800 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021801 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021802 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021803 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021804 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021805 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021806 if (*p == '\'')
21807 {
21808 /* skip over 'string' to avoid counting [ and ] inside it. */
21809 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21810 ;
21811 if (*p == NUL)
21812 break;
21813 }
21814 else if (*p == '"')
21815 {
21816 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21817 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21818 if (*p == '\\' && p[1] != NUL)
21819 ++p;
21820 if (*p == NUL)
21821 break;
21822 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021823 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21824 {
21825 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021826 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021827 len = (int)(p - arg);
21828 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021829 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021830 break;
21831 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021833 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021835 if (*p == '[')
21836 ++br_nest;
21837 else if (*p == ']')
21838 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021841 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021843 if (*p == '{')
21844 {
21845 mb_nest++;
21846 if (expr_start != NULL && *expr_start == NULL)
21847 *expr_start = p;
21848 }
21849 else if (*p == '}')
21850 {
21851 mb_nest--;
21852 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21853 *expr_end = p;
21854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 }
21857
21858 return p;
21859}
21860
21861/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021862 * Expands out the 'magic' {}'s in a variable/function name.
21863 * Note that this can call itself recursively, to deal with
21864 * constructs like foo{bar}{baz}{bam}
21865 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21866 * "in_start" ^
21867 * "expr_start" ^
21868 * "expr_end" ^
21869 * "in_end" ^
21870 *
21871 * Returns a new allocated string, which the caller must free.
21872 * Returns NULL for failure.
21873 */
21874 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021875make_expanded_name(
21876 char_u *in_start,
21877 char_u *expr_start,
21878 char_u *expr_end,
21879 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021880{
21881 char_u c1;
21882 char_u *retval = NULL;
21883 char_u *temp_result;
21884 char_u *nextcmd = NULL;
21885
21886 if (expr_end == NULL || in_end == NULL)
21887 return NULL;
21888 *expr_start = NUL;
21889 *expr_end = NUL;
21890 c1 = *in_end;
21891 *in_end = NUL;
21892
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021893 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021894 if (temp_result != NULL && nextcmd == NULL)
21895 {
21896 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21897 + (in_end - expr_end) + 1));
21898 if (retval != NULL)
21899 {
21900 STRCPY(retval, in_start);
21901 STRCAT(retval, temp_result);
21902 STRCAT(retval, expr_end + 1);
21903 }
21904 }
21905 vim_free(temp_result);
21906
21907 *in_end = c1; /* put char back for error messages */
21908 *expr_start = '{';
21909 *expr_end = '}';
21910
21911 if (retval != NULL)
21912 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021913 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021914 if (expr_start != NULL)
21915 {
21916 /* Further expansion! */
21917 temp_result = make_expanded_name(retval, expr_start,
21918 expr_end, temp_result);
21919 vim_free(retval);
21920 retval = temp_result;
21921 }
21922 }
21923
21924 return retval;
21925}
21926
21927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021929 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930 */
21931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021932eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021934 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21935}
21936
21937/*
21938 * Return TRUE if character "c" can be used as the first character in a
21939 * variable or function name (excluding '{' and '}').
21940 */
21941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021942eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021943{
21944 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945}
21946
21947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 * Set number v: variable to "val".
21949 */
21950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021951set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021953 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954}
21955
21956/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021957 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 */
21959 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021960get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021962 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963}
21964
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021965/*
21966 * Get string v: variable value. Uses a static buffer, can only be used once.
21967 */
21968 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021969get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021970{
21971 return get_tv_string(&vimvars[idx].vv_tv);
21972}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021973
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021975 * Get List v: variable value. Caller must take care of reference count when
21976 * needed.
21977 */
21978 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021979get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021980{
21981 return vimvars[idx].vv_list;
21982}
21983
21984/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021985 * Set v:char to character "c".
21986 */
21987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021988set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021989{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021990 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021991
21992#ifdef FEAT_MBYTE
21993 if (has_mbyte)
21994 buf[(*mb_char2bytes)(c, buf)] = NUL;
21995 else
21996#endif
21997 {
21998 buf[0] = c;
21999 buf[1] = NUL;
22000 }
22001 set_vim_var_string(VV_CHAR, buf, -1);
22002}
22003
22004/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022005 * Set v:count to "count" and v:count1 to "count1".
22006 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 */
22008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022009set_vcount(
22010 long count,
22011 long count1,
22012 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022014 if (set_prevcount)
22015 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022016 vimvars[VV_COUNT].vv_nr = count;
22017 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018}
22019
22020/*
22021 * Set string v: variable to a copy of "val".
22022 */
22023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022024set_vim_var_string(
22025 int idx,
22026 char_u *val,
22027 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028{
Bram Moolenaara542c682016-01-31 16:28:04 +010022029 clear_tv(&vimvars[idx].vv_di.di_tv);
22030 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022032 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022034 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022036 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037}
22038
22039/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022040 * Set List v: variable to "val".
22041 */
22042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022043set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022044{
Bram Moolenaara542c682016-01-31 16:28:04 +010022045 clear_tv(&vimvars[idx].vv_di.di_tv);
22046 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022047 vimvars[idx].vv_list = val;
22048 if (val != NULL)
22049 ++val->lv_refcount;
22050}
22051
22052/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022053 * Set Dictionary v: variable to "val".
22054 */
22055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022056set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022057{
22058 int todo;
22059 hashitem_T *hi;
22060
Bram Moolenaara542c682016-01-31 16:28:04 +010022061 clear_tv(&vimvars[idx].vv_di.di_tv);
22062 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022063 vimvars[idx].vv_dict = val;
22064 if (val != NULL)
22065 {
22066 ++val->dv_refcount;
22067
22068 /* Set readonly */
22069 todo = (int)val->dv_hashtab.ht_used;
22070 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22071 {
22072 if (HASHITEM_EMPTY(hi))
22073 continue;
22074 --todo;
22075 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22076 }
22077 }
22078}
22079
22080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 * Set v:register if needed.
22082 */
22083 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022084set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085{
22086 char_u regname;
22087
22088 if (c == 0 || c == ' ')
22089 regname = '"';
22090 else
22091 regname = c;
22092 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022093 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 set_vim_var_string(VV_REG, &regname, 1);
22095}
22096
22097/*
22098 * Get or set v:exception. If "oldval" == NULL, return the current value.
22099 * Otherwise, restore the value to "oldval" and return NULL.
22100 * Must always be called in pairs to save and restore v:exception! Does not
22101 * take care of memory allocations.
22102 */
22103 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022104v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105{
22106 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022107 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108
Bram Moolenaare9a41262005-01-15 22:18:47 +000022109 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110 return NULL;
22111}
22112
22113/*
22114 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22115 * Otherwise, restore the value to "oldval" and return NULL.
22116 * Must always be called in pairs to save and restore v:throwpoint! Does not
22117 * take care of memory allocations.
22118 */
22119 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022120v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121{
22122 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022123 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124
Bram Moolenaare9a41262005-01-15 22:18:47 +000022125 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126 return NULL;
22127}
22128
22129#if defined(FEAT_AUTOCMD) || defined(PROTO)
22130/*
22131 * Set v:cmdarg.
22132 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22133 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22134 * Must always be called in pairs!
22135 */
22136 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022137set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138{
22139 char_u *oldval;
22140 char_u *newval;
22141 unsigned len;
22142
Bram Moolenaare9a41262005-01-15 22:18:47 +000022143 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022144 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022146 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022147 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022148 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 }
22150
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022151 if (eap->force_bin == FORCE_BIN)
22152 len = 6;
22153 else if (eap->force_bin == FORCE_NOBIN)
22154 len = 8;
22155 else
22156 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022157
22158 if (eap->read_edit)
22159 len += 7;
22160
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022161 if (eap->force_ff != 0)
22162 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22163# ifdef FEAT_MBYTE
22164 if (eap->force_enc != 0)
22165 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022166 if (eap->bad_char != 0)
22167 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022168# endif
22169
22170 newval = alloc(len + 1);
22171 if (newval == NULL)
22172 return NULL;
22173
22174 if (eap->force_bin == FORCE_BIN)
22175 sprintf((char *)newval, " ++bin");
22176 else if (eap->force_bin == FORCE_NOBIN)
22177 sprintf((char *)newval, " ++nobin");
22178 else
22179 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022180
22181 if (eap->read_edit)
22182 STRCAT(newval, " ++edit");
22183
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022184 if (eap->force_ff != 0)
22185 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22186 eap->cmd + eap->force_ff);
22187# ifdef FEAT_MBYTE
22188 if (eap->force_enc != 0)
22189 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22190 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022191 if (eap->bad_char == BAD_KEEP)
22192 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22193 else if (eap->bad_char == BAD_DROP)
22194 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22195 else if (eap->bad_char != 0)
22196 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022197# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022198 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022199 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200}
22201#endif
22202
22203/*
22204 * Get the value of internal variable "name".
22205 * Return OK or FAIL.
22206 */
22207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022208get_var_tv(
22209 char_u *name,
22210 int len, /* length of "name" */
22211 typval_T *rettv, /* NULL when only checking existence */
22212 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22213 int verbose, /* may give error message */
22214 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215{
22216 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022217 typval_T *tv = NULL;
22218 typval_T atv;
22219 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221
22222 /* truncate the name, so that we can use strcmp() */
22223 cc = name[len];
22224 name[len] = NUL;
22225
22226 /*
22227 * Check for "b:changedtick".
22228 */
22229 if (STRCMP(name, "b:changedtick") == 0)
22230 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022231 atv.v_type = VAR_NUMBER;
22232 atv.vval.v_number = curbuf->b_changedtick;
22233 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 }
22235
22236 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 * Check for user-defined variables.
22238 */
22239 else
22240 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022241 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022243 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022244 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022245 if (dip != NULL)
22246 *dip = v;
22247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 }
22249
Bram Moolenaare9a41262005-01-15 22:18:47 +000022250 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022252 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022253 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254 ret = FAIL;
22255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022256 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022257 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258
22259 name[len] = cc;
22260
22261 return ret;
22262}
22263
22264/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022265 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22266 * Also handle function call with Funcref variable: func(expr)
22267 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22268 */
22269 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022270handle_subscript(
22271 char_u **arg,
22272 typval_T *rettv,
22273 int evaluate, /* do more than finding the end */
22274 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022275{
22276 int ret = OK;
22277 dict_T *selfdict = NULL;
22278 char_u *s;
22279 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022280 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022281
22282 while (ret == OK
22283 && (**arg == '['
22284 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022285 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022286 && !vim_iswhite(*(*arg - 1)))
22287 {
22288 if (**arg == '(')
22289 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022290 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022291 if (evaluate)
22292 {
22293 functv = *rettv;
22294 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022295
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022296 /* Invoke the function. Recursive! */
22297 s = functv.vval.v_string;
22298 }
22299 else
22300 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022301 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022302 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22303 &len, evaluate, selfdict);
22304
22305 /* Clear the funcref afterwards, so that deleting it while
22306 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022307 if (evaluate)
22308 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022309
22310 /* Stop the expression evaluation when immediately aborting on
22311 * error, or when an interrupt occurred or an exception was thrown
22312 * but not caught. */
22313 if (aborting())
22314 {
22315 if (ret == OK)
22316 clear_tv(rettv);
22317 ret = FAIL;
22318 }
22319 dict_unref(selfdict);
22320 selfdict = NULL;
22321 }
22322 else /* **arg == '[' || **arg == '.' */
22323 {
22324 dict_unref(selfdict);
22325 if (rettv->v_type == VAR_DICT)
22326 {
22327 selfdict = rettv->vval.v_dict;
22328 if (selfdict != NULL)
22329 ++selfdict->dv_refcount;
22330 }
22331 else
22332 selfdict = NULL;
22333 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22334 {
22335 clear_tv(rettv);
22336 ret = FAIL;
22337 }
22338 }
22339 }
22340 dict_unref(selfdict);
22341 return ret;
22342}
22343
22344/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022345 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022346 * value).
22347 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022348 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022349alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022350{
Bram Moolenaar33570922005-01-25 22:26:29 +000022351 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022352}
22353
22354/*
22355 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 * The string "s" must have been allocated, it is consumed.
22357 * Return NULL for out of memory, the variable otherwise.
22358 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022359 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022360alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361{
Bram Moolenaar33570922005-01-25 22:26:29 +000022362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022364 rettv = alloc_tv();
22365 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022367 rettv->v_type = VAR_STRING;
22368 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 }
22370 else
22371 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022372 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373}
22374
22375/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022376 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022379free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380{
22381 if (varp != NULL)
22382 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022383 switch (varp->v_type)
22384 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022385 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022386 func_unref(varp->vval.v_string);
22387 /*FALLTHROUGH*/
22388 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022389 vim_free(varp->vval.v_string);
22390 break;
22391 case VAR_LIST:
22392 list_unref(varp->vval.v_list);
22393 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022394 case VAR_DICT:
22395 dict_unref(varp->vval.v_dict);
22396 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022397 case VAR_JOB:
22398#ifdef FEAT_JOB
22399 job_unref(varp->vval.v_job);
22400 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022401#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022402 case VAR_CHANNEL:
22403#ifdef FEAT_CHANNEL
22404 channel_unref(varp->vval.v_channel);
22405 break;
22406#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022407 case VAR_NUMBER:
22408 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022409 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022410 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022411 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 vim_free(varp);
22414 }
22415}
22416
22417/*
22418 * Free the memory for a variable value and set the value to NULL or 0.
22419 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022420 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022421clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422{
22423 if (varp != NULL)
22424 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022425 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022427 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022428 func_unref(varp->vval.v_string);
22429 /*FALLTHROUGH*/
22430 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022431 vim_free(varp->vval.v_string);
22432 varp->vval.v_string = NULL;
22433 break;
22434 case VAR_LIST:
22435 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022436 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022437 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022438 case VAR_DICT:
22439 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022440 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022441 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022442 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022443 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022444 varp->vval.v_number = 0;
22445 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022446 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022447#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022448 varp->vval.v_float = 0.0;
22449 break;
22450#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022451 case VAR_JOB:
22452#ifdef FEAT_JOB
22453 job_unref(varp->vval.v_job);
22454 varp->vval.v_job = NULL;
22455#endif
22456 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022457 case VAR_CHANNEL:
22458#ifdef FEAT_CHANNEL
22459 channel_unref(varp->vval.v_channel);
22460 varp->vval.v_channel = NULL;
22461#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022462 case VAR_UNKNOWN:
22463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022465 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 }
22467}
22468
22469/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022470 * Set the value of a variable to NULL without freeing items.
22471 */
22472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022473init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022474{
22475 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022476 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022477}
22478
22479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480 * Get the number value of a variable.
22481 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022482 * For incompatible types, return 0.
22483 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22484 * caller of incompatible types: it sets *denote to TRUE if "denote"
22485 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 */
22487 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022488get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022490 int error = FALSE;
22491
22492 return get_tv_number_chk(varp, &error); /* return 0L on error */
22493}
22494
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022495 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022496get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022497{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022498 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022500 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022502 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022503 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022504 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022505#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022506 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022507 break;
22508#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022509 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022510 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022511 break;
22512 case VAR_STRING:
22513 if (varp->vval.v_string != NULL)
22514 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022515 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022516 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022517 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022518 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022519 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022520 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022521 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022522 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022523 case VAR_SPECIAL:
22524 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22525 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022526 case VAR_JOB:
22527#ifdef FEAT_JOB
22528 EMSG(_("E910: Using a Job as a Number"));
22529 break;
22530#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022531 case VAR_CHANNEL:
22532#ifdef FEAT_CHANNEL
22533 EMSG(_("E913: Using a Channel as a Number"));
22534 break;
22535#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022536 case VAR_UNKNOWN:
22537 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022538 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022540 if (denote == NULL) /* useful for values that must be unsigned */
22541 n = -1;
22542 else
22543 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022544 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545}
22546
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022547#ifdef FEAT_FLOAT
22548 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022549get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022550{
22551 switch (varp->v_type)
22552 {
22553 case VAR_NUMBER:
22554 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022555 case VAR_FLOAT:
22556 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022557 case VAR_FUNC:
22558 EMSG(_("E891: Using a Funcref as a Float"));
22559 break;
22560 case VAR_STRING:
22561 EMSG(_("E892: Using a String as a Float"));
22562 break;
22563 case VAR_LIST:
22564 EMSG(_("E893: Using a List as a Float"));
22565 break;
22566 case VAR_DICT:
22567 EMSG(_("E894: Using a Dictionary as a Float"));
22568 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022569 case VAR_SPECIAL:
22570 EMSG(_("E907: Using a special value as a Float"));
22571 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022572 case VAR_JOB:
22573# ifdef FEAT_JOB
22574 EMSG(_("E911: Using a Job as a Float"));
22575 break;
22576# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022577 case VAR_CHANNEL:
22578# ifdef FEAT_CHANNEL
22579 EMSG(_("E914: Using a Channel as a Float"));
22580 break;
22581# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022582 case VAR_UNKNOWN:
22583 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022584 break;
22585 }
22586 return 0;
22587}
22588#endif
22589
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022591 * Get the lnum from the first argument.
22592 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022593 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 */
22595 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022596get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597{
Bram Moolenaar33570922005-01-25 22:26:29 +000022598 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 linenr_T lnum;
22600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022601 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 if (lnum == 0) /* no valid number, try using line() */
22603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022604 rettv.v_type = VAR_NUMBER;
22605 f_line(argvars, &rettv);
22606 lnum = rettv.vval.v_number;
22607 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022608 }
22609 return lnum;
22610}
22611
22612/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022613 * Get the lnum from the first argument.
22614 * Also accepts "$", then "buf" is used.
22615 * Returns 0 on error.
22616 */
22617 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022618get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022619{
22620 if (argvars[0].v_type == VAR_STRING
22621 && argvars[0].vval.v_string != NULL
22622 && argvars[0].vval.v_string[0] == '$'
22623 && buf != NULL)
22624 return buf->b_ml.ml_line_count;
22625 return get_tv_number_chk(&argvars[0], NULL);
22626}
22627
22628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 * Get the string value of a variable.
22630 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022631 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22632 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 * If the String variable has never been set, return an empty string.
22634 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022635 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22636 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 */
22638 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022639get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640{
22641 static char_u mybuf[NUMBUFLEN];
22642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022643 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644}
22645
22646 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022647get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022649 char_u *res = get_tv_string_buf_chk(varp, buf);
22650
22651 return res != NULL ? res : (char_u *)"";
22652}
22653
Bram Moolenaar7d647822014-04-05 21:28:56 +020022654/*
22655 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22656 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022657 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022658get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022659{
22660 static char_u mybuf[NUMBUFLEN];
22661
22662 return get_tv_string_buf_chk(varp, mybuf);
22663}
22664
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022665 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022666get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022667{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022668 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022670 case VAR_NUMBER:
22671 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22672 return buf;
22673 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022674 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022675 break;
22676 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022677 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022678 break;
22679 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022680 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022681 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022682 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022683#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022684 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022685 break;
22686#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022687 case VAR_STRING:
22688 if (varp->vval.v_string != NULL)
22689 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022690 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022691 case VAR_SPECIAL:
22692 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22693 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022694 case VAR_JOB:
22695#ifdef FEAT_JOB
22696 {
22697 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022698 char *status;
22699
22700 if (job == NULL)
22701 return (char_u *)"no process";
22702 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022703 : job->jv_status == JOB_ENDED ? "dead"
22704 : "run";
22705# ifdef UNIX
22706 vim_snprintf((char *)buf, NUMBUFLEN,
22707 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022708# elif defined(WIN32)
22709 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022710 "process %ld %s",
22711 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022712 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022713# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022714 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022715 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22716# endif
22717 return buf;
22718 }
22719#endif
22720 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022721 case VAR_CHANNEL:
22722#ifdef FEAT_CHANNEL
22723 {
22724 channel_T *channel = varp->vval.v_channel;
22725 char *status = channel_status(channel);
22726
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022727 if (channel == NULL)
22728 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22729 else
22730 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022731 "channel %d %s", channel->ch_id, status);
22732 return buf;
22733 }
22734#endif
22735 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022736 case VAR_UNKNOWN:
22737 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022738 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022740 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741}
22742
22743/*
22744 * Find variable "name" in the list of variables.
22745 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022746 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022747 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022748 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022750 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022751find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755
Bram Moolenaara7043832005-01-21 11:56:39 +000022756 ht = find_var_ht(name, &varname);
22757 if (htp != NULL)
22758 *htp = ht;
22759 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022761 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762}
22763
22764/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022765 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022766 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022768 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022769find_var_in_ht(
22770 hashtab_T *ht,
22771 int htname,
22772 char_u *varname,
22773 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022774{
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 hashitem_T *hi;
22776
22777 if (*varname == NUL)
22778 {
22779 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022780 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022781 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022782 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022783 case 'g': return &globvars_var;
22784 case 'v': return &vimvars_var;
22785 case 'b': return &curbuf->b_bufvar;
22786 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022787#ifdef FEAT_WINDOWS
22788 case 't': return &curtab->tp_winvar;
22789#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022790 case 'l': return current_funccal == NULL
22791 ? NULL : &current_funccal->l_vars_var;
22792 case 'a': return current_funccal == NULL
22793 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022794 }
22795 return NULL;
22796 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022797
22798 hi = hash_find(ht, varname);
22799 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022800 {
22801 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022802 * worked find the variable again. Don't auto-load a script if it was
22803 * loaded already, otherwise it would be loaded every time when
22804 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022805 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022806 {
22807 /* Note: script_autoload() may make "hi" invalid. It must either
22808 * be obtained again or not used. */
22809 if (!script_autoload(varname, FALSE) || aborting())
22810 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022811 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022812 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022813 if (HASHITEM_EMPTY(hi))
22814 return NULL;
22815 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022816 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022817}
22818
22819/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022820 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022821 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022822 * Set "varname" to the start of name without ':'.
22823 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022824 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022825find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022827 hashitem_T *hi;
22828
Bram Moolenaar73627d02015-08-11 15:46:09 +020022829 if (name[0] == NUL)
22830 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 if (name[1] != ':')
22832 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022833 /* The name must not start with a colon or #. */
22834 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835 return NULL;
22836 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022837
22838 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022839 hi = hash_find(&compat_hashtab, name);
22840 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022841 return &compat_hashtab;
22842
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022844 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022845 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 }
22847 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022848 if (*name == 'g') /* global variable */
22849 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022850 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22851 */
22852 if (vim_strchr(name + 2, ':') != NULL
22853 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022854 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022856 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022858 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022859#ifdef FEAT_WINDOWS
22860 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022861 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022862#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022863 if (*name == 'v') /* v: variable */
22864 return &vimvarht;
22865 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022866 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022867 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022868 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 if (*name == 's' /* script variable */
22870 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22871 return &SCRIPT_VARS(current_SID);
22872 return NULL;
22873}
22874
22875/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022876 * Get function call environment based on bactrace debug level
22877 */
22878 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022879get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022880{
22881 int i;
22882 funccall_T *funccal;
22883 funccall_T *temp_funccal;
22884
22885 funccal = current_funccal;
22886 if (debug_backtrace_level > 0)
22887 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022888 for (i = 0; i < debug_backtrace_level; i++)
22889 {
22890 temp_funccal = funccal->caller;
22891 if (temp_funccal)
22892 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022893 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022894 /* backtrace level overflow. reset to max */
22895 debug_backtrace_level = i;
22896 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022897 }
22898 return funccal;
22899}
22900
22901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022903 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 * Returns NULL when it doesn't exist.
22905 */
22906 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022907get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908{
Bram Moolenaar33570922005-01-25 22:26:29 +000022909 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022911 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 if (v == NULL)
22913 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022914 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915}
22916
22917/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022918 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 * sourcing this script and when executing functions defined in the script.
22920 */
22921 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022922new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923{
Bram Moolenaara7043832005-01-21 11:56:39 +000022924 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022925 hashtab_T *ht;
22926 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022927
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22929 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022930 /* Re-allocating ga_data means that an ht_array pointing to
22931 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022932 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022933 for (i = 1; i <= ga_scripts.ga_len; ++i)
22934 {
22935 ht = &SCRIPT_VARS(i);
22936 if (ht->ht_mask == HT_INIT_SIZE - 1)
22937 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022938 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022939 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022940 }
22941
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 while (ga_scripts.ga_len < id)
22943 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022944 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022945 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022946 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 }
22949 }
22950}
22951
22952/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022953 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22954 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 */
22956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022957init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958{
Bram Moolenaar33570922005-01-25 22:26:29 +000022959 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022960 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022961 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022962 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022963 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022964 dict_var->di_tv.vval.v_dict = dict;
22965 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022966 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022967 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22968 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969}
22970
22971/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022972 * Unreference a dictionary initialized by init_var_dict().
22973 */
22974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022975unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022976{
22977 /* Now the dict needs to be freed if no one else is using it, go back to
22978 * normal reference counting. */
22979 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22980 dict_unref(dict);
22981}
22982
22983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022985 * Frees all allocated variables and the value they contain.
22986 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 */
22988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022989vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022990{
22991 vars_clear_ext(ht, TRUE);
22992}
22993
22994/*
22995 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22996 */
22997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022998vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999{
Bram Moolenaara7043832005-01-21 11:56:39 +000023000 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023001 hashitem_T *hi;
23002 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003
Bram Moolenaar33570922005-01-25 22:26:29 +000023004 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023005 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023006 for (hi = ht->ht_array; todo > 0; ++hi)
23007 {
23008 if (!HASHITEM_EMPTY(hi))
23009 {
23010 --todo;
23011
Bram Moolenaar33570922005-01-25 22:26:29 +000023012 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023013 * ht_array might change then. hash_clear() takes care of it
23014 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023015 v = HI2DI(hi);
23016 if (free_val)
23017 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023018 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023019 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023020 }
23021 }
23022 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023023 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024}
23025
Bram Moolenaara7043832005-01-21 11:56:39 +000023026/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023027 * Delete a variable from hashtab "ht" at item "hi".
23028 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023031delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032{
Bram Moolenaar33570922005-01-25 22:26:29 +000023033 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023034
23035 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023036 clear_tv(&di->di_tv);
23037 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038}
23039
23040/*
23041 * List the value of one internal variable.
23042 */
23043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023044list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023046 char_u *tofree;
23047 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023048 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023049
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023050 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023051 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023052 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023053 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054}
23055
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023057list_one_var_a(
23058 char_u *prefix,
23059 char_u *name,
23060 int type,
23061 char_u *string,
23062 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063{
Bram Moolenaar31859182007-08-14 20:41:13 +000023064 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23065 msg_start();
23066 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 if (name != NULL) /* "a:" vars don't have a name stored */
23068 msg_puts(name);
23069 msg_putchar(' ');
23070 msg_advance(22);
23071 if (type == VAR_NUMBER)
23072 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023073 else if (type == VAR_FUNC)
23074 msg_putchar('*');
23075 else if (type == VAR_LIST)
23076 {
23077 msg_putchar('[');
23078 if (*string == '[')
23079 ++string;
23080 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023081 else if (type == VAR_DICT)
23082 {
23083 msg_putchar('{');
23084 if (*string == '{')
23085 ++string;
23086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087 else
23088 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023089
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023091
23092 if (type == VAR_FUNC)
23093 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023094 if (*first)
23095 {
23096 msg_clr_eos();
23097 *first = FALSE;
23098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099}
23100
23101/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023102 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 * If the variable already exists, the value is updated.
23104 * Otherwise the variable is created.
23105 */
23106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023107set_var(
23108 char_u *name,
23109 typval_T *tv,
23110 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111{
Bram Moolenaar33570922005-01-25 22:26:29 +000023112 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023114 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023116 ht = find_var_ht(name, &varname);
23117 if (ht == NULL || *varname == NUL)
23118 {
23119 EMSG2(_(e_illvar), name);
23120 return;
23121 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023122 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023123
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023124 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
23125 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023126
Bram Moolenaar33570922005-01-25 22:26:29 +000023127 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023129 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023130 if (var_check_ro(v->di_flags, name, FALSE)
23131 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023132 return;
23133 if (v->di_tv.v_type != tv->v_type
23134 && !((v->di_tv.v_type == VAR_STRING
23135 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023136 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023137 || tv->v_type == VAR_NUMBER))
23138#ifdef FEAT_FLOAT
23139 && !((v->di_tv.v_type == VAR_NUMBER
23140 || v->di_tv.v_type == VAR_FLOAT)
23141 && (tv->v_type == VAR_NUMBER
23142 || tv->v_type == VAR_FLOAT))
23143#endif
23144 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023145 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000023146 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023147 return;
23148 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023149
23150 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023151 * Handle setting internal v: variables separately where needed to
23152 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023153 */
23154 if (ht == &vimvarht)
23155 {
23156 if (v->di_tv.v_type == VAR_STRING)
23157 {
23158 vim_free(v->di_tv.vval.v_string);
23159 if (copy || tv->v_type != VAR_STRING)
23160 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23161 else
23162 {
23163 /* Take over the string to avoid an extra alloc/free. */
23164 v->di_tv.vval.v_string = tv->vval.v_string;
23165 tv->vval.v_string = NULL;
23166 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023167 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023168 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023169 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023170 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023171 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023172 if (STRCMP(varname, "searchforward") == 0)
23173 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023174#ifdef FEAT_SEARCH_EXTRA
23175 else if (STRCMP(varname, "hlsearch") == 0)
23176 {
23177 no_hlsearch = !v->di_tv.vval.v_number;
23178 redraw_all_later(SOME_VALID);
23179 }
23180#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023181 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023182 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023183 else if (v->di_tv.v_type != tv->v_type)
23184 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023185 }
23186
23187 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 }
23189 else /* add a new variable */
23190 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023191 /* Can't add "v:" variable. */
23192 if (ht == &vimvarht)
23193 {
23194 EMSG2(_(e_illvar), name);
23195 return;
23196 }
23197
Bram Moolenaar92124a32005-06-17 22:03:40 +000023198 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023199 if (!valid_varname(varname))
23200 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023201
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023202 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23203 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023204 if (v == NULL)
23205 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023206 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023207 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023209 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 return;
23211 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023212 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023214
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023215 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023216 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023217 else
23218 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023219 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023220 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023221 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223}
23224
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023225/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023226 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023227 * Also give an error message.
23228 */
23229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023230var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023231{
23232 if (flags & DI_FLAGS_RO)
23233 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023234 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023235 return TRUE;
23236 }
23237 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23238 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023239 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023240 return TRUE;
23241 }
23242 return FALSE;
23243}
23244
23245/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023246 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23247 * Also give an error message.
23248 */
23249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023250var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023251{
23252 if (flags & DI_FLAGS_FIX)
23253 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023254 EMSG2(_("E795: Cannot delete variable %s"),
23255 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023256 return TRUE;
23257 }
23258 return FALSE;
23259}
23260
23261/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023262 * Check if a funcref is assigned to a valid variable name.
23263 * Return TRUE and give an error if not.
23264 */
23265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023266var_check_func_name(
23267 char_u *name, /* points to start of variable name */
23268 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023269{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023270 /* Allow for w: b: s: and t:. */
23271 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023272 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23273 ? name[2] : name[0]))
23274 {
23275 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23276 name);
23277 return TRUE;
23278 }
23279 /* Don't allow hiding a function. When "v" is not NULL we might be
23280 * assigning another function to the same var, the type is checked
23281 * below. */
23282 if (new_var && function_exists(name))
23283 {
23284 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23285 name);
23286 return TRUE;
23287 }
23288 return FALSE;
23289}
23290
23291/*
23292 * Check if a variable name is valid.
23293 * Return FALSE and give an error if not.
23294 */
23295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023296valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023297{
23298 char_u *p;
23299
23300 for (p = varname; *p != NUL; ++p)
23301 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23302 && *p != AUTOLOAD_CHAR)
23303 {
23304 EMSG2(_(e_illvar), varname);
23305 return FALSE;
23306 }
23307 return TRUE;
23308}
23309
23310/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023311 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023312 * Also give an error message, using "name" or _("name") when use_gettext is
23313 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023314 */
23315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023316tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023317{
23318 if (lock & VAR_LOCKED)
23319 {
23320 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023321 name == NULL ? (char_u *)_("Unknown")
23322 : use_gettext ? (char_u *)_(name)
23323 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023324 return TRUE;
23325 }
23326 if (lock & VAR_FIXED)
23327 {
23328 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023329 name == NULL ? (char_u *)_("Unknown")
23330 : use_gettext ? (char_u *)_(name)
23331 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023332 return TRUE;
23333 }
23334 return FALSE;
23335}
23336
23337/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023338 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023339 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023340 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023341 * It is OK for "from" and "to" to point to the same item. This is used to
23342 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023343 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023345copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023347 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023348 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023349 switch (from->v_type)
23350 {
23351 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023352 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023353 to->vval.v_number = from->vval.v_number;
23354 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023355 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023356#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023357 to->vval.v_float = from->vval.v_float;
23358 break;
23359#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023360 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023361#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023362 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023363 if (to->vval.v_job != NULL)
23364 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023365 break;
23366#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023367 case VAR_CHANNEL:
23368#ifdef FEAT_CHANNEL
23369 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023370 if (to->vval.v_channel != NULL)
23371 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023372 break;
23373#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023374 case VAR_STRING:
23375 case VAR_FUNC:
23376 if (from->vval.v_string == NULL)
23377 to->vval.v_string = NULL;
23378 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023379 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023380 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023381 if (from->v_type == VAR_FUNC)
23382 func_ref(to->vval.v_string);
23383 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023384 break;
23385 case VAR_LIST:
23386 if (from->vval.v_list == NULL)
23387 to->vval.v_list = NULL;
23388 else
23389 {
23390 to->vval.v_list = from->vval.v_list;
23391 ++to->vval.v_list->lv_refcount;
23392 }
23393 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023394 case VAR_DICT:
23395 if (from->vval.v_dict == NULL)
23396 to->vval.v_dict = NULL;
23397 else
23398 {
23399 to->vval.v_dict = from->vval.v_dict;
23400 ++to->vval.v_dict->dv_refcount;
23401 }
23402 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023403 case VAR_UNKNOWN:
23404 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023405 break;
23406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407}
23408
23409/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023410 * Make a copy of an item.
23411 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023412 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23413 * reference to an already copied list/dict can be used.
23414 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023415 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023416 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023417item_copy(
23418 typval_T *from,
23419 typval_T *to,
23420 int deep,
23421 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023422{
23423 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023424 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023425
Bram Moolenaar33570922005-01-25 22:26:29 +000023426 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023427 {
23428 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023429 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023430 }
23431 ++recurse;
23432
23433 switch (from->v_type)
23434 {
23435 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023436 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023437 case VAR_STRING:
23438 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023439 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023440 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023441 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023442 copy_tv(from, to);
23443 break;
23444 case VAR_LIST:
23445 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023446 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023447 if (from->vval.v_list == NULL)
23448 to->vval.v_list = NULL;
23449 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23450 {
23451 /* use the copy made earlier */
23452 to->vval.v_list = from->vval.v_list->lv_copylist;
23453 ++to->vval.v_list->lv_refcount;
23454 }
23455 else
23456 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23457 if (to->vval.v_list == NULL)
23458 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023459 break;
23460 case VAR_DICT:
23461 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023462 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023463 if (from->vval.v_dict == NULL)
23464 to->vval.v_dict = NULL;
23465 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23466 {
23467 /* use the copy made earlier */
23468 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23469 ++to->vval.v_dict->dv_refcount;
23470 }
23471 else
23472 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23473 if (to->vval.v_dict == NULL)
23474 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023475 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023476 case VAR_UNKNOWN:
23477 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023478 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023479 }
23480 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023481 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023482}
23483
23484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 * ":echo expr1 ..." print each argument separated with a space, add a
23486 * newline at the end.
23487 * ":echon expr1 ..." print each argument plain.
23488 */
23489 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023490ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491{
23492 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023493 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023494 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495 char_u *p;
23496 int needclr = TRUE;
23497 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023498 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499
23500 if (eap->skip)
23501 ++emsg_skip;
23502 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23503 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023504 /* If eval1() causes an error message the text from the command may
23505 * still need to be cleared. E.g., "echo 22,44". */
23506 need_clr_eos = needclr;
23507
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023509 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 {
23511 /*
23512 * Report the invalid expression unless the expression evaluation
23513 * has been cancelled due to an aborting error, an interrupt, or an
23514 * exception.
23515 */
23516 if (!aborting())
23517 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023518 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 break;
23520 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023521 need_clr_eos = FALSE;
23522
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 if (!eap->skip)
23524 {
23525 if (atstart)
23526 {
23527 atstart = FALSE;
23528 /* Call msg_start() after eval1(), evaluating the expression
23529 * may cause a message to appear. */
23530 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023531 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023532 /* Mark the saved text as finishing the line, so that what
23533 * follows is displayed on a new line when scrolling back
23534 * at the more prompt. */
23535 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 }
23539 else if (eap->cmdidx == CMD_echo)
23540 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023541 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023542 if (p != NULL)
23543 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023545 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023547 if (*p != TAB && needclr)
23548 {
23549 /* remove any text still there from the command */
23550 msg_clr_eos();
23551 needclr = FALSE;
23552 }
23553 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 }
23555 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023556 {
23557#ifdef FEAT_MBYTE
23558 if (has_mbyte)
23559 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023560 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023561
23562 (void)msg_outtrans_len_attr(p, i, echo_attr);
23563 p += i - 1;
23564 }
23565 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023567 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023570 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023572 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 arg = skipwhite(arg);
23574 }
23575 eap->nextcmd = check_nextcmd(arg);
23576
23577 if (eap->skip)
23578 --emsg_skip;
23579 else
23580 {
23581 /* remove text that may still be there from the command */
23582 if (needclr)
23583 msg_clr_eos();
23584 if (eap->cmdidx == CMD_echo)
23585 msg_end();
23586 }
23587}
23588
23589/*
23590 * ":echohl {name}".
23591 */
23592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023593ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594{
23595 int id;
23596
23597 id = syn_name2id(eap->arg);
23598 if (id == 0)
23599 echo_attr = 0;
23600 else
23601 echo_attr = syn_id2attr(id);
23602}
23603
23604/*
23605 * ":execute expr1 ..." execute the result of an expression.
23606 * ":echomsg expr1 ..." Print a message
23607 * ":echoerr expr1 ..." Print an error
23608 * Each gets spaces around each argument and a newline at the end for
23609 * echo commands
23610 */
23611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023612ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613{
23614 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023615 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 int ret = OK;
23617 char_u *p;
23618 garray_T ga;
23619 int len;
23620 int save_did_emsg;
23621
23622 ga_init2(&ga, 1, 80);
23623
23624 if (eap->skip)
23625 ++emsg_skip;
23626 while (*arg != NUL && *arg != '|' && *arg != '\n')
23627 {
23628 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023629 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 {
23631 /*
23632 * Report the invalid expression unless the expression evaluation
23633 * has been cancelled due to an aborting error, an interrupt, or an
23634 * exception.
23635 */
23636 if (!aborting())
23637 EMSG2(_(e_invexpr2), p);
23638 ret = FAIL;
23639 break;
23640 }
23641
23642 if (!eap->skip)
23643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023644 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 len = (int)STRLEN(p);
23646 if (ga_grow(&ga, len + 2) == FAIL)
23647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023648 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 ret = FAIL;
23650 break;
23651 }
23652 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 ga.ga_len += len;
23656 }
23657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023658 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 arg = skipwhite(arg);
23660 }
23661
23662 if (ret != FAIL && ga.ga_data != NULL)
23663 {
23664 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023666 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023667 out_flush();
23668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669 else if (eap->cmdidx == CMD_echoerr)
23670 {
23671 /* We don't want to abort following commands, restore did_emsg. */
23672 save_did_emsg = did_emsg;
23673 EMSG((char_u *)ga.ga_data);
23674 if (!force_abort)
23675 did_emsg = save_did_emsg;
23676 }
23677 else if (eap->cmdidx == CMD_execute)
23678 do_cmdline((char_u *)ga.ga_data,
23679 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23680 }
23681
23682 ga_clear(&ga);
23683
23684 if (eap->skip)
23685 --emsg_skip;
23686
23687 eap->nextcmd = check_nextcmd(arg);
23688}
23689
23690/*
23691 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23692 * "arg" points to the "&" or '+' when called, to "option" when returning.
23693 * Returns NULL when no option name found. Otherwise pointer to the char
23694 * after the option name.
23695 */
23696 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023697find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698{
23699 char_u *p = *arg;
23700
23701 ++p;
23702 if (*p == 'g' && p[1] == ':')
23703 {
23704 *opt_flags = OPT_GLOBAL;
23705 p += 2;
23706 }
23707 else if (*p == 'l' && p[1] == ':')
23708 {
23709 *opt_flags = OPT_LOCAL;
23710 p += 2;
23711 }
23712 else
23713 *opt_flags = 0;
23714
23715 if (!ASCII_ISALPHA(*p))
23716 return NULL;
23717 *arg = p;
23718
23719 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23720 p += 4; /* termcap option */
23721 else
23722 while (ASCII_ISALPHA(*p))
23723 ++p;
23724 return p;
23725}
23726
23727/*
23728 * ":function"
23729 */
23730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023731ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732{
23733 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023734 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735 int j;
23736 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023738 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739 char_u *name = NULL;
23740 char_u *p;
23741 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023742 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743 garray_T newargs;
23744 garray_T newlines;
23745 int varargs = FALSE;
23746 int mustend = FALSE;
23747 int flags = 0;
23748 ufunc_T *fp;
23749 int indent;
23750 int nesting;
23751 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023752 dictitem_T *v;
23753 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023754 static int func_nr = 0; /* number for nameless function */
23755 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023756 hashtab_T *ht;
23757 int todo;
23758 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023759 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760
23761 /*
23762 * ":function" without argument: list functions.
23763 */
23764 if (ends_excmd(*eap->arg))
23765 {
23766 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023767 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023768 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023769 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023770 {
23771 if (!HASHITEM_EMPTY(hi))
23772 {
23773 --todo;
23774 fp = HI2UF(hi);
23775 if (!isdigit(*fp->uf_name))
23776 list_func_head(fp, FALSE);
23777 }
23778 }
23779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780 eap->nextcmd = check_nextcmd(eap->arg);
23781 return;
23782 }
23783
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023784 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023785 * ":function /pat": list functions matching pattern.
23786 */
23787 if (*eap->arg == '/')
23788 {
23789 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23790 if (!eap->skip)
23791 {
23792 regmatch_T regmatch;
23793
23794 c = *p;
23795 *p = NUL;
23796 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23797 *p = c;
23798 if (regmatch.regprog != NULL)
23799 {
23800 regmatch.rm_ic = p_ic;
23801
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023802 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023803 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23804 {
23805 if (!HASHITEM_EMPTY(hi))
23806 {
23807 --todo;
23808 fp = HI2UF(hi);
23809 if (!isdigit(*fp->uf_name)
23810 && vim_regexec(&regmatch, fp->uf_name, 0))
23811 list_func_head(fp, FALSE);
23812 }
23813 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023814 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023815 }
23816 }
23817 if (*p == '/')
23818 ++p;
23819 eap->nextcmd = check_nextcmd(p);
23820 return;
23821 }
23822
23823 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023824 * Get the function name. There are these situations:
23825 * func normal function name
23826 * "name" == func, "fudi.fd_dict" == NULL
23827 * dict.func new dictionary entry
23828 * "name" == NULL, "fudi.fd_dict" set,
23829 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23830 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023831 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023832 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23833 * dict.func existing dict entry that's not a Funcref
23834 * "name" == NULL, "fudi.fd_dict" set,
23835 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023836 * s:func script-local function name
23837 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023840 name = trans_function_name(&p, eap->skip, 0, &fudi);
23841 paren = (vim_strchr(p, '(') != NULL);
23842 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 {
23844 /*
23845 * Return on an invalid expression in braces, unless the expression
23846 * evaluation has been cancelled due to an aborting error, an
23847 * interrupt, or an exception.
23848 */
23849 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023850 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023851 if (!eap->skip && fudi.fd_newkey != NULL)
23852 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023853 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856 else
23857 eap->skip = TRUE;
23858 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023859
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 /* An error in a function call during evaluation of an expression in magic
23861 * braces should not cause the function not to be defined. */
23862 saved_did_emsg = did_emsg;
23863 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864
23865 /*
23866 * ":function func" with only function name: list function.
23867 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023868 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 {
23870 if (!ends_excmd(*skipwhite(p)))
23871 {
23872 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023873 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 }
23875 eap->nextcmd = check_nextcmd(p);
23876 if (eap->nextcmd != NULL)
23877 *p = NUL;
23878 if (!eap->skip && !got_int)
23879 {
23880 fp = find_func(name);
23881 if (fp != NULL)
23882 {
23883 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023884 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023886 if (FUNCLINE(fp, j) == NULL)
23887 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 msg_putchar('\n');
23889 msg_outnum((long)(j + 1));
23890 if (j < 9)
23891 msg_putchar(' ');
23892 if (j < 99)
23893 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023894 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023895 out_flush(); /* show a line at a time */
23896 ui_breakcheck();
23897 }
23898 if (!got_int)
23899 {
23900 msg_putchar('\n');
23901 msg_puts((char_u *)" endfunction");
23902 }
23903 }
23904 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023905 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023907 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023908 }
23909
23910 /*
23911 * ":function name(arg1, arg2)" Define function.
23912 */
23913 p = skipwhite(p);
23914 if (*p != '(')
23915 {
23916 if (!eap->skip)
23917 {
23918 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023919 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 }
23921 /* attempt to continue by skipping some text */
23922 if (vim_strchr(p, '(') != NULL)
23923 p = vim_strchr(p, '(');
23924 }
23925 p = skipwhite(p + 1);
23926
23927 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23928 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23929
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023930 if (!eap->skip)
23931 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023932 /* Check the name of the function. Unless it's a dictionary function
23933 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023934 if (name != NULL)
23935 arg = name;
23936 else
23937 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023938 if (arg != NULL && (fudi.fd_di == NULL
23939 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023940 {
23941 if (*arg == K_SPECIAL)
23942 j = 3;
23943 else
23944 j = 0;
23945 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23946 : eval_isnamec(arg[j])))
23947 ++j;
23948 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023949 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023950 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023951 /* Disallow using the g: dict. */
23952 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23953 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023954 }
23955
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 /*
23957 * Isolate the arguments: "arg1, arg2, ...)"
23958 */
23959 while (*p != ')')
23960 {
23961 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23962 {
23963 varargs = TRUE;
23964 p += 3;
23965 mustend = TRUE;
23966 }
23967 else
23968 {
23969 arg = p;
23970 while (ASCII_ISALNUM(*p) || *p == '_')
23971 ++p;
23972 if (arg == p || isdigit(*arg)
23973 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23974 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23975 {
23976 if (!eap->skip)
23977 EMSG2(_("E125: Illegal argument: %s"), arg);
23978 break;
23979 }
23980 if (ga_grow(&newargs, 1) == FAIL)
23981 goto erret;
23982 c = *p;
23983 *p = NUL;
23984 arg = vim_strsave(arg);
23985 if (arg == NULL)
23986 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023987
23988 /* Check for duplicate argument name. */
23989 for (i = 0; i < newargs.ga_len; ++i)
23990 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23991 {
23992 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023993 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023994 goto erret;
23995 }
23996
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23998 *p = c;
23999 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 if (*p == ',')
24001 ++p;
24002 else
24003 mustend = TRUE;
24004 }
24005 p = skipwhite(p);
24006 if (mustend && *p != ')')
24007 {
24008 if (!eap->skip)
24009 EMSG2(_(e_invarg2), eap->arg);
24010 break;
24011 }
24012 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024013 if (*p != ')')
24014 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024015 ++p; /* skip the ')' */
24016
Bram Moolenaare9a41262005-01-15 22:18:47 +000024017 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 for (;;)
24019 {
24020 p = skipwhite(p);
24021 if (STRNCMP(p, "range", 5) == 0)
24022 {
24023 flags |= FC_RANGE;
24024 p += 5;
24025 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024026 else if (STRNCMP(p, "dict", 4) == 0)
24027 {
24028 flags |= FC_DICT;
24029 p += 4;
24030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 else if (STRNCMP(p, "abort", 5) == 0)
24032 {
24033 flags |= FC_ABORT;
24034 p += 5;
24035 }
24036 else
24037 break;
24038 }
24039
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024040 /* When there is a line break use what follows for the function body.
24041 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24042 if (*p == '\n')
24043 line_arg = p + 1;
24044 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024045 EMSG(_(e_trailing));
24046
24047 /*
24048 * Read the body of the function, until ":endfunction" is found.
24049 */
24050 if (KeyTyped)
24051 {
24052 /* Check if the function already exists, don't let the user type the
24053 * whole function before telling him it doesn't work! For a script we
24054 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024055 if (!eap->skip && !eap->forceit)
24056 {
24057 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24058 EMSG(_(e_funcdict));
24059 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024060 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024063 if (!eap->skip && did_emsg)
24064 goto erret;
24065
Bram Moolenaar071d4272004-06-13 20:20:40 +000024066 msg_putchar('\n'); /* don't overwrite the function name */
24067 cmdline_row = msg_row;
24068 }
24069
24070 indent = 2;
24071 nesting = 0;
24072 for (;;)
24073 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024074 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024075 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024076 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024077 saved_wait_return = FALSE;
24078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024080 sourcing_lnum_off = sourcing_lnum;
24081
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024082 if (line_arg != NULL)
24083 {
24084 /* Use eap->arg, split up in parts by line breaks. */
24085 theline = line_arg;
24086 p = vim_strchr(theline, '\n');
24087 if (p == NULL)
24088 line_arg += STRLEN(line_arg);
24089 else
24090 {
24091 *p = NUL;
24092 line_arg = p + 1;
24093 }
24094 }
24095 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024096 theline = getcmdline(':', 0L, indent);
24097 else
24098 theline = eap->getline(':', eap->cookie, indent);
24099 if (KeyTyped)
24100 lines_left = Rows - 1;
24101 if (theline == NULL)
24102 {
24103 EMSG(_("E126: Missing :endfunction"));
24104 goto erret;
24105 }
24106
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024107 /* Detect line continuation: sourcing_lnum increased more than one. */
24108 if (sourcing_lnum > sourcing_lnum_off + 1)
24109 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24110 else
24111 sourcing_lnum_off = 0;
24112
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 if (skip_until != NULL)
24114 {
24115 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24116 * don't check for ":endfunc". */
24117 if (STRCMP(theline, skip_until) == 0)
24118 {
24119 vim_free(skip_until);
24120 skip_until = NULL;
24121 }
24122 }
24123 else
24124 {
24125 /* skip ':' and blanks*/
24126 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24127 ;
24128
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024129 /* Check for "endfunction". */
24130 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024132 if (line_arg == NULL)
24133 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 break;
24135 }
24136
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024137 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 * at "end". */
24139 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24140 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024141 else if (STRNCMP(p, "if", 2) == 0
24142 || STRNCMP(p, "wh", 2) == 0
24143 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 || STRNCMP(p, "try", 3) == 0)
24145 indent += 2;
24146
24147 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024148 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024150 if (*p == '!')
24151 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024152 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010024153 vim_free(trans_function_name(&p, TRUE, 0, NULL));
24154 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024156 ++nesting;
24157 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 }
24159 }
24160
24161 /* Check for ":append" or ":insert". */
24162 p = skip_range(p, NULL);
24163 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24164 || (p[0] == 'i'
24165 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24166 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24167 skip_until = vim_strsave((char_u *)".");
24168
24169 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24170 arg = skipwhite(skiptowhite(p));
24171 if (arg[0] == '<' && arg[1] =='<'
24172 && ((p[0] == 'p' && p[1] == 'y'
24173 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24174 || (p[0] == 'p' && p[1] == 'e'
24175 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24176 || (p[0] == 't' && p[1] == 'c'
24177 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024178 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24179 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024180 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24181 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024182 || (p[0] == 'm' && p[1] == 'z'
24183 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 ))
24185 {
24186 /* ":python <<" continues until a dot, like ":append" */
24187 p = skipwhite(arg + 2);
24188 if (*p == NUL)
24189 skip_until = vim_strsave((char_u *)".");
24190 else
24191 skip_until = vim_strsave(p);
24192 }
24193 }
24194
24195 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024196 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024197 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024198 if (line_arg == NULL)
24199 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024201 }
24202
24203 /* Copy the line to newly allocated memory. get_one_sourceline()
24204 * allocates 250 bytes per line, this saves 80% on average. The cost
24205 * is an extra alloc/free. */
24206 p = vim_strsave(theline);
24207 if (p != NULL)
24208 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024209 if (line_arg == NULL)
24210 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024211 theline = p;
24212 }
24213
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024214 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24215
24216 /* Add NULL lines for continuation lines, so that the line count is
24217 * equal to the index in the growarray. */
24218 while (sourcing_lnum_off-- > 0)
24219 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024220
24221 /* Check for end of eap->arg. */
24222 if (line_arg != NULL && *line_arg == NUL)
24223 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024224 }
24225
24226 /* Don't define the function when skipping commands or when an error was
24227 * detected. */
24228 if (eap->skip || did_emsg)
24229 goto erret;
24230
24231 /*
24232 * If there are no errors, add the function
24233 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024234 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024235 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024236 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024237 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024238 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024239 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024240 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024241 goto erret;
24242 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024243
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024244 fp = find_func(name);
24245 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024247 if (!eap->forceit)
24248 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024249 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024250 goto erret;
24251 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024252 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024253 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024254 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024255 name);
24256 goto erret;
24257 }
24258 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024259 ga_clear_strings(&(fp->uf_args));
24260 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024261 vim_free(name);
24262 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 }
24265 else
24266 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024267 char numbuf[20];
24268
24269 fp = NULL;
24270 if (fudi.fd_newkey == NULL && !eap->forceit)
24271 {
24272 EMSG(_(e_funcdict));
24273 goto erret;
24274 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024275 if (fudi.fd_di == NULL)
24276 {
24277 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024278 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024279 goto erret;
24280 }
24281 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024282 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024283 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024284
24285 /* Give the function a sequential number. Can only be used with a
24286 * Funcref! */
24287 vim_free(name);
24288 sprintf(numbuf, "%d", ++func_nr);
24289 name = vim_strsave((char_u *)numbuf);
24290 if (name == NULL)
24291 goto erret;
24292 }
24293
24294 if (fp == NULL)
24295 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024296 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024297 {
24298 int slen, plen;
24299 char_u *scriptname;
24300
24301 /* Check that the autoload name matches the script name. */
24302 j = FAIL;
24303 if (sourcing_name != NULL)
24304 {
24305 scriptname = autoload_name(name);
24306 if (scriptname != NULL)
24307 {
24308 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024309 plen = (int)STRLEN(p);
24310 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024311 if (slen > plen && fnamecmp(p,
24312 sourcing_name + slen - plen) == 0)
24313 j = OK;
24314 vim_free(scriptname);
24315 }
24316 }
24317 if (j == FAIL)
24318 {
24319 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24320 goto erret;
24321 }
24322 }
24323
24324 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 if (fp == NULL)
24326 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024327
24328 if (fudi.fd_dict != NULL)
24329 {
24330 if (fudi.fd_di == NULL)
24331 {
24332 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024333 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024334 if (fudi.fd_di == NULL)
24335 {
24336 vim_free(fp);
24337 goto erret;
24338 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024339 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24340 {
24341 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024342 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024343 goto erret;
24344 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024345 }
24346 else
24347 /* overwrite existing dict entry */
24348 clear_tv(&fudi.fd_di->di_tv);
24349 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024350 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024351 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024352 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024353
24354 /* behave like "dict" was used */
24355 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024356 }
24357
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024359 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024360 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24361 {
24362 vim_free(fp);
24363 goto erret;
24364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024366 fp->uf_args = newargs;
24367 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024368#ifdef FEAT_PROFILE
24369 fp->uf_tml_count = NULL;
24370 fp->uf_tml_total = NULL;
24371 fp->uf_tml_self = NULL;
24372 fp->uf_profiling = FALSE;
24373 if (prof_def_func())
24374 func_do_profile(fp);
24375#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024376 fp->uf_varargs = varargs;
24377 fp->uf_flags = flags;
24378 fp->uf_calls = 0;
24379 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024380 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381
24382erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 ga_clear_strings(&newargs);
24384 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024385ret_free:
24386 vim_free(skip_until);
24387 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024390 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391}
24392
24393/*
24394 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024395 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024396 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024397 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024398 * TFN_INT: internal function name OK
24399 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024400 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 * Advances "pp" to just after the function name (if no error).
24402 */
24403 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024404trans_function_name(
24405 char_u **pp,
24406 int skip, /* only find the end, don't evaluate */
24407 int flags,
24408 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024410 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024411 char_u *start;
24412 char_u *end;
24413 int lead;
24414 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024415 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024416 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024417
24418 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024419 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024421
24422 /* Check for hard coded <SNR>: already translated function ID (from a user
24423 * command). */
24424 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24425 && (*pp)[2] == (int)KE_SNR)
24426 {
24427 *pp += 3;
24428 len = get_id_len(pp) + 3;
24429 return vim_strnsave(start, len);
24430 }
24431
24432 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24433 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024435 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024437
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024438 /* Note that TFN_ flags use the same values as GLV_ flags. */
24439 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024440 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024441 if (end == start)
24442 {
24443 if (!skip)
24444 EMSG(_("E129: Function name required"));
24445 goto theend;
24446 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024447 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024448 {
24449 /*
24450 * Report an invalid expression in braces, unless the expression
24451 * evaluation has been cancelled due to an aborting error, an
24452 * interrupt, or an exception.
24453 */
24454 if (!aborting())
24455 {
24456 if (end != NULL)
24457 EMSG2(_(e_invarg2), start);
24458 }
24459 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024460 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024461 goto theend;
24462 }
24463
24464 if (lv.ll_tv != NULL)
24465 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024466 if (fdp != NULL)
24467 {
24468 fdp->fd_dict = lv.ll_dict;
24469 fdp->fd_newkey = lv.ll_newkey;
24470 lv.ll_newkey = NULL;
24471 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024472 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024473 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24474 {
24475 name = vim_strsave(lv.ll_tv->vval.v_string);
24476 *pp = end;
24477 }
24478 else
24479 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024480 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24481 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024482 EMSG(_(e_funcref));
24483 else
24484 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024485 name = NULL;
24486 }
24487 goto theend;
24488 }
24489
24490 if (lv.ll_name == NULL)
24491 {
24492 /* Error found, but continue after the function name. */
24493 *pp = end;
24494 goto theend;
24495 }
24496
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024497 /* Check if the name is a Funcref. If so, use the value. */
24498 if (lv.ll_exp_name != NULL)
24499 {
24500 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024501 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024502 if (name == lv.ll_exp_name)
24503 name = NULL;
24504 }
24505 else
24506 {
24507 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024508 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024509 if (name == *pp)
24510 name = NULL;
24511 }
24512 if (name != NULL)
24513 {
24514 name = vim_strsave(name);
24515 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024516 if (STRNCMP(name, "<SNR>", 5) == 0)
24517 {
24518 /* Change "<SNR>" to the byte sequence. */
24519 name[0] = K_SPECIAL;
24520 name[1] = KS_EXTRA;
24521 name[2] = (int)KE_SNR;
24522 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24523 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024524 goto theend;
24525 }
24526
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024527 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024528 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024529 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024530 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24531 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24532 {
24533 /* When there was "s:" already or the name expanded to get a
24534 * leading "s:" then remove it. */
24535 lv.ll_name += 2;
24536 len -= 2;
24537 lead = 2;
24538 }
24539 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024540 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024541 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024542 /* skip over "s:" and "g:" */
24543 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024544 lv.ll_name += 2;
24545 len = (int)(end - lv.ll_name);
24546 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024547
24548 /*
24549 * Copy the function name to allocated memory.
24550 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24551 * Accept <SNR>123_name() outside a script.
24552 */
24553 if (skip)
24554 lead = 0; /* do nothing */
24555 else if (lead > 0)
24556 {
24557 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024558 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24559 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024560 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024561 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024562 if (current_SID <= 0)
24563 {
24564 EMSG(_(e_usingsid));
24565 goto theend;
24566 }
24567 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24568 lead += (int)STRLEN(sid_buf);
24569 }
24570 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024571 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024572 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024573 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024574 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024575 goto theend;
24576 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024577 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024578 {
24579 char_u *cp = vim_strchr(lv.ll_name, ':');
24580
24581 if (cp != NULL && cp < end)
24582 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024583 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024584 goto theend;
24585 }
24586 }
24587
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024588 name = alloc((unsigned)(len + lead + 1));
24589 if (name != NULL)
24590 {
24591 if (lead > 0)
24592 {
24593 name[0] = K_SPECIAL;
24594 name[1] = KS_EXTRA;
24595 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024596 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024597 STRCPY(name + 3, sid_buf);
24598 }
24599 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024600 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024601 }
24602 *pp = end;
24603
24604theend:
24605 clear_lval(&lv);
24606 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607}
24608
24609/*
24610 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24611 * Return 2 if "p" starts with "s:".
24612 * Return 0 otherwise.
24613 */
24614 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024615eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024617 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24618 * the standard library function. */
24619 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24620 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 return 5;
24622 if (p[0] == 's' && p[1] == ':')
24623 return 2;
24624 return 0;
24625}
24626
24627/*
24628 * Return TRUE if "p" starts with "<SID>" or "s:".
24629 * Only works if eval_fname_script() returned non-zero for "p"!
24630 */
24631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024632eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633{
24634 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24635}
24636
24637/*
24638 * List the head of the function: "name(arg1, arg2)".
24639 */
24640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024641list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642{
24643 int j;
24644
24645 msg_start();
24646 if (indent)
24647 MSG_PUTS(" ");
24648 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024649 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 {
24651 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024652 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 }
24654 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024655 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024656 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024657 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 {
24659 if (j)
24660 MSG_PUTS(", ");
24661 msg_puts(FUNCARG(fp, j));
24662 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024663 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 {
24665 if (j)
24666 MSG_PUTS(", ");
24667 MSG_PUTS("...");
24668 }
24669 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024670 if (fp->uf_flags & FC_ABORT)
24671 MSG_PUTS(" abort");
24672 if (fp->uf_flags & FC_RANGE)
24673 MSG_PUTS(" range");
24674 if (fp->uf_flags & FC_DICT)
24675 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024676 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024677 if (p_verbose > 0)
24678 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679}
24680
24681/*
24682 * Find a function by name, return pointer to it in ufuncs.
24683 * Return NULL for unknown function.
24684 */
24685 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024686find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024688 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024690 hi = hash_find(&func_hashtab, name);
24691 if (!HASHITEM_EMPTY(hi))
24692 return HI2UF(hi);
24693 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694}
24695
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024696#if defined(EXITFREE) || defined(PROTO)
24697 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024698free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024699{
24700 hashitem_T *hi;
24701
24702 /* Need to start all over every time, because func_free() may change the
24703 * hash table. */
24704 while (func_hashtab.ht_used > 0)
24705 for (hi = func_hashtab.ht_array; ; ++hi)
24706 if (!HASHITEM_EMPTY(hi))
24707 {
24708 func_free(HI2UF(hi));
24709 break;
24710 }
24711}
24712#endif
24713
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024714 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024715translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024716{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024717 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024718 return find_internal_func(name) >= 0;
24719 return find_func(name) != NULL;
24720}
24721
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024722/*
24723 * Return TRUE if a function "name" exists.
24724 */
24725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024726function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024727{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024728 char_u *nm = name;
24729 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024730 int n = FALSE;
24731
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024732 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24733 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024734 nm = skipwhite(nm);
24735
24736 /* Only accept "funcname", "funcname ", "funcname (..." and
24737 * "funcname(...", not "funcname!...". */
24738 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024739 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024740 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024741 return n;
24742}
24743
Bram Moolenaara1544c02013-05-30 12:35:52 +020024744 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024745get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024746{
24747 char_u *nm = name;
24748 char_u *p;
24749
24750 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24751
24752 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024753 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024754 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024755
Bram Moolenaara1544c02013-05-30 12:35:52 +020024756 vim_free(p);
24757 return NULL;
24758}
24759
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024760/*
24761 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024762 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24763 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024764 */
24765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024766builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024767{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024768 char_u *p;
24769
24770 if (!ASCII_ISLOWER(name[0]))
24771 return FALSE;
24772 p = vim_strchr(name, AUTOLOAD_CHAR);
24773 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024774}
24775
Bram Moolenaar05159a02005-02-26 23:04:13 +000024776#if defined(FEAT_PROFILE) || defined(PROTO)
24777/*
24778 * Start profiling function "fp".
24779 */
24780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024781func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024782{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024783 int len = fp->uf_lines.ga_len;
24784
24785 if (len == 0)
24786 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024787 fp->uf_tm_count = 0;
24788 profile_zero(&fp->uf_tm_self);
24789 profile_zero(&fp->uf_tm_total);
24790 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024791 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024792 if (fp->uf_tml_total == NULL)
24793 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024794 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024795 if (fp->uf_tml_self == NULL)
24796 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024797 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024798 fp->uf_tml_idx = -1;
24799 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24800 || fp->uf_tml_self == NULL)
24801 return; /* out of memory */
24802
24803 fp->uf_profiling = TRUE;
24804}
24805
24806/*
24807 * Dump the profiling results for all functions in file "fd".
24808 */
24809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024810func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024811{
24812 hashitem_T *hi;
24813 int todo;
24814 ufunc_T *fp;
24815 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024816 ufunc_T **sorttab;
24817 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024818
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024819 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024820 if (todo == 0)
24821 return; /* nothing to dump */
24822
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024823 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024824
Bram Moolenaar05159a02005-02-26 23:04:13 +000024825 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24826 {
24827 if (!HASHITEM_EMPTY(hi))
24828 {
24829 --todo;
24830 fp = HI2UF(hi);
24831 if (fp->uf_profiling)
24832 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024833 if (sorttab != NULL)
24834 sorttab[st_len++] = fp;
24835
Bram Moolenaar05159a02005-02-26 23:04:13 +000024836 if (fp->uf_name[0] == K_SPECIAL)
24837 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24838 else
24839 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24840 if (fp->uf_tm_count == 1)
24841 fprintf(fd, "Called 1 time\n");
24842 else
24843 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24844 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24845 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24846 fprintf(fd, "\n");
24847 fprintf(fd, "count total (s) self (s)\n");
24848
24849 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24850 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024851 if (FUNCLINE(fp, i) == NULL)
24852 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024853 prof_func_line(fd, fp->uf_tml_count[i],
24854 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024855 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24856 }
24857 fprintf(fd, "\n");
24858 }
24859 }
24860 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024861
24862 if (sorttab != NULL && st_len > 0)
24863 {
24864 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24865 prof_total_cmp);
24866 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24867 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24868 prof_self_cmp);
24869 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24870 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024871
24872 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024873}
Bram Moolenaar73830342005-02-28 22:48:19 +000024874
24875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024876prof_sort_list(
24877 FILE *fd,
24878 ufunc_T **sorttab,
24879 int st_len,
24880 char *title,
24881 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024882{
24883 int i;
24884 ufunc_T *fp;
24885
24886 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24887 fprintf(fd, "count total (s) self (s) function\n");
24888 for (i = 0; i < 20 && i < st_len; ++i)
24889 {
24890 fp = sorttab[i];
24891 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24892 prefer_self);
24893 if (fp->uf_name[0] == K_SPECIAL)
24894 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24895 else
24896 fprintf(fd, " %s()\n", fp->uf_name);
24897 }
24898 fprintf(fd, "\n");
24899}
24900
24901/*
24902 * Print the count and times for one function or function line.
24903 */
24904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024905prof_func_line(
24906 FILE *fd,
24907 int count,
24908 proftime_T *total,
24909 proftime_T *self,
24910 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024911{
24912 if (count > 0)
24913 {
24914 fprintf(fd, "%5d ", count);
24915 if (prefer_self && profile_equal(total, self))
24916 fprintf(fd, " ");
24917 else
24918 fprintf(fd, "%s ", profile_msg(total));
24919 if (!prefer_self && profile_equal(total, self))
24920 fprintf(fd, " ");
24921 else
24922 fprintf(fd, "%s ", profile_msg(self));
24923 }
24924 else
24925 fprintf(fd, " ");
24926}
24927
24928/*
24929 * Compare function for total time sorting.
24930 */
24931 static int
24932#ifdef __BORLANDC__
24933_RTLENTRYF
24934#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024935prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024936{
24937 ufunc_T *p1, *p2;
24938
24939 p1 = *(ufunc_T **)s1;
24940 p2 = *(ufunc_T **)s2;
24941 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24942}
24943
24944/*
24945 * Compare function for self time sorting.
24946 */
24947 static int
24948#ifdef __BORLANDC__
24949_RTLENTRYF
24950#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024951prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024952{
24953 ufunc_T *p1, *p2;
24954
24955 p1 = *(ufunc_T **)s1;
24956 p2 = *(ufunc_T **)s2;
24957 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24958}
24959
Bram Moolenaar05159a02005-02-26 23:04:13 +000024960#endif
24961
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024962/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024963 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024964 * Return TRUE if a package was loaded.
24965 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024967script_autoload(
24968 char_u *name,
24969 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024970{
24971 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024972 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024973 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024974 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024975
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024976 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024977 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024978 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024979 return FALSE;
24980
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024981 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024982
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024983 /* Find the name in the list of previously loaded package names. Skip
24984 * "autoload/", it's always the same. */
24985 for (i = 0; i < ga_loaded.ga_len; ++i)
24986 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24987 break;
24988 if (!reload && i < ga_loaded.ga_len)
24989 ret = FALSE; /* was loaded already */
24990 else
24991 {
24992 /* Remember the name if it wasn't loaded already. */
24993 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24994 {
24995 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24996 tofree = NULL;
24997 }
24998
24999 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000025000 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025001 ret = TRUE;
25002 }
25003
25004 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025005 return ret;
25006}
25007
25008/*
25009 * Return the autoload script name for a function or variable name.
25010 * Returns NULL when out of memory.
25011 */
25012 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025013autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025014{
25015 char_u *p;
25016 char_u *scriptname;
25017
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025018 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025019 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25020 if (scriptname == NULL)
25021 return FALSE;
25022 STRCPY(scriptname, "autoload/");
25023 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025024 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025025 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025026 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025027 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025028 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025029}
25030
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25032
25033/*
25034 * Function given to ExpandGeneric() to obtain the list of user defined
25035 * function names.
25036 */
25037 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025038get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025040 static long_u done;
25041 static hashitem_T *hi;
25042 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043
25044 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025045 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025046 done = 0;
25047 hi = func_hashtab.ht_array;
25048 }
25049 if (done < func_hashtab.ht_used)
25050 {
25051 if (done++ > 0)
25052 ++hi;
25053 while (HASHITEM_EMPTY(hi))
25054 ++hi;
25055 fp = HI2UF(hi);
25056
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025057 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025058 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025059
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025060 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25061 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062
25063 cat_func_name(IObuff, fp);
25064 if (xp->xp_context != EXPAND_USER_FUNC)
25065 {
25066 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025067 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 STRCAT(IObuff, ")");
25069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025070 return IObuff;
25071 }
25072 return NULL;
25073}
25074
25075#endif /* FEAT_CMDL_COMPL */
25076
25077/*
25078 * Copy the function name of "fp" to buffer "buf".
25079 * "buf" must be able to hold the function name plus three bytes.
25080 * Takes care of script-local function names.
25081 */
25082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025083cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025085 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025086 {
25087 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025088 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 }
25090 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025091 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092}
25093
25094/*
25095 * ":delfunction {name}"
25096 */
25097 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025098ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025099{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025100 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101 char_u *p;
25102 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025103 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104
25105 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025106 name = trans_function_name(&p, eap->skip, 0, &fudi);
25107 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025108 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025109 {
25110 if (fudi.fd_dict != NULL && !eap->skip)
25111 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025114 if (!ends_excmd(*skipwhite(p)))
25115 {
25116 vim_free(name);
25117 EMSG(_(e_trailing));
25118 return;
25119 }
25120 eap->nextcmd = check_nextcmd(p);
25121 if (eap->nextcmd != NULL)
25122 *p = NUL;
25123
25124 if (!eap->skip)
25125 fp = find_func(name);
25126 vim_free(name);
25127
25128 if (!eap->skip)
25129 {
25130 if (fp == NULL)
25131 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025132 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133 return;
25134 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025135 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025136 {
25137 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25138 return;
25139 }
25140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025141 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025143 /* Delete the dict item that refers to the function, it will
25144 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025145 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025147 else
25148 func_free(fp);
25149 }
25150}
25151
25152/*
25153 * Free a function and remove it from the list of functions.
25154 */
25155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025156func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025158 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025159
25160 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025161 ga_clear_strings(&(fp->uf_args));
25162 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025163#ifdef FEAT_PROFILE
25164 vim_free(fp->uf_tml_count);
25165 vim_free(fp->uf_tml_total);
25166 vim_free(fp->uf_tml_self);
25167#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025169 /* remove the function from the function hashtable */
25170 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25171 if (HASHITEM_EMPTY(hi))
25172 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025173 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025174 hash_remove(&func_hashtab, hi);
25175
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025176 vim_free(fp);
25177}
25178
25179/*
25180 * Unreference a Function: decrement the reference count and free it when it
25181 * becomes zero. Only for numbered functions.
25182 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025183 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025184func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025185{
25186 ufunc_T *fp;
25187
25188 if (name != NULL && isdigit(*name))
25189 {
25190 fp = find_func(name);
25191 if (fp == NULL)
25192 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025193 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025194 {
25195 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025196 * when "uf_calls" becomes zero. */
25197 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025198 func_free(fp);
25199 }
25200 }
25201}
25202
25203/*
25204 * Count a reference to a Function.
25205 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025207func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025208{
25209 ufunc_T *fp;
25210
25211 if (name != NULL && isdigit(*name))
25212 {
25213 fp = find_func(name);
25214 if (fp == NULL)
25215 EMSG2(_(e_intern2), "func_ref()");
25216 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025217 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025218 }
25219}
25220
25221/*
25222 * Call a user function.
25223 */
25224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025225call_user_func(
25226 ufunc_T *fp, /* pointer to function */
25227 int argcount, /* nr of args */
25228 typval_T *argvars, /* arguments */
25229 typval_T *rettv, /* return value */
25230 linenr_T firstline, /* first line of range */
25231 linenr_T lastline, /* last line of range */
25232 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233{
Bram Moolenaar33570922005-01-25 22:26:29 +000025234 char_u *save_sourcing_name;
25235 linenr_T save_sourcing_lnum;
25236 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025237 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025238 int save_did_emsg;
25239 static int depth = 0;
25240 dictitem_T *v;
25241 int fixvar_idx = 0; /* index in fixvar[] */
25242 int i;
25243 int ai;
25244 char_u numbuf[NUMBUFLEN];
25245 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025246 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025247#ifdef FEAT_PROFILE
25248 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025249 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025251
25252 /* If depth of calling is getting too high, don't execute the function */
25253 if (depth >= p_mfd)
25254 {
25255 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025256 rettv->v_type = VAR_NUMBER;
25257 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258 return;
25259 }
25260 ++depth;
25261
25262 line_breakcheck(); /* check for CTRL-C hit */
25263
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025264 fc = (funccall_T *)alloc(sizeof(funccall_T));
25265 fc->caller = current_funccal;
25266 current_funccal = fc;
25267 fc->func = fp;
25268 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025269 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025270 fc->linenr = 0;
25271 fc->returned = FALSE;
25272 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025274 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25275 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276
Bram Moolenaar33570922005-01-25 22:26:29 +000025277 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025278 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025279 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25280 * each argument variable and saves a lot of time.
25281 */
25282 /*
25283 * Init l: variables.
25284 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025285 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025286 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025287 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025288 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25289 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025290 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025291 name = v->di_key;
25292 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025293 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025294 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025295 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025296 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025297 v->di_tv.vval.v_dict = selfdict;
25298 ++selfdict->dv_refcount;
25299 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025300
Bram Moolenaar33570922005-01-25 22:26:29 +000025301 /*
25302 * Init a: variables.
25303 * Set a:0 to "argcount".
25304 * Set a:000 to a list with room for the "..." arguments.
25305 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025306 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025307 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025308 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025309 /* Use "name" to avoid a warning from some compiler that checks the
25310 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025311 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025312 name = v->di_key;
25313 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025314 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025315 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025316 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025317 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025318 v->di_tv.vval.v_list = &fc->l_varlist;
25319 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25320 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25321 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025322
25323 /*
25324 * Set a:firstline to "firstline" and a:lastline to "lastline".
25325 * Set a:name to named arguments.
25326 * Set a:N to the "..." arguments.
25327 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025328 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025329 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025330 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025331 (varnumber_T)lastline);
25332 for (i = 0; i < argcount; ++i)
25333 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025334 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025335 if (ai < 0)
25336 /* named argument a:name */
25337 name = FUNCARG(fp, i);
25338 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025339 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025340 /* "..." argument a:1, a:2, etc. */
25341 sprintf((char *)numbuf, "%d", ai + 1);
25342 name = numbuf;
25343 }
25344 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25345 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025346 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025347 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25348 }
25349 else
25350 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025351 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25352 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025353 if (v == NULL)
25354 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025355 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025356 }
25357 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025358 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025359
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025360 /* Note: the values are copied directly to avoid alloc/free.
25361 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025362 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025363 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025364
25365 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25366 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025367 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25368 fc->l_listitems[ai].li_tv = argvars[i];
25369 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025370 }
25371 }
25372
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373 /* Don't redraw while executing the function. */
25374 ++RedrawingDisabled;
25375 save_sourcing_name = sourcing_name;
25376 save_sourcing_lnum = sourcing_lnum;
25377 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025378 /* need space for function name + ("function " + 3) or "[number]" */
25379 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25380 + STRLEN(fp->uf_name) + 20;
25381 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382 if (sourcing_name != NULL)
25383 {
25384 if (save_sourcing_name != NULL
25385 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025386 sprintf((char *)sourcing_name, "%s[%d]..",
25387 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388 else
25389 STRCPY(sourcing_name, "function ");
25390 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25391
25392 if (p_verbose >= 12)
25393 {
25394 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025395 verbose_enter_scroll();
25396
Bram Moolenaar555b2802005-05-19 21:08:39 +000025397 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398 if (p_verbose >= 14)
25399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025400 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025401 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025402 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025403 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404
25405 msg_puts((char_u *)"(");
25406 for (i = 0; i < argcount; ++i)
25407 {
25408 if (i > 0)
25409 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025410 if (argvars[i].v_type == VAR_NUMBER)
25411 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412 else
25413 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025414 /* Do not want errors such as E724 here. */
25415 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025416 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025417 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025418 if (s != NULL)
25419 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025420 if (vim_strsize(s) > MSG_BUF_CLEN)
25421 {
25422 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25423 s = buf;
25424 }
25425 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025426 vim_free(tofree);
25427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428 }
25429 }
25430 msg_puts((char_u *)")");
25431 }
25432 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025433
25434 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435 --no_wait_return;
25436 }
25437 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025438#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025439 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025440 {
25441 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25442 func_do_profile(fp);
25443 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025444 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025445 {
25446 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025447 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025448 profile_zero(&fp->uf_tm_children);
25449 }
25450 script_prof_save(&wait_start);
25451 }
25452#endif
25453
Bram Moolenaar071d4272004-06-13 20:20:40 +000025454 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025455 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025456 save_did_emsg = did_emsg;
25457 did_emsg = FALSE;
25458
25459 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025460 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25462
25463 --RedrawingDisabled;
25464
25465 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025466 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025468 clear_tv(rettv);
25469 rettv->v_type = VAR_NUMBER;
25470 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471 }
25472
Bram Moolenaar05159a02005-02-26 23:04:13 +000025473#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025474 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025475 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025476 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025477 profile_end(&call_start);
25478 profile_sub_wait(&wait_start, &call_start);
25479 profile_add(&fp->uf_tm_total, &call_start);
25480 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025481 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025482 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025483 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25484 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025485 }
25486 }
25487#endif
25488
Bram Moolenaar071d4272004-06-13 20:20:40 +000025489 /* when being verbose, mention the return value */
25490 if (p_verbose >= 12)
25491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025493 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494
Bram Moolenaar071d4272004-06-13 20:20:40 +000025495 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025496 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025497 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025498 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025499 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025502 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025503 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025504 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025505 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025506
Bram Moolenaar555b2802005-05-19 21:08:39 +000025507 /* The value may be very long. Skip the middle part, so that we
25508 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025509 * truncate it at the end. Don't want errors such as E724 here. */
25510 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025511 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025512 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025513 if (s != NULL)
25514 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025515 if (vim_strsize(s) > MSG_BUF_CLEN)
25516 {
25517 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25518 s = buf;
25519 }
25520 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025521 vim_free(tofree);
25522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025523 }
25524 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025525
25526 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527 --no_wait_return;
25528 }
25529
25530 vim_free(sourcing_name);
25531 sourcing_name = save_sourcing_name;
25532 sourcing_lnum = save_sourcing_lnum;
25533 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025534#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025535 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025536 script_prof_restore(&wait_start);
25537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538
25539 if (p_verbose >= 12 && sourcing_name != NULL)
25540 {
25541 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025542 verbose_enter_scroll();
25543
Bram Moolenaar555b2802005-05-19 21:08:39 +000025544 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025546
25547 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548 --no_wait_return;
25549 }
25550
25551 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025552 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025554
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025555 /* If the a:000 list and the l: and a: dicts are not referenced we can
25556 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025557 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25558 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25559 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25560 {
25561 free_funccal(fc, FALSE);
25562 }
25563 else
25564 {
25565 hashitem_T *hi;
25566 listitem_T *li;
25567 int todo;
25568
25569 /* "fc" is still in use. This can happen when returning "a:000" or
25570 * assigning "l:" to a global variable.
25571 * Link "fc" in the list for garbage collection later. */
25572 fc->caller = previous_funccal;
25573 previous_funccal = fc;
25574
25575 /* Make a copy of the a: variables, since we didn't do that above. */
25576 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25577 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25578 {
25579 if (!HASHITEM_EMPTY(hi))
25580 {
25581 --todo;
25582 v = HI2DI(hi);
25583 copy_tv(&v->di_tv, &v->di_tv);
25584 }
25585 }
25586
25587 /* Make a copy of the a:000 items, since we didn't do that above. */
25588 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25589 copy_tv(&li->li_tv, &li->li_tv);
25590 }
25591}
25592
25593/*
25594 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025595 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025596 */
25597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025598can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025599{
25600 return (fc->l_varlist.lv_copyID != copyID
25601 && fc->l_vars.dv_copyID != copyID
25602 && fc->l_avars.dv_copyID != copyID);
25603}
25604
25605/*
25606 * Free "fc" and what it contains.
25607 */
25608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025609free_funccal(
25610 funccall_T *fc,
25611 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025612{
25613 listitem_T *li;
25614
25615 /* The a: variables typevals may not have been allocated, only free the
25616 * allocated variables. */
25617 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25618
25619 /* free all l: variables */
25620 vars_clear(&fc->l_vars.dv_hashtab);
25621
25622 /* Free the a:000 variables if they were allocated. */
25623 if (free_val)
25624 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25625 clear_tv(&li->li_tv);
25626
25627 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025628}
25629
25630/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025631 * Add a number variable "name" to dict "dp" with value "nr".
25632 */
25633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025634add_nr_var(
25635 dict_T *dp,
25636 dictitem_T *v,
25637 char *name,
25638 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025639{
25640 STRCPY(v->di_key, name);
25641 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25642 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25643 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025644 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025645 v->di_tv.vval.v_number = nr;
25646}
25647
25648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025649 * ":return [expr]"
25650 */
25651 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025652ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653{
25654 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025655 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025656 int returning = FALSE;
25657
25658 if (current_funccal == NULL)
25659 {
25660 EMSG(_("E133: :return not inside a function"));
25661 return;
25662 }
25663
25664 if (eap->skip)
25665 ++emsg_skip;
25666
25667 eap->nextcmd = NULL;
25668 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025669 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670 {
25671 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025672 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025673 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025674 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025675 }
25676 /* It's safer to return also on error. */
25677 else if (!eap->skip)
25678 {
25679 /*
25680 * Return unless the expression evaluation has been cancelled due to an
25681 * aborting error, an interrupt, or an exception.
25682 */
25683 if (!aborting())
25684 returning = do_return(eap, FALSE, TRUE, NULL);
25685 }
25686
25687 /* When skipping or the return gets pending, advance to the next command
25688 * in this line (!returning). Otherwise, ignore the rest of the line.
25689 * Following lines will be ignored by get_func_line(). */
25690 if (returning)
25691 eap->nextcmd = NULL;
25692 else if (eap->nextcmd == NULL) /* no argument */
25693 eap->nextcmd = check_nextcmd(arg);
25694
25695 if (eap->skip)
25696 --emsg_skip;
25697}
25698
25699/*
25700 * Return from a function. Possibly makes the return pending. Also called
25701 * for a pending return at the ":endtry" or after returning from an extra
25702 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025703 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025704 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025705 * FALSE when the return gets pending.
25706 */
25707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025708do_return(
25709 exarg_T *eap,
25710 int reanimate,
25711 int is_cmd,
25712 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025713{
25714 int idx;
25715 struct condstack *cstack = eap->cstack;
25716
25717 if (reanimate)
25718 /* Undo the return. */
25719 current_funccal->returned = FALSE;
25720
25721 /*
25722 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25723 * not in its finally clause (which then is to be executed next) is found.
25724 * In this case, make the ":return" pending for execution at the ":endtry".
25725 * Otherwise, return normally.
25726 */
25727 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25728 if (idx >= 0)
25729 {
25730 cstack->cs_pending[idx] = CSTP_RETURN;
25731
25732 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025733 /* A pending return again gets pending. "rettv" points to an
25734 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025735 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025736 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025737 else
25738 {
25739 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025740 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025742 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025744 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025745 {
25746 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025747 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025748 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749 else
25750 EMSG(_(e_outofmem));
25751 }
25752 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025753 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754
25755 if (reanimate)
25756 {
25757 /* The pending return value could be overwritten by a ":return"
25758 * without argument in a finally clause; reset the default
25759 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025760 current_funccal->rettv->v_type = VAR_NUMBER;
25761 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762 }
25763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025764 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025765 }
25766 else
25767 {
25768 current_funccal->returned = TRUE;
25769
25770 /* If the return is carried out now, store the return value. For
25771 * a return immediately after reanimation, the value is already
25772 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025773 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025775 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025776 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025778 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779 }
25780 }
25781
25782 return idx < 0;
25783}
25784
25785/*
25786 * Free the variable with a pending return value.
25787 */
25788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025789discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025790{
Bram Moolenaar33570922005-01-25 22:26:29 +000025791 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025792}
25793
25794/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025795 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796 * is an allocated string. Used by report_pending() for verbose messages.
25797 */
25798 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025799get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025800{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025801 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025802 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025803 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025804
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025805 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025806 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025807 if (s == NULL)
25808 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025809
25810 STRCPY(IObuff, ":return ");
25811 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25812 if (STRLEN(s) + 8 >= IOSIZE)
25813 STRCPY(IObuff + IOSIZE - 4, "...");
25814 vim_free(tofree);
25815 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816}
25817
25818/*
25819 * Get next function line.
25820 * Called by do_cmdline() to get the next line.
25821 * Returns allocated string, or NULL for end of function.
25822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025823 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025824get_func_line(
25825 int c UNUSED,
25826 void *cookie,
25827 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025828{
Bram Moolenaar33570922005-01-25 22:26:29 +000025829 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025830 ufunc_T *fp = fcp->func;
25831 char_u *retval;
25832 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025833
25834 /* If breakpoints have been added/deleted need to check for it. */
25835 if (fcp->dbg_tick != debug_tick)
25836 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025837 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 sourcing_lnum);
25839 fcp->dbg_tick = debug_tick;
25840 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025841#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025842 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025843 func_line_end(cookie);
25844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025845
Bram Moolenaar05159a02005-02-26 23:04:13 +000025846 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025847 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25848 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025849 retval = NULL;
25850 else
25851 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025852 /* Skip NULL lines (continuation lines). */
25853 while (fcp->linenr < gap->ga_len
25854 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25855 ++fcp->linenr;
25856 if (fcp->linenr >= gap->ga_len)
25857 retval = NULL;
25858 else
25859 {
25860 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25861 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025862#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025863 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025864 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025865#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025867 }
25868
25869 /* Did we encounter a breakpoint? */
25870 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25871 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025872 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025873 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025874 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875 sourcing_lnum);
25876 fcp->dbg_tick = debug_tick;
25877 }
25878
25879 return retval;
25880}
25881
Bram Moolenaar05159a02005-02-26 23:04:13 +000025882#if defined(FEAT_PROFILE) || defined(PROTO)
25883/*
25884 * Called when starting to read a function line.
25885 * "sourcing_lnum" must be correct!
25886 * When skipping lines it may not actually be executed, but we won't find out
25887 * until later and we need to store the time now.
25888 */
25889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025890func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025891{
25892 funccall_T *fcp = (funccall_T *)cookie;
25893 ufunc_T *fp = fcp->func;
25894
25895 if (fp->uf_profiling && sourcing_lnum >= 1
25896 && sourcing_lnum <= fp->uf_lines.ga_len)
25897 {
25898 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025899 /* Skip continuation lines. */
25900 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25901 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025902 fp->uf_tml_execed = FALSE;
25903 profile_start(&fp->uf_tml_start);
25904 profile_zero(&fp->uf_tml_children);
25905 profile_get_wait(&fp->uf_tml_wait);
25906 }
25907}
25908
25909/*
25910 * Called when actually executing a function line.
25911 */
25912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025913func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025914{
25915 funccall_T *fcp = (funccall_T *)cookie;
25916 ufunc_T *fp = fcp->func;
25917
25918 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25919 fp->uf_tml_execed = TRUE;
25920}
25921
25922/*
25923 * Called when done with a function line.
25924 */
25925 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025926func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025927{
25928 funccall_T *fcp = (funccall_T *)cookie;
25929 ufunc_T *fp = fcp->func;
25930
25931 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25932 {
25933 if (fp->uf_tml_execed)
25934 {
25935 ++fp->uf_tml_count[fp->uf_tml_idx];
25936 profile_end(&fp->uf_tml_start);
25937 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025938 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025939 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25940 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025941 }
25942 fp->uf_tml_idx = -1;
25943 }
25944}
25945#endif
25946
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947/*
25948 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025949 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950 */
25951 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025952func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953{
Bram Moolenaar33570922005-01-25 22:26:29 +000025954 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025955
25956 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25957 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025958 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 || fcp->returned);
25960}
25961
25962/*
25963 * return TRUE if cookie indicates a function which "abort"s on errors.
25964 */
25965 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025966func_has_abort(
25967 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025969 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970}
25971
25972#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25973typedef enum
25974{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025975 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25976 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25977 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025978} var_flavour_T;
25979
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025980static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025981
25982 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025983var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025984{
25985 char_u *p = varname;
25986
25987 if (ASCII_ISUPPER(*p))
25988 {
25989 while (*(++p))
25990 if (ASCII_ISLOWER(*p))
25991 return VAR_FLAVOUR_SESSION;
25992 return VAR_FLAVOUR_VIMINFO;
25993 }
25994 else
25995 return VAR_FLAVOUR_DEFAULT;
25996}
25997#endif
25998
25999#if defined(FEAT_VIMINFO) || defined(PROTO)
26000/*
26001 * Restore global vars that start with a capital from the viminfo file
26002 */
26003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026004read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026005{
26006 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026007 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026008 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026009 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026010
26011 if (!writing && (find_viminfo_parameter('!') != NULL))
26012 {
26013 tab = vim_strchr(virp->vir_line + 1, '\t');
26014 if (tab != NULL)
26015 {
26016 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026017 switch (*tab)
26018 {
26019 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026020#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026021 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026022#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026023 case 'D': type = VAR_DICT; break;
26024 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026025 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026027
26028 tab = vim_strchr(tab, '\t');
26029 if (tab != NULL)
26030 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026031 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026032 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026033 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026034 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026035#ifdef FEAT_FLOAT
26036 else if (type == VAR_FLOAT)
26037 (void)string2float(tab + 1, &tv.vval.v_float);
26038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026039 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026040 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026041 if (type == VAR_DICT || type == VAR_LIST)
26042 {
26043 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26044
26045 if (etv == NULL)
26046 /* Failed to parse back the dict or list, use it as a
26047 * string. */
26048 tv.v_type = VAR_STRING;
26049 else
26050 {
26051 vim_free(tv.vval.v_string);
26052 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026053 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026054 }
26055 }
26056
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026057 /* when in a function use global variables */
26058 save_funccal = current_funccal;
26059 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026060 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026061 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026062
26063 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026064 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026065 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26066 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026067 }
26068 }
26069 }
26070
26071 return viminfo_readline(virp);
26072}
26073
26074/*
26075 * Write global vars that start with a capital to the viminfo file
26076 */
26077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026078write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026079{
Bram Moolenaar33570922005-01-25 22:26:29 +000026080 hashitem_T *hi;
26081 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026082 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026083 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026084 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026085 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026086 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026087
26088 if (find_viminfo_parameter('!') == NULL)
26089 return;
26090
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026091 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026092
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026093 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026094 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026095 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026096 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026097 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026098 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026099 this_var = HI2DI(hi);
26100 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026101 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026102 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026103 {
26104 case VAR_STRING: s = "STR"; break;
26105 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026106 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026107 case VAR_DICT: s = "DIC"; break;
26108 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026109 case VAR_SPECIAL: s = "XPL"; break;
26110
26111 case VAR_UNKNOWN:
26112 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026113 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026114 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026115 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026116 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026117 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026118 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026119 if (p != NULL)
26120 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026121 vim_free(tofree);
26122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026123 }
26124 }
26125}
26126#endif
26127
26128#if defined(FEAT_SESSION) || defined(PROTO)
26129 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026130store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131{
Bram Moolenaar33570922005-01-25 22:26:29 +000026132 hashitem_T *hi;
26133 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026134 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026135 char_u *p, *t;
26136
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026137 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026138 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026139 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026140 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026142 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026143 this_var = HI2DI(hi);
26144 if ((this_var->di_tv.v_type == VAR_NUMBER
26145 || this_var->di_tv.v_type == VAR_STRING)
26146 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026147 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026148 /* Escape special characters with a backslash. Turn a LF and
26149 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026150 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026151 (char_u *)"\\\"\n\r");
26152 if (p == NULL) /* out of memory */
26153 break;
26154 for (t = p; *t != NUL; ++t)
26155 if (*t == '\n')
26156 *t = 'n';
26157 else if (*t == '\r')
26158 *t = 'r';
26159 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026160 this_var->di_key,
26161 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26162 : ' ',
26163 p,
26164 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26165 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026166 || put_eol(fd) == FAIL)
26167 {
26168 vim_free(p);
26169 return FAIL;
26170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171 vim_free(p);
26172 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026173#ifdef FEAT_FLOAT
26174 else if (this_var->di_tv.v_type == VAR_FLOAT
26175 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26176 {
26177 float_T f = this_var->di_tv.vval.v_float;
26178 int sign = ' ';
26179
26180 if (f < 0)
26181 {
26182 f = -f;
26183 sign = '-';
26184 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026185 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026186 this_var->di_key, sign, f) < 0)
26187 || put_eol(fd) == FAIL)
26188 return FAIL;
26189 }
26190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026191 }
26192 }
26193 return OK;
26194}
26195#endif
26196
Bram Moolenaar661b1822005-07-28 22:36:45 +000026197/*
26198 * Display script name where an item was last set.
26199 * Should only be invoked when 'verbose' is non-zero.
26200 */
26201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026202last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026203{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026204 char_u *p;
26205
Bram Moolenaar661b1822005-07-28 22:36:45 +000026206 if (scriptID != 0)
26207 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026208 p = home_replace_save(NULL, get_scriptname(scriptID));
26209 if (p != NULL)
26210 {
26211 verbose_enter();
26212 MSG_PUTS(_("\n\tLast set from "));
26213 MSG_PUTS(p);
26214 vim_free(p);
26215 verbose_leave();
26216 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026217 }
26218}
26219
Bram Moolenaard812df62008-11-09 12:46:09 +000026220/*
26221 * List v:oldfiles in a nice way.
26222 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026223 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026224ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026225{
26226 list_T *l = vimvars[VV_OLDFILES].vv_list;
26227 listitem_T *li;
26228 int nr = 0;
26229
26230 if (l == NULL)
26231 msg((char_u *)_("No old files"));
26232 else
26233 {
26234 msg_start();
26235 msg_scroll = TRUE;
26236 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26237 {
26238 msg_outnum((long)++nr);
26239 MSG_PUTS(": ");
26240 msg_outtrans(get_tv_string(&li->li_tv));
26241 msg_putchar('\n');
26242 out_flush(); /* output one line at a time */
26243 ui_breakcheck();
26244 }
26245 /* Assume "got_int" was set to truncate the listing. */
26246 got_int = FALSE;
26247
26248#ifdef FEAT_BROWSE_CMD
26249 if (cmdmod.browse)
26250 {
26251 quit_more = FALSE;
26252 nr = prompt_for_number(FALSE);
26253 msg_starthere();
26254 if (nr > 0)
26255 {
26256 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26257 (long)nr);
26258
26259 if (p != NULL)
26260 {
26261 p = expand_env_save(p);
26262 eap->arg = p;
26263 eap->cmdidx = CMD_edit;
26264 cmdmod.browse = FALSE;
26265 do_exedit(eap, NULL);
26266 vim_free(p);
26267 }
26268 }
26269 }
26270#endif
26271 }
26272}
26273
Bram Moolenaar53744302015-07-17 17:38:22 +020026274/* reset v:option_new, v:option_old and v:option_type */
26275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026276reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026277{
26278 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26279 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26280 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26281}
26282
26283
Bram Moolenaar071d4272004-06-13 20:20:40 +000026284#endif /* FEAT_EVAL */
26285
Bram Moolenaar071d4272004-06-13 20:20:40 +000026286
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026287#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026288
26289#ifdef WIN3264
26290/*
26291 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26292 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026293static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26294static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26295static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026296
26297/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026298 * Get the short path (8.3) for the filename in "fnamep".
26299 * Only works for a valid file name.
26300 * When the path gets longer "fnamep" is changed and the allocated buffer
26301 * is put in "bufp".
26302 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26303 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026304 */
26305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026306get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026307{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026308 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026309 char_u *newbuf;
26310
26311 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026312 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026313 if (l > len - 1)
26314 {
26315 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026316 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317 newbuf = vim_strnsave(*fnamep, l);
26318 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026319 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026320
26321 vim_free(*bufp);
26322 *fnamep = *bufp = newbuf;
26323
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026324 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026325 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026326 }
26327
26328 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026329 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026330}
26331
26332/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026333 * Get the short path (8.3) for the filename in "fname". The converted
26334 * path is returned in "bufp".
26335 *
26336 * Some of the directories specified in "fname" may not exist. This function
26337 * will shorten the existing directories at the beginning of the path and then
26338 * append the remaining non-existing path.
26339 *
26340 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026341 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026342 * bufp - Pointer to an allocated buffer for the filename.
26343 * fnamelen - Length of the filename pointed to by fname
26344 *
26345 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026346 */
26347 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026348shortpath_for_invalid_fname(
26349 char_u **fname,
26350 char_u **bufp,
26351 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026352{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026353 char_u *short_fname, *save_fname, *pbuf_unused;
26354 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026355 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026356 int old_len, len;
26357 int new_len, sfx_len;
26358 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026359
26360 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026361 old_len = *fnamelen;
26362 save_fname = vim_strnsave(*fname, old_len);
26363 pbuf_unused = NULL;
26364 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026365
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026366 endp = save_fname + old_len - 1; /* Find the end of the copy */
26367 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026368
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026369 /*
26370 * Try shortening the supplied path till it succeeds by removing one
26371 * directory at a time from the tail of the path.
26372 */
26373 len = 0;
26374 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026375 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026376 /* go back one path-separator */
26377 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26378 --endp;
26379 if (endp <= save_fname)
26380 break; /* processed the complete path */
26381
26382 /*
26383 * Replace the path separator with a NUL and try to shorten the
26384 * resulting path.
26385 */
26386 ch = *endp;
26387 *endp = 0;
26388 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026389 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026390 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26391 {
26392 retval = FAIL;
26393 goto theend;
26394 }
26395 *endp = ch; /* preserve the string */
26396
26397 if (len > 0)
26398 break; /* successfully shortened the path */
26399
26400 /* failed to shorten the path. Skip the path separator */
26401 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026402 }
26403
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026404 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026405 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026406 /*
26407 * Succeeded in shortening the path. Now concatenate the shortened
26408 * path with the remaining path at the tail.
26409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026410
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026411 /* Compute the length of the new path. */
26412 sfx_len = (int)(save_endp - endp) + 1;
26413 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026414
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026415 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026416 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026417 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026418 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026419 /* There is not enough space in the currently allocated string,
26420 * copy it to a buffer big enough. */
26421 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026422 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026423 {
26424 retval = FAIL;
26425 goto theend;
26426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026427 }
26428 else
26429 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026430 /* Transfer short_fname to the main buffer (it's big enough),
26431 * unless get_short_pathname() did its work in-place. */
26432 *fname = *bufp = save_fname;
26433 if (short_fname != save_fname)
26434 vim_strncpy(save_fname, short_fname, len);
26435 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026436 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026437
26438 /* concat the not-shortened part of the path */
26439 vim_strncpy(*fname + len, endp, sfx_len);
26440 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026441 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026442
26443theend:
26444 vim_free(pbuf_unused);
26445 vim_free(save_fname);
26446
26447 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026448}
26449
26450/*
26451 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026452 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026453 */
26454 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026455shortpath_for_partial(
26456 char_u **fnamep,
26457 char_u **bufp,
26458 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026459{
26460 int sepcount, len, tflen;
26461 char_u *p;
26462 char_u *pbuf, *tfname;
26463 int hasTilde;
26464
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026465 /* Count up the path separators from the RHS.. so we know which part
26466 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026467 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026468 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469 if (vim_ispathsep(*p))
26470 ++sepcount;
26471
26472 /* Need full path first (use expand_env() to remove a "~/") */
26473 hasTilde = (**fnamep == '~');
26474 if (hasTilde)
26475 pbuf = tfname = expand_env_save(*fnamep);
26476 else
26477 pbuf = tfname = FullName_save(*fnamep, FALSE);
26478
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026479 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026480
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026481 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26482 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026483
26484 if (len == 0)
26485 {
26486 /* Don't have a valid filename, so shorten the rest of the
26487 * path if we can. This CAN give us invalid 8.3 filenames, but
26488 * there's not a lot of point in guessing what it might be.
26489 */
26490 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026491 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26492 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026493 }
26494
26495 /* Count the paths backward to find the beginning of the desired string. */
26496 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026497 {
26498#ifdef FEAT_MBYTE
26499 if (has_mbyte)
26500 p -= mb_head_off(tfname, p);
26501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026502 if (vim_ispathsep(*p))
26503 {
26504 if (sepcount == 0 || (hasTilde && sepcount == 1))
26505 break;
26506 else
26507 sepcount --;
26508 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026510 if (hasTilde)
26511 {
26512 --p;
26513 if (p >= tfname)
26514 *p = '~';
26515 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026516 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026517 }
26518 else
26519 ++p;
26520
26521 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26522 vim_free(*bufp);
26523 *fnamelen = (int)STRLEN(p);
26524 *bufp = pbuf;
26525 *fnamep = p;
26526
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026527 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026528}
26529#endif /* WIN3264 */
26530
26531/*
26532 * Adjust a filename, according to a string of modifiers.
26533 * *fnamep must be NUL terminated when called. When returning, the length is
26534 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026535 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026536 * When there is an error, *fnamep is set to NULL.
26537 */
26538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026539modify_fname(
26540 char_u *src, /* string with modifiers */
26541 int *usedlen, /* characters after src that are used */
26542 char_u **fnamep, /* file name so far */
26543 char_u **bufp, /* buffer for allocated file name or NULL */
26544 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026545{
26546 int valid = 0;
26547 char_u *tail;
26548 char_u *s, *p, *pbuf;
26549 char_u dirname[MAXPATHL];
26550 int c;
26551 int has_fullname = 0;
26552#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026553 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026554 int has_shortname = 0;
26555#endif
26556
26557repeat:
26558 /* ":p" - full path/file_name */
26559 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26560 {
26561 has_fullname = 1;
26562
26563 valid |= VALID_PATH;
26564 *usedlen += 2;
26565
26566 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26567 if ((*fnamep)[0] == '~'
26568#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26569 && ((*fnamep)[1] == '/'
26570# ifdef BACKSLASH_IN_FILENAME
26571 || (*fnamep)[1] == '\\'
26572# endif
26573 || (*fnamep)[1] == NUL)
26574
26575#endif
26576 )
26577 {
26578 *fnamep = expand_env_save(*fnamep);
26579 vim_free(*bufp); /* free any allocated file name */
26580 *bufp = *fnamep;
26581 if (*fnamep == NULL)
26582 return -1;
26583 }
26584
26585 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026586 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026587 {
26588 if (vim_ispathsep(*p)
26589 && p[1] == '.'
26590 && (p[2] == NUL
26591 || vim_ispathsep(p[2])
26592 || (p[2] == '.'
26593 && (p[3] == NUL || vim_ispathsep(p[3])))))
26594 break;
26595 }
26596
26597 /* FullName_save() is slow, don't use it when not needed. */
26598 if (*p != NUL || !vim_isAbsName(*fnamep))
26599 {
26600 *fnamep = FullName_save(*fnamep, *p != NUL);
26601 vim_free(*bufp); /* free any allocated file name */
26602 *bufp = *fnamep;
26603 if (*fnamep == NULL)
26604 return -1;
26605 }
26606
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026607#ifdef WIN3264
26608# if _WIN32_WINNT >= 0x0500
26609 if (vim_strchr(*fnamep, '~') != NULL)
26610 {
26611 /* Expand 8.3 filename to full path. Needed to make sure the same
26612 * file does not have two different names.
26613 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26614 p = alloc(_MAX_PATH + 1);
26615 if (p != NULL)
26616 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026617 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026618 {
26619 vim_free(*bufp);
26620 *bufp = *fnamep = p;
26621 }
26622 else
26623 vim_free(p);
26624 }
26625 }
26626# endif
26627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026628 /* Append a path separator to a directory. */
26629 if (mch_isdir(*fnamep))
26630 {
26631 /* Make room for one or two extra characters. */
26632 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26633 vim_free(*bufp); /* free any allocated file name */
26634 *bufp = *fnamep;
26635 if (*fnamep == NULL)
26636 return -1;
26637 add_pathsep(*fnamep);
26638 }
26639 }
26640
26641 /* ":." - path relative to the current directory */
26642 /* ":~" - path relative to the home directory */
26643 /* ":8" - shortname path - postponed till after */
26644 while (src[*usedlen] == ':'
26645 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26646 {
26647 *usedlen += 2;
26648 if (c == '8')
26649 {
26650#ifdef WIN3264
26651 has_shortname = 1; /* Postpone this. */
26652#endif
26653 continue;
26654 }
26655 pbuf = NULL;
26656 /* Need full path first (use expand_env() to remove a "~/") */
26657 if (!has_fullname)
26658 {
26659 if (c == '.' && **fnamep == '~')
26660 p = pbuf = expand_env_save(*fnamep);
26661 else
26662 p = pbuf = FullName_save(*fnamep, FALSE);
26663 }
26664 else
26665 p = *fnamep;
26666
26667 has_fullname = 0;
26668
26669 if (p != NULL)
26670 {
26671 if (c == '.')
26672 {
26673 mch_dirname(dirname, MAXPATHL);
26674 s = shorten_fname(p, dirname);
26675 if (s != NULL)
26676 {
26677 *fnamep = s;
26678 if (pbuf != NULL)
26679 {
26680 vim_free(*bufp); /* free any allocated file name */
26681 *bufp = pbuf;
26682 pbuf = NULL;
26683 }
26684 }
26685 }
26686 else
26687 {
26688 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26689 /* Only replace it when it starts with '~' */
26690 if (*dirname == '~')
26691 {
26692 s = vim_strsave(dirname);
26693 if (s != NULL)
26694 {
26695 *fnamep = s;
26696 vim_free(*bufp);
26697 *bufp = s;
26698 }
26699 }
26700 }
26701 vim_free(pbuf);
26702 }
26703 }
26704
26705 tail = gettail(*fnamep);
26706 *fnamelen = (int)STRLEN(*fnamep);
26707
26708 /* ":h" - head, remove "/file_name", can be repeated */
26709 /* Don't remove the first "/" or "c:\" */
26710 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26711 {
26712 valid |= VALID_HEAD;
26713 *usedlen += 2;
26714 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026715 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026716 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026717 *fnamelen = (int)(tail - *fnamep);
26718#ifdef VMS
26719 if (*fnamelen > 0)
26720 *fnamelen += 1; /* the path separator is part of the path */
26721#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026722 if (*fnamelen == 0)
26723 {
26724 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26725 p = vim_strsave((char_u *)".");
26726 if (p == NULL)
26727 return -1;
26728 vim_free(*bufp);
26729 *bufp = *fnamep = tail = p;
26730 *fnamelen = 1;
26731 }
26732 else
26733 {
26734 while (tail > s && !after_pathsep(s, tail))
26735 mb_ptr_back(*fnamep, tail);
26736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026737 }
26738
26739 /* ":8" - shortname */
26740 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26741 {
26742 *usedlen += 2;
26743#ifdef WIN3264
26744 has_shortname = 1;
26745#endif
26746 }
26747
26748#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026749 /*
26750 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026751 */
26752 if (has_shortname)
26753 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026754 /* Copy the string if it is shortened by :h and when it wasn't copied
26755 * yet, because we are going to change it in place. Avoids changing
26756 * the buffer name for "%:8". */
26757 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026758 {
26759 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026760 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026761 return -1;
26762 vim_free(*bufp);
26763 *bufp = *fnamep = p;
26764 }
26765
26766 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026767 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026768 if (!has_fullname && !vim_isAbsName(*fnamep))
26769 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026770 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026771 return -1;
26772 }
26773 else
26774 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026775 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026776
Bram Moolenaardc935552011-08-17 15:23:23 +020026777 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026778 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026779 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026780 return -1;
26781
26782 if (l == 0)
26783 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026784 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026785 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026786 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026787 return -1;
26788 }
26789 *fnamelen = l;
26790 }
26791 }
26792#endif /* WIN3264 */
26793
26794 /* ":t" - tail, just the basename */
26795 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26796 {
26797 *usedlen += 2;
26798 *fnamelen -= (int)(tail - *fnamep);
26799 *fnamep = tail;
26800 }
26801
26802 /* ":e" - extension, can be repeated */
26803 /* ":r" - root, without extension, can be repeated */
26804 while (src[*usedlen] == ':'
26805 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26806 {
26807 /* find a '.' in the tail:
26808 * - for second :e: before the current fname
26809 * - otherwise: The last '.'
26810 */
26811 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26812 s = *fnamep - 2;
26813 else
26814 s = *fnamep + *fnamelen - 1;
26815 for ( ; s > tail; --s)
26816 if (s[0] == '.')
26817 break;
26818 if (src[*usedlen + 1] == 'e') /* :e */
26819 {
26820 if (s > tail)
26821 {
26822 *fnamelen += (int)(*fnamep - (s + 1));
26823 *fnamep = s + 1;
26824#ifdef VMS
26825 /* cut version from the extension */
26826 s = *fnamep + *fnamelen - 1;
26827 for ( ; s > *fnamep; --s)
26828 if (s[0] == ';')
26829 break;
26830 if (s > *fnamep)
26831 *fnamelen = s - *fnamep;
26832#endif
26833 }
26834 else if (*fnamep <= tail)
26835 *fnamelen = 0;
26836 }
26837 else /* :r */
26838 {
26839 if (s > tail) /* remove one extension */
26840 *fnamelen = (int)(s - *fnamep);
26841 }
26842 *usedlen += 2;
26843 }
26844
26845 /* ":s?pat?foo?" - substitute */
26846 /* ":gs?pat?foo?" - global substitute */
26847 if (src[*usedlen] == ':'
26848 && (src[*usedlen + 1] == 's'
26849 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26850 {
26851 char_u *str;
26852 char_u *pat;
26853 char_u *sub;
26854 int sep;
26855 char_u *flags;
26856 int didit = FALSE;
26857
26858 flags = (char_u *)"";
26859 s = src + *usedlen + 2;
26860 if (src[*usedlen + 1] == 'g')
26861 {
26862 flags = (char_u *)"g";
26863 ++s;
26864 }
26865
26866 sep = *s++;
26867 if (sep)
26868 {
26869 /* find end of pattern */
26870 p = vim_strchr(s, sep);
26871 if (p != NULL)
26872 {
26873 pat = vim_strnsave(s, (int)(p - s));
26874 if (pat != NULL)
26875 {
26876 s = p + 1;
26877 /* find end of substitution */
26878 p = vim_strchr(s, sep);
26879 if (p != NULL)
26880 {
26881 sub = vim_strnsave(s, (int)(p - s));
26882 str = vim_strnsave(*fnamep, *fnamelen);
26883 if (sub != NULL && str != NULL)
26884 {
26885 *usedlen = (int)(p + 1 - src);
26886 s = do_string_sub(str, pat, sub, flags);
26887 if (s != NULL)
26888 {
26889 *fnamep = s;
26890 *fnamelen = (int)STRLEN(s);
26891 vim_free(*bufp);
26892 *bufp = s;
26893 didit = TRUE;
26894 }
26895 }
26896 vim_free(sub);
26897 vim_free(str);
26898 }
26899 vim_free(pat);
26900 }
26901 }
26902 /* after using ":s", repeat all the modifiers */
26903 if (didit)
26904 goto repeat;
26905 }
26906 }
26907
Bram Moolenaar26df0922014-02-23 23:39:13 +010026908 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26909 {
26910 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26911 if (p == NULL)
26912 return -1;
26913 vim_free(*bufp);
26914 *bufp = *fnamep = p;
26915 *fnamelen = (int)STRLEN(p);
26916 *usedlen += 2;
26917 }
26918
Bram Moolenaar071d4272004-06-13 20:20:40 +000026919 return valid;
26920}
26921
26922/*
26923 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26924 * "flags" can be "g" to do a global substitute.
26925 * Returns an allocated string, NULL for error.
26926 */
26927 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026928do_string_sub(
26929 char_u *str,
26930 char_u *pat,
26931 char_u *sub,
26932 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026933{
26934 int sublen;
26935 regmatch_T regmatch;
26936 int i;
26937 int do_all;
26938 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026939 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026940 garray_T ga;
26941 char_u *ret;
26942 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026943 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026944
26945 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26946 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026947 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026948
26949 ga_init2(&ga, 1, 200);
26950
26951 do_all = (flags[0] == 'g');
26952
26953 regmatch.rm_ic = p_ic;
26954 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26955 if (regmatch.regprog != NULL)
26956 {
26957 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026958 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026959 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26960 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026961 /* Skip empty match except for first match. */
26962 if (regmatch.startp[0] == regmatch.endp[0])
26963 {
26964 if (zero_width == regmatch.startp[0])
26965 {
26966 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026967 i = MB_PTR2LEN(tail);
26968 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26969 (size_t)i);
26970 ga.ga_len += i;
26971 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026972 continue;
26973 }
26974 zero_width = regmatch.startp[0];
26975 }
26976
Bram Moolenaar071d4272004-06-13 20:20:40 +000026977 /*
26978 * Get some space for a temporary buffer to do the substitution
26979 * into. It will contain:
26980 * - The text up to where the match is.
26981 * - The substituted text.
26982 * - The text after the match.
26983 */
26984 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026985 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026986 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26987 {
26988 ga_clear(&ga);
26989 break;
26990 }
26991
26992 /* copy the text up to where the match is */
26993 i = (int)(regmatch.startp[0] - tail);
26994 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26995 /* add the substituted text */
26996 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26997 + ga.ga_len + i, TRUE, TRUE, FALSE);
26998 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026999 tail = regmatch.endp[0];
27000 if (*tail == NUL)
27001 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027002 if (!do_all)
27003 break;
27004 }
27005
27006 if (ga.ga_data != NULL)
27007 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27008
Bram Moolenaar473de612013-06-08 18:19:48 +020027009 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027010 }
27011
27012 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27013 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027014 if (p_cpo == empty_option)
27015 p_cpo = save_cpo;
27016 else
27017 /* Darn, evaluating {sub} expression changed the value. */
27018 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027019
27020 return ret;
27021}
27022
27023#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */